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