update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / actions / SelectInAction.java
blobef6d36b51c79c31a46dc5c79845143929414a25a
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.
17 package com.intellij.ide.actions;
19 import com.intellij.featureStatistics.FeatureUsageTracker;
20 import com.intellij.ide.*;
21 import com.intellij.openapi.actionSystem.*;
22 import com.intellij.openapi.project.DumbAware;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.ui.popup.JBPopupFactory;
25 import com.intellij.openapi.ui.popup.ListPopup;
26 import com.intellij.openapi.ui.popup.PopupStep;
27 import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
28 import org.jetbrains.annotations.NotNull;
30 import java.util.*;
32 public class SelectInAction extends AnAction implements DumbAware {
33 public void actionPerformed(AnActionEvent e) {
34 FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.select.in");
35 SelectInContext context = SelectInContextImpl.createContext(e);
36 if (context == null) return;
37 invoke(e.getDataContext(), context);
40 public void update(AnActionEvent event) {
41 Presentation presentation = event.getPresentation();
43 if (SelectInContextImpl.createContext(event) == null) {
44 presentation.setEnabled(false);
45 presentation.setVisible(false);
47 else {
48 presentation.setEnabled(true);
49 presentation.setVisible(true);
53 private static void invoke(DataContext dataContext, SelectInContext context) {
54 final List<SelectInTarget> targetVector = Arrays.asList(getSelectInManager(context.getProject()).getTargets());
55 ListPopup popup;
56 if (targetVector.isEmpty()) {
57 DefaultActionGroup group = new DefaultActionGroup();
58 group.add(new NoTargetsAction());
59 popup = JBPopupFactory.getInstance().createActionGroupPopup(IdeBundle.message("title.popup.select.target"), group, dataContext,
60 JBPopupFactory.ActionSelectionAid.MNEMONICS, true);
62 else {
63 popup = JBPopupFactory.getInstance().createListPopup(new SelectInActionsStep(targetVector, context));
66 popup.showInBestPositionFor(dataContext);
69 private static class SelectInActionsStep extends BaseListPopupStep<SelectInTarget> {
70 private final SelectInContext mySelectInContext;
71 private final List<SelectInTarget> myVisibleTargets;
73 public SelectInActionsStep(@NotNull final Collection<SelectInTarget> targetVector, SelectInContext selectInContext) {
74 mySelectInContext = selectInContext;
75 myVisibleTargets = new ArrayList<SelectInTarget>();
76 for (SelectInTarget target : targetVector) {
77 myVisibleTargets.add(target);
79 init(IdeBundle.message("title.popup.select.target"), myVisibleTargets, null);
82 @NotNull
83 public String getTextFor(final SelectInTarget value) {
84 String text = value.toString();
85 int n = myVisibleTargets.indexOf(value);
86 return numberingText(n, text);
89 public PopupStep onChosen(final SelectInTarget target, final boolean finalChoice) {
90 if (finalChoice) {
91 target.selectIn(mySelectInContext, true);
92 return FINAL_CHOICE;
94 if (target instanceof CompositeSelectInTarget) {
95 final ArrayList<SelectInTarget> subTargets = new ArrayList<SelectInTarget>(((CompositeSelectInTarget)target).getSubTargets(mySelectInContext));
96 if (subTargets.size() > 0) {
97 Collections.sort(subTargets, new SelectInManager.SelectInTargetComparator());
98 return new SelectInActionsStep(subTargets, mySelectInContext);
101 return FINAL_CHOICE;
104 public boolean hasSubstep(final SelectInTarget selectedValue) {
105 return selectedValue instanceof CompositeSelectInTarget &&
106 ((CompositeSelectInTarget)selectedValue).getSubTargets(mySelectInContext).size() != 0;
109 public boolean isSelectable(final SelectInTarget target) {
110 return target.canSelect(mySelectInContext);
113 public boolean isMnemonicsNavigationEnabled() {
114 return true;
118 private static String numberingText(final int n, String text) {
119 if (n < 9) {
120 text = "&" + (n + 1) + ". " + text;
122 else if (n == 9) {
123 text = "&" + 0 + ". " + text;
125 else {
126 text = "&" + (char)('A' + n - 10) + ". " + text;
128 return text;
131 private static SelectInManager getSelectInManager(Project project) {
132 return SelectInManager.getInstance(project);
135 private static class NoTargetsAction extends AnAction {
136 public NoTargetsAction() {
137 super(IdeBundle.message("message.no.targets.available"));
140 public void actionPerformed(AnActionEvent e) {