refactoring to remove duplicated code from project structure configurables in RubyMin...
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / JavaContentEntriesEditor.java
blob09d9da350a3bc981d59a2e9b6ea4957e80b927e0
1 /*
2 * Copyright 2000-2009 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.
16 package com.intellij.openapi.roots.ui.configuration;
18 import com.intellij.Patches;
19 import com.intellij.ide.util.JavaUtil;
20 import com.intellij.openapi.module.Module;
21 import com.intellij.openapi.progress.ProgressIndicator;
22 import com.intellij.openapi.progress.ProgressManager;
23 import com.intellij.openapi.progress.util.ProgressWindow;
24 import com.intellij.openapi.progress.util.SmoothProgressAdapter;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.project.ProjectBundle;
27 import com.intellij.openapi.roots.ContentEntry;
28 import com.intellij.openapi.roots.ModifiableRootModel;
29 import com.intellij.openapi.util.Pair;
30 import com.intellij.openapi.vfs.LocalFileSystem;
31 import com.intellij.openapi.vfs.VfsUtil;
32 import com.intellij.openapi.vfs.VirtualFile;
33 import com.intellij.util.concurrency.SwingWorker;
35 import javax.swing.*;
36 import java.awt.*;
37 import java.io.File;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
42 public class JavaContentEntriesEditor extends CommonContentEntriesEditor {
43 public JavaContentEntriesEditor(String moduleName, ModuleConfigurationState state) {
44 super(moduleName, state, true, true);
47 protected ContentEntryEditor createContentEntryEditor(final String contentEntryUrl) {
48 return new JavaContentEntryEditor(contentEntryUrl) {
49 @Override
50 protected ModifiableRootModel getModel() {
51 return JavaContentEntriesEditor.this.getModel();
56 protected ContentEntryTreeEditor createContentEntryTreeEditor(Project project) {
57 return new ContentEntryTreeEditor(project, true, true);
60 @Override
61 protected List<ContentEntry> addContentEntries(VirtualFile[] files) {
62 List<ContentEntry> contentEntries = super.addContentEntries(files);
63 if (!contentEntries.isEmpty()) {
64 final ContentEntry[] contentEntriesArray = contentEntries.toArray(new ContentEntry[contentEntries.size()]);
65 addSourceRoots(myProject, contentEntriesArray, new Runnable() {
66 public void run() {
67 addContentEntryPanels(contentEntriesArray);
69 });
71 return contentEntries;
74 private static void addSourceRoots(final Project project, final ContentEntry[] contentEntries, final Runnable finishRunnable) {
75 final HashMap<ContentEntry, List<Pair<File, String>>> entryToRootMap = new HashMap<ContentEntry, List<Pair<File, String>>>();
76 final Map<File, ContentEntry> fileToEntryMap = new HashMap<File, ContentEntry>();
77 for (final ContentEntry contentEntry : contentEntries) {
78 final VirtualFile file = contentEntry.getFile();
79 if (file != null) {
80 entryToRootMap.put(contentEntry, null);
81 fileToEntryMap.put(VfsUtil.virtualToIoFile(file), contentEntry);
85 final ProgressWindow progressWindow = new ProgressWindow(true, project);
86 final ProgressIndicator progressIndicator = Patches.MAC_HIDE_QUIT_HACK
87 ? progressWindow
88 : new SmoothProgressAdapter(progressWindow, project);
90 final Runnable searchRunnable = new Runnable() {
91 public void run() {
92 final Runnable process = new Runnable() {
93 public void run() {
94 for (final File file : fileToEntryMap.keySet()) {
95 progressIndicator.setText(ProjectBundle.message("module.paths.searching.source.roots.progress", file.getPath()));
96 final List<Pair<File, String>> roots = JavaUtil.suggestRoots(file);
97 entryToRootMap.put(fileToEntryMap.get(file), roots);
101 progressWindow.setTitle(ProjectBundle.message("module.paths.searching.source.roots.title"));
102 ProgressManager.getInstance().runProcess(process, progressIndicator);
106 final Runnable addSourcesRunnable = new Runnable() {
107 public void run() {
108 for (final ContentEntry contentEntry : contentEntries) {
109 final List<Pair<File, String>> suggestedRoots = entryToRootMap.get(contentEntry);
110 if (suggestedRoots != null) {
111 for (final Pair<File, String> suggestedRoot : suggestedRoots) {
112 final VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByIoFile(suggestedRoot.first);
113 final VirtualFile fileContent = contentEntry.getFile();
114 if (sourceRoot != null && fileContent != null && VfsUtil.isAncestor(fileContent, sourceRoot, false)) {
115 contentEntry.addSourceFolder(sourceRoot, false, suggestedRoot.getSecond());
120 if (finishRunnable != null) {
121 finishRunnable.run();
126 new SwingWorker() {
127 public Object construct() {
128 searchRunnable.run();
129 return null;
132 public void finished() {
133 addSourcesRunnable.run();
135 }.start();
138 protected JPanel createBottomControl(Module module) {
139 final JPanel innerPanel = new JPanel(new GridBagLayout());
140 innerPanel.setBorder(BorderFactory.createEmptyBorder(6, 0, 0, 6));
141 return innerPanel;