1 11 12 package org.eclipse.ui.internal; 13 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.viewers.ISelection; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 import org.eclipse.ui.IEditorPart; 18 import org.eclipse.ui.IViewPart; 19 import org.eclipse.ui.IWorkbenchPage; 20 import org.eclipse.ui.IWorkbenchPart; 21 import org.eclipse.ui.IWorkbenchWindow; 22 import org.eclipse.ui.PartInitException; 23 import org.eclipse.ui.internal.util.Util; 24 import org.eclipse.ui.part.IShowInSource; 25 import org.eclipse.ui.part.IShowInTarget; 26 import org.eclipse.ui.part.ShowInContext; 27 import org.eclipse.ui.views.IViewDescriptor; 28 29 32 public class ShowInAction extends Action { 33 private IWorkbenchWindow window; 34 35 private IViewDescriptor desc; 36 37 40 protected ShowInAction(IWorkbenchWindow window, IViewDescriptor desc) { 41 super(desc.getLabel()); 42 setImageDescriptor(desc.getImageDescriptor()); 43 window.getWorkbench().getHelpSystem().setHelp(this, 44 IWorkbenchHelpContextIds.SHOW_IN_ACTION); 45 this.window = window; 46 this.desc = desc; 47 } 48 49 52 public void run() { 53 IWorkbenchPage page = window.getActivePage(); 54 if (page == null) { 55 beep(); 56 return; 57 } 58 59 IWorkbenchPart sourcePart = page.getActivePart(); 60 if (sourcePart == null) { 61 beep(); 62 return; 63 } 64 65 ShowInContext context = getContext(sourcePart); 66 if (context == null) { 67 beep(); 68 return; 69 } 70 71 try { 72 IViewPart view = page.showView(desc.getId()); 73 IShowInTarget target = getShowInTarget(view); 74 if (target != null && target.show(context)) { 75 } else { 77 beep(); 78 } 79 ((WorkbenchPage) page).performedShowIn(desc.getId()); } catch (PartInitException e) { 81 WorkbenchPlugin.log( 82 "Error showing view in ShowInAction.run", e.getStatus()); } 84 } 85 86 93 private IShowInSource getShowInSource(IWorkbenchPart sourcePart) { 94 return (IShowInSource)Util.getAdapter(sourcePart, IShowInSource.class); 95 } 96 97 104 private IShowInTarget getShowInTarget(IWorkbenchPart targetPart) { 105 return (IShowInTarget)Util.getAdapter(targetPart, IShowInTarget.class); 106 } 107 108 120 private ShowInContext getContext(IWorkbenchPart sourcePart) { 121 IShowInSource source = getShowInSource(sourcePart); 122 if (source != null) { 123 ShowInContext context = source.getShowInContext(); 124 if (context != null) { 125 return context; 126 } 127 } else if (sourcePart instanceof IEditorPart) { 128 Object input = ((IEditorPart) sourcePart).getEditorInput(); 129 ISelectionProvider sp = sourcePart.getSite().getSelectionProvider(); 130 ISelection sel = sp == null ? null : sp.getSelection(); 131 return new ShowInContext(input, sel); 132 } 133 return null; 134 } 135 136 139 private void beep() { 140 window.getShell().getDisplay().beep(); 141 } 142 143 } 144 | Popular Tags |