1 11 package org.eclipse.ui.actions; 12 13 import com.ibm.icu.text.Collator; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.eclipse.jface.action.Action; 23 import org.eclipse.jface.action.ContributionItem; 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.action.IContributionItem; 26 import org.eclipse.jface.action.IMenuListener; 27 import org.eclipse.jface.action.IMenuManager; 28 import org.eclipse.jface.action.MenuManager; 29 import org.eclipse.jface.action.Separator; 30 import org.eclipse.jface.window.Window; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.widgets.Event; 34 import org.eclipse.swt.widgets.Menu; 35 import org.eclipse.swt.widgets.MenuItem; 36 import org.eclipse.ui.IPerspectiveDescriptor; 37 import org.eclipse.ui.IPerspectiveRegistry; 38 import org.eclipse.ui.IWorkbenchPage; 39 import org.eclipse.ui.IWorkbenchWindow; 40 import org.eclipse.ui.activities.WorkbenchActivityHelper; 41 import org.eclipse.ui.internal.WorkbenchMessages; 42 import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog; 43 44 59 public abstract class PerspectiveMenu extends ContributionItem { 60 private IPerspectiveRegistry reg; 61 62 private IWorkbenchWindow window; 63 64 private boolean showActive = false; 65 66 private boolean dirty = true; 67 68 private IMenuListener menuListener = new IMenuListener() { 69 public void menuAboutToShow(IMenuManager manager) { 70 manager.markDirty(); 71 dirty = true; 72 } 73 }; 74 75 private Comparator comparator = new Comparator () { 76 private Collator collator = Collator.getInstance(); 77 78 public int compare(Object ob1, Object ob2) { 79 IPerspectiveDescriptor d1 = (IPerspectiveDescriptor) ob1; 80 IPerspectiveDescriptor d2 = (IPerspectiveDescriptor) ob2; 81 return collator.compare(d1.getLabel(), d2.getLabel()); 82 } 83 }; 84 85 90 private static final String NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoPerspectives; 91 92 99 private Map actions = new HashMap (); 100 101 106 private Action openOtherAction = new Action(WorkbenchMessages.PerspectiveMenu_otherItem) { 107 public final void runWithEvent(final Event event) { 108 runOther(new SelectionEvent(event)); 109 } 110 }; 111 112 113 119 public PerspectiveMenu(IWorkbenchWindow window, String id) { 120 super(id); 121 this.window = window; 122 reg = window.getWorkbench().getPerspectiveRegistry(); 123 openOtherAction 124 .setActionDefinitionId("org.eclipse.ui.perspectives.showPerspective"); } 126 127 130 public void fill(Menu menu, int index) { 131 if (getParent() instanceof MenuManager) { 132 ((MenuManager) getParent()).addMenuListener(menuListener); 133 } 134 135 if (!dirty) { 136 return; 137 } 138 139 final MenuManager manager = new MenuManager(); 140 fillMenu(manager); 141 final IContributionItem items[] = manager.getItems(); 142 if (items.length == 0) { 143 MenuItem item = new MenuItem(menu, SWT.NONE, index++); 144 item.setText(NO_TARGETS_MSG); 145 item.setEnabled(false); 146 } else { 147 for (int i = 0; i < items.length; i++) { 148 items[i].fill(menu, index++); 149 } 150 } 151 dirty = false; 152 } 153 154 164 private final void fillMenu(final MenuManager manager) { 165 manager.removeAll(); 167 168 final List persps = getPerspectiveItems(); 170 Collections.sort(persps, comparator); 171 172 176 final List actions = new ArrayList (persps.size()); 177 for (Iterator i = persps.iterator(); i.hasNext();) { 178 final IPerspectiveDescriptor descriptor = (IPerspectiveDescriptor) i 179 .next(); 180 final IAction action = getAction(descriptor.getId()); 181 if (action != null) { 182 if (WorkbenchActivityHelper.filterItem(action)) { 183 continue; 184 } 185 actions.add(action); 186 } 187 } 188 189 for (Iterator i = actions.iterator(); i.hasNext();) { 191 manager.add((IAction) i.next()); 192 } 193 194 if (actions.size() > 0) { 196 manager.add(new Separator()); 197 } 198 manager.add(openOtherAction); 199 } 200 201 213 private final IAction getAction(final String id) { 214 IAction action = (IAction) actions.get(id); 215 if (action == null) { 216 IPerspectiveDescriptor descriptor = reg.findPerspectiveWithId(id); 217 if (descriptor != null) { 218 action = new OpenPerspectiveAction(window, descriptor, this); 219 action.setActionDefinitionId(id); 220 actions.put(id, action); 221 } 222 } 223 return action; 224 } 225 226 231 private ArrayList getPerspectiveShortcuts() { 232 ArrayList list = new ArrayList (); 233 234 IWorkbenchPage page = window.getActivePage(); 235 if (page == null) { 236 return list; 237 } 238 239 String [] ids = page.getPerspectiveShortcuts(); 240 241 for (int i = 0; i < ids.length; i++) { 242 IPerspectiveDescriptor desc = reg.findPerspectiveWithId(ids[i]); 243 if (desc != null && !list.contains(desc)) { 244 if (WorkbenchActivityHelper.filterItem(desc)) { 245 continue; 246 } 247 list.add(desc); 248 } 249 } 250 251 return list; 252 } 253 254 265 protected ArrayList getPerspectiveItems() { 266 268 ArrayList shortcuts = getPerspectiveShortcuts(); 269 ArrayList list = new ArrayList (shortcuts.size()); 270 271 int size = shortcuts.size(); 273 for (int i = 0; i < size; i++) { 274 if (!list.contains(shortcuts.get(i))) { 275 list.add(shortcuts.get(i)); 276 } 277 } 278 279 return list; 280 } 281 282 288 protected boolean getShowActive() { 289 return showActive; 290 } 291 292 297 protected IWorkbenchWindow getWindow() { 298 return window; 299 } 300 301 304 public boolean isDirty() { 305 return dirty; 306 } 307 308 311 public boolean isDynamic() { 312 return true; 313 } 314 315 321 protected abstract void run(IPerspectiveDescriptor desc); 322 323 334 protected void run(IPerspectiveDescriptor desc, SelectionEvent event) { 335 run(desc); 337 } 338 339 343 void runOther(SelectionEvent event) { 344 SelectPerspectiveDialog dlg = new SelectPerspectiveDialog(window 345 .getShell(), reg); 346 dlg.open(); 347 if (dlg.getReturnCode() == Window.CANCEL) { 348 return; 349 } 350 IPerspectiveDescriptor desc = dlg.getSelection(); 351 if (desc != null) { 352 run(desc, event); 353 } 354 } 355 356 362 protected void showActive(boolean b) { 363 showActive = b; 364 } 365 } 366 | Popular Tags |