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