1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.MultiStatus; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.core.resources.IFile; 22 23 import org.eclipse.jface.dialogs.ErrorDialog; 24 import org.eclipse.jface.util.OpenStrategy; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 27 import org.eclipse.jface.text.ITextSelection; 28 29 import org.eclipse.ui.IEditorPart; 30 import org.eclipse.ui.IWorkbenchSite; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.texteditor.IEditorStatusLine; 34 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.ISourceReference; 38 import org.eclipse.jdt.core.JavaModelException; 39 40 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 41 import org.eclipse.jdt.internal.corext.util.Messages; 42 43 import org.eclipse.jdt.ui.JavaUI; 44 45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 48 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 49 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 50 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 51 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 52 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 53 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; 54 55 68 public class OpenAction extends SelectionDispatchAction { 69 70 private JavaEditor fEditor; 71 72 79 public OpenAction(IWorkbenchSite site) { 80 super(site); 81 setText(ActionMessages.OpenAction_label); 82 setToolTipText(ActionMessages.OpenAction_tooltip); 83 setDescription(ActionMessages.OpenAction_description); 84 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_ACTION); 85 } 86 87 91 public OpenAction(JavaEditor editor) { 92 this(editor.getEditorSite()); 93 fEditor= editor; 94 setText(ActionMessages.OpenAction_declaration_label); 95 setEnabled(EditorUtility.getEditorInputJavaElement(fEditor, false) != null); 96 } 97 98 101 public void selectionChanged(ITextSelection selection) { 102 } 103 104 107 public void selectionChanged(IStructuredSelection selection) { 108 setEnabled(checkEnabled(selection)); 109 } 110 111 private boolean checkEnabled(IStructuredSelection selection) { 112 if (selection.isEmpty()) 113 return false; 114 for (Iterator iter= selection.iterator(); iter.hasNext();) { 115 Object element= iter.next(); 116 if (element instanceof ISourceReference) 117 continue; 118 if (element instanceof IFile) 119 continue; 120 if (JavaModelUtil.isOpenableStorage(element)) 121 continue; 122 return false; 123 } 124 return true; 125 } 126 127 130 public void run(ITextSelection selection) { 131 if (!isProcessable()) 132 return; 133 try { 134 IJavaElement[] elements= SelectionConverter.codeResolveForked(fEditor, false); 135 if (elements == null || elements.length == 0) { 136 IEditorStatusLine statusLine= (IEditorStatusLine) fEditor.getAdapter(IEditorStatusLine.class); 137 if (statusLine != null) 138 statusLine.setMessage(true, ActionMessages.OpenAction_error_messageBadSelection, null); 139 getShell().getDisplay().beep(); 140 return; 141 } 142 IJavaElement element= elements[0]; 143 if (elements.length > 1) { 144 element= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.OpenAction_select_element); 145 if (element == null) 146 return; 147 } 148 149 int type= element.getElementType(); 150 if (type == IJavaElement.JAVA_PROJECT || type == IJavaElement.PACKAGE_FRAGMENT_ROOT || type == IJavaElement.PACKAGE_FRAGMENT) 151 element= EditorUtility.getEditorInputJavaElement(fEditor, false); 152 run(new Object [] {element} ); 153 } catch (InvocationTargetException e) { 154 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message); 155 } catch (InterruptedException e) { 156 } 158 } 159 160 private boolean isProcessable() { 161 if (fEditor != null) { 162 IJavaElement je= EditorUtility.getEditorInputJavaElement(fEditor, false); 163 if (je instanceof ICompilationUnit && !JavaModelUtil.isPrimary((ICompilationUnit)je)) 164 return true; } 166 return ActionUtil.isProcessable(fEditor); 167 } 168 169 172 public void run(IStructuredSelection selection) { 173 if (!checkEnabled(selection)) 174 return; 175 run(selection.toArray()); 176 } 177 178 183 public void run(Object [] elements) { 184 if (elements == null) 185 return; 186 187 MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, ActionMessages.OpenAction_multistatus_message, null); 188 189 for (int i= 0; i < elements.length; i++) { 190 Object element= elements[i]; 191 try { 192 element= getElementToOpen(element); 193 boolean activateOnOpen= fEditor != null ? true : OpenStrategy.activateOnOpen(); 194 IEditorPart part= EditorUtility.openInEditor(element, activateOnOpen); 195 if (part != null && element instanceof IJavaElement) 196 JavaUI.revealInEditor(part, (IJavaElement)element); 197 } catch (PartInitException e) { 198 String message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String [] { new JavaUILabelProvider().getText(element), e.getStatus().getMessage() }); 199 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null)); 200 } catch (CoreException e) { 201 String message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String [] { new JavaUILabelProvider().getText(element), e.getStatus().getMessage() }); 202 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null)); 203 JavaPlugin.log(e); 204 } 205 } 206 if (!status.isOK()) { 207 IStatus[] children= status.getChildren(); 208 ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message, children.length == 1 ? children[0] : status); 209 } 210 } 211 212 219 public Object getElementToOpen(Object object) throws JavaModelException { 220 return object; 221 } 222 223 private String getDialogTitle() { 224 return ActionMessages.OpenAction_error_title; 225 } 226 } 227 | Popular Tags |