1 11 package org.eclipse.ui.navigator; 12 13 import java.util.Collections ; 14 import java.util.Comparator ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.SortedSet ; 19 import java.util.TreeMap ; 20 import java.util.TreeSet ; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.jface.action.IAction; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.Separator; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.ui.IWorkbench; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.actions.ActionContext; 32 import org.eclipse.ui.actions.ActionGroup; 33 import org.eclipse.ui.internal.navigator.wizards.CommonWizardDescriptor; 34 import org.eclipse.ui.internal.navigator.wizards.CommonWizardDescriptorManager; 35 import org.eclipse.ui.internal.navigator.wizards.WizardShortcutAction; 36 import org.eclipse.ui.wizards.IWizardDescriptor; 37 import org.eclipse.ui.wizards.IWizardRegistry; 38 39 78 public final class WizardActionGroup extends ActionGroup { 79 80 84 public static final String TYPE_NEW = "new"; 86 90 public static final String TYPE_IMPORT = "import"; 92 96 public static final String TYPE_EXPORT = "export"; 98 private static final CommonWizardDescriptor[] NO_DESCRIPTORS = new CommonWizardDescriptor[0]; 99 100 private static final String [] NO_IDS = new String [0]; 101 102 private CommonWizardDescriptor[] descriptors; 103 104 105 private Map actions; 106 107 111 private IWorkbenchWindow window; 112 113 114 private IWizardRegistry wizardRegistry; 115 116 private boolean disposed = false; 117 118 private String type; 119 120 private INavigatorContentService contentService; 121 122 139 public WizardActionGroup(IWorkbenchWindow aWindow, 140 IWizardRegistry aWizardRegistry, String aType) { 141 super(); 142 Assert.isNotNull(aWindow); 143 Assert.isNotNull(aWizardRegistry); 144 Assert 145 .isTrue(aType != null 146 && (TYPE_NEW.equals(aType) || TYPE_IMPORT.equals(aType) || TYPE_EXPORT 147 .equals(aType))); 148 window = aWindow; 149 wizardRegistry = aWizardRegistry; 150 type = aType; 151 152 } 153 154 155 174 public WizardActionGroup(IWorkbenchWindow aWindow, 175 IWizardRegistry aWizardRegistry, String aType, INavigatorContentService aContentService) { 176 this(aWindow, aWizardRegistry, aType); 177 contentService = aContentService; 178 179 } 180 181 public void setContext(ActionContext aContext) { 182 Assert.isTrue(!disposed); 183 184 super.setContext(aContext); 185 if (aContext != null) { 186 ISelection selection = aContext.getSelection(); 187 Object element = null; 188 if (selection instanceof IStructuredSelection) { 189 element = ((IStructuredSelection) selection).getFirstElement(); 190 } 191 if(element == null) { 192 element = Collections.EMPTY_LIST; 193 } 194 setWizardActionDescriptors(CommonWizardDescriptorManager.getInstance() 196 .getEnabledCommonWizardDescriptors(element, type, contentService)); 197 } else { 198 setWizardActionDescriptors(NO_DESCRIPTORS); 199 } 200 } 201 202 207 public void fillContextMenu(IMenuManager menu) { 208 Assert.isTrue(!disposed); 209 210 if (descriptors != null) { 211 Map groups = findGroups(); 212 SortedSet sortedWizards = null; 213 String menuGroupId = null; 214 for (Iterator menuGroupItr = groups.keySet().iterator(); menuGroupItr.hasNext();) { 215 menuGroupId = (String ) menuGroupItr.next(); 216 sortedWizards = (SortedSet ) groups.get(menuGroupId); 217 menu.add(new Separator(menuGroupId)); 218 for (Iterator wizardItr = sortedWizards.iterator(); wizardItr.hasNext();) { 219 menu.add((IAction) wizardItr.next()); 220 } 221 } 222 } 223 } 224 225 228 private synchronized Map findGroups() { 229 IAction action = null; 230 Map groups = new TreeMap (); 231 SortedSet sortedWizards = null; 232 String menuGroupId = null; 233 for (int i = 0; i < descriptors.length; i++) { 234 menuGroupId = descriptors[i].getMenuGroupId() != null ? 235 descriptors[i].getMenuGroupId() : CommonWizardDescriptor.DEFAULT_MENU_GROUP_ID; 236 sortedWizards = (SortedSet ) groups.get(menuGroupId); 237 if(sortedWizards == null) { 238 groups.put(descriptors[i].getMenuGroupId(), sortedWizards = new TreeSet (ActionComparator.INSTANCE)); 239 } 240 if ((action = getAction(descriptors[i].getWizardId())) != null) { 241 sortedWizards.add(action); 242 } 243 } 244 return groups; 245 } 246 247 248 public void dispose() { 249 super.dispose(); 250 actions = null; 251 window = null; 252 descriptors = null; 253 wizardRegistry = null; 254 disposed = true; 255 } 256 257 261 protected IAction getAction(String id) { 262 if (id == null || id.length() == 0) { 263 return null; 264 } 265 266 IAction action = (IAction) getActions().get(id); 269 if (action == null) { 270 IWizardDescriptor descriptor = wizardRegistry.findWizard(id); 271 if (descriptor != null) { 272 action = new WizardShortcutAction(window, descriptor); 273 getActions().put(id, action); 274 } 275 } 276 277 return action; 278 } 279 280 283 protected Map getActions() { 284 if (actions == null) { 285 actions = new HashMap (); 286 } 287 return actions; 288 } 289 290 293 public synchronized String [] getWizardActionIds() { 294 if(descriptors != null && descriptors.length > 0) { 295 String [] wizardActionIds = new String [descriptors.length]; 296 for (int i = 0; i < descriptors.length; i++) { 297 wizardActionIds[i] = descriptors[i].getWizardId(); 298 } 299 return wizardActionIds; 300 } 301 return NO_IDS; 302 } 303 304 309 protected synchronized void setWizardActionDescriptors(CommonWizardDescriptor[] theWizardDescriptors) { 310 descriptors = theWizardDescriptors; 311 } 312 313 private static class ActionComparator implements Comparator { 314 315 private static final ActionComparator INSTANCE = new ActionComparator(); 316 319 public int compare(Object arg0, Object arg1) { 320 return ((IAction)arg0).getText().compareTo(((IAction)arg1).getText()); 321 } 322 } 323 } 324 | Popular Tags |