replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / actions / ToggleBreakpointAdapter.java
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 org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.debug.core.DebugPlugin;
31 import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.ITextSelection;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IWorkbenchPart;
40 import org.eclipse.ui.texteditor.IEditorStatusLine;
41 import org.eclipse.ui.texteditor.ITextEditor;
42
43 /**
44  * Toggles a line breakpoint in a Java editor.
45  *
46  * @since 3.0
47  */
48 public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {
49
50         protected void report(String message, IWorkbenchPart part) {
51                 IEditorStatusLine statusLine= (IEditorStatusLine) part.getAdapter(IEditorStatusLine.class);
52                 if (statusLine != null) {
53                         if (message != null) {
54                                 statusLine.setMessage(true, message, null);
55                         } else {
56                                 statusLine.setMessage(true, null, null);
57                         }
58                 }
59                 if (message != null && PHPDebugUiPlugin.getActiveWorkbenchShell() != null) {
60                         PHPDebugUiPlugin.getActiveWorkbenchShell().getDisplay().beep();
61                 }
62         }
63
64 //      protected IType getType(ITextSelection selection) {
65 //              IMember member= ActionDelegateHelper.getDefault().getCurrentMember(selection);
66 //              IType type= null;
67 //              if (member instanceof IType) {
68 //                      type = (IType)member;
69 //              } else if (member != null) {
70 //                      type= member.getDeclaringType();
71 //              }
72 //              // bug 52385: we don't want local and anonymous types from compilation unit,
73 //              // we are getting 'not-always-correct' names for them.
74 //              try {
75 //                      while (type != null && !type.isBinary() && type.isLocal()) {
76 //                              type= type.getDeclaringType();
77 //                      }
78 //              } catch (JavaModelException e) {
79 //                      PHPDebugUiPlugin.log(e);
80 //              }
81 //              return type;
82 //      }
83
84         /* (non-Javadoc)
85          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleLineBreakpoints(IWorkbenchPart, ISelection)
86          */
87         public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
88                 if (selection instanceof ITextSelection) {
89                         report(null, part);
90                         IEditorPart editorPart = (IEditorPart)part;
91                         ITextSelection textSelection = (ITextSelection)selection;
92 //                      IType type = getType(textSelection);
93                         IEditorInput editorInput = editorPart.getEditorInput();
94                         IDocument document= ((ITextEditor)editorPart).getDocumentProvider().getDocument(editorInput);
95                         int lineNumber= textSelection.getStartLine() + 1;
96                         int offset= textSelection.getOffset();
97                         try {
98 //                              if (type == null) {
99 //                                      IClassFile classFile= (IClassFile)editorInput.getAdapter(IClassFile.class);
100 //                                      if (classFile != null) {
101 //                                              type= classFile.getType();
102 //                                              // bug 34856 - if this is an inner type, ensure the breakpoint is not
103 //                                              // being added to the outer type
104 //                                              if (type.getDeclaringType() != null) {
105 //                                                      ISourceRange sourceRange= type.getSourceRange();
106 //                                                      int start= sourceRange.getOffset();
107 //                                                      int end= start + sourceRange.getLength();
108 //                                                      if (offset < start || offset > end) {
109 //                                                              // not in the inner type
110 //                                                              IStatusLineManager statusLine = editorPart.getEditorSite().getActionBars().getStatusLineManager();
111 //                                                              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$
112 //                                                              Display.getCurrent().beep();
113 //                                                              return;
114 //                                                      }
115 //                                              }
116 //                                      }
117 //                              }
118
119 //                              String typeName= null;
120                                 IResource resource;
121                                 PHPLineBreakpoint breakpoint= null;
122 //                              if (type == null) {
123                                         if (editorInput instanceof IFileEditorInput) {
124                                                 resource= ((IFileEditorInput)editorInput).getFile();
125                                         } else {
126                                                 resource= ResourcesPlugin.getWorkspace().getRoot();
127                                         }
128 //                              } else {
129 //                                      typeName= type.getFullyQualifiedName();
130 //                                      PHPLineBreakpoint breakpoint=PHPDebugModel.lineBreakpointExists(lineNumber);
131 //                                      if (breakpoint==null)
132 //                                              PHPDebugModel.createLineBreakpoint(getFile(), lineNumber, 0, true, null);
133 //                                      else
134 //                                              DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint( breakpoint, true );
135 //
136                                         PHPLineBreakpoint existingBreakpoint= PHPDebugModel.lineBreakpointExists(lineNumber); //typeName, lineNumber);
137                                         if (existingBreakpoint != null) {
138                                                 DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(existingBreakpoint, true);
139                                                 return;
140                                         } else {
141                                                 breakpoint = PHPDebugModel.createLineBreakpoint(resource, lineNumber, 0, true, null);
142                                         }
143                                 //                              }
144 //                              new BreakpointLocationVerifierJob(document, breakpoint, lineNumber, typeName, type, resource, (IEditorStatusLine) editorPart.getAdapter(IEditorStatusLine.class)).schedule();
145                         } catch (CoreException ce) {
146                // TODO: no message in ActionMessages
147                 //ExceptionHandler.handle(ce, ActionMessages.getString("ManageBreakpointActionDelegate.error.title1"), ActionMessages.getString("ManageBreakpointActionDelegate.error.message1")); //$NON-NLS-1$ //$NON-NLS-2$
148                                 return;
149                         }
150                 }
151         }
152         /*(non-Javadoc)
153          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleLineBreakpoints(IWorkbenchPart, ISelection)
154          */
155         public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) {
156                 return selection instanceof ITextSelection;
157         }
158
159         /* (non-Javadoc)
160          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
161          */
162         public void toggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
163 //              report(null, part);
164 //              selection = translateToMembers(part, selection);
165 //              if (selection instanceof ITextSelection) {
166 //                      ITextSelection textSelection = (ITextSelection) selection;
167 //                      if (selection != null) {
168 //                              CompilationUnit compilationUnit= parseCompilationUnit((ITextEditor)part);
169 //                              if (compilationUnit != null) {
170 //                                      BreakpointMethodLocator locator= new BreakpointMethodLocator(textSelection.getOffset());
171 //                                      compilationUnit.accept(locator);
172 //                                      String methodName= locator.getMethodName();
173 //                                      if (methodName == null) {
174 //                                              report(ActionMessages.getString("ManageMethodBreakpointActionDelegate.CantAdd"), part); //$NON-NLS-1$
175 //                                              return;
176 //                                      }
177 //                                      String typeName= locator.getTypeName();
178 //                                      String methodSignature= locator.getMethodSignature();
179 //                                      if (methodSignature == null) {
180 //                                              report(ActionMessages.getString("ManageMethodBreakpointActionDelegate.methodNonAvailable"), part); //$NON-NLS-1$
181 //                                              return;
182 //                                      }
183 //                                      // check if this method breakpoint already exist. If yes, remove it.
184 //                                      IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
185 //                                      IBreakpoint[] breakpoints= breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
186 //                                      for (int i= 0; i < breakpoints.length; i++) {
187 //                                              IBreakpoint breakpoint= breakpoints[i];
188 //                                              if (breakpoint instanceof IJavaMethodBreakpoint) {
189 //                                                      IJavaMethodBreakpoint methodBreakpoint= (IJavaMethodBreakpoint)breakpoint;
190 //                                                      if (typeName.equals(methodBreakpoint.getTypeName())
191 //                                                                      && methodName.equals(methodBreakpoint.getMethodName())
192 //                                                                      && methodSignature.equals(methodBreakpoint.getMethodSignature())) {
193 //                                                              breakpointManager.removeBreakpoint(methodBreakpoint, true);
194 //                                                              return;
195 //                                                      }
196 //                                              }
197 //                                      }
198 //                                      // add the breakpoint
199 //                                      JDIDebugModel.createMethodBreakpoint(getResource((IEditorPart)part), typeName, methodName, methodSignature, true, false, false, -1, -1, -1, 0, true, new HashMap(10));
200 //                              }
201 //                      }
202 //              } else if (selection instanceof IStructuredSelection) {
203 //                      IMethod[] members= getMethods((IStructuredSelection)selection);
204 //                      if (members.length == 0) {
205 //                              report(ActionMessages.getString("ToggleBreakpointAdapter.9"), part); //$NON-NLS-1$
206 //                              return;
207 //                      }
208 //                      // add or remove the breakpoint
209 //                      IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
210 //                      for (int i= 0, length= members.length; i < length; i++) {
211 //                              IMethod method= members[i];
212 //                              IJavaBreakpoint breakpoint= getBreakpoint(method);
213 //                              if (breakpoint == null) {
214 //                                      // add breakpoint
215 //                                      int start = -1;
216 //                                      int end = -1;
217 //                                      ISourceRange range = method.getNameRange();
218 //                                      if (range != null) {
219 //                                              start = range.getOffset();
220 //                                              end = start + range.getLength();
221 //                                      }
222 //                                      Map attributes = new HashMap(10);
223 //                                      BreakpointUtils.addJavaBreakpointAttributes(attributes, method);
224 //                                      String methodName = method.getElementName();
225 //                                      if (method.isConstructor()) {
226 //                                              methodName = "<init>"; //$NON-NLS-1$
227 //                                      }
228 //                                      IType type= method.getDeclaringType();
229 //                                      String methodSignature= method.getSignature();
230 //                                      if (!type.isBinary()) {
231 //                                              //resolve the type names
232 //                                              methodSignature= resolveMethodSignature(type, methodSignature);
233 //                                              if (methodSignature == null) {
234 //                                                      IStatus status = new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, "Source method signature could not be resolved", null); //$NON-NLS-1$
235 //                                                      throw new CoreException(status);
236 //                                              }
237 //                                      }
238 //                                      JDIDebugModel.createMethodBreakpoint(BreakpointUtils.getBreakpointResource(method), type.getFullyQualifiedName(), methodName, methodSignature, true, false, false, -1, start, end, 0, true, attributes);
239 //                              } else {
240 //                                      // remove breakpoint
241 //                                      breakpointManager.removeBreakpoint(breakpoint, true);
242 //                              }
243 //                      }
244 //              }
245         }
246
247         /* (non-Javadoc)
248          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
249          */
250         public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) {
251 //              if (selection instanceof IStructuredSelection) {
252 //                      IStructuredSelection ss = (IStructuredSelection) selection;
253 //                      return getMethods(ss).length > 0;
254 //              } else {
255 //                      return selection instanceof ITextSelection;
256 //              }
257                 return false;
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 }