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