KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ActionSetManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.ui.IPropertyListener;
20 import org.eclipse.ui.contexts.IContextActivation;
21 import org.eclipse.ui.contexts.IContextService;
22 import org.eclipse.ui.internal.registry.IActionSetDescriptor;
23 import org.eclipse.ui.services.IServiceLocator;
24
25 /**
26  * Maintains a reference counted set of action sets, with a visibility mask.
27  * This is used to determine the visibility of actions in a workbench page. In a
28  * workbench page, there may be may be many conditions that can cause an action
29  * set to become visible (such as the active part, the active editor, the
30  * default visibility of the action, the properties of the perspective, etc.)
31  * The user can also explicitly mask off particular action sets in each
32  * perspective.
33  * <p>
34  * The reference count indicates how many conditions have requested that the
35  * actions be active and the mask indicates whether or not the set was disabled
36  * by the user.
37  * </p>
38  *
39  * @since 3.1
40  */

41 public class ActionSetManager {
42
43     private static class ActionSetRec {
44         int showCount;
45
46         int maskCount;
47         
48         public boolean isVisible() {
49             return maskCount == 0 && showCount > 0;
50         }
51         
52         public boolean isEmpty() {
53             return maskCount == 0 && showCount == 0;
54         }
55     }
56
57     private HashMap JavaDoc actionSets = new HashMap JavaDoc();
58     private HashSet JavaDoc visibleItems = new HashSet JavaDoc();
59     
60     public static final int PROP_VISIBLE = 0;
61     public static final int PROP_HIDDEN = 1;
62     public static final int CHANGE_MASK = 0;
63     public static final int CHANGE_UNMASK = 1;
64     public static final int CHANGE_SHOW = 2;
65     public static final int CHANGE_HIDE = 3;
66     
67     private ListenerList listeners = new ListenerList();
68     private IPropertyListener contextListener;
69     private Map JavaDoc activationsById = new HashMap JavaDoc();
70     private IContextService contextService;
71     
72     public ActionSetManager(IServiceLocator locator) {
73         contextService = (IContextService) locator.getService(IContextService.class);
74         addListener(getContextListener());
75     }
76     
77     /**
78      * @return
79      */

80     private IPropertyListener getContextListener() {
81         if (contextListener == null) {
82             contextListener = new IPropertyListener() {
83                 public void propertyChanged(Object JavaDoc source, int propId) {
84                     if (source instanceof IActionSetDescriptor) {
85                         IActionSetDescriptor desc = (IActionSetDescriptor) source;
86                         String JavaDoc id = desc.getId();
87                         if (propId == PROP_VISIBLE) {
88                             activationsById.put(id, contextService
89                                     .activateContext(id));
90                         } else if (propId == PROP_HIDDEN) {
91                             IContextActivation act = (IContextActivation) activationsById
92                                     .remove(id);
93                             if (act != null) {
94                                 contextService.deactivateContext(act);
95                             }
96                         }
97                     }
98                 }
99             };
100         }
101         return contextListener;
102     }
103
104     public void addListener(IPropertyListener l) {
105         listeners.add(l);
106     }
107
108     public void removeListener(IPropertyListener l) {
109         listeners.remove(l);
110     }
111     
112     private void firePropertyChange(IActionSetDescriptor descriptor, int id) {
113         Object JavaDoc[] l = listeners.getListeners();
114         for (int i=0; i<l.length; i++) {
115             IPropertyListener listener = (IPropertyListener) l[i];
116             listener.propertyChanged(descriptor, id);
117         }
118     }
119     
120     private ActionSetRec getRec(IActionSetDescriptor descriptor) {
121         ActionSetRec rec = (ActionSetRec)actionSets.get(descriptor);
122         
123         if (rec == null) {
124             rec = new ActionSetRec();
125             actionSets.put(descriptor, rec);
126         }
127         
128         return rec;
129     }
130     
131     public void showAction(IActionSetDescriptor descriptor) {
132         ActionSetRec rec = getRec(descriptor);
133         
134         boolean wasVisible = rec.isVisible();
135         rec.showCount++;
136         if (!wasVisible && rec.isVisible()) {
137             visibleItems.add(descriptor);
138             firePropertyChange(descriptor, PROP_VISIBLE);
139             if (rec.isEmpty()) {
140                 actionSets.remove(descriptor);
141             }
142         }
143     }
144     
145     public void hideAction(IActionSetDescriptor descriptor) {
146         ActionSetRec rec = getRec(descriptor);
147         
148         boolean wasVisible = rec.isVisible();
149         rec.showCount--;
150         if (wasVisible && !rec.isVisible()) {
151             visibleItems.remove(descriptor);
152             firePropertyChange(descriptor, PROP_HIDDEN);
153             if (rec.isEmpty()) {
154                 actionSets.remove(descriptor);
155             }
156         }
157     }
158     
159     public void maskAction(IActionSetDescriptor descriptor) {
160         ActionSetRec rec = getRec(descriptor);
161         
162         boolean wasVisible = rec.isVisible();
163         rec.maskCount++;
164         if (wasVisible && !rec.isVisible()) {
165             visibleItems.remove(descriptor);
166             firePropertyChange(descriptor, PROP_HIDDEN);
167             if (rec.isEmpty()) {
168                 actionSets.remove(descriptor);
169             }
170         }
171     }
172     
173     public void unmaskAction(IActionSetDescriptor descriptor) {
174         ActionSetRec rec = getRec(descriptor);
175         
176         boolean wasVisible = rec.isVisible();
177         rec.maskCount--;
178         if (!wasVisible && rec.isVisible()) {
179             visibleItems.add(descriptor);
180             firePropertyChange(descriptor, PROP_VISIBLE);
181             if (rec.isEmpty()) {
182                 actionSets.remove(descriptor);
183             }
184         }
185     }
186     
187     public Collection JavaDoc getVisibleItems() {
188         return visibleItems;
189     }
190     
191     public void change(IActionSetDescriptor descriptor, int changeType) {
192         switch(changeType) {
193         case CHANGE_SHOW:
194             showAction(descriptor); break;
195         case CHANGE_HIDE:
196             hideAction(descriptor); break;
197         case CHANGE_MASK:
198             maskAction(descriptor); break;
199         case CHANGE_UNMASK:
200             unmaskAction(descriptor); break;
201         }
202     }
203 }
204
Popular Tags