|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * IBM Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package ts.eclipse.ide.jsdt.internal.ui.actions; |
| 12 | + |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | + |
| 15 | +import org.eclipse.core.runtime.IAdaptable; |
| 16 | +import org.eclipse.core.runtime.IStatus; |
| 17 | +import org.eclipse.jface.dialogs.ErrorDialog; |
| 18 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 19 | +import org.eclipse.jface.text.ITextSelection; |
| 20 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 21 | +import org.eclipse.jface.window.Window; |
| 22 | +import org.eclipse.ui.IWorkbenchSite; |
| 23 | +import org.eclipse.ui.PlatformUI; |
| 24 | +import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
| 25 | +import org.eclipse.ui.progress.IProgressService; |
| 26 | +import org.eclipse.wst.jsdt.core.IClassFile; |
| 27 | +import org.eclipse.wst.jsdt.core.IJavaScriptElement; |
| 28 | +import org.eclipse.wst.jsdt.core.IJavaScriptUnit; |
| 29 | +import org.eclipse.wst.jsdt.core.ILocalVariable; |
| 30 | +import org.eclipse.wst.jsdt.core.IMember; |
| 31 | +import org.eclipse.wst.jsdt.core.IPackageFragment; |
| 32 | +import org.eclipse.wst.jsdt.core.IType; |
| 33 | +import org.eclipse.wst.jsdt.core.JavaScriptCore; |
| 34 | +import org.eclipse.wst.jsdt.core.JavaScriptModelException; |
| 35 | +import org.eclipse.wst.jsdt.core.search.IJavaScriptSearchScope; |
| 36 | +import org.eclipse.wst.jsdt.internal.corext.util.JavaModelUtil; |
| 37 | +import org.eclipse.wst.jsdt.internal.ui.JavaScriptPlugin; |
| 38 | +import org.eclipse.wst.jsdt.internal.ui.actions.ActionUtil; |
| 39 | +import org.eclipse.wst.jsdt.internal.ui.actions.SelectionConverter; |
| 40 | +import org.eclipse.wst.jsdt.internal.ui.javaeditor.JavaEditor; |
| 41 | +import org.eclipse.wst.jsdt.internal.ui.search.JavaSearchQuery; |
| 42 | +import org.eclipse.wst.jsdt.internal.ui.search.JavaSearchScopeFactory; |
| 43 | +import org.eclipse.wst.jsdt.internal.ui.search.SearchMessages; |
| 44 | +import org.eclipse.wst.jsdt.internal.ui.search.SearchUtil; |
| 45 | +import org.eclipse.wst.jsdt.internal.ui.util.ExceptionHandler; |
| 46 | +import org.eclipse.wst.jsdt.ui.JavaScriptElementLabelProvider; |
| 47 | +import org.eclipse.wst.jsdt.ui.search.ElementQuerySpecification; |
| 48 | +import org.eclipse.wst.jsdt.ui.search.QuerySpecification; |
| 49 | + |
| 50 | +import ts.eclipse.ide.jsdt.internal.ui.editor.TypeScriptEditor; |
| 51 | + |
| 52 | +/** |
| 53 | + * Abstract class for JavaScript search actions. |
| 54 | + * <p> |
| 55 | + * Note: This class is for internal use only. Clients should not use this class. |
| 56 | + * </p> |
| 57 | + * |
| 58 | + * |
| 59 | + * Provisional API: This class/interface is part of an interim API that is still under development and expected to |
| 60 | + * change significantly before reaching stability. It is being made available at this early stage to solicit feedback |
| 61 | + * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken |
| 62 | + * (repeatedly) as the API evolves. |
| 63 | + */ |
| 64 | +public abstract class FindAction extends SelectionDispatchAction { |
| 65 | + |
| 66 | + // A dummy which can't be selected in the UI |
| 67 | + private static final IJavaScriptElement RETURN_WITHOUT_BEEP= JavaScriptCore.create(JavaScriptPlugin.getWorkspace().getRoot()); |
| 68 | + |
| 69 | + private Class[] fValidTypes; |
| 70 | + private TypeScriptEditor fEditor; |
| 71 | + |
| 72 | + |
| 73 | + FindAction(IWorkbenchSite site) { |
| 74 | + super(site); |
| 75 | + fValidTypes= getValidTypes(); |
| 76 | + init(); |
| 77 | + } |
| 78 | + |
| 79 | + FindAction(TypeScriptEditor editor) { |
| 80 | + this(editor.getEditorSite()); |
| 81 | + fEditor= editor; |
| 82 | + //setEnabled(SelectionConverter.canOperateOn(fEditor)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Called once by the constructors to initialize label, tooltip, image and help support of the action. |
| 87 | + * To be overridden by implementors of this action. |
| 88 | + */ |
| 89 | + abstract void init(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Called once by the constructors to get the list of the valid input types of the action. |
| 93 | + * To be overridden by implementors of this action. |
| 94 | + * @return the valid input types of the action |
| 95 | + */ |
| 96 | + abstract Class[] getValidTypes(); |
| 97 | + |
| 98 | + private boolean canOperateOn(IStructuredSelection sel) { |
| 99 | + return sel != null && !sel.isEmpty() && canOperateOn(getJavaElement(sel, true)); |
| 100 | + } |
| 101 | + |
| 102 | + boolean canOperateOn(IJavaScriptElement element) { |
| 103 | + if (element == null || fValidTypes == null || fValidTypes.length == 0 || !ActionUtil.isOnBuildPath(element)) |
| 104 | + return false; |
| 105 | + |
| 106 | + for (int i= 0; i < fValidTypes.length; i++) { |
| 107 | + if (fValidTypes[i].isInstance(element)) { |
| 108 | + if (element.getElementType() == IJavaScriptElement.PACKAGE_FRAGMENT) |
| 109 | + return hasChildren((IPackageFragment)element); |
| 110 | + else |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + private boolean hasChildren(IPackageFragment packageFragment) { |
| 118 | + try { |
| 119 | + return packageFragment.hasChildren(); |
| 120 | + } catch (JavaScriptModelException ex) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private IJavaScriptElement getTypeIfPossible(IJavaScriptElement o, boolean silent) { |
| 126 | + switch (o.getElementType()) { |
| 127 | + case IJavaScriptElement.JAVASCRIPT_UNIT: |
| 128 | + if (silent) |
| 129 | + return o; |
| 130 | + else |
| 131 | + return findType((IJavaScriptUnit)o, silent); |
| 132 | + case IJavaScriptElement.CLASS_FILE: |
| 133 | + return ((IClassFile)o).getType(); |
| 134 | + default: |
| 135 | + return o; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + IJavaScriptElement getJavaElement(IStructuredSelection selection, boolean silent) { |
| 140 | + if (selection.size() == 1) { |
| 141 | + Object firstElement= selection.getFirstElement(); |
| 142 | + IJavaScriptElement elem= null; |
| 143 | + if (firstElement instanceof IJavaScriptElement) |
| 144 | + elem= (IJavaScriptElement) firstElement; |
| 145 | + else if (firstElement instanceof IAdaptable) |
| 146 | + elem= (IJavaScriptElement) ((IAdaptable) firstElement).getAdapter(IJavaScriptElement.class); |
| 147 | + if (elem != null) { |
| 148 | + return getTypeIfPossible(elem, silent); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + private void showOperationUnavailableDialog() { |
| 156 | + MessageDialog.openInformation(getShell(), SearchMessages.JavaElementAction_operationUnavailable_title, getOperationUnavailableMessage()); |
| 157 | + } |
| 158 | + |
| 159 | + String getOperationUnavailableMessage() { |
| 160 | + return SearchMessages.JavaElementAction_operationUnavailable_generic; |
| 161 | + } |
| 162 | + |
| 163 | + private IJavaScriptElement findType(IJavaScriptUnit cu, boolean silent) { |
| 164 | + IType[] types= null; |
| 165 | + try { |
| 166 | + types= cu.getAllTypes(); |
| 167 | + } catch (JavaScriptModelException ex) { |
| 168 | + if (JavaModelUtil.isExceptionToBeLogged(ex)) |
| 169 | + ExceptionHandler.log(ex, SearchMessages.JavaElementAction_error_open_message); |
| 170 | + if (silent) |
| 171 | + return RETURN_WITHOUT_BEEP; |
| 172 | + else |
| 173 | + return null; |
| 174 | + } |
| 175 | + if (types.length == 1 || (silent && types.length > 0)) |
| 176 | + return types[0]; |
| 177 | + if (silent) |
| 178 | + return RETURN_WITHOUT_BEEP; |
| 179 | + if (types.length == 0) |
| 180 | + return null; |
| 181 | + String title= SearchMessages.JavaElementAction_typeSelectionDialog_title; |
| 182 | + String message = SearchMessages.JavaElementAction_typeSelectionDialog_message; |
| 183 | + int flags= (JavaScriptElementLabelProvider.SHOW_DEFAULT); |
| 184 | + |
| 185 | + ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaScriptElementLabelProvider(flags)); |
| 186 | + dialog.setTitle(title); |
| 187 | + dialog.setMessage(message); |
| 188 | + dialog.setElements(types); |
| 189 | + |
| 190 | + if (dialog.open() == Window.OK) |
| 191 | + return (IType)dialog.getFirstResult(); |
| 192 | + else |
| 193 | + return RETURN_WITHOUT_BEEP; |
| 194 | + } |
| 195 | + |
| 196 | + /* |
| 197 | + * Method declared on SelectionChangedAction. |
| 198 | + */ |
| 199 | + public void run(IStructuredSelection selection) { |
| 200 | + IJavaScriptElement element= getJavaElement(selection, false); |
| 201 | + if (element == null || !element.exists()) { |
| 202 | + showOperationUnavailableDialog(); |
| 203 | + return; |
| 204 | + } |
| 205 | + else if (element == RETURN_WITHOUT_BEEP) |
| 206 | + return; |
| 207 | + |
| 208 | + run(element); |
| 209 | + } |
| 210 | + |
| 211 | + /* |
| 212 | + * Method declared on SelectionChangedAction. |
| 213 | + */ |
| 214 | + public void run(ITextSelection selection) { |
| 215 | + //if (!ActionUtil.isProcessable(fEditor)) |
| 216 | + // return; |
| 217 | +// try { |
| 218 | +// String title= SearchMessages.SearchElementSelectionDialog_title; |
| 219 | +// String message= SearchMessages.SearchElementSelectionDialog_message; |
| 220 | +// |
| 221 | +//// IJavaScriptElement[] elements= SelectionConverter.codeResolveForked(fEditor, true); |
| 222 | +//// if (elements.length > 0 && canOperateOn(elements[0])) { |
| 223 | +//// IJavaScriptElement element= elements[0]; |
| 224 | +//// if (elements.length > 1) |
| 225 | +//// element= SelectionConverter.selectJavaElement(elements, getShell(), title, message); |
| 226 | +//// if (element != null) |
| 227 | +//// run(element); |
| 228 | +//// } |
| 229 | +//// else |
| 230 | +//// showOperationUnavailableDialog(); |
| 231 | +// } catch (InvocationTargetException ex) { |
| 232 | +// String title= SearchMessages.Search_Error_search_title; |
| 233 | +// String message= SearchMessages.Search_Error_codeResolve; |
| 234 | +// ExceptionHandler.handle(ex, getShell(), title, message); |
| 235 | +// } catch (InterruptedException e) { |
| 236 | +// // ignore |
| 237 | +// } |
| 238 | + } |
| 239 | + |
| 240 | + /* |
| 241 | + * Method declared on SelectionChangedAction. |
| 242 | + */ |
| 243 | + public void selectionChanged(IStructuredSelection selection) { |
| 244 | + setEnabled(canOperateOn(selection)); |
| 245 | + } |
| 246 | + |
| 247 | + /* |
| 248 | + * Method declared on SelectionChangedAction. |
| 249 | + */ |
| 250 | + public void selectionChanged(ITextSelection selection) { |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Executes this action for the given JavaScript element. |
| 255 | + * @param element The JavaScript element to be found. |
| 256 | + */ |
| 257 | + public void run(IJavaScriptElement element) { |
| 258 | + |
| 259 | + if (!ActionUtil.isProcessable(getShell(), element)) |
| 260 | + return; |
| 261 | + |
| 262 | + // will return true except for debugging purposes. |
| 263 | + try { |
| 264 | + performNewSearch(element); |
| 265 | + } catch (JavaScriptModelException ex) { |
| 266 | + ExceptionHandler.handle(ex, getShell(), SearchMessages.Search_Error_search_notsuccessful_title, SearchMessages.Search_Error_search_notsuccessful_message); |
| 267 | + } catch (InterruptedException e) { |
| 268 | + // cancelled |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + private void performNewSearch(IJavaScriptElement element) throws JavaScriptModelException, InterruptedException { |
| 273 | + JavaSearchQuery query= new JavaSearchQuery(createQuery(element)); |
| 274 | + if (query.canRunInBackground()) { |
| 275 | + /* |
| 276 | + * This indirection with Object as parameter is needed to prevent the loading |
| 277 | + * of the Search plug-in: the VM verifies the method call and hence loads the |
| 278 | + * types used in the method signature, eventually triggering the loading of |
| 279 | + * a plug-in (in this case ISearchQuery results in Search plug-in being loaded). |
| 280 | + */ |
| 281 | + SearchUtil.runQueryInBackground(query); |
| 282 | + } else { |
| 283 | + IProgressService progressService= PlatformUI.getWorkbench().getProgressService(); |
| 284 | + /* |
| 285 | + * This indirection with Object as parameter is needed to prevent the loading |
| 286 | + * of the Search plug-in: the VM verifies the method call and hence loads the |
| 287 | + * types used in the method signature, eventually triggering the loading of |
| 288 | + * a plug-in (in this case it would be ISearchQuery). |
| 289 | + */ |
| 290 | + IStatus status= SearchUtil.runQueryInForeground(progressService, query); |
| 291 | + if (status.matches(IStatus.ERROR | IStatus.INFO | IStatus.WARNING)) { |
| 292 | + ErrorDialog.openError(getShell(), SearchMessages.Search_Error_search_title, SearchMessages.Search_Error_search_message, status); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + QuerySpecification createQuery(IJavaScriptElement element) throws JavaScriptModelException, InterruptedException { |
| 298 | + JavaSearchScopeFactory factory= JavaSearchScopeFactory.getInstance(); |
| 299 | + IJavaScriptSearchScope scope= factory.createWorkspaceScope(true); |
| 300 | + String description= factory.getWorkspaceScopeDescription(true); |
| 301 | + return new ElementQuerySpecification(element, getLimitTo(), scope, description); |
| 302 | + } |
| 303 | + |
| 304 | + abstract int getLimitTo(); |
| 305 | + |
| 306 | + IType getType(IJavaScriptElement element) { |
| 307 | + if (element == null) |
| 308 | + return null; |
| 309 | + |
| 310 | + IType type= null; |
| 311 | + if (element.getElementType() == IJavaScriptElement.TYPE) |
| 312 | + type= (IType)element; |
| 313 | + else if (element instanceof IMember) |
| 314 | + type= ((IMember)element).getDeclaringType(); |
| 315 | + else if (element instanceof ILocalVariable) { |
| 316 | + type= (IType)element.getAncestor(IJavaScriptElement.TYPE); |
| 317 | + } |
| 318 | + return type; |
| 319 | + } |
| 320 | + |
| 321 | + TypeScriptEditor getEditor() { |
| 322 | + return fEditor; |
| 323 | + } |
| 324 | + |
| 325 | +} |
0 commit comments