2 * Copyright 2000-2007 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.maddyhome
.idea
.copyright
.actions
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.application
.ModalityState
;
21 import com
.intellij
.openapi
.command
.CommandProcessor
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.module
.Module
;
24 import com
.intellij
.openapi
.module
.ModuleManager
;
25 import com
.intellij
.openapi
.progress
.ProcessCanceledException
;
26 import com
.intellij
.openapi
.progress
.ProgressIndicator
;
27 import com
.intellij
.openapi
.progress
.ProgressManager
;
28 import com
.intellij
.openapi
.progress
.util
.ProgressWindow
;
29 import com
.intellij
.openapi
.project
.Project
;
30 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
31 import com
.intellij
.openapi
.vfs
.ReadonlyStatusHandler
;
32 import com
.intellij
.openapi
.vfs
.VirtualFile
;
33 import com
.intellij
.psi
.PsiDirectory
;
34 import com
.intellij
.psi
.PsiFile
;
35 import com
.intellij
.psi
.PsiManager
;
36 import com
.intellij
.util
.IncorrectOperationException
;
37 import com
.maddyhome
.idea
.copyright
.CopyrightManager
;
38 import com
.maddyhome
.idea
.copyright
.CopyrightProfile
;
39 import com
.maddyhome
.idea
.copyright
.util
.FileTypeUtil
;
41 import java
.util
.ArrayList
;
42 import java
.util
.Arrays
;
43 import java
.util
.List
;
45 public abstract class AbstractFileProcessor
47 private final Project myProject
;
48 private final Module myModule
;
49 private PsiDirectory directory
= null;
50 private PsiFile file
= null;
51 private PsiFile
[] files
= null;
52 private boolean subdirs
= false;
53 private final String message
;
54 private final String title
;
55 private final Runnable postProcess
;
57 protected abstract Runnable
preprocessFile(PsiFile psifile
) throws IncorrectOperationException
;
59 protected AbstractFileProcessor(Project project
, String title
, String message
)
66 this.message
= message
;
70 protected AbstractFileProcessor(Project project
, Module module
, String title
, String message
)
77 this.message
= message
;
81 protected AbstractFileProcessor(Project project
, PsiDirectory dir
, boolean subdirs
, String title
, String message
)
86 this.subdirs
= subdirs
;
87 this.message
= message
;
92 protected AbstractFileProcessor(Project project
, PsiFile file
, String title
, String message
)
97 this.message
= message
;
102 protected AbstractFileProcessor(Project project
, PsiFile
[] files
, String title
, String message
, Runnable runnable
)
107 this.message
= message
;
109 postProcess
= runnable
;
114 if (directory
!= null)
116 process(directory
, subdirs
);
118 else if (files
!= null)
122 else if (file
!= null)
126 else if (myModule
!= null)
130 else if (myProject
!= null)
136 private void process(final PsiFile file
)
138 final VirtualFile virtualFile
= file
.getVirtualFile();
139 assert virtualFile
!= null;
140 if (!ReadonlyStatusHandler
.getInstance(myProject
).ensureFilesWritable(virtualFile
).hasReadonlyFiles()) {
141 final Runnable
[] resultRunnable
= new Runnable
[1];
143 execute(new Runnable()
149 resultRunnable
[0] = preprocessFile(file
);
151 catch (IncorrectOperationException incorrectoperationexception
)
153 logger
.error(incorrectoperationexception
);
160 if (resultRunnable
[0] != null)
162 resultRunnable
[0].run();
170 private Runnable
prepareFiles(List
<PsiFile
> files
)
172 ProgressIndicator indicator
= ProgressManager
.getInstance().getProgressIndicator();
174 double fraction
= 0.0D
;
175 if (indicator
!= null)
177 msg
= indicator
.getText();
178 fraction
= indicator
.getFraction();
179 indicator
.setText(message
);
182 final Runnable
[] runnables
= new Runnable
[files
.size()];
183 for (int i
= 0; i
< files
.size(); i
++)
185 PsiFile pfile
= files
.get(i
);
188 logger
.debug("Unexpected null file at " + i
);
191 if (indicator
!= null)
193 if (indicator
.isCanceled())
198 indicator
.setFraction((double)i
/ (double)files
.size());
201 if (pfile
.isWritable())
205 runnables
[i
] = preprocessFile(pfile
);
207 catch (IncorrectOperationException incorrectoperationexception
)
209 logger
.error(incorrectoperationexception
);
216 if (indicator
!= null)
218 indicator
.setText(msg
);
219 indicator
.setFraction(fraction
);
222 return new Runnable()
226 ProgressIndicator indicator
= ProgressManager
.getInstance().getProgressIndicator();
228 double fraction
= 0.0D
;
229 if (indicator
!= null)
231 msg
= indicator
.getText();
232 fraction
= indicator
.getFraction();
233 indicator
.setText(message
);
236 for (int j
= 0; j
< runnables
.length
; j
++)
238 if (indicator
!= null)
240 if (indicator
.isCanceled())
245 indicator
.setFraction((double)j
/ (double)runnables
.length
);
248 Runnable runnable
= runnables
[j
];
249 if (runnable
!= null)
256 if (indicator
!= null)
258 indicator
.setText(msg
);
259 indicator
.setFraction(fraction
);
265 private void process(final PsiFile
[] files
)
267 final Runnable
[] resultRunnable
= new Runnable
[1];
268 execute(new Runnable()
272 resultRunnable
[0] = prepareFiles(new ArrayList
<PsiFile
>(Arrays
.asList(files
)));
278 if (resultRunnable
[0] != null)
280 resultRunnable
[0].run();
286 private void process(PsiDirectory dir
, boolean subdirs
)
288 List
<PsiFile
> pfiles
= new ArrayList
<PsiFile
>();
289 findFiles(pfiles
, dir
, subdirs
);
293 private void process(Project project
)
295 List
<PsiFile
> pfiles
= new ArrayList
<PsiFile
>();
296 findFiles(project
, pfiles
);
300 private void process(Module module
)
302 List
<PsiFile
> pfiles
= new ArrayList
<PsiFile
>();
303 findFiles(module
, pfiles
);
307 private void findFiles(Project project
, List
<PsiFile
> files
)
309 Module
[] modules
= ModuleManager
.getInstance(project
).getModules();
310 for (Module module
: modules
)
312 findFiles(module
, files
);
317 private void findFiles(Module module
, List
<PsiFile
> files
)
319 VirtualFile
[] roots
= ModuleRootManager
.getInstance(module
).getContentRoots();
320 for (VirtualFile file
: roots
)
322 PsiDirectory dir
= PsiManager
.getInstance(myProject
).findDirectory(file
);
325 findFiles(files
, dir
, true);
331 private void handleFiles(final List
<PsiFile
> files
)
333 final List
<VirtualFile
> vFiles
= new ArrayList
<VirtualFile
>();
334 for (PsiFile psiFile
: files
) {
335 vFiles
.add(psiFile
.getVirtualFile());
337 if (!ReadonlyStatusHandler
.getInstance(myProject
).ensureFilesWritable(vFiles
.toArray(new VirtualFile
[vFiles
.size()])).hasReadonlyFiles()) {
338 if (!files
.isEmpty())
340 final Runnable
[] resultRunnable
= new Runnable
[1];
341 execute(new Runnable()
345 resultRunnable
[0] = prepareFiles(files
);
351 if (resultRunnable
[0] != null)
353 resultRunnable
[0].run();
361 private static void findFiles(List
<PsiFile
> files
, PsiDirectory directory
, boolean subdirs
)
363 final Project project
= directory
.getProject();
364 PsiFile
[] locals
= directory
.getFiles();
365 for (PsiFile local
: locals
)
367 CopyrightProfile opts
= CopyrightManager
.getInstance(project
).getCopyrightOptions(local
);
368 if (opts
!= null && FileTypeUtil
.getInstance().isSupportedFile(local
)) {
376 PsiDirectory
[] dirs
= directory
.getSubdirectories();
377 for (PsiDirectory dir
: dirs
)
379 findFiles(files
, dir
, subdirs
);
385 private void execute(final Runnable readAction
, final Runnable writeAction
)
387 final ProgressWindow progressWindow
= new ProgressWindow(true, myProject
);
388 progressWindow
.setTitle(title
);
389 progressWindow
.setText(message
);
390 final ModalityState modalityState
= ModalityState
.current();
391 final Runnable process
= new Runnable()
395 ApplicationManager
.getApplication().runReadAction(readAction
);
399 Runnable runnable
= new Runnable()
405 ProgressManager
.getInstance().runProcess(process
, progressWindow
);
407 catch (ProcessCanceledException processcanceledexception
)
412 ApplicationManager
.getApplication().invokeLater(new Runnable()
416 CommandProcessor
.getInstance().executeCommand(myProject
, new Runnable()
420 CommandProcessor
.getInstance().markCurrentCommandAsComplex(myProject
);
423 ApplicationManager
.getApplication().runWriteAction(writeAction
);
425 catch (ProcessCanceledException processcanceledexception
)
429 if (postProcess
!= null)
431 ApplicationManager
.getApplication().invokeLater(postProcess
);
440 (new Thread(runnable
, title
)).start();
443 private static final Logger logger
= Logger
.getInstance(AbstractFileProcessor
.class.getName());