1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.core.commands.Command; 24 import org.eclipse.core.commands.ExecutionException; 25 import org.eclipse.core.commands.IParameter; 26 import org.eclipse.core.commands.NotEnabledException; 27 import org.eclipse.core.commands.NotHandledException; 28 import org.eclipse.core.commands.Parameterization; 29 import org.eclipse.core.commands.ParameterizedCommand; 30 import org.eclipse.core.commands.common.NotDefinedException; 31 import org.eclipse.jface.action.Action; 32 import org.eclipse.jface.action.ContributionItem; 33 import org.eclipse.jface.action.IAction; 34 import org.eclipse.jface.action.IContributionItem; 35 import org.eclipse.jface.action.IMenuListener; 36 import org.eclipse.jface.action.IMenuManager; 37 import org.eclipse.jface.action.MenuManager; 38 import org.eclipse.jface.action.Separator; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.widgets.Menu; 41 import org.eclipse.swt.widgets.MenuItem; 42 import org.eclipse.ui.IWorkbenchPage; 43 import org.eclipse.ui.IWorkbenchWindow; 44 import org.eclipse.ui.activities.WorkbenchActivityHelper; 45 import org.eclipse.ui.commands.ICommandService; 46 import org.eclipse.ui.handlers.IHandlerService; 47 import org.eclipse.ui.internal.intro.IIntroConstants; 48 import org.eclipse.ui.views.IViewDescriptor; 49 import org.eclipse.ui.views.IViewRegistry; 50 51 import com.ibm.icu.text.Collator; 52 53 58 public class ShowViewMenu extends ContributionItem { 59 private static final String SHOW_VIEW_ID = "org.eclipse.ui.views.showView"; private static final String PARAMETER_MAKE_FAST = "org.eclipse.ui.views.showView.makeFast"; 62 private IWorkbenchWindow window; 63 64 private static final String NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoTargets; 65 66 private Comparator actionComparator = new Comparator () { 67 public int compare(Object o1, Object o2) { 68 if (collator == null) { 69 collator = Collator.getInstance(); 70 } 71 IAction a1 = (IAction) o1; 72 IAction a2 = (IAction) o2; 73 return collator.compare(a1.getText(), a2.getText()); 74 } 75 }; 76 77 private Action showDlgAction; 78 79 private Map actions = new HashMap (21); 80 81 private Map openedViews = new HashMap (); 83 84 protected boolean dirty = true; 85 86 private IMenuListener menuListener = new IMenuListener() { 87 public void menuAboutToShow(IMenuManager manager) { 88 manager.markDirty(); 89 dirty = true; 90 } 91 }; 92 93 private static Collator collator; 94 private boolean makeFast; 95 96 104 public ShowViewMenu(IWorkbenchWindow window, String id) { 105 this(window, id, false); 106 } 107 108 116 public ShowViewMenu(IWorkbenchWindow window, String id, final boolean makeFast) { 117 super(id); 118 this.window = window; 119 final IHandlerService handlerService = (IHandlerService) window 120 .getService(IHandlerService.class); 121 final ICommandService commandService = (ICommandService) window 122 .getService(ICommandService.class); 123 final ParameterizedCommand cmd = getCommand(commandService, makeFast); 124 125 showDlgAction = new Action(WorkbenchMessages.ShowView_title) { 126 public void run() { 127 try { 128 handlerService.executeCommand( 129 cmd, null); 130 } catch (final ExecutionException e) { 131 } catch (NotDefinedException e) { 133 } catch (NotEnabledException e) { 135 } catch (NotHandledException e) { 137 } 139 } 140 }; 141 142 window.getWorkbench().getHelpSystem().setHelp(showDlgAction, 143 IWorkbenchHelpContextIds.SHOW_VIEW_OTHER_ACTION); 144 ((WorkbenchWindow) window) 146 .addSubmenu(WorkbenchWindow.SHOW_VIEW_SUBMENU); 147 148 showDlgAction.setActionDefinitionId(SHOW_VIEW_ID); 149 this.makeFast = makeFast; 150 } 151 152 public boolean isDirty() { 153 return dirty; 154 } 155 156 159 public boolean isDynamic() { 160 return true; 161 } 162 163 166 private void fillMenu(IMenuManager innerMgr) { 167 innerMgr.removeAll(); 169 170 IWorkbenchPage page = window.getActivePage(); 172 if (page == null) { 173 return; 174 } 175 176 if (page.getPerspective() == null) { 178 return; 179 } 180 181 List viewIds = Arrays.asList(page.getShowViewShortcuts()); 183 184 viewIds = addOpenedViews(page, viewIds); 186 187 List actions = new ArrayList (viewIds.size()); 188 for (Iterator i = viewIds.iterator(); i.hasNext();) { 189 String id = (String ) i.next(); 190 if (id.equals(IIntroConstants.INTRO_VIEW_ID)) { 191 continue; 192 } 193 IAction action = getAction(id); 194 if (action != null) { 195 if (WorkbenchActivityHelper.filterItem(action)) { 196 continue; 197 } 198 actions.add(action); 199 } 200 } 201 Collections.sort(actions, actionComparator); 202 for (Iterator i = actions.iterator(); i.hasNext();) { 203 innerMgr.add((IAction) i.next()); 204 } 205 206 innerMgr.add(new Separator()); 208 innerMgr.add(showDlgAction); 209 } 210 211 private List addOpenedViews(IWorkbenchPage page, List actions) { 212 ArrayList views = getParts(page); 213 ArrayList result = new ArrayList (views.size() + actions.size()); 214 215 for (int i = 0; i < actions.size(); i++) { 216 Object element = actions.get(i); 217 if (result.indexOf(element) < 0) { 218 result.add(element); 219 } 220 } 221 for (int i = 0; i < views.size(); i++) { 222 Object element = views.get(i); 223 if (result.indexOf(element) < 0) { 224 result.add(element); 225 } 226 } 227 return result; 228 } 229 230 233 private IAction getAction(String id) { 234 IAction action = (IAction) actions.get(id); 237 if (action == null) { 238 IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry(); 239 IViewDescriptor desc = reg.find(id); 240 if (desc != null) { 241 action = new ShowViewAction(window, desc, makeFast); 242 action.setActionDefinitionId(id); 243 actions.put(id, action); 244 } 245 } 246 return action; 247 } 248 249 private ArrayList getParts(IWorkbenchPage page) { 250 ArrayList parts = (ArrayList ) openedViews.get(page); 251 if (parts == null) { 252 parts = new ArrayList (); 253 openedViews.put(page, parts); 254 } 255 return parts; 256 } 257 258 public void fill(Menu menu, int index) { 259 if (getParent() instanceof MenuManager) { 260 ((MenuManager) getParent()).addMenuListener(menuListener); 261 } 262 263 if (!dirty) { 264 return; 265 } 266 267 MenuManager manager = new MenuManager(); 268 fillMenu(manager); 269 IContributionItem items[] = manager.getItems(); 270 if (items.length == 0) { 271 MenuItem item = new MenuItem(menu, SWT.NONE, index++); 272 item.setText(NO_TARGETS_MSG); 273 item.setEnabled(false); 274 } else { 275 for (int i = 0; i < items.length; i++) { 276 items[i].fill(menu, index++); 277 } 278 } 279 dirty = false; 280 } 281 282 protected void removeAction(String viewId) { 284 actions.remove(viewId); 285 } 286 287 288 292 private ParameterizedCommand getCommand(ICommandService commandService, 293 final boolean makeFast) { 294 Command c = commandService.getCommand(SHOW_VIEW_ID); 295 Parameterization[] parms = null; 296 if (makeFast) { 297 try { 298 IParameter parmDef = c.getParameter(PARAMETER_MAKE_FAST); 299 parms = new Parameterization[] { 300 new Parameterization(parmDef, "true") }; 302 } catch (NotDefinedException e) { 303 } 305 } 306 return new ParameterizedCommand(c, parms); 307 } 308 } 309 | Popular Tags |