1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.eclipse.jface.action.ContributionItem; 20 import org.eclipse.jface.action.IAction; 21 import org.eclipse.jface.action.IContributionItem; 22 import org.eclipse.jface.action.IMenuListener; 23 import org.eclipse.jface.action.IMenuManager; 24 import org.eclipse.jface.action.MenuManager; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.ISelectionProvider; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.widgets.Menu; 29 import org.eclipse.swt.widgets.MenuItem; 30 import org.eclipse.ui.IEditorPart; 31 import org.eclipse.ui.IWorkbenchPage; 32 import org.eclipse.ui.IWorkbenchPart; 33 import org.eclipse.ui.IWorkbenchWindow; 34 import org.eclipse.ui.internal.util.Util; 35 import org.eclipse.ui.part.IShowInSource; 36 import org.eclipse.ui.part.IShowInTargetList; 37 import org.eclipse.ui.part.ShowInContext; 38 import org.eclipse.ui.views.IViewDescriptor; 39 import org.eclipse.ui.views.IViewRegistry; 40 41 46 public class ShowInMenu extends ContributionItem { 47 48 private static final String NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoTargets; 49 50 private IWorkbenchWindow window; 51 52 private Map actions = new HashMap (21); 53 54 private boolean dirty = true; 55 56 private IMenuListener menuListener = new IMenuListener() { 57 public void menuAboutToShow(IMenuManager manager) { 58 manager.markDirty(); 59 dirty = true; 60 } 61 }; 62 63 68 public ShowInMenu(IWorkbenchWindow window, String id) { 69 super(id); 70 this.window = window; 71 } 72 73 protected IWorkbenchWindow getWindow() { 74 return window; 75 } 76 77 public boolean isDirty() { 78 return dirty; 79 } 80 81 84 public boolean isDynamic() { 85 return true; 86 } 87 88 public void fill(Menu menu, int index) { 89 if (getParent() instanceof MenuManager) { 90 ((MenuManager) getParent()).addMenuListener(menuListener); 91 } 92 93 if (!dirty) { 94 return; 95 } 96 97 MenuManager manager = new MenuManager(); 98 fillMenu(manager); 99 IContributionItem[] items = manager.getItems(); 100 if (items.length == 0) { 101 MenuItem item = new MenuItem(menu, SWT.NONE, index++); 102 item.setText(NO_TARGETS_MSG); 103 item.setEnabled(false); 104 } else { 105 for (int i = 0; i < items.length; i++) { 106 items[i].fill(menu, index++); 107 } 108 } 109 dirty = false; 110 } 111 112 115 private void fillMenu(IMenuManager innerMgr) { 116 innerMgr.removeAll(); 118 119 IWorkbenchPart sourcePart = getSourcePart(); 120 if (sourcePart == null) { 121 return; 122 } 123 ShowInContext context = getContext(sourcePart); 124 if (context == null) { 125 return; 126 } 127 if (context.getInput() == null 128 && (context.getSelection() == null || context.getSelection() 129 .isEmpty())) { 130 return; 131 } 132 133 IViewDescriptor[] viewDescs = getViewDescriptors(sourcePart); 134 if (viewDescs.length == 0) { 135 return; 136 } 137 138 for (int i = 0; i < viewDescs.length; ++i) { 139 IAction action = getAction(viewDescs[i]); 140 if (action != null) { 141 innerMgr.add(action); 142 } 143 } 144 } 145 146 149 private IAction getAction(IViewDescriptor desc) { 150 IAction action = (IAction) actions.get(desc.getId()); 153 if (action == null) { 154 if (desc != null) { 155 action = new ShowInAction(window, desc); 156 actions.put(desc.getId(), action); 157 } 158 } 159 return action; 160 } 161 162 166 private ArrayList getShowInPartIds(IWorkbenchPart sourcePart) { 167 ArrayList targetIds = new ArrayList (); 168 WorkbenchPage page = (WorkbenchPage) getWindow().getActivePage(); 169 if (page != null) { 170 targetIds.addAll(page.getShowInPartIds()); 171 } 172 IShowInTargetList targetList = getShowInTargetList(sourcePart); 173 if (targetList != null) { 174 String [] partIds = targetList.getShowInTargetIds(); 175 if (partIds != null) { 176 for (int i = 0; i < partIds.length; ++i) { 177 if (!targetIds.contains(partIds[i])) { 178 targetIds.add(partIds[i]); 179 } 180 } 181 } 182 } 183 page.sortShowInPartIds(targetIds); 184 return targetIds; 185 } 186 187 196 private IWorkbenchPart getSourcePart() { 197 IWorkbenchPage page = getWindow().getActivePage(); 198 if (page != null) { 199 return page.getActivePart(); 200 } 201 return null; 202 } 203 204 211 private IShowInSource getShowInSource(IWorkbenchPart sourcePart) { 212 return (IShowInSource)Util.getAdapter(sourcePart, IShowInSource.class); 213 } 214 215 222 private IShowInTargetList getShowInTargetList(IWorkbenchPart sourcePart) { 223 return (IShowInTargetList)Util.getAdapter(sourcePart, IShowInTargetList.class); 224 } 225 226 238 private ShowInContext getContext(IWorkbenchPart sourcePart) { 239 IShowInSource source = getShowInSource(sourcePart); 240 if (source != null) { 241 ShowInContext context = source.getShowInContext(); 242 if (context != null) { 243 return context; 244 } 245 } else if (sourcePart instanceof IEditorPart) { 246 Object input = ((IEditorPart) sourcePart).getEditorInput(); 247 ISelectionProvider sp = sourcePart.getSite().getSelectionProvider(); 248 ISelection sel = sp == null ? null : sp.getSelection(); 249 return new ShowInContext(input, sel); 250 } 251 return null; 252 } 253 254 257 private IViewDescriptor[] getViewDescriptors(IWorkbenchPart sourcePart) { 258 String srcId = sourcePart.getSite().getId(); 259 ArrayList ids = getShowInPartIds(sourcePart); 260 ArrayList descs = new ArrayList (); 261 IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry(); 262 for (Iterator i = ids.iterator(); i.hasNext();) { 263 String id = (String ) i.next(); 264 if (!id.equals(srcId)) { 265 IViewDescriptor desc = reg.find(id); 266 if (desc != null) { 267 descs.add(desc); 268 } 269 } 270 } 271 return (IViewDescriptor[]) descs.toArray(new IViewDescriptor[descs 272 .size()]); 273 } 274 275 } 276 | Popular Tags |