1 11 package org.eclipse.ui.internal.part.services; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 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 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 keySet = actionHandlers.keySet(); 66 for (Iterator iter = keySet.iterator(); iter.hasNext();) { 67 String handler = (String ) iter.next(); 68 69 parent.setGlobalActionHandler(handler, null); 70 } 71 } 72 73 private void registerAllHandlers() { 74 Collection keySet = actionHandlers.keySet(); 75 for (Iterator iter = keySet.iterator(); iter.hasNext();) { 76 String handler = (String ) iter.next(); 77 78 parent.setGlobalActionHandler(handler, (IAction) actionHandlers.get(handler)); 79 } 80 } 81 82 83 public IAction getGlobalActionHandler(String actionId) { 84 if (actionHandlers == null) 85 return null; 86 return (IAction) actionHandlers.get(actionId); 87 } 88 89 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 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 actionID, IAction handler) { 143 if (handler != null) { 144 if (actionHandlers == null) 145 actionHandlers = new HashMap (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 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 |