1 11 package org.eclipse.ui.internal.navigator.actions; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.ISafeRunnable; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.SafeRunner; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.ISelectionChangedListener; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 import org.eclipse.jface.viewers.SelectionChangedEvent; 24 import org.eclipse.ui.IEditorInput; 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IPartListener; 27 import org.eclipse.ui.IPropertyListener; 28 import org.eclipse.ui.IWorkbenchPage; 29 import org.eclipse.ui.IWorkbenchPart; 30 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages; 31 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 32 import org.eclipse.ui.internal.navigator.extensions.LinkHelperService; 33 import org.eclipse.ui.navigator.CommonNavigator; 34 import org.eclipse.ui.navigator.CommonViewer; 35 import org.eclipse.ui.navigator.ILinkHelper; 36 import org.eclipse.ui.navigator.INavigatorContentService; 37 import org.eclipse.ui.part.ISetSelectionTarget; 38 import org.eclipse.ui.progress.UIJob; 39 40 45 public class LinkEditorAction extends Action implements 46 ISelectionChangedListener, IAction, IPropertyListener { 47 48 private static final long BRIEF_DELAY = 100; 49 50 private IPartListener partListener; 51 52 private final CommonNavigator commonNavigator; 53 54 private final CommonViewer commonViewer; 55 56 private final LinkHelperService linkService; 57 58 private UIJob activateEditorJob = new UIJob( 59 CommonNavigatorMessages.Link_With_Editor_Job_) { 60 public IStatus runInUIThread(IProgressMonitor monitor) { 61 62 if (!commonViewer.getControl().isDisposed()) { 63 ISelection selection = commonViewer.getSelection(); 64 if (selection != null && !selection.isEmpty() 65 && selection instanceof IStructuredSelection) { 66 67 IStructuredSelection sSelection = (IStructuredSelection) selection; 68 if (sSelection.size() == 1) { 69 ILinkHelper[] helpers = linkService 70 .getLinkHelpersFor(sSelection.getFirstElement()); 71 if (helpers.length > 0) { 72 helpers[0].activateEditor(commonNavigator.getSite() 73 .getPage(), sSelection); 74 } 75 } 76 } 77 } 78 return Status.OK_STATUS; 79 } 80 }; 81 82 private UIJob updateSelectionJob = new UIJob( 83 CommonNavigatorMessages.Link_With_Editor_Job_) { 84 public IStatus runInUIThread(IProgressMonitor monitor) { 85 86 if (!commonNavigator.getCommonViewer().getControl().isDisposed()) { 87 SafeRunner.run(new ISafeRunnable() { 88 89 public void run() throws Exception { 90 IWorkbenchPage page = commonNavigator.getSite() 91 .getPage(); 92 if (page != null) { 93 IEditorPart editor = page.getActiveEditor(); 94 if (editor != null) { 95 IEditorInput input = editor.getEditorInput(); 96 IStructuredSelection newSelection = linkService 97 .getSelectionFor(input); 98 if (!newSelection.isEmpty()) { 99 commonNavigator.selectReveal(newSelection); 100 } 101 } 102 } 103 } 104 105 public void handleException(Throwable e) { 106 String msg = e.getMessage() != null ? e.getMessage() 107 : e.toString(); 108 NavigatorPlugin.logError(0, msg, e); 109 } 110 }); 111 112 } 113 114 return Status.OK_STATUS; 115 } 116 }; 117 118 129 public LinkEditorAction(CommonNavigator aNavigator, CommonViewer aViewer, 130 LinkHelperService linkHelperService) { 131 super(CommonNavigatorMessages.LinkEditorActionDelegate_0); 132 linkService = linkHelperService; 133 setToolTipText(CommonNavigatorMessages.LinkEditorActionDelegate_1); 134 commonNavigator = aNavigator; 135 commonViewer = aViewer; 136 init(); 137 } 138 139 142 protected void init() { 143 partListener = new IPartListener() { 144 145 public void partActivated(IWorkbenchPart part) { 146 if (part instanceof IEditorPart) { 147 updateSelectionJob.schedule(BRIEF_DELAY); 148 } 149 } 150 151 public void partBroughtToTop(IWorkbenchPart part) { 152 if (part instanceof IEditorPart) { 153 updateSelectionJob.schedule(BRIEF_DELAY); 154 } 155 } 156 157 public void partClosed(IWorkbenchPart part) { 158 159 } 160 161 public void partDeactivated(IWorkbenchPart part) { 162 } 163 164 public void partOpened(IWorkbenchPart part) { 165 } 166 }; 167 168 updateLinkingEnabled(commonNavigator.isLinkingEnabled()); 169 170 commonNavigator.addPropertyListener(this); 171 172 } 175 176 179 public void dispose() { 180 commonNavigator.removePropertyListener(this); 181 if (isChecked()) { 182 commonViewer.removePostSelectionChangedListener(this); 183 commonNavigator.getSite().getPage() 184 .removePartListener(partListener); 185 } 186 187 } 188 189 192 public void run() { 193 commonNavigator.setLinkingEnabled(!commonNavigator.isLinkingEnabled()); 194 } 195 196 201 public void selectionChanged(SelectionChangedEvent event) { 202 if (commonNavigator.isLinkingEnabled()) { 203 activateEditor(); 204 } 205 206 } 207 208 211 protected void activateEditor() { 212 ISelection selection = commonViewer.getSelection(); 213 if (selection != null && !selection.isEmpty() 214 && selection instanceof IStructuredSelection) { 215 219 activateEditorJob.schedule(BRIEF_DELAY); 220 } 221 } 222 223 229 public void propertyChanged(Object aSource, int aPropertyId) { 230 switch (aPropertyId) { 231 case CommonNavigator.IS_LINKING_ENABLED_PROPERTY: 232 updateLinkingEnabled(((CommonNavigator) aSource).isLinkingEnabled()); 233 } 234 } 235 236 239 private void updateLinkingEnabled(boolean toEnableLinking) { 240 setChecked(toEnableLinking); 241 242 if (toEnableLinking) { 243 244 updateSelectionJob.schedule(BRIEF_DELAY); 245 246 commonViewer.addPostSelectionChangedListener(this); 247 commonNavigator.getSite().getPage().addPartListener(partListener); 248 } else { 249 commonViewer.removePostSelectionChangedListener(this); 250 commonNavigator.getSite().getPage() 251 .removePartListener(partListener); 252 } 253 } 254 255 } 256 | Popular Tags |