KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > services > ChildPartActionBars


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

11 package org.eclipse.ui.internal.part.services;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.ICoolBarManager;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.IToolBarManager;
22 import org.eclipse.jface.action.SubCoolBarManager;
23 import org.eclipse.jface.action.SubMenuManager;
24 import org.eclipse.jface.action.SubToolBarManager;
25 import org.eclipse.ui.internal.components.framework.ComponentException;
26 import org.eclipse.ui.internal.components.framework.IDisposable;
27 import org.eclipse.ui.internal.components.framework.IServiceProvider;
28 import org.eclipse.ui.internal.part.Part;
29 import org.eclipse.ui.internal.part.components.services.IPartActionBars;
30 import org.eclipse.ui.internal.part.multiplexer.INestedComponent;
31 import org.eclipse.ui.internal.part.multiplexer.ISharedContext;
32
33 public class ChildPartActionBars implements IPartActionBars, INestedComponent, IDisposable {
34
35     private IPartActionBars parent;
36     
37     private Map JavaDoc actionHandlers;
38
39     private boolean active = false;
40
41     private IMenuManager partMenuMgr;
42
43     private IMenuManager menuBarMgr;
44     
45     private SubToolBarManager toolBarMgr;
46     
47     private SubCoolBarManager subCbm;
48     
49     public ChildPartActionBars(IServiceProvider childContainer, ISharedContext shared) throws ComponentException {
50         IServiceProvider sharedContainer = shared.getSharedComponents();
51         parent = (IPartActionBars)sharedContainer.getService(IPartActionBars.class);
52     }
53     
54     public void clearGlobalActionHandlers() {
55         if (actionHandlers != null) {
56             if (active) {
57                 unregisterAllHandlers();
58             }
59             
60             actionHandlers.clear();
61         }
62     }
63
64     private void unregisterAllHandlers() {
65         Collection JavaDoc keySet = actionHandlers.keySet();
66         for (Iterator JavaDoc iter = keySet.iterator(); iter.hasNext();) {
67             String JavaDoc handler = (String JavaDoc) iter.next();
68             
69             parent.setGlobalActionHandler(handler, null);
70         }
71     }
72
73     private void registerAllHandlers() {
74         Collection JavaDoc keySet = actionHandlers.keySet();
75         for (Iterator JavaDoc iter = keySet.iterator(); iter.hasNext();) {
76             String JavaDoc handler = (String JavaDoc) iter.next();
77             
78             parent.setGlobalActionHandler(handler, (IAction) actionHandlers.get(handler));
79         }
80     }
81
82     
83     public IAction getGlobalActionHandler(String JavaDoc actionId) {
84         if (actionHandlers == null)
85             return null;
86         return (IAction) actionHandlers.get(actionId);
87     }
88
89     /**
90      * Returns a new sub menu manager.
91      *
92      * @param parent the parent menu manager
93      * @return the menu manager
94      */

95     protected SubMenuManager createSubMenuManager(IMenuManager parent) {
96         return new SubMenuManager(parent);
97     }
98     
99     public IMenuManager getPartMenuManager() {
100         if (partMenuMgr == null) {
101             partMenuMgr = createSubMenuManager(parent.getPartMenuManager());
102             partMenuMgr.setVisible(active);
103         }
104         return partMenuMgr;
105     }
106
107     public IMenuManager getMenuBarManager() {
108         if (menuBarMgr == null) {
109             menuBarMgr = createSubMenuManager(parent.getPartMenuManager());
110             menuBarMgr.setVisible(active);
111         }
112         return menuBarMgr;
113     }
114
115     /**
116      * Returns a new sub toolbar manager.
117      *
118      * @param parent the parent toolbar manager
119      * @return the tool bar manager
120      */

121     protected SubToolBarManager createSubToolBarManager(IToolBarManager parent) {
122         return new SubToolBarManager(parent);
123     }
124     
125     public IToolBarManager getToolBarManager() {
126         if (toolBarMgr == null) {
127             toolBarMgr = createSubToolBarManager(parent.getToolBarManager());
128             toolBarMgr.setVisible(active);
129         }
130         return toolBarMgr;
131     }
132
133     public ICoolBarManager getCoolBarManager() {
134         if (subCbm == null) {
135             subCbm = new SubCoolBarManager(parent.getCoolBarManager());
136             subCbm.setVisible(active);
137         }
138         
139         return subCbm;
140     }
141     
142     public void setGlobalActionHandler(String JavaDoc actionID, IAction handler) {
143         if (handler != null) {
144             if (actionHandlers == null)
145                 actionHandlers = new HashMap JavaDoc(11);
146             actionHandlers.put(actionID, handler);
147         } else {
148             if (actionHandlers != null)
149                 actionHandlers.remove(actionID);
150         }
151         if (active) {
152             parent.setGlobalActionHandler(actionID, handler);
153         }
154     }
155
156     public void updateActionBars() {
157         if (active) {
158             parent.updateActionBars();
159         }
160     }
161     
162     public void activate(Part newActivePart) {
163         setActive(true);
164     }
165
166     public void deactivate(Object JavaDoc newActive) {
167         setActive(false);
168     }
169     
170     private void setActive(boolean isActive) {
171         if (isActive == active) {
172             return;
173         }
174         
175         active = isActive;
176         
177         if (partMenuMgr != null)
178             partMenuMgr.setVisible(isActive);
179
180         if (menuBarMgr != null)
181             menuBarMgr.setVisible(isActive);
182         
183         if (toolBarMgr != null)
184             toolBarMgr.setVisible(isActive);
185         
186         if (actionHandlers != null) {
187             if (active) {
188                 registerAllHandlers();
189             } else {
190                 unregisterAllHandlers();
191             }
192             parent.updateActionBars();
193         }
194         
195         if (subCbm != null) {
196             subCbm.setVisible(isActive);
197         }
198         
199     }
200     
201     public void dispose() {
202         if (actionHandlers != null)
203             actionHandlers.clear();
204     }
205 }
206
207
Popular Tags