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
.libraryEditor
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.openapi
.ui
.DialogWrapper
;
20 import com
.intellij
.openapi
.ui
.TitlePanel
;
21 import com
.intellij
.openapi
.vfs
.VfsUtil
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import com
.intellij
.ui
.CheckboxTree
;
24 import com
.intellij
.ui
.CheckedTreeNode
;
25 import com
.intellij
.ui
.ColoredTreeCellRenderer
;
26 import com
.intellij
.ui
.SimpleTextAttributes
;
27 import com
.intellij
.util
.Icons
;
28 import com
.intellij
.util
.ui
.tree
.TreeUtil
;
29 import org
.jetbrains
.annotations
.NonNls
;
30 import org
.jetbrains
.annotations
.Nullable
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Enumeration
;
37 import java
.util
.List
;
41 * This dialog allows selecting source paths inside selected source archives or directories.
44 * @author Constantine.Plotnikov
46 public class DetectedSourceRootsDialog
extends DialogWrapper
{
48 * A tree with paths. The tree relies on the CheckboxTree for selection and unselection policy.
50 private final CheckboxTree myTree
;
52 * Root node for the tree. The tree is three-level:
54 * <li>The root is a fake node that just holds child nodes.</li>
55 * <li>The second level is archives or directories selected on the previous selection step.</li>
56 * <li>The third level are paths with java sources inside pervious selection.</li>
59 private final CheckedTreeNode myRootNode
;
61 * A scrollable pane that contains myTree
63 private final JScrollPane myPane
;
68 * @param project a project context
69 * @param detectedRoots a roots detected inside file
70 * @param baseRoot base root file
72 public DetectedSourceRootsDialog(Project project
, List
<VirtualFile
> detectedRoots
, VirtualFile baseRoot
) {
73 this(project
, createTree(baseRoot
, detectedRoots
));
79 * @param project a project context
80 * @param detectedRoots a map from baseRoot to detected roots inside file
82 public DetectedSourceRootsDialog(Project project
, Map
<VirtualFile
, List
<VirtualFile
>> detectedRoots
) {
83 this(project
, createTree(detectedRoots
));
89 * @param component a parent component
90 * @param detectedRoots a map from baseRoot to detected roots inside file
92 public DetectedSourceRootsDialog(Component component
, Map
<VirtualFile
, List
<VirtualFile
>> detectedRoots
) {
93 this(component
, createTree(detectedRoots
));
99 * @param project a project context
100 * @param tree a checkbox tree to use
102 private DetectedSourceRootsDialog(Project project
, CheckedTreeNode tree
) {
103 super(project
, true);
105 myTree
= createCheckboxTree();
106 myPane
= new JScrollPane(myTree
);
107 setTitle("Detected Source Roots");
114 * @param component a parent component
115 * @param tree a checkbox tree to use
117 private DetectedSourceRootsDialog(Component component
, CheckedTreeNode tree
) {
118 super(component
, true);
120 myTree
= createCheckboxTree();
121 myPane
= new JScrollPane(myTree
);
122 setTitle("Detected Source Roots");
128 * Create a checkbox tree component for this dialog
130 * @return a created component
132 private CheckboxTree
createCheckboxTree() {
133 CheckboxTree tree
= new CheckboxTree(new CheckboxTree
.CheckboxTreeCellRenderer(true) {
134 public void customizeCellRenderer(JTree tree
,
141 CheckedTreeNode node
= (CheckedTreeNode
)value
;
142 VirtualFile file
= (VirtualFile
)node
.getUserObject();
144 SimpleTextAttributes attributes
;
146 boolean isValid
= true;
148 VirtualFile ancestor
= (VirtualFile
)((CheckedTreeNode
)node
.getParent()).getUserObject();
149 if (ancestor
!= null) {
150 text
= VfsUtil
.getRelativePath(file
, ancestor
, File
.separatorChar
);
153 text
= file
.getPresentableUrl();
157 text
= file
.getPresentableUrl();
159 attributes
= SimpleTextAttributes
.REGULAR_ATTRIBUTES
;
160 icon
= Icons
.DIRECTORY_CLOSED_ICON
;
163 text
= file
== null ?
"found files" : file
.getPresentableUrl();
167 attributes
= SimpleTextAttributes
.REGULAR_BOLD_ATTRIBUTES
;
168 icon
= expanded ? Icons
.DIRECTORY_OPEN_ICON
: Icons
.DIRECTORY_CLOSED_ICON
;
170 final ColoredTreeCellRenderer textRenderer
= getTextRenderer();
171 textRenderer
.setIcon(icon
);
173 textRenderer
.append("[INVALID] ", SimpleTextAttributes
.ERROR_ATTRIBUTES
);
176 textRenderer
.append(text
, attributes
);
180 tree
.setRootVisible(false);
181 TreeUtil
.expandAll(tree
);
186 * Create tree basing on baseRoot and detectedRoots
188 * @param detectedRoots a detected roots (map from base root to detected roots)
189 * @return a root node of the tree
191 private static CheckedTreeNode
createTree(Map
<VirtualFile
, List
<VirtualFile
>> detectedRoots
) {
192 CheckedTreeNode root
= new CheckedTreeNode(null);
193 for (Map
.Entry
<VirtualFile
, List
<VirtualFile
>> e
: detectedRoots
.entrySet()) {
194 root
.add(createTreeNode(e
.getKey(), e
.getValue()));
200 * Create tree basing on baseRoot and detectedRoots
202 * @param baseRoot a base root
203 * @param detectedRoots a detected roots
204 * @return a root node of the tree
206 private static CheckedTreeNode
createTree(final VirtualFile baseRoot
, final List
<VirtualFile
> detectedRoots
) {
207 CheckedTreeNode root
= new CheckedTreeNode(null);
208 root
.add(createTreeNode(baseRoot
, detectedRoots
));
213 * Create tree node from baseRoot and detectedRoots
215 * @param baseRoot a base root
216 * @param detectedRoots a detected roots
217 * @return a root node for the base root
219 private static CheckedTreeNode
createTreeNode(final VirtualFile baseRoot
, final List
<VirtualFile
> detectedRoots
) {
220 CheckedTreeNode node
= new CheckedTreeNode(baseRoot
);
221 for (VirtualFile f
: detectedRoots
) {
222 node
.add(new CheckedTreeNode(f
));
228 protected JComponent
createTitlePane() {
229 return new TitlePanel("Choose Source Roots", "<html><body>IntelliJ IDEA just scanned files and detected following source root(s).<br>" +
230 "Select items in the tree below or press Cancel to cancel operation.</body></html>");
235 protected JComponent
createCenterPanel() {
240 * @return roots that has been chosen using this dialog
242 public List
<VirtualFile
> getChosenRoots() {
243 ArrayList
<VirtualFile
> rc
= new ArrayList
<VirtualFile
>();
244 for (Enumeration be
= myRootNode
.children(); be
.hasMoreElements();) {
245 CheckedTreeNode baseFileNode
= (CheckedTreeNode
)be
.nextElement();
246 for (Enumeration de
= baseFileNode
.children(); de
.hasMoreElements();) {
247 CheckedTreeNode dirNode
= (CheckedTreeNode
)de
.nextElement();
248 if (dirNode
.isChecked()) {
249 rc
.add((VirtualFile
)dirNode
.getUserObject());
258 protected String
getDimensionServiceKey() {
259 return "DetectedSourceRootsDialog";