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.actionSystem;
18 import org.jetbrains.annotations.NonNls;
19 import org.jetbrains.annotations.NotNull;
20 import org.jetbrains.annotations.Nullable;
23 import java.beans.PropertyChangeListener;
24 import java.beans.PropertyChangeSupport;
25 import java.lang.reflect.Method;
26 import java.util.HashSet;
30 * Represents a group of actions.
32 public abstract class ActionGroup extends AnAction {
33 private boolean myPopup;
34 private final PropertyChangeSupport myChangeSupport = new PropertyChangeSupport(this);
35 public static final ActionGroup EMPTY_GROUP = new ActionGroup() {
38 public AnAction[] getChildren(@Nullable AnActionEvent e) {
43 private Set<AnAction> mySecondaryActions;
46 * The actual value is a Boolean.
48 @NonNls public static final String PROP_POPUP = "popup";
50 private Boolean myDumbAware;
53 * Creates a new <code>ActionGroup</code> with shortName set to <code>null</code> and
61 * Creates a new <code>ActionGroup</code> with the specified shortName
64 * @param shortName Text that represents a short name for this action group
66 * @param popup <code>true</code> if this group is a popup, <code>false</code>
69 public ActionGroup(String shortName, boolean popup){
74 public ActionGroup(String text, String description, Icon icon) {
75 super(text, description, icon);
79 * This method can be called in popup menus if {@link #canBePerformed(DataContext)} is true
81 public void actionPerformed(AnActionEvent e){
85 public void update(AnActionEvent e) {
90 * @return true if {@link #actionPerformed(AnActionEvent)} should be called
92 public boolean canBePerformed(DataContext context) {
97 * Returns the type of the group.
99 * @return <code>true</code> if the group is a popup, <code>false</code> otherwise
101 public boolean isPopup(){
106 * Sets the type of the group.
108 * @param popup If <code>true</code> the group will be shown as a popup in menus
110 public final void setPopup(boolean popup){
111 boolean oldPopup = myPopup;
113 firePropertyChange(PROP_POPUP, oldPopup?Boolean.TRUE:Boolean.FALSE, myPopup?Boolean.TRUE:Boolean.FALSE);
116 public final void addPropertyChangeListener(PropertyChangeListener l){
117 myChangeSupport.addPropertyChangeListener(l);
120 public final void removePropertyChangeListener(PropertyChangeListener l){
121 myChangeSupport.removePropertyChangeListener(l);
124 protected final void firePropertyChange(String propertyName, Object oldValue, Object newValue){
125 myChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
129 * Returns the children of the group.
131 * @return An array represting children of this group. All returned children must be not <code>null</code>.
134 public abstract AnAction[] getChildren(@Nullable AnActionEvent e);
136 final void setAsPrimary(AnAction action, boolean isPrimary) {
138 if (mySecondaryActions != null) {
139 mySecondaryActions.remove(action);
142 if (mySecondaryActions == null) {
143 mySecondaryActions = new HashSet<AnAction>();
146 mySecondaryActions.add(action);
150 public final boolean isPrimary(AnAction action) {
151 return mySecondaryActions == null || !mySecondaryActions.contains(action);
154 protected final void replace(AnAction originalAction, AnAction newAction) {
155 if (mySecondaryActions != null) {
156 if (mySecondaryActions.contains(originalAction)) {
157 mySecondaryActions.remove(originalAction);
158 mySecondaryActions.add(newAction);
164 public boolean isDumbAware() {
165 if (myDumbAware != null) {
169 boolean dumbAware = super.isDumbAware();
171 myDumbAware = Boolean.valueOf(dumbAware);
173 if (myDumbAware == null) {
175 Method updateMethod = getClass().getMethod("update", AnActionEvent.class);
176 Class<?> declaringClass = updateMethod.getDeclaringClass();
177 myDumbAware = AnAction.class.equals(declaringClass) || ActionGroup.class.equals(declaringClass);
179 catch (NoSuchMethodException e) {
180 myDumbAware = Boolean.FALSE;