1 11 package org.eclipse.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtension; 21 import org.eclipse.core.runtime.IRegistryChangeEvent; 22 import org.eclipse.core.runtime.IRegistryChangeListener; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 26 import org.eclipse.jface.action.Action; 27 import org.eclipse.jface.action.ActionContributionItem; 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.action.IContributionItem; 30 import org.eclipse.jface.action.Separator; 31 import org.eclipse.ui.IWorkbenchPage; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.activities.WorkbenchActivityHelper; 34 import org.eclipse.ui.internal.WorkbenchMessages; 35 import org.eclipse.ui.internal.WorkbenchPlugin; 36 import org.eclipse.ui.internal.WorkbenchWindow; 37 import org.eclipse.ui.internal.actions.NewWizardShortcutAction; 38 import org.eclipse.ui.internal.util.Util; 39 import org.eclipse.ui.wizards.IWizardDescriptor; 40 41 48 public class BaseNewWizardMenu extends CompoundContributionItem { 49 55 56 private final Map actions = new HashMap (21); 57 58 private final IExtensionChangeHandler configListener = new IExtensionChangeHandler() { 59 60 63 public void removeExtension(IExtension source, Object [] objects) { 64 for (int i = 0; i < objects.length; i++) { 65 if (objects[i] instanceof NewWizardShortcutAction) { 66 actions.values().remove(objects[i]); 67 } 68 } 69 } 70 71 74 public void addExtension(IExtensionTracker tracker, IExtension extension) { 75 } 77 }; 78 79 82 private final IRegistryChangeListener registryListener = new IRegistryChangeListener() { 83 84 public void registryChanged(IRegistryChangeEvent event) { 85 if (getParent() != null) { 88 getParent().markDirty(); 89 } 90 } 91 92 }; 93 94 private final IAction showDlgAction; 95 96 private IWorkbenchWindow workbenchWindow; 97 98 106 public BaseNewWizardMenu(IWorkbenchWindow window, String id) { 107 super(id); 108 Assert.isNotNull(window); 109 this.workbenchWindow = window; 110 showDlgAction = ActionFactory.NEW.create(window); 111 registerListeners(); 112 ((WorkbenchWindow) window) 114 .addSubmenu(WorkbenchWindow.NEW_WIZARD_SUBMENU); 115 } 116 117 122 protected void addItems(List list) { 123 if (addShortcuts(list)) { 124 list.add(new Separator()); 125 } 126 list.add(new ActionContributionItem(getShowDialogAction())); 127 } 128 129 135 protected boolean addShortcuts(List list) { 136 boolean added = false; 137 IWorkbenchPage page = workbenchWindow.getActivePage(); 138 if (page != null) { 139 String [] wizardIds = page.getNewWizardShortcuts(); 140 for (int i = 0; i < wizardIds.length; i++) { 141 IAction action = getAction(wizardIds[i]); 142 if (action != null) { 143 if (!WorkbenchActivityHelper.filterItem(action)) { 144 list.add(new ActionContributionItem(action)); 145 added = true; 146 } 147 } 148 } 149 } 150 return added; 151 } 152 153 158 public void dispose() { 159 if (workbenchWindow != null) { 160 super.dispose(); 161 unregisterListeners(); 162 workbenchWindow = null; 163 } 164 } 165 166 170 private IAction getAction(String id) { 171 IAction action = (IAction) actions.get(id); 174 if (action == null) { 175 IWizardDescriptor wizardDesc = WorkbenchPlugin.getDefault() 176 .getNewWizardRegistry().findWizard(id); 177 if (wizardDesc != null) { 178 action = new NewWizardShortcutAction(workbenchWindow, 179 wizardDesc); 180 actions.put(id, action); 181 IConfigurationElement element = (IConfigurationElement) Util 182 .getAdapter(wizardDesc, IConfigurationElement.class); 183 if (element != null) { 184 workbenchWindow.getExtensionTracker().registerObject( 185 element.getDeclaringExtension(), action, 186 IExtensionTracker.REF_WEAK); 187 } 188 } 189 } 190 return action; 191 } 192 193 198 protected IContributionItem[] getContributionItems() { 199 ArrayList list = new ArrayList (); 200 if (workbenchWindow != null && workbenchWindow.getActivePage() != null 201 && workbenchWindow.getActivePage().getPerspective() != null) { 202 addItems(list); 203 } else { 204 String text = WorkbenchMessages.Workbench_noApplicableItems; 205 Action dummyAction = new Action(text) { 206 }; 208 dummyAction.setEnabled(false); 209 list.add(new ActionContributionItem(dummyAction)); 210 } 211 return (IContributionItem[]) list.toArray(new IContributionItem[list.size()]); 212 } 213 214 219 protected IAction getShowDialogAction() { 220 return showDlgAction; 221 } 222 223 228 protected IWorkbenchWindow getWindow() { 229 return workbenchWindow; 230 } 231 232 237 private void registerListeners() { 238 Platform.getExtensionRegistry().addRegistryChangeListener( 239 registryListener); 240 workbenchWindow.getExtensionTracker().registerHandler( 241 configListener, null); 242 } 243 244 253 protected boolean registryHasCategory(String categoryId) { 254 return WorkbenchPlugin.getDefault().getNewWizardRegistry() 255 .findCategory(categoryId) != null; 256 } 257 258 263 private void unregisterListeners() { 264 Platform.getExtensionRegistry().removeRegistryChangeListener( 265 registryListener); 266 workbenchWindow.getExtensionTracker().unregisterHandler(configListener); 267 } 268 } 269 | Popular Tags |