summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/tools/BaseExternalToolsGroup.java
blob: 4d0d2867e38752152b006e0e4702e474333f14c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.tools;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author Eugene Belyaev
 */
public abstract class BaseExternalToolsGroup<T extends Tool> extends SimpleActionGroup implements DumbAware {
  @Override
  public void update(AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    removeAll();
    String context = event.getPlace();
    Project project = event.getData(CommonDataKeys.PROJECT);
    if (project == null) {
      presentation.setVisible(false);
      presentation.setEnabled(false);
      return;
    }
    presentation.setEnabled(true);
    List<ToolsGroup<T>> groups = getToolsGroups();
    for (ToolsGroup group : groups) {
      String groupName = group.getName();
      if (!StringUtil.isEmptyOrSpaces(groupName)) {
        SimpleActionGroup subgroup = new SimpleActionGroup();
        subgroup.getTemplatePresentation().setText(groupName, false);
        subgroup.setPopup(true);
        fillGroup(context, groupName, subgroup);
        if (subgroup.getChildrenCount() > 0) {
          add(subgroup);
        }
      }
      else {
        fillGroup(context, null, this);
      }
    }
    presentation.setVisible(getChildrenCount() > 0);
  }

  protected abstract List<ToolsGroup<T>> getToolsGroups();

  private void fillGroup(String context, @Nullable String groupName, SimpleActionGroup group) {
    List<T> tools = getToolsByGroupName(groupName);
    for (T tool : tools) {
      if (isToolVisible(tool, context)) {
        addToolToGroup(tool, group);
      }
    }
  }

  protected abstract List<T> getToolsByGroupName(String groupName);

  private void addToolToGroup(T tool, SimpleActionGroup group) {
    String id = tool.getActionId();
    AnAction action = ActionManager.getInstance().getAction(id);
    if (action == null) {
      action = createToolAction(tool);
    }

    group.add(action);
  }

  protected abstract ToolAction createToolAction(T tool);

  private boolean isToolVisible(T tool, String context) {
    if (!tool.isEnabled()) return false;
    if (ActionPlaces.EDITOR_POPUP.equals(context) ||
        ActionPlaces.EDITOR_TAB_POPUP.equals(context)) {
      return tool.isShownInEditor();
    }
    else if (
      ActionPlaces.PROJECT_VIEW_POPUP.equals(context) ||
      ActionPlaces.COMMANDER_POPUP.equals(context) ||
      ActionPlaces.J2EE_VIEW_POPUP.equals(context) ||
      ActionPlaces.TYPE_HIERARCHY_VIEW_POPUP.equals(context) ||
      ActionPlaces.CALL_HIERARCHY_VIEW_POPUP.equals(context) ||
      ActionPlaces.METHOD_HIERARCHY_VIEW_POPUP.equals(context) ||
      ActionPlaces.FAVORITES_VIEW_POPUP.equals(context) ||
      ActionPlaces.SCOPE_VIEW_POPUP.equals(context) ||
      ActionPlaces.NAVIGATION_BAR_POPUP.equals(context)
      ) {
      return tool.isShownInProjectViews();
    }
    else if (ActionPlaces.MAIN_MENU.equals(context)) {
      return tool.isShownInMainMenu();
    }
    else if (ActionPlaces.USAGE_VIEW_POPUP.equals(context)) {
      return tool.isShownInSearchResultsPopup();
    }
    return false;
  }
}