update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / FindUsagesInProjectStructureActionBase.java
blob05c52edaef9adafbe870e762d871f6fc9512e757
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.projectRoot;
18 import com.intellij.find.FindBundle;
19 import com.intellij.openapi.actionSystem.ActionManager;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.IdeActions;
23 import com.intellij.openapi.diagnostic.Logger;
24 import com.intellij.openapi.module.Module;
25 import com.intellij.openapi.project.DumbAware;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.ProjectBundle;
28 import com.intellij.openapi.projectRoots.Sdk;
29 import com.intellij.openapi.roots.ModuleRootModel;
30 import com.intellij.openapi.roots.OrderEntry;
31 import com.intellij.openapi.roots.impl.OrderEntryUtil;
32 import com.intellij.openapi.roots.libraries.Library;
33 import com.intellij.openapi.roots.ui.configuration.ModulesConfigurator;
34 import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
35 import com.intellij.openapi.ui.Messages;
36 import com.intellij.openapi.ui.popup.JBPopupFactory;
37 import com.intellij.openapi.ui.popup.PopupStep;
38 import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
39 import com.intellij.openapi.util.IconLoader;
40 import com.intellij.ui.awt.RelativePoint;
41 import com.intellij.util.ArrayUtil;
42 import org.jetbrains.annotations.NotNull;
43 import org.jetbrains.annotations.Nullable;
45 import javax.swing.*;
46 import java.util.Set;
48 /**
49 * @author nik
51 public abstract class FindUsagesInProjectStructureActionBase extends AnAction implements DumbAware {
52 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.roots.ui.configuration.projectRoot.FindUsagesInProjectStructureActionBase");
53 private static final Icon FIND_ICON = IconLoader.getIcon("/actions/find.png");
54 private final JComponent myParentComponent;
55 private final Project myProject;
57 public FindUsagesInProjectStructureActionBase(JComponent parentComponent, Project project) {
58 super(ProjectBundle.message("find.usages.action.text"), ProjectBundle.message("find.usages.action.text"), FIND_ICON);
59 registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_USAGES).getShortcutSet(), parentComponent);
60 myParentComponent = parentComponent;
61 myProject = project;
64 public void update(AnActionEvent e) {
65 e.getPresentation().setEnabled(isEnabled());
68 protected abstract boolean isEnabled();
70 public void actionPerformed(AnActionEvent e) {
71 final Object selectedObject = getSelectedObject();
72 if (selectedObject == null) return;
74 final Set<String> dependencies = getContext().getCachedDependencies(selectedObject, true);
75 if (dependencies == null || dependencies.isEmpty()) {
76 Messages.showInfoMessage(myParentComponent, FindBundle.message("find.usage.view.no.usages.text"),
77 FindBundle.message("find.pointcut.applications.not.found.title"));
78 return;
80 RelativePoint point = getPointToShowResults();
81 JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<String>(ProjectBundle.message("dependencies.used.in.popup.title"),
82 ArrayUtil.toStringArray(dependencies)) {
84 public PopupStep onChosen(final String nameToSelect, final boolean finalChoice) {
85 navigateToObject(nameToSelect, selectedObject);
86 return FINAL_CHOICE;
89 public Icon getIconFor(String selection) {
90 final Module module = getContext().myModulesConfigurator.getModule(selection);
91 LOG.assertTrue(module != null, selection + " was not found");
92 return module.getModuleType().getNodeIcon(false);
95 }).show(point);
98 private void navigateToObject(final String moduleName, final @NotNull Object selectedObject) {
99 ModulesConfigurator modulesConfigurator = getContext().myModulesConfigurator;
100 Module module = modulesConfigurator.getModule(moduleName);
101 if (module == null) {
102 ProjectStructureConfigurable.getInstance(myProject).select(moduleName, null, true);
103 return;
106 ModuleRootModel rootModel = modulesConfigurator.getRootModel(module);
107 OrderEntry entry;
108 if (selectedObject instanceof Library) {
109 entry = OrderEntryUtil.findLibraryOrderEntry(rootModel, (Library)selectedObject);
111 else if (selectedObject instanceof Module) {
112 entry = OrderEntryUtil.findModuleOrderEntry(rootModel, (Module)selectedObject);
114 else if (selectedObject instanceof Sdk) {
115 entry = OrderEntryUtil.findJdkOrderEntry(rootModel, (Sdk)selectedObject);
117 else {
118 entry = null;
120 ModuleStructureConfigurable.getInstance(myProject).selectOrderEntry(module, entry);
123 @Nullable
124 protected abstract Object getSelectedObject();
126 protected StructureConfigurableContext getContext() {
127 return ModuleStructureConfigurable.getInstance(myProject).getContext();
130 protected abstract RelativePoint getPointToShowResults();