copyright: update license
[fedora-idea.git] / plugins / Copyright / src / com / maddyhome / idea / copyright / actions / AbstractFileProcessor.java
blob830add609d7036deda3677fd9b8dc20f2aa5a5b0
1 /*
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)
61 myProject = project;
62 myModule = null;
63 directory = null;
64 subdirs = true;
65 this.title = title;
66 this.message = message;
67 postProcess = null;
70 protected AbstractFileProcessor(Project project, Module module, String title, String message)
72 myProject = project;
73 myModule = module;
74 directory = null;
75 subdirs = true;
76 this.title = title;
77 this.message = message;
78 postProcess = null;
81 protected AbstractFileProcessor(Project project, PsiDirectory dir, boolean subdirs, String title, String message)
83 myProject = project;
84 myModule = null;
85 directory = dir;
86 this.subdirs = subdirs;
87 this.message = message;
88 this.title = title;
89 postProcess = null;
92 protected AbstractFileProcessor(Project project, PsiFile file, String title, String message)
94 myProject = project;
95 myModule = null;
96 this.file = file;
97 this.message = message;
98 this.title = title;
99 postProcess = null;
102 protected AbstractFileProcessor(Project project, PsiFile[] files, String title, String message, Runnable runnable)
104 myProject = project;
105 myModule = null;
106 this.files = files;
107 this.message = message;
108 this.title = title;
109 postProcess = runnable;
112 public void run()
114 if (directory != null)
116 process(directory, subdirs);
118 else if (files != null)
120 process(files);
122 else if (file != null)
124 process(file);
126 else if (myModule != null)
128 process(myModule);
130 else if (myProject != null)
132 process(myProject);
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()
145 public void run()
149 resultRunnable[0] = preprocessFile(file);
151 catch (IncorrectOperationException incorrectoperationexception)
153 logger.error(incorrectoperationexception);
156 }, new Runnable()
158 public void run()
160 if (resultRunnable[0] != null)
162 resultRunnable[0].run();
170 private Runnable prepareFiles(List<PsiFile> files)
172 ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
173 String msg = null;
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);
186 if (pfile == null)
188 logger.debug("Unexpected null file at " + i);
189 continue;
191 if (indicator != null)
193 if (indicator.isCanceled())
195 return null;
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);
213 files.set(i, null);
216 if (indicator != null)
218 indicator.setText(msg);
219 indicator.setFraction(fraction);
222 return new Runnable()
224 public void run()
226 ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
227 String msg = null;
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())
242 return;
245 indicator.setFraction((double)j / (double)runnables.length);
248 Runnable runnable = runnables[j];
249 if (runnable != null)
251 runnable.run();
253 runnables[j] = 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()
270 public void run()
272 resultRunnable[0] = prepareFiles(new ArrayList<PsiFile>(Arrays.asList(files)));
274 }, new Runnable()
276 public void run()
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);
290 handleFiles(pfiles);
293 private void process(Project project)
295 List<PsiFile> pfiles = new ArrayList<PsiFile>();
296 findFiles(project, pfiles);
297 handleFiles(pfiles);
300 private void process(Module module)
302 List<PsiFile> pfiles = new ArrayList<PsiFile>();
303 findFiles(module, pfiles);
304 handleFiles(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);
323 if (dir != null)
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()
343 public void run()
345 resultRunnable[0] = prepareFiles(files);
347 }, new Runnable()
349 public void run()
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)) {
369 files.add(local);
374 if (subdirs)
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()
393 public void run()
395 ApplicationManager.getApplication().runReadAction(readAction);
399 Runnable runnable = new Runnable()
401 public void run()
405 ProgressManager.getInstance().runProcess(process, progressWindow);
407 catch (ProcessCanceledException processcanceledexception)
409 return;
412 ApplicationManager.getApplication().invokeLater(new Runnable()
414 public void run()
416 CommandProcessor.getInstance().executeCommand(myProject, new Runnable()
418 public void run()
420 CommandProcessor.getInstance().markCurrentCommandAsComplex(myProject);
423 ApplicationManager.getApplication().runWriteAction(writeAction);
425 catch (ProcessCanceledException processcanceledexception)
427 return;
429 if (postProcess != null)
431 ApplicationManager.getApplication().invokeLater(postProcess);
434 }, title, null);
436 }, modalityState);
440 (new Thread(runnable, title)).start();
443 private static final Logger logger = Logger.getInstance(AbstractFileProcessor.class.getName());