1 13 14 package org.eclipse.ui.views.markers.internal; 15 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.resources.IMarker; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jface.util.OpenStrategy; 22 import org.eclipse.jface.viewers.ISelectionProvider; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.ui.IEditorInput; 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.PartInitException; 28 import org.eclipse.ui.ide.IDE; 29 import org.eclipse.ui.ide.ResourceUtil; 30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 31 import org.eclipse.ui.statushandlers.StatusAdapter; 32 import org.eclipse.ui.statushandlers.StatusManager; 33 34 37 public class ActionOpenMarker extends MarkerSelectionProviderAction { 38 39 private final String IMAGE_PATH = "elcl16/gotoobj_tsk.gif"; 41 private final String DISABLED_IMAGE_PATH = "dlcl16/gotoobj_tsk.gif"; 43 protected IWorkbenchPart part; 44 45 51 public ActionOpenMarker(IWorkbenchPart part, ISelectionProvider provider) { 52 super(provider, MarkerMessages.openAction_title); 53 this.part = part; 54 setImageDescriptor(IDEWorkbenchPlugin.getIDEImageDescriptor(IMAGE_PATH)); 55 setDisabledImageDescriptor(IDEWorkbenchPlugin 56 .getIDEImageDescriptor(DISABLED_IMAGE_PATH)); 57 setEnabled(false); 58 } 59 60 65 public void run() { 66 IMarker[] markers = getSelectedMarkers(); 67 for (int i = 0; i < markers.length; i++) { 68 IMarker marker = markers[i]; 69 70 IEditorPart editor = part.getSite().getPage().getActiveEditor(); 75 if (editor != null) { 76 IEditorInput input = editor.getEditorInput(); 77 IFile file = ResourceUtil.getFile(input); 78 if (file != null) { 79 if (marker.getResource().equals(file)) { 80 part.getSite().getPage().activate(editor); 81 } 82 } 83 } 84 85 if (marker.getResource() instanceof IFile) { 86 try { 87 IFile file = (IFile) marker.getResource(); 88 if (file.getLocation() == null 89 || file.getLocationURI() == null) 90 return; IDE.openEditor(part.getSite().getPage(), marker, 92 OpenStrategy.activateOnOpen()); 93 } catch (PartInitException e) { 94 98 CoreException nestedException = null; 100 IStatus status = e.getStatus(); 101 if (status != null 102 && status.getException() instanceof CoreException) { 103 nestedException = (CoreException) status.getException(); 104 } 105 106 if (nestedException != null) { 107 reportStatus(nestedException.getStatus()); 110 } else { 111 reportError(e.getLocalizedMessage()); 114 } 115 } 116 } 117 } 118 } 119 120 125 private void reportError(String message) { 126 IStatus status = new Status(IStatus.ERROR, 127 IDEWorkbenchPlugin.IDE_WORKBENCH, message); 128 reportStatus(status); 129 } 130 131 136 private void reportStatus(IStatus status) { 137 StatusAdapter adapter = new StatusAdapter(status); 138 adapter.setProperty(StatusAdapter.TITLE_PROPERTY, 139 MarkerMessages.OpenMarker_errorTitle); 140 StatusManager.getManager().handle(adapter, StatusManager.SHOW); 141 } 142 143 148 public void selectionChanged(IStructuredSelection selection) { 149 setEnabled(Util.allConcreteSelection(selection)); 150 } 151 } 152 | Popular Tags |