Search in sources :

Example 1 with SwingWorker

use of com.intellij.util.concurrency.SwingWorker in project intellij-community by JetBrains.

the class JavaContentEntriesEditor method addSourceRoots.

private static void addSourceRoots(@NotNull Project project, final ContentEntry[] contentEntries, final Runnable finishRunnable) {
    final HashMap<ContentEntry, Collection<JavaModuleSourceRoot>> entryToRootMap = new HashMap<>();
    final Map<File, ContentEntry> fileToEntryMap = new HashMap<>();
    for (final ContentEntry contentEntry : contentEntries) {
        final VirtualFile file = contentEntry.getFile();
        if (file != null) {
            entryToRootMap.put(contentEntry, null);
            fileToEntryMap.put(VfsUtilCore.virtualToIoFile(file), contentEntry);
        }
    }
    final ProgressWindow progressWindow = new ProgressWindow(true, project);
    final ProgressIndicator progressIndicator = new SmoothProgressAdapter(progressWindow, project);
    final Runnable searchRunnable = () -> {
        final Runnable process = () -> {
            for (final File file : fileToEntryMap.keySet()) {
                progressIndicator.setText(ProjectBundle.message("module.paths.searching.source.roots.progress", file.getPath()));
                final Collection<JavaModuleSourceRoot> roots = JavaSourceRootDetectionUtil.suggestRoots(file);
                entryToRootMap.put(fileToEntryMap.get(file), roots);
            }
        };
        progressWindow.setTitle(ProjectBundle.message("module.paths.searching.source.roots.title"));
        ProgressManager.getInstance().runProcess(process, progressIndicator);
    };
    final Runnable addSourcesRunnable = () -> {
        for (final ContentEntry contentEntry : contentEntries) {
            final Collection<JavaModuleSourceRoot> suggestedRoots = entryToRootMap.get(contentEntry);
            if (suggestedRoots != null) {
                for (final JavaModuleSourceRoot suggestedRoot : suggestedRoots) {
                    final VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByIoFile(suggestedRoot.getDirectory());
                    final VirtualFile fileContent = contentEntry.getFile();
                    if (sourceRoot != null && fileContent != null && VfsUtilCore.isAncestor(fileContent, sourceRoot, false)) {
                        contentEntry.addSourceFolder(sourceRoot, false, suggestedRoot.getPackagePrefix());
                    }
                }
            }
        }
        if (finishRunnable != null) {
            finishRunnable.run();
        }
    };
    new SwingWorker() {

        @Override
        public Object construct() {
            searchRunnable.run();
            return null;
        }

        @Override
        public void finished() {
            addSourcesRunnable.run();
        }
    }.start();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SmoothProgressAdapter(com.intellij.openapi.progress.util.SmoothProgressAdapter) HashMap(java.util.HashMap) ContentEntry(com.intellij.openapi.roots.ContentEntry) ProgressWindow(com.intellij.openapi.progress.util.ProgressWindow) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Collection(java.util.Collection) SwingWorker(com.intellij.util.concurrency.SwingWorker) JavaModuleSourceRoot(com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

JavaModuleSourceRoot (com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressWindow (com.intellij.openapi.progress.util.ProgressWindow)1 SmoothProgressAdapter (com.intellij.openapi.progress.util.SmoothProgressAdapter)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 SwingWorker (com.intellij.util.concurrency.SwingWorker)1 File (java.io.File)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1