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 org.jetbrains.idea.maven.navigator;
18 import com.intellij.execution.Location;
19 import com.intellij.openapi.actionSystem.*;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.SimpleToolWindowPanel;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.openapi.vfs.VfsUtil;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.pom.Navigatable;
26 import com.intellij.ui.PopupHandler;
27 import com.intellij.ui.treeStructure.SimpleTree;
28 import gnu.trove.THashMap;
29 import gnu.trove.THashSet;
30 import org.jetbrains.annotations.NonNls;
31 import org.jetbrains.annotations.Nullable;
32 import org.jetbrains.idea.maven.embedder.MavenEmbedderWrapper;
33 import org.jetbrains.idea.maven.execution.MavenGoalLocation;
34 import org.jetbrains.idea.maven.project.MavenArtifact;
35 import org.jetbrains.idea.maven.project.MavenProject;
36 import org.jetbrains.idea.maven.utils.MavenDataKeys;
41 import java.util.List;
43 public class MavenProjectsNavigatorPanel extends SimpleToolWindowPanel implements DataProvider {
44 private final Project myProject;
45 private final SimpleTree myTree;
47 private Map<String, Integer> standardGoalOrder;
49 private final Comparator<String> myGoalOrderComparator = new Comparator<String>() {
50 public int compare(String o1, String o2) {
51 return getStandardGoalOrder(o1) - getStandardGoalOrder(o2);
55 public MavenProjectsNavigatorPanel(Project project, SimpleTree tree) {
60 final ActionManager actionManager = ActionManager.getInstance();
61 ActionToolbar actionToolbar = actionManager.createActionToolbar("Maven Navigator Toolbar",
62 (DefaultActionGroup)actionManager
63 .getAction("Maven.NavigatorActionsToolbar"),
66 actionToolbar.setTargetComponent(tree);
67 setToolbar(actionToolbar.getComponent());
68 setContent(new JScrollPane(myTree));
70 myTree.addMouseListener(new PopupHandler() {
71 public void invokePopup(final Component comp, final int x, final int y) {
72 final String id = getMenuId(getSelectedNodes(MavenProjectsStructure.MavenSimpleNode.class));
74 final ActionGroup actionGroup = (ActionGroup)actionManager.getAction(id);
75 if (actionGroup != null) {
76 actionManager.createActionPopupMenu("", actionGroup).getComponent().show(comp, x, y);
82 private String getMenuId(Collection<? extends MavenProjectsStructure.MavenSimpleNode> nodes) {
84 for (MavenProjectsStructure.MavenSimpleNode node : nodes) {
85 String menuId = node.getMenuId();
92 else if (!id.equals(menuId)) {
102 public Object getData(@NonNls String dataId) {
103 if (PlatformDataKeys.HELP_ID.is(dataId)) return "reference.toolWindows.mavenProjects";
105 if (PlatformDataKeys.PROJECT.is(dataId)) return myProject;
107 if (PlatformDataKeys.VIRTUAL_FILE.is(dataId)) return extractVirtualFile();
108 if (PlatformDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) return extractVirtualFiles();
110 if (Location.DATA_KEY.is(dataId)) return extractLocation();
111 if (PlatformDataKeys.NAVIGATABLE_ARRAY.is(dataId)) return extractNavigatables();
113 if (MavenDataKeys.MAVEN_GOALS.is(dataId)) return extractGoals();
114 if (MavenDataKeys.MAVEN_PROFILES.is(dataId)) return extractProfiles();
116 if (MavenDataKeys.MAVEN_DEPENDENCIES.is(dataId)) return extractDependencies();
121 private VirtualFile extractVirtualFile() {
122 for (MavenProjectsStructure.MavenSimpleNode each : getSelectedNodes(MavenProjectsStructure.MavenSimpleNode.class)) {
123 VirtualFile file = each.getVirtualFile();
124 if (file != null && file.isValid()) return file;
127 final MavenProjectsStructure.ProjectNode projectNode = getContextProjectNode();
128 if (projectNode == null) return null;
129 VirtualFile file = projectNode.getVirtualFile();
130 if (file == null || !file.isValid()) return null;
134 private Object extractVirtualFiles() {
135 final List<VirtualFile> files = new ArrayList<VirtualFile>();
136 for (MavenProjectsStructure.MavenSimpleNode each : getSelectedNodes(MavenProjectsStructure.MavenSimpleNode.class)) {
137 VirtualFile file = each.getVirtualFile();
138 if (file != null && file.isValid()) files.add(file);
140 return files.isEmpty() ? null : VfsUtil.toVirtualFileArray(files);
143 private Object extractNavigatables() {
144 final List<Navigatable> navigatables = new ArrayList<Navigatable>();
145 for (MavenProjectsStructure.MavenSimpleNode each : getSelectedNodes(MavenProjectsStructure.MavenSimpleNode.class)) {
146 Navigatable navigatable = each.getNavigatable();
147 if (navigatable != null) navigatables.add(navigatable);
149 return navigatables.isEmpty() ? null : navigatables.toArray(new Navigatable[navigatables.size()]);
152 private Object extractLocation() {
153 VirtualFile file = extractVirtualFile();
154 List<String> goals = extractGoals();
155 if (file == null || goals == null) return null;
157 return new MavenGoalLocation(myProject, file, extractGoals());
160 private List<String> extractGoals() {
161 final MavenProjectsStructure.ProjectNode projectNode = getSelectedProjectNode();
162 if (projectNode != null) {
163 MavenProject project = projectNode.getMavenProject();
164 String goal = project.getDefaultGoal();
165 if (!StringUtil.isEmptyOrSpaces(goal)) {
166 return Collections.singletonList(goal);
170 final List<MavenProjectsStructure.GoalNode> nodes = getSelectedNodes(MavenProjectsStructure.GoalNode.class);
171 if (MavenProjectsStructure.getCommonProjectNode(nodes) == null) {
174 final List<String> goals = new ArrayList<String>();
175 for (MavenProjectsStructure.GoalNode node : nodes) {
176 goals.add(node.getGoal());
178 Collections.sort(goals, myGoalOrderComparator);
184 private Object extractProfiles() {
185 final List<MavenProjectsStructure.ProfileNode> nodes = getSelectedNodes(MavenProjectsStructure.ProfileNode.class);
186 final List<String> profiles = new ArrayList<String>();
187 for (MavenProjectsStructure.ProfileNode node : nodes) {
188 profiles.add(node.getProfileName());
193 private Set<MavenArtifact> extractDependencies() {
194 Set<MavenArtifact> result = new THashSet<MavenArtifact>();
196 List<MavenProjectsStructure.ProjectNode> projectNodes = getSelectedProjectNodes();
197 if (!projectNodes.isEmpty()) {
198 for (MavenProjectsStructure.ProjectNode each : projectNodes) {
199 result.addAll(each.getMavenProject().getDependencies());
204 List<MavenProjectsStructure.BaseDependenciesNode> nodes = getSelectedNodes(MavenProjectsStructure.BaseDependenciesNode.class);
205 for (MavenProjectsStructure.BaseDependenciesNode each : nodes) {
206 if (each instanceof MavenProjectsStructure.DependenciesNode) {
207 result.addAll(each.getMavenProject().getDependencies());
209 result.add(((MavenProjectsStructure.DependencyNode)each).getArtifact());
215 private <T extends MavenProjectsStructure.MavenSimpleNode> List<T> getSelectedNodes(Class<T> aClass) {
216 return MavenProjectsStructure.getSelectedNodes(myTree, aClass);
219 private List<MavenProjectsStructure.ProjectNode> getSelectedProjectNodes() {
220 return getSelectedNodes(MavenProjectsStructure.ProjectNode.class);
224 private MavenProjectsStructure.ProjectNode getSelectedProjectNode() {
225 final List<MavenProjectsStructure.ProjectNode> projectNodes = getSelectedProjectNodes();
226 return projectNodes.size() == 1 ? projectNodes.get(0) : null;
230 private MavenProjectsStructure.ProjectNode getContextProjectNode() {
231 MavenProjectsStructure.ProjectNode projectNode = getSelectedProjectNode();
232 if (projectNode != null) return projectNode;
233 return MavenProjectsStructure.getCommonProjectNode(getSelectedNodes(MavenProjectsStructure.MavenSimpleNode.class));
236 private int getStandardGoalOrder(String goal) {
237 if (standardGoalOrder == null) {
238 standardGoalOrder = new THashMap<String, Integer>();
240 for (String aGoal : MavenEmbedderWrapper.PHASES) {
241 standardGoalOrder.put(aGoal, i++);
244 Integer order = standardGoalOrder.get(goal);
245 return order != null ? order.intValue() : standardGoalOrder.size();