1 11 package org.eclipse.jdt.internal.debug.ui.heapwalking; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.debug.core.DebugException; 15 import org.eclipse.debug.core.model.IValue; 16 import org.eclipse.debug.ui.DebugUITools; 17 import org.eclipse.debug.ui.IDebugView; 18 import org.eclipse.debug.ui.InspectPopupDialog; 19 import org.eclipse.jdt.core.ICodeAssist; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IMethod; 22 import org.eclipse.jdt.core.IType; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 25 import org.eclipse.jdt.debug.core.IJavaType; 26 import org.eclipse.jdt.debug.core.IJavaValue; 27 import org.eclipse.jdt.debug.core.IJavaVariable; 28 import org.eclipse.jdt.internal.debug.core.logicalstructures.JDIAllInstancesValue; 29 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget; 30 import org.eclipse.jdt.internal.debug.core.model.JDIReferenceType; 31 import org.eclipse.jdt.internal.debug.ui.DebugWorkingCopyManager; 32 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 33 import org.eclipse.jdt.internal.debug.ui.JavaWordFinder; 34 import org.eclipse.jdt.internal.debug.ui.actions.ObjectActionDelegate; 35 import org.eclipse.jdt.internal.debug.ui.actions.PopupInspectAction; 36 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression; 37 import org.eclipse.jdt.ui.JavaUI; 38 import org.eclipse.jface.action.IAction; 39 import org.eclipse.jface.text.IDocument; 40 import org.eclipse.jface.text.IRegion; 41 import org.eclipse.jface.text.ITextSelection; 42 import org.eclipse.jface.viewers.ISelection; 43 import org.eclipse.jface.viewers.ISelectionProvider; 44 import org.eclipse.jface.viewers.IStructuredSelection; 45 import org.eclipse.swt.custom.StyledText; 46 import org.eclipse.swt.graphics.GC; 47 import org.eclipse.swt.graphics.Point; 48 import org.eclipse.swt.graphics.Rectangle; 49 import org.eclipse.swt.widgets.Control; 50 import org.eclipse.swt.widgets.Shell; 51 import org.eclipse.swt.widgets.Tree; 52 import org.eclipse.swt.widgets.TreeItem; 53 import org.eclipse.ui.IEditorActionDelegate; 54 import org.eclipse.ui.IEditorInput; 55 import org.eclipse.ui.IEditorPart; 56 import org.eclipse.ui.IWorkbenchPage; 57 import org.eclipse.ui.IWorkbenchPart; 58 import org.eclipse.ui.IWorkbenchWindow; 59 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 60 import org.eclipse.ui.part.IPage; 61 import org.eclipse.ui.part.PageBookView; 62 import org.eclipse.ui.texteditor.IDocumentProvider; 63 import org.eclipse.ui.texteditor.IEditorStatusLine; 64 import org.eclipse.ui.texteditor.ITextEditor; 65 66 import com.ibm.icu.text.MessageFormat; 67 68 74 public class AllInstancesActionDelegate extends ObjectActionDelegate implements IEditorActionDelegate, IWorkbenchWindowActionDelegate { 75 76 private IWorkbenchWindow fWindow; 77 78 81 public void run(IAction action) { 82 if (getPart() != null){ 83 ISelectionProvider provider = getPart().getSite().getSelectionProvider(); 84 if (provider != null){ 85 ISelection selection = provider.getSelection(); 86 87 if (getPart() instanceof IEditorPart && selection instanceof ITextSelection){ 89 ITextEditor editor = getTextEditor(getPart()); 90 IDocumentProvider documentProvider = editor.getDocumentProvider(); 91 if (documentProvider != null) { 92 IDocument document = documentProvider.getDocument(editor.getEditorInput()); 93 IRegion selectedWord = JavaWordFinder.findWord(document,((ITextSelection)selection).getOffset()); 94 if (selectedWord != null){ 95 IJavaElement element = getJavaElement(editor.getEditorInput()); 96 if (element instanceof ICodeAssist){ 97 try{ 98 IJavaElement[] selectedTypes = ((ICodeAssist)element).codeSelect(selectedWord.getOffset(), selectedWord.getLength()); 99 if (selectedTypes.length > 0){ 101 runAllInstancesForType(selectedTypes[0]); 102 return; 103 } 104 } catch (JavaModelException e){ 105 JDIDebugUIPlugin.log(e.getStatus()); 106 report(Messages.AllInstancesActionDelegate_0,getPart()); 107 } 108 } 109 } 110 } 111 112 } else if (selection instanceof IStructuredSelection){ 114 runAllInstancesForType(((IStructuredSelection)selection).getFirstElement()); 115 return; 116 } 117 } 118 } 119 report(Messages.AllInstancesActionDelegate_3,getPart()); 120 } 121 122 128 protected void runAllInstancesForType(Object selectedElement){ 129 if (selectedElement != null){ 130 131 IJavaType type = null; 132 try { 133 if (selectedElement instanceof IMethod){ 135 if (((IMethod)selectedElement).isConstructor()){ 136 selectedElement = ((IMethod)selectedElement).getDeclaringType(); 137 } 138 } 139 if(selectedElement instanceof IType) { 141 IAdaptable adapt = DebugUITools.getDebugContext(); 142 if(adapt != null) { 143 IJavaDebugTarget target = (IJavaDebugTarget) adapt.getAdapter(IJavaDebugTarget.class); 144 if(target != null) { 145 IType itype = (IType) selectedElement; 146 IJavaType[] types = target.getJavaTypes(itype.getFullyQualifiedName()); 147 if(types != null && types.length > 0) { 148 type = types[0]; 149 } else { 150 JDIAllInstancesValue aiv = new JDIAllInstancesValue((JDIDebugTarget)target, null); 152 InspectPopupDialog ipd = new InspectPopupDialog(getShell(), 153 getAnchor(), 154 PopupInspectAction.ACTION_DEFININITION_ID, 155 new JavaInspectExpression(MessageFormat.format(Messages.AllInstancesActionDelegate_2, new String []{itype.getElementName()}), aiv)); 156 ipd.open(); 157 return; 158 } 159 } 160 } 161 } 162 if (selectedElement instanceof IJavaVariable) { 164 IJavaVariable var = (IJavaVariable) selectedElement; 165 IValue val = var.getValue(); 166 if (val instanceof IJavaValue){ 167 type = ((IJavaValue)val).getJavaType(); 168 } 169 if (type == null){ 170 type = var.getJavaType(); 171 } 172 } 173 174 } catch (JavaModelException e){ 175 JDIDebugUIPlugin.log(e.getStatus()); 176 } catch (DebugException e) { 177 JDIDebugUIPlugin.log(e.getStatus()); 178 } 179 180 if(type instanceof JDIReferenceType) { 181 JDIReferenceType rtype = (JDIReferenceType) type; 182 try{ 183 JDIAllInstancesValue aiv = new JDIAllInstancesValue((JDIDebugTarget)rtype.getDebugTarget(), rtype); 184 InspectPopupDialog ipd = new InspectPopupDialog(getShell(), 185 getAnchor(), 186 PopupInspectAction.ACTION_DEFININITION_ID, 187 new JavaInspectExpression(MessageFormat.format(Messages.AllInstancesActionDelegate_2, new String []{type.getName()}), aiv)); 188 ipd.open(); 189 return; 190 } catch (DebugException e) { 191 JDIDebugUIPlugin.log(e); 192 report(Messages.AllInstancesActionDelegate_0,getPart()); 193 } 194 } 195 } 196 report(Messages.AllInstancesActionDelegate_3,getPart()); 197 } 198 199 204 protected void report(final String message, final IWorkbenchPart part) { 205 JDIDebugUIPlugin.getStandardDisplay().asyncExec(new Runnable () { 206 public void run() { 207 IEditorStatusLine statusLine = (IEditorStatusLine) part.getAdapter(IEditorStatusLine.class); 208 if (statusLine != null) { 209 if (message != null) { 210 statusLine.setMessage(true, message, null); 211 } else { 212 statusLine.setMessage(true, null, null); 213 } 214 } 215 if (message != null && JDIDebugUIPlugin.getActiveWorkbenchShell() != null) { 216 JDIDebugUIPlugin.getActiveWorkbenchShell().getDisplay().beep(); 217 } 218 } 219 }); 220 } 221 222 227 protected Point getAnchor() { 228 229 IDebugView debugView = (IDebugView)getPart().getAdapter(IDebugView.class); 231 if (debugView != null){ 232 Control control = debugView.getViewer().getControl(); 233 if (control instanceof Tree) { 234 Tree tree = (Tree) control; 235 TreeItem[] selection = tree.getSelection(); 236 if (selection.length > 0) { 237 Rectangle bounds = selection[0].getBounds(); 238 return tree.toDisplay(new Point(bounds.x, bounds.y + bounds.height)); 239 } 240 } 241 } 242 243 Control widget = (Control)getPart().getAdapter(Control.class); 245 if (widget == null){ 246 if(getPart() instanceof PageBookView) { 247 PageBookView view = (PageBookView) getPart(); 249 IPage page = view.getCurrentPage(); 250 if(page != null) { 251 widget = page.getControl(); 252 } 253 } 254 } 255 if (widget instanceof StyledText){ 256 StyledText textWidget = (StyledText)widget; 257 Point docRange = textWidget.getSelectionRange(); 258 int midOffset = docRange.x + (docRange.y / 2); 259 Point point = textWidget.getLocationAtOffset(midOffset); 260 point = textWidget.toDisplay(point); 261 262 GC gc = new GC(textWidget); 263 gc.setFont(textWidget.getFont()); 264 int height = gc.getFontMetrics().getHeight(); 265 gc.dispose(); 266 point.y += height; 267 return point; 268 } 269 if (widget instanceof Tree) { 270 Tree tree = (Tree) widget; 271 TreeItem[] selection = tree.getSelection(); 272 if (selection.length > 0) { 273 Rectangle bounds = selection[0].getBounds(); 274 return tree.toDisplay(new Point(bounds.x, bounds.y + bounds.height)); 275 } 276 } 277 return null; 278 } 279 280 285 private IJavaElement getJavaElement(IEditorInput input) { 286 IJavaElement je = JavaUI.getEditorInputJavaElement(input); 287 if(je != null) { 288 return je; 289 } 290 return DebugWorkingCopyManager.getWorkingCopy(input, false); 292 } 293 294 295 303 private ITextEditor getTextEditor(IWorkbenchPart part) { 304 if (part instanceof ITextEditor) { 305 return (ITextEditor) part; 306 } 307 return (ITextEditor) part.getAdapter(ITextEditor.class); 308 } 309 310 313 public void setActiveEditor(IAction action, IEditorPart targetEditor) { 314 setActivePart(action, targetEditor); 315 } 316 317 320 public void init(IWorkbenchWindow window) { 321 fWindow = window; 322 } 323 324 327 private Shell getShell(){ 328 if (fWindow != null){ 329 return fWindow.getShell(); 330 } 331 if (getWorkbenchWindow() != null){ 332 return getWorkbenchWindow().getShell(); 333 } 334 return null; 335 } 336 337 340 protected IWorkbenchPart getPart() { 341 IWorkbenchPart part = super.getPart(); 342 if (part != null){ 343 return part; 344 } else if (fWindow != null){ 345 IWorkbenchPage page = fWindow.getActivePage(); 346 if(page != null) { 347 return page.getActivePart(); 348 } 349 } 350 return null; 351 } 352 353 } 354 | Popular Tags |