update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / libraryEditor / DetectedSourceRootsDialog.java
blob769a89ad54b841698b98243b714b656176346c29
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.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;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.Enumeration;
37 import java.util.List;
38 import java.util.Map;
40 /**
41 * This dialog allows selecting source paths inside selected source archives or directories.
43 * @author max
44 * @author Constantine.Plotnikov
46 public class DetectedSourceRootsDialog extends DialogWrapper {
47 /**
48 * A tree with paths. The tree relies on the CheckboxTree for selection and unselection policy.
50 private final CheckboxTree myTree;
51 /**
52 * Root node for the tree. The tree is three-level:
53 * <ul>
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>
57 * </ul>
59 private final CheckedTreeNode myRootNode;
60 /**
61 * A scrollable pane that contains myTree
63 private final JScrollPane myPane;
65 /**
66 * A constructor
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));
76 /**
77 * A constructor
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));
86 /**
87 * A constructor
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));
96 /**
97 * A constructor
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);
104 myRootNode = tree;
105 myTree = createCheckboxTree();
106 myPane = new JScrollPane(myTree);
107 setTitle("Detected Source Roots");
108 init();
112 * A constructor
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);
119 myRootNode = tree;
120 myTree = createCheckboxTree();
121 myPane = new JScrollPane(myTree);
122 setTitle("Detected Source Roots");
123 init();
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,
135 Object value,
136 boolean selected,
137 boolean expanded,
138 boolean leaf,
139 int row,
140 boolean hasFocus) {
141 CheckedTreeNode node = (CheckedTreeNode)value;
142 VirtualFile file = (VirtualFile)node.getUserObject();
143 String text;
144 SimpleTextAttributes attributes;
145 Icon icon;
146 boolean isValid = true;
147 if (leaf) {
148 VirtualFile ancestor = (VirtualFile)((CheckedTreeNode)node.getParent()).getUserObject();
149 if (ancestor != null) {
150 text = VfsUtil.getRelativePath(file, ancestor, File.separatorChar);
152 else {
153 text = file.getPresentableUrl();
155 if (text == null) {
156 isValid = false;
157 text = file.getPresentableUrl();
159 attributes = SimpleTextAttributes.REGULAR_ATTRIBUTES;
160 icon = Icons.DIRECTORY_CLOSED_ICON;
162 else {
163 text = file == null ? "found files" : file.getPresentableUrl();
164 if (text == null) {
165 isValid = false;
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);
172 if (!isValid) {
173 textRenderer.append("[INVALID] ", SimpleTextAttributes.ERROR_ATTRIBUTES);
175 if (text != null) {
176 textRenderer.append(text, attributes);
179 }, myRootNode);
180 tree.setRootVisible(false);
181 TreeUtil.expandAll(tree);
182 return 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()));
196 return root;
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));
209 return root;
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));
224 return node;
227 @Override
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>");
233 @Nullable
234 @Override
235 protected JComponent createCenterPanel() {
236 return myPane;
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());
253 return rc;
256 @NonNls
257 @Override
258 protected String getDimensionServiceKey() {
259 return "DetectedSourceRootsDialog";