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
;
38 import java
.util
.HashMap
;
39 import java
.util
.List
;
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
) {
50 protected ModifiableRootModel
getModel() {
51 return JavaContentEntriesEditor
.this.getModel();
56 protected ContentEntryTreeEditor
createContentEntryTreeEditor(Project project
) {
57 return new ContentEntryTreeEditor(project
, true, true);
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() {
67 addContentEntryPanels(contentEntriesArray
);
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();
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
88 : new SmoothProgressAdapter(progressWindow
, project
);
90 final Runnable searchRunnable
= new Runnable() {
92 final Runnable process
= new Runnable() {
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() {
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();
127 public Object
construct() {
128 searchRunnable
.run();
132 public void finished() {
133 addSourcesRunnable
.run();
138 protected JPanel
createBottomControl(Module module
) {
139 final JPanel innerPanel
= new JPanel(new GridBagLayout());
140 innerPanel
.setBorder(BorderFactory
.createEmptyBorder(6, 0, 0, 6));