26ea2091f0918800029980ba8dc4fec59ec6e98a
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.debug.ui.actions;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.core.Flags;
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.core.IJavaElement;
20 import net.sourceforge.phpdt.core.IMethod;
21 import net.sourceforge.phpdt.core.IType;
22 import net.sourceforge.phpdt.core.JavaModelException;
23 import net.sourceforge.phpdt.core.Signature;
24 import net.sourceforge.phpdt.debug.core.PHPDebugModel;
25 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
26 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
27
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.debug.core.DebugPlugin;
32 import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
33 //import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.ITextSelection;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.ui.IEditorInput;
38 import org.eclipse.ui.IEditorPart;
39 import org.eclipse.ui.IFileEditorInput;
40 import org.eclipse.ui.IWorkbenchPart;
41 import org.eclipse.ui.texteditor.IEditorStatusLine;
42 //import org.eclipse.ui.texteditor.ITextEditor;
43
44 /**
45  * Toggles a line breakpoint in a Java editor.
46  * 
47  * @since 3.0
48  */
49 public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {
50
51         protected void report(String message, IWorkbenchPart part) {
52                 IEditorStatusLine statusLine = (IEditorStatusLine) part
53                                 .getAdapter(IEditorStatusLine.class);
54                 if (statusLine != null) {
55                         if (message != null) {
56                                 statusLine.setMessage(true, message, null);
57                         } else {
58                                 statusLine.setMessage(true, null, null);
59                         }
60                 }
61                 if (message != null
62                                 && PHPDebugUiPlugin.getActiveWorkbenchShell() != null) {
63                         PHPDebugUiPlugin.getActiveWorkbenchShell().getDisplay().beep();
64                 }
65         }
66
67         // protected IType getType(ITextSelection selection) {
68         // IMember member=
69         // ActionDelegateHelper.getDefault().getCurrentMember(selection);
70         // IType type= null;
71         // if (member instanceof IType) {
72         // type = (IType)member;
73         // } else if (member != null) {
74         // type= member.getDeclaringType();
75         // }
76         // // bug 52385: we don't want local and anonymous types from compilation
77         // unit,
78         // // we are getting 'not-always-correct' names for them.
79         // try {
80         // while (type != null && !type.isBinary() && type.isLocal()) {
81         // type= type.getDeclaringType();
82         // }
83         // } catch (JavaModelException e) {
84         // PHPDebugUiPlugin.log(e);
85         // }
86         // return type;
87         // }
88
89         /*
90          * (non-Javadoc)
91          * 
92          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleLineBreakpoints(IWorkbenchPart,
93          *      ISelection)
94          */
95         public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection)
96                         throws CoreException {
97                 if (selection instanceof ITextSelection) {
98                         report(null, part);
99                         IEditorPart editorPart = (IEditorPart) part;
100                         ITextSelection textSelection = (ITextSelection) selection;
101                         // IType type = getType(textSelection);
102                         IEditorInput editorInput = editorPart.getEditorInput();
103 //                      IDocument document = ((ITextEditor) editorPart)
104 //                                      .getDocumentProvider().getDocument(editorInput);
105                         int lineNumber = textSelection.getStartLine() + 1;
106                         int offset = textSelection.getOffset();
107                         try {
108                                 // if (type == null) {
109                                 // IClassFile classFile=
110                                 // (IClassFile)editorInput.getAdapter(IClassFile.class);
111                                 // if (classFile != null) {
112                                 // type= classFile.getType();
113                                 // // bug 34856 - if this is an inner type, ensure the
114                                 // breakpoint is not
115                                 // // being added to the outer type
116                                 // if (type.getDeclaringType() != null) {
117                                 // ISourceRange sourceRange= type.getSourceRange();
118                                 // int start= sourceRange.getOffset();
119                                 // int end= start + sourceRange.getLength();
120                                 // if (offset < start || offset > end) {
121                                 // // not in the inner type
122                                 // IStatusLineManager statusLine =
123                                 // editorPart.getEditorSite().getActionBars().getStatusLineManager();
124                                 // statusLine
125                                 // .setErrorMessage(MessageFormat.format(ActionMessages.getString("ManageBreakpointRulerAction.Breakpoints_can_only_be_created_within_the_type_associated_with_the_editor__{0}._1"),
126                                 // new String[] { type.getTypeQualifiedName()})); //$NON-NLS-1$
127                                 // Display.getCurrent().beep();
128                                 // return;
129                                 // }
130                                 // }
131                                 // }
132                                 // }
133
134                                 // String typeName= null;
135                                 IResource resource;
136                                 PHPLineBreakpoint breakpoint = null;
137                                 // if (type == null) {
138                                 if (editorInput instanceof IFileEditorInput) {
139                                         resource = ((IFileEditorInput) editorInput).getFile();
140                                 } else {
141                                         resource = ResourcesPlugin.getWorkspace().getRoot();
142                                 }
143                                 // } else {
144                                 // typeName= type.getFullyQualifiedName();
145                                 // PHPLineBreakpoint
146                                 // breakpoint=PHPDebugModel.lineBreakpointExists(lineNumber);
147                                 // if (breakpoint==null)
148                                 // PHPDebugModel.createLineBreakpoint(getFile(), lineNumber, 0,
149                                 // true, null);
150                                 // else
151                                 // DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(
152                                 // breakpoint, true );
153                                 //
154                                 PHPLineBreakpoint existingBreakpoint = PHPDebugModel
155                                                 .lineBreakpointExists(resource, lineNumber); // typeName,
156                                                                                                                         // lineNumber);
157                                 if (existingBreakpoint != null) {
158                                         DebugPlugin.getDefault().getBreakpointManager()
159                                                         .removeBreakpoint(existingBreakpoint, true);
160                                         return;
161                                 } else {
162                                         breakpoint = PHPDebugModel.createLineBreakpoint(resource,
163                                                         lineNumber, 0, true, null);
164                                 }
165                                 // }
166                                 // new BreakpointLocationVerifierJob(document, breakpoint,
167                                 // lineNumber, typeName, type, resource, (IEditorStatusLine)
168                                 // editorPart.getAdapter(IEditorStatusLine.class)).schedule();
169                         } catch (CoreException ce) {
170                                 // TODO: no message in ActionMessages
171                                 // ExceptionHandler.handle(ce,
172                                 // ActionMessages.getString("ManageBreakpointActionDelegate.error.title1"),
173                                 // ActionMessages.getString("ManageBreakpointActionDelegate.error.message1"));
174                                 // //$NON-NLS-1$ //$NON-NLS-2$
175                                 return;
176                         }
177                 }
178         }
179
180         /*
181          * (non-Javadoc)
182          * 
183          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleLineBreakpoints(IWorkbenchPart,
184          *      ISelection)
185          */
186         public boolean canToggleLineBreakpoints(IWorkbenchPart part,
187                         ISelection selection) {
188                 return selection instanceof ITextSelection;
189         }
190
191         /*
192          * (non-Javadoc)
193          * 
194          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart,
195          *      org.eclipse.jface.viewers.ISelection)
196          */
197         public void toggleMethodBreakpoints(IWorkbenchPart part,
198                         ISelection selection) throws CoreException {
199                 // report(null, part);
200                 // selection = translateToMembers(part, selection);
201                 // if (selection instanceof ITextSelection) {
202                 // ITextSelection textSelection = (ITextSelection) selection;
203                 // if (selection != null) {
204                 // CompilationUnit compilationUnit=
205                 // parseCompilationUnit((ITextEditor)part);
206                 // if (compilationUnit != null) {
207                 // BreakpointMethodLocator locator= new
208                 // BreakpointMethodLocator(textSelection.getOffset());
209                 // compilationUnit.accept(locator);
210                 // String methodName= locator.getMethodName();
211                 // if (methodName == null) {
212                 // report(ActionMessages.getString("ManageMethodBreakpointActionDelegate.CantAdd"),
213                 // part); //$NON-NLS-1$
214                 // return;
215                 // }
216                 // String typeName= locator.getTypeName();
217                 // String methodSignature= locator.getMethodSignature();
218                 // if (methodSignature == null) {
219                 // report(ActionMessages.getString("ManageMethodBreakpointActionDelegate.methodNonAvailable"),
220                 // part); //$NON-NLS-1$
221                 // return;
222                 // }
223                 // // check if this method breakpoint already exist. If yes, remove it.
224                 // IBreakpointManager breakpointManager=
225                 // DebugPlugin.getDefault().getBreakpointManager();
226                 // IBreakpoint[] breakpoints=
227                 // breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
228                 // for (int i= 0; i < breakpoints.length; i++) {
229                 // IBreakpoint breakpoint= breakpoints[i];
230                 // if (breakpoint instanceof IJavaMethodBreakpoint) {
231                 // IJavaMethodBreakpoint methodBreakpoint=
232                 // (IJavaMethodBreakpoint)breakpoint;
233                 // if (typeName.equals(methodBreakpoint.getTypeName())
234                 // && methodName.equals(methodBreakpoint.getMethodName())
235                 // && methodSignature.equals(methodBreakpoint.getMethodSignature())) {
236                 // breakpointManager.removeBreakpoint(methodBreakpoint, true);
237                 // return;
238                 // }
239                 // }
240                 // }
241                 // // add the breakpoint
242                 // JDIDebugModel.createMethodBreakpoint(getResource((IEditorPart)part),
243                 // typeName, methodName, methodSignature, true, false, false, -1, -1,
244                 // -1, 0, true, new HashMap(10));
245                 // }
246                 // }
247                 // } else if (selection instanceof IStructuredSelection) {
248                 // IMethod[] members= getMethods((IStructuredSelection)selection);
249                 // if (members.length == 0) {
250                 // report(ActionMessages.getString("ToggleBreakpointAdapter.9"), part);
251                 // //$NON-NLS-1$
252                 // return;
253                 // }
254                 // // add or remove the breakpoint
255                 // IBreakpointManager breakpointManager=
256                 // DebugPlugin.getDefault().getBreakpointManager();
257                 // for (int i= 0, length= members.length; i < length; i++) {
258                 // IMethod method= members[i];
259                 // IJavaBreakpoint breakpoint= getBreakpoint(method);
260                 // if (breakpoint == null) {
261                 // // add breakpoint
262                 // int start = -1;
263                 // int end = -1;
264                 // ISourceRange range = method.getNameRange();
265                 // if (range != null) {
266                 // start = range.getOffset();
267                 // end = start + range.getLength();
268                 // }
269                 // Map attributes = new HashMap(10);
270                 // BreakpointUtils.addJavaBreakpointAttributes(attributes, method);
271                 // String methodName = method.getElementName();
272                 // if (method.isConstructor()) {
273                 // methodName = "<init>"; //$NON-NLS-1$
274                 // }
275                 // IType type= method.getDeclaringType();
276                 // String methodSignature= method.getSignature();
277                 // if (!type.isBinary()) {
278                 // //resolve the type names
279                 // methodSignature= resolveMethodSignature(type, methodSignature);
280                 // if (methodSignature == null) {
281                 // IStatus status = new Status(IStatus.ERROR,
282                 // JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, "Source method
283                 // signature could not be resolved", null); //$NON-NLS-1$
284                 // throw new CoreException(status);
285                 // }
286                 // }
287                 // JDIDebugModel.createMethodBreakpoint(BreakpointUtils.getBreakpointResource(method),
288                 // type.getFullyQualifiedName(), methodName, methodSignature, true,
289                 // false, false, -1, start, end, 0, true, attributes);
290                 // } else {
291                 // // remove breakpoint
292                 // breakpointManager.removeBreakpoint(breakpoint, true);
293                 // }
294                 // }
295                 // }
296         }
297
298         /*
299          * (non-Javadoc)
300          * 
301          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart,
302          *      org.eclipse.jface.viewers.ISelection)
303          */
304         public boolean canToggleMethodBreakpoints(IWorkbenchPart part,
305                         ISelection selection) {
306                 // if (selection instanceof IStructuredSelection) {
307                 // IStructuredSelection ss = (IStructuredSelection) selection;
308                 // return getMethods(ss).length > 0;
309                 // } else {
310                 // return selection instanceof ITextSelection;
311                 // }
312                 return false;
313         }
314
315         protected IMethod[] getMethods(IStructuredSelection selection) {
316                 if (selection.isEmpty()) {
317                         return new IMethod[0];
318                 } else {
319                         List methods = new ArrayList(selection.size());
320                         Iterator iterator = selection.iterator();
321                         while (iterator.hasNext()) {
322                                 Object thing = iterator.next();
323                                 try {
324                                         if (thing instanceof IMethod
325                                                         && !Flags.isAbstract(((IMethod) thing).getFlags())) {
326                                                 methods.add(thing);
327                                         }
328                                 } catch (JavaModelException e) {
329                                 }
330                         }
331                         return (IMethod[]) methods.toArray(new IMethod[methods.size()]);
332                 }
333         }
334
335         // protected IField[] getFields(IStructuredSelection selection) {
336         // if (selection.isEmpty()) {
337         // return new IField[0];
338         // } else {
339         // List fields = new ArrayList(selection.size());
340         // Iterator iterator = selection.iterator();
341         // while (iterator.hasNext()) {
342         // Object thing = iterator.next();
343         // if (thing instanceof IField) {
344         // fields.add(thing);
345         // } else if (thing instanceof IJavaFieldVariable) {
346         // IField field= getField((IJavaFieldVariable) thing);
347         // if (field != null) {
348         // fields.add(field);
349         // }
350         // }
351         // }
352         // return (IField[]) fields.toArray(new IField[fields.size()]);
353         // }
354         // }
355
356         /*
357          * (non-Javadoc)
358          * 
359          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints(org.eclipse.ui.IWorkbenchPart,
360          *      org.eclipse.jface.viewers.ISelection)
361          */
362         public void toggleWatchpoints(IWorkbenchPart part, ISelection selection)
363                         throws CoreException {
364                 // report(null, part);
365                 // selection = translateToMembers(part, selection);
366                 // if (selection instanceof ITextSelection) {
367                 // ITextSelection textSelection= (ITextSelection) selection;
368                 // CompilationUnit compilationUnit=
369                 // parseCompilationUnit((ITextEditor)part);
370                 // if (compilationUnit != null) {
371                 // BreakpointFieldLocator locator= new
372                 // BreakpointFieldLocator(textSelection.getOffset());
373                 // compilationUnit.accept(locator);
374                 // String fieldName= locator.getFieldName();
375                 // if (fieldName == null) {
376                 // report(ActionMessages.getString("ManageWatchpointActionDelegate.CantAdd"),
377                 // part); //$NON-NLS-1$
378                 // return;
379                 // }
380                 // String typeName= locator.getTypeName();
381                 // // check if the watchpoint already exists. If yes, remove it
382                 // IBreakpointManager breakpointManager=
383                 // DebugPlugin.getDefault().getBreakpointManager();
384                 // IBreakpoint[] breakpoints=
385                 // breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
386                 // for (int i= 0; i < breakpoints.length; i++) {
387                 // IBreakpoint breakpoint= breakpoints[i];
388                 // if (breakpoint instanceof IJavaWatchpoint) {
389                 // IJavaWatchpoint watchpoint= (IJavaWatchpoint)breakpoint;
390                 // if (typeName.equals(watchpoint.getTypeName()) &&
391                 // fieldName.equals(watchpoint.getFieldName())) {
392                 // breakpointManager.removeBreakpoint(watchpoint, true);
393                 // return;
394                 // }
395                 // }
396                 // }
397                 // // add the watchpoint
398                 // JDIDebugModel.createWatchpoint(getResource((IEditorPart)part),
399                 // typeName, fieldName, -1, -1, -1, 0, true, new HashMap(10));
400                 // }
401                 // } else if (selection instanceof IStructuredSelection) {
402                 // IField[] members = getFields((IStructuredSelection)selection);
403                 // if (members.length == 0) {
404                 // report(ActionMessages.getString("ToggleBreakpointAdapter.10"), part);
405                 // //$NON-NLS-1$
406                 // return;
407                 // }
408                 // // add or remove watchpoint
409                 // IBreakpointManager breakpointManager=
410                 // DebugPlugin.getDefault().getBreakpointManager();
411                 // for (int i= 0, length= members.length; i < length; i++) {
412                 // IField element= members[i];
413                 // IJavaBreakpoint breakpoint= getBreakpoint(element);
414                 // if (breakpoint == null) {
415                 // IType type = element.getDeclaringType();
416                 // int start = -1;
417                 // int end = -1;
418                 // ISourceRange range = element.getNameRange();
419                 // if (range != null) {
420                 // start = range.getOffset();
421                 // end = start + range.getLength();
422                 // }
423                 // Map attributes = new HashMap(10);
424                 // BreakpointUtils.addJavaBreakpointAttributes(attributes, element);
425                 // JDIDebugModel.createWatchpoint(BreakpointUtils.getBreakpointResource(type),
426                 // type.getFullyQualifiedName(), element.getElementName(), -1, start,
427                 // end, 0, true, attributes);
428                 // } else {
429                 // // remove breakpoint
430                 // breakpointManager.removeBreakpoint(breakpoint, true);
431                 // }
432                 // }
433                 // }
434         }
435
436         public static String resolveMethodSignature(IType type,
437                         String methodSignature) throws JavaModelException {
438                 String[] parameterTypes = Signature.getParameterTypes(methodSignature);
439                 int length = parameterTypes.length;
440                 String[] resolvedParameterTypes = new String[length];
441
442                 for (int i = 0; i < length; i++) {
443                         resolvedParameterTypes[i] = resolveType(type, parameterTypes[i]);
444                         if (resolvedParameterTypes[i] == null) {
445                                 return null;
446                         }
447                 }
448
449                 String resolvedReturnType = resolveType(type, Signature
450                                 .getReturnType(methodSignature));
451                 if (resolvedReturnType == null) {
452                         return null;
453                 }
454
455                 return Signature.createMethodSignature(resolvedParameterTypes,
456                                 resolvedReturnType);
457         }
458
459         private static String resolveType(IType type, String typeSignature)
460                         throws JavaModelException {
461                 // int count= Signature.getArrayCount(typeSignature);
462                 // String elementTypeSignature= Signature.getElementType(typeSignature);
463                 // if (elementTypeSignature.length() == 1) {
464                 // // no need to resolve primitive types
465                 // return typeSignature;
466                 // }
467                 // String elementTypeName= Signature.toString(elementTypeSignature);
468                 // String[][] resolvedElementTypeNames=
469                 // type.resolveType(elementTypeName);
470                 // if (resolvedElementTypeNames == null ||
471                 // resolvedElementTypeNames.length != 1) {
472                 // // the type name cannot be resolved
473                 // return null;
474                 // }
475                 // String resolvedElementTypeName=
476                 // Signature.toQualifiedName(resolvedElementTypeNames[0]);
477                 // String resolvedElementTypeSignature=
478                 // Signature.createTypeSignature(resolvedElementTypeName,
479                 // true).replace('.', '/');
480                 // return Signature.createArraySignature(resolvedElementTypeSignature,
481                 // count);
482                 return "";
483         }
484
485         protected static IResource getResource(IEditorPart editor) {
486                 IResource resource;
487                 IEditorInput editorInput = editor.getEditorInput();
488                 if (editorInput instanceof IFileEditorInput) {
489                         resource = ((IFileEditorInput) editorInput).getFile();
490                 } else {
491                         resource = ResourcesPlugin.getWorkspace().getRoot();
492                 }
493                 return resource;
494         }
495
496         /**
497          * Returns a handle to the specified method or <code>null</code> if none.
498          * 
499          * @param editorPart
500          *            the editor containing the method
501          * @param typeName
502          * @param methodName
503          * @param signature
504          * @return handle or <code>null</code>
505          */
506         protected IMethod getMethodHandle(IEditorPart editorPart, String typeName,
507                         String methodName, String signature) throws CoreException {
508                 IJavaElement element = (IJavaElement) editorPart.getEditorInput()
509                                 .getAdapter(IJavaElement.class);
510                 IType type = null;
511                 if (element instanceof ICompilationUnit) {
512                         IType[] types = ((ICompilationUnit) element).getAllTypes();
513                         for (int i = 0; i < types.length; i++) {
514                                 if (types[i].getFullyQualifiedName().equals(typeName)) {
515                                         type = types[i];
516                                         break;
517                                 }
518                         }
519                 }
520                 // else if (element instanceof IClassFile) {
521                 // type = ((IClassFile)element).getType();
522                 // }
523                 if (type != null) {
524                         String[] sigs = Signature.getParameterTypes(signature);
525                         return type.getMethod(methodName, sigs);
526                 }
527                 return null;
528         }
529
530         // protected IJavaBreakpoint getBreakpoint(IMember element) {
531         // IBreakpointManager breakpointManager=
532         // DebugPlugin.getDefault().getBreakpointManager();
533         // IBreakpoint[] breakpoints=
534         // breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
535         // if (element instanceof IMethod) {
536         // IMethod method= (IMethod)element;
537         // for (int i= 0; i < breakpoints.length; i++) {
538         // IBreakpoint breakpoint= breakpoints[i];
539         // if (breakpoint instanceof IJavaMethodBreakpoint) {
540         // IJavaMethodBreakpoint methodBreakpoint=
541         // (IJavaMethodBreakpoint)breakpoint;
542         // IMember container = null;
543         // try {
544         // container= BreakpointUtils.getMember(methodBreakpoint);
545         // } catch (CoreException e) {
546         // JDIDebugUIPlugin.log(e);
547         // return null;
548         // }
549         // if (container == null) {
550         // try {
551         // if
552         // (method.getDeclaringType().getFullyQualifiedName().equals(methodBreakpoint.getTypeName())
553         // && method.getElementName().equals(methodBreakpoint.getMethodName())
554         // && method.getSignature().equals(methodBreakpoint.getMethodSignature())) {
555         // return methodBreakpoint;
556         // }
557         // } catch (CoreException e) {
558         // JDIDebugUIPlugin.log(e);
559         // }
560         // } else {
561         // if (container instanceof IMethod) {
562         // if
563         // (method.getDeclaringType().getFullyQualifiedName().equals(container.getDeclaringType().getFullyQualifiedName()))
564         // {
565         // if (method.isSimilar((IMethod)container)) {
566         // return methodBreakpoint;
567         // }
568         // }
569         // }
570         // }
571         // }
572         // }
573         // } else if (element instanceof IField) {
574         // for (int i= 0; i < breakpoints.length; i++) {
575         // IBreakpoint breakpoint= breakpoints[i];
576         // if (breakpoint instanceof IJavaWatchpoint) {
577         // try {
578         // if (equalFields(element, (IJavaWatchpoint)breakpoint))
579         // return (IJavaBreakpoint)breakpoint;
580         // } catch (CoreException e) {
581         // JDIDebugUIPlugin.log(e);
582         // }
583         // }
584         // }
585         // }
586         // return null;
587         // }
588
589         /**
590          * Compare two fields. The default <code>equals()</code> method for
591          * <code>IField</code> doesn't give the comparison desired.
592          */
593         // private boolean equalFields(IMember breakpointField, IJavaWatchpoint
594         // watchpoint) throws CoreException {
595         // return
596         // (breakpointField.getElementName().equals(watchpoint.getFieldName()) &&
597         // breakpointField.getDeclaringType().getFullyQualifiedName().equals(watchpoint.getTypeName()));
598         // }
599         //
600         // protected CompilationUnit parseCompilationUnit(ITextEditor editor) {
601         // IEditorInput editorInput = editor.getEditorInput();
602         // IDocument document=
603         // editor.getDocumentProvider().getDocument(editorInput);
604         // ASTParser parser = ASTParser.newParser(AST.JLS2);
605         // parser.setSource(document.get().toCharArray());
606         // return (CompilationUnit) parser.createAST(null);
607         // }
608         /*
609          * (non-Javadoc)
610          * 
611          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleWatchpoints(org.eclipse.ui.IWorkbenchPart,
612          *      org.eclipse.jface.viewers.ISelection)
613          */
614         public boolean canToggleWatchpoints(IWorkbenchPart part,
615                         ISelection selection) {
616                 // if (selection instanceof IStructuredSelection) {
617                 // IStructuredSelection ss = (IStructuredSelection) selection;
618                 // return getFields(ss).length > 0;
619                 // } else {
620                 // return selection instanceof ITextSelection;
621                 // }
622                 return false;
623         }
624
625         /**
626          * Returns a selection of the member in the given text selection, or the
627          * original selection if none.
628          * 
629          * @param part
630          * @param selection
631          * @return a structured selection of the member in the given text selection,
632          *         or the original selection if none
633          * @exception CoreException
634          *                if an exceptoin occurrs
635          */
636         // protected ISelection translateToMembers(IWorkbenchPart part, ISelection
637         // selection) throws CoreException {
638         // if (selection instanceof ITextSelection && part instanceof IEditorPart) {
639         // ITextSelection textSelection = (ITextSelection)selection;
640         // IEditorPart editorPart = (IEditorPart) part;
641         // IEditorInput editorInput = editorPart.getEditorInput();
642         // IMember m= null;
643         // IClassFile classFile=
644         // (IClassFile)editorInput.getAdapter(IClassFile.class);
645         // if (classFile != null) {
646         // IJavaElement e= classFile.getElementAt(textSelection.getOffset());
647         // if (e instanceof IMember) {
648         // m= (IMember)e;
649         // }
650         // } else {
651         // IWorkingCopyManager manager= JavaUI.getWorkingCopyManager();
652         // ICompilationUnit unit= manager.getWorkingCopy(editorInput);
653         // if (unit != null) {
654         // synchronized (unit) {
655         // unit.reconcile(ICompilationUnit.NO_AST /*don't create ast*/, false/*don't
656         // force problem detection*/, null/*use primary owner*/, null/*no progress
657         // monitor*/);
658         // }
659         // IJavaElement e = unit.getElementAt(textSelection.getOffset());
660         // if (e instanceof IMember) {
661         // m= (IMember)e;
662         // }
663         // }
664         // }
665         // if (m != null) {
666         // return new StructuredSelection(m);
667         // }
668         // }
669         // return selection;
670         // }
671         /**
672          * Returns a list of matching types (IType - Java model) that correspond to
673          * the declaring type (ReferenceType - JDI model) of the given variable.
674          */
675         // protected static List searchForDeclaringType(IJavaFieldVariable variable)
676         // {
677         // List types= new ArrayList();
678         // ILaunch launch = variable.getDebugTarget().getLaunch();
679         // if (launch == null) {
680         // return types;
681         // }
682         //
683         // ILaunchConfiguration configuration= launch.getLaunchConfiguration();
684         // IJavaProject[] javaProjects = null;
685         // IWorkspace workspace= ResourcesPlugin.getWorkspace();
686         // if (configuration != null) {
687         // // Launch configuration support
688         // try {
689         // String projectName=
690         // configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
691         // ""); //$NON-NLS-1$
692         // if (projectName.length() != 0) {
693         // javaProjects= new IJavaProject[]
694         // {JavaCore.create(workspace.getRoot().getProject(projectName))};
695         // } else {
696         // IProject[] projects=
697         // ResourcesPlugin.getWorkspace().getRoot().getProjects();
698         // IProject project;
699         // List projectList= new ArrayList();
700         // for (int i= 0, numProjects= projects.length; i < numProjects; i++) {
701         // project= projects[i];
702         // if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
703         // projectList.add(JavaCore.create(project));
704         // }
705         // }
706         // javaProjects= new IJavaProject[projectList.size()];
707         // projectList.toArray(javaProjects);
708         // }
709         // } catch (CoreException e) {
710         // JDIDebugUIPlugin.log(e);
711         // }
712         // }
713         // if (javaProjects == null) {
714         // return types;
715         // }
716         //
717         // SearchEngine engine= new SearchEngine();
718         // IJavaSearchScope scope= SearchEngine.createJavaSearchScope(javaProjects,
719         // true);
720         // String declaringType= null;
721         // try {
722         // declaringType= variable.getDeclaringType().getName();
723         // } catch (DebugException x) {
724         // JDIDebugUIPlugin.log(x);
725         // return types;
726         // }
727         // ArrayList typeRefsFound= new ArrayList(3);
728         // ITypeNameRequestor requestor= new TypeInfoRequestor(typeRefsFound);
729         // try {
730         // engine.searchAllTypeNames(
731         // getPackage(declaringType),
732         // getTypeName(declaringType),
733         // SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
734         // IJavaSearchConstants.CLASS,
735         // scope,
736         // requestor,
737         // IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
738         // null);
739         // } catch (JavaModelException x) {
740         // JDIDebugUIPlugin.log(x);
741         // return types;
742         // }
743         // Iterator iter= typeRefsFound.iterator();
744         // TypeInfo typeInfo= null;
745         // while (iter.hasNext()) {
746         // typeInfo= (TypeInfo)iter.next();
747         // try {
748         // types.add(typeInfo.resolveType(scope));
749         // } catch (JavaModelException jme) {
750         // JDIDebugUIPlugin.log(jme);
751         // }
752         // }
753         // return types;
754         // }
755         /**
756          * Returns the package name of the given fully qualified type name. The
757          * package name is assumed to be the dot-separated prefix of the type name.
758          */
759         // protected static char[] getPackage(String fullyQualifiedName) {
760         // int index= fullyQualifiedName.lastIndexOf('.');
761         // if (index == -1) {
762         // return new char[0];
763         // }
764         // return fullyQualifiedName.substring(0, index).toCharArray();
765         // }
766         //
767         // /**
768         // * Returns a simple type name from the given fully qualified type name.
769         // * The type name is assumed to be the last contiguous segment of the
770         // * fullyQualifiedName not containing a '.' or '$'
771         // */
772         // protected static char[] getTypeName(String fullyQualifiedName) {
773         // int index= fullyQualifiedName.lastIndexOf('.');
774         // String typeName= fullyQualifiedName.substring(index + 1);
775         // int lastInnerClass= typeName.lastIndexOf('$');
776         // if (lastInnerClass != -1) {
777         // typeName= typeName.substring(lastInnerClass + 1);
778         // }
779         // return typeName.toCharArray();
780         // }
781         //
782         // /**
783         // * Return the associated IField (Java model) for the given
784         // * IJavaFieldVariable (JDI model)
785         // */
786         // private IField getField(IJavaFieldVariable variable) {
787         // String varName= null;
788         // try {
789         // varName= variable.getName();
790         // } catch (DebugException x) {
791         // JDIDebugUIPlugin.log(x);
792         // return null;
793         // }
794         // IField field;
795         // List types= searchForDeclaringType(variable);
796         // Iterator iter= types.iterator();
797         // while (iter.hasNext()) {
798         // IType type= (IType)iter.next();
799         // field= type.getField(varName);
800         // if (field.exists()) {
801         // return field;
802         // }
803         // }
804         // return null;
805         // }
806 }