6fa47db3d0e98749429b477a8bed4c9988742e83
[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 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                 return false;
259         }
260         
261         protected IMethod[] getMethods(IStructuredSelection selection) {
262                 if (selection.isEmpty()) {
263                         return new IMethod[0];
264                 } else {
265                         List methods = new ArrayList(selection.size());
266                         Iterator iterator = selection.iterator();
267                         while (iterator.hasNext()) {
268                                 Object thing = iterator.next();
269                                 try {
270                                         if (thing instanceof IMethod && !Flags.isAbstract(((IMethod)thing).getFlags())) {       
271                                                 methods.add(thing);
272                                         }
273                                 } catch (JavaModelException e) {
274                                 }
275                         }
276                         return (IMethod[]) methods.toArray(new IMethod[methods.size()]);
277                 }
278         }
279         
280 //      protected IField[] getFields(IStructuredSelection selection) {
281 //              if (selection.isEmpty()) {
282 //                      return new IField[0];
283 //              } else {
284 //                      List fields = new ArrayList(selection.size());
285 //                      Iterator iterator = selection.iterator();
286 //                      while (iterator.hasNext()) {
287 //                              Object thing = iterator.next();
288 //                              if (thing instanceof IField) {  
289 //                                      fields.add(thing);
290 //                              } else if (thing instanceof IJavaFieldVariable) {
291 //                                      IField field= getField((IJavaFieldVariable) thing);
292 //                                      if (field != null) {
293 //                                              fields.add(field);
294 //                                      }
295 //                              }
296 //                      }
297 //                      return (IField[]) fields.toArray(new IField[fields.size()]);
298 //              }
299 //      }       
300
301         /* (non-Javadoc)
302          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
303          */
304         public void toggleWatchpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
305 //              report(null, part);
306 //              selection = translateToMembers(part, selection);
307 //              if (selection instanceof ITextSelection) {
308 //                      ITextSelection textSelection= (ITextSelection) selection;
309 //                      CompilationUnit compilationUnit= parseCompilationUnit((ITextEditor)part);
310 //                      if (compilationUnit != null) {
311 //                              BreakpointFieldLocator locator= new BreakpointFieldLocator(textSelection.getOffset());
312 //                              compilationUnit.accept(locator);
313 //                              String fieldName= locator.getFieldName();
314 //                              if (fieldName == null) {
315 //                                      report(ActionMessages.getString("ManageWatchpointActionDelegate.CantAdd"), part); //$NON-NLS-1$
316 //                                      return;
317 //                              }
318 //                              String typeName= locator.getTypeName();
319 //                              // check if the watchpoint already exists. If yes, remove it
320 //                              IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
321 //                              IBreakpoint[] breakpoints= breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
322 //                              for (int i= 0; i < breakpoints.length; i++) {
323 //                                      IBreakpoint breakpoint= breakpoints[i];
324 //                                      if (breakpoint instanceof IJavaWatchpoint) {
325 //                                              IJavaWatchpoint watchpoint= (IJavaWatchpoint)breakpoint;
326 //                                              if (typeName.equals(watchpoint.getTypeName()) && fieldName.equals(watchpoint.getFieldName())) {
327 //                                                      breakpointManager.removeBreakpoint(watchpoint, true);
328 //                                                      return;
329 //                                              }
330 //                                      }
331 //                              }
332 //                              // add the watchpoint
333 //                              JDIDebugModel.createWatchpoint(getResource((IEditorPart)part), typeName, fieldName, -1, -1, -1, 0, true, new HashMap(10));
334 //                      }
335 //              } else if (selection instanceof IStructuredSelection) {
336 //                      IField[] members = getFields((IStructuredSelection)selection);
337 //                      if (members.length == 0) {
338 //                              report(ActionMessages.getString("ToggleBreakpointAdapter.10"), part); //$NON-NLS-1$
339 //                              return;
340 //                      }
341 //                      // add or remove watchpoint
342 //                      IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
343 //                      for (int i= 0, length= members.length; i < length; i++) {
344 //                              IField element= members[i];
345 //                              IJavaBreakpoint breakpoint= getBreakpoint(element);
346 //                              if (breakpoint == null) {
347 //                                      IType type = element.getDeclaringType();
348 //                                      int start = -1;
349 //                                      int end = -1;
350 //                                      ISourceRange range = element.getNameRange();
351 //                                      if (range != null) {
352 //                                              start = range.getOffset();
353 //                                              end = start + range.getLength();
354 //                                      }
355 //                                      Map attributes = new HashMap(10);
356 //                                      BreakpointUtils.addJavaBreakpointAttributes(attributes, element);
357 //                                      JDIDebugModel.createWatchpoint(BreakpointUtils.getBreakpointResource(type), type.getFullyQualifiedName(), element.getElementName(), -1, start, end, 0, true, attributes);
358 //                              } else {
359 //                                      // remove breakpoint
360 //                                      breakpointManager.removeBreakpoint(breakpoint, true);
361 //                              }
362 //                      }
363 //              }
364         }
365         
366         public static String resolveMethodSignature(IType type, String methodSignature) throws JavaModelException {
367                 String[] parameterTypes= Signature.getParameterTypes(methodSignature);
368                 int length= length= parameterTypes.length;
369                 String[] resolvedParameterTypes= new String[length];
370                 
371                 for (int i = 0; i < length; i++) {
372                         resolvedParameterTypes[i]= resolveType(type, parameterTypes[i]);
373                         if (resolvedParameterTypes[i] == null) {
374                                 return null;
375                         }
376                 }
377                 
378                 String resolvedReturnType= resolveType(type, Signature.getReturnType(methodSignature));
379                 if (resolvedReturnType == null) {
380                         return null;
381                 }
382                                 
383                 return Signature.createMethodSignature(resolvedParameterTypes, resolvedReturnType);
384         }
385         
386         private static String resolveType(IType type, String typeSignature) throws JavaModelException {
387 //              int count= Signature.getArrayCount(typeSignature);
388 //              String elementTypeSignature= Signature.getElementType(typeSignature);
389 //              if (elementTypeSignature.length() == 1) {
390 //                      // no need to resolve primitive types
391 //                      return typeSignature;
392 //              }
393 //              String elementTypeName= Signature.toString(elementTypeSignature);
394 //              String[][] resolvedElementTypeNames= type.resolveType(elementTypeName);
395 //              if (resolvedElementTypeNames == null || resolvedElementTypeNames.length != 1) {
396 //                      // the type name cannot be resolved
397 //                      return null;
398 //              }
399 //              String resolvedElementTypeName= Signature.toQualifiedName(resolvedElementTypeNames[0]);
400 //              String resolvedElementTypeSignature= Signature.createTypeSignature(resolvedElementTypeName, true).replace('.', '/');
401 //              return Signature.createArraySignature(resolvedElementTypeSignature, count);
402                 return "";
403         }       
404         
405         protected static IResource getResource(IEditorPart editor) {
406                 IResource resource;
407                 IEditorInput editorInput = editor.getEditorInput();
408                 if (editorInput instanceof IFileEditorInput) {
409                         resource= ((IFileEditorInput)editorInput).getFile();
410                 } else {
411                         resource= ResourcesPlugin.getWorkspace().getRoot();
412                 }
413                 return resource;
414         }
415         
416         /**
417          * Returns a handle to the specified method or <code>null</code> if none.
418          * 
419          * @param editorPart the editor containing the method
420          * @param typeName
421          * @param methodName
422          * @param signature
423          * @return handle or <code>null</code>
424          */
425         protected IMethod getMethodHandle(IEditorPart editorPart, String typeName, String methodName, String signature) throws CoreException {
426                 IJavaElement element = (IJavaElement) editorPart.getEditorInput().getAdapter(IJavaElement.class);
427                 IType type = null;
428                 if (element instanceof ICompilationUnit) {
429                         IType[] types = ((ICompilationUnit)element).getAllTypes();
430                         for (int i = 0; i < types.length; i++) {
431                                 if (types[i].getFullyQualifiedName().equals(typeName)) {
432                                         type = types[i];
433                                         break;
434                                 }
435                         }
436                 }
437 //              else if (element instanceof IClassFile) {
438 //                      type = ((IClassFile)element).getType();
439 //              }
440                 if (type != null) {
441                         String[] sigs = Signature.getParameterTypes(signature);
442                         return type.getMethod(methodName, sigs);
443                 }
444                 return null;
445         }
446         
447 //      protected IJavaBreakpoint getBreakpoint(IMember element) {
448 //              IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
449 //              IBreakpoint[] breakpoints= breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
450 //              if (element instanceof IMethod) {
451 //                      IMethod method= (IMethod)element;
452 //                      for (int i= 0; i < breakpoints.length; i++) {
453 //                              IBreakpoint breakpoint= breakpoints[i];
454 //                              if (breakpoint instanceof IJavaMethodBreakpoint) {
455 //                                      IJavaMethodBreakpoint methodBreakpoint= (IJavaMethodBreakpoint)breakpoint;
456 //                                      IMember container = null;
457 //                                      try {
458 //                                              container= BreakpointUtils.getMember(methodBreakpoint);
459 //                                      } catch (CoreException e) {
460 //                                              JDIDebugUIPlugin.log(e);
461 //                                              return null;
462 //                                      }
463 //                                      if (container == null) {
464 //                                              try {
465 //                                                      if (method.getDeclaringType().getFullyQualifiedName().equals(methodBreakpoint.getTypeName())
466 //                                                                      && method.getElementName().equals(methodBreakpoint.getMethodName())
467 //                                                                      && method.getSignature().equals(methodBreakpoint.getMethodSignature())) {
468 //                                                              return methodBreakpoint;
469 //                                                      }
470 //                                              } catch (CoreException e) {
471 //                                                      JDIDebugUIPlugin.log(e);
472 //                                              }
473 //                                      } else {
474 //                                              if (container instanceof IMethod) {
475 //                                                      if (method.getDeclaringType().getFullyQualifiedName().equals(container.getDeclaringType().getFullyQualifiedName())) {
476 //                                                              if (method.isSimilar((IMethod)container)) {
477 //                                                                      return methodBreakpoint;
478 //                                                              }
479 //                                                      }
480 //                                              }
481 //                                      }
482 //                              }
483 //                      }
484 //              } else if (element instanceof IField) {
485 //                      for (int i= 0; i < breakpoints.length; i++) {
486 //                              IBreakpoint breakpoint= breakpoints[i];
487 //                              if (breakpoint instanceof IJavaWatchpoint) {
488 //                                      try {
489 //                                              if (equalFields(element, (IJavaWatchpoint)breakpoint))
490 //                                                      return (IJavaBreakpoint)breakpoint;
491 //                                      } catch (CoreException e) {
492 //                                              JDIDebugUIPlugin.log(e);
493 //                                      }
494 //                              }
495 //                      }                       
496 //              }
497 //              return null;
498 //      }
499
500         /**
501          * Compare two fields. The default <code>equals()</code>
502          * method for <code>IField</code> doesn't give the comparison desired.
503          */
504 //      private boolean equalFields(IMember breakpointField, IJavaWatchpoint watchpoint) throws CoreException {
505 //              return (breakpointField.getElementName().equals(watchpoint.getFieldName()) &&
506 //              breakpointField.getDeclaringType().getFullyQualifiedName().equals(watchpoint.getTypeName()));
507 //      }
508 //      
509 //      protected CompilationUnit parseCompilationUnit(ITextEditor editor) {
510 //              IEditorInput editorInput = editor.getEditorInput();
511 //              IDocument document= editor.getDocumentProvider().getDocument(editorInput);
512 //              ASTParser parser = ASTParser.newParser(AST.JLS2);
513 //              parser.setSource(document.get().toCharArray());
514 //              return (CompilationUnit) parser.createAST(null);
515 //      }
516         
517         /* (non-Javadoc)
518          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
519          */
520         public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
521 //              if (selection instanceof IStructuredSelection) {
522 //                      IStructuredSelection ss = (IStructuredSelection) selection;
523 //                      return getFields(ss).length > 0;
524 //              } else {
525 //                      return selection instanceof ITextSelection;
526 //              }
527                 return false;
528         }
529         
530         /**
531          * Returns a selection of the member in the given text selection,
532          * or the original selection if none.
533          *    
534          * @param part
535          * @param selection
536          * @return a structured selection of the member in the given text selection,
537          * or the original selection if none
538          * @exception CoreException if an exceptoin occurrs
539          */
540 //      protected ISelection translateToMembers(IWorkbenchPart part, ISelection selection) throws CoreException {
541 //              if (selection instanceof ITextSelection && part instanceof IEditorPart) {
542 //                      ITextSelection textSelection = (ITextSelection)selection;
543 //                      IEditorPart editorPart = (IEditorPart) part;
544 //                      IEditorInput editorInput = editorPart.getEditorInput();
545 //                      IMember m= null;
546 //                      IClassFile classFile= (IClassFile)editorInput.getAdapter(IClassFile.class);
547 //                      if (classFile != null) {
548 //                              IJavaElement e= classFile.getElementAt(textSelection.getOffset());
549 //                              if (e instanceof IMember) {
550 //                                      m= (IMember)e;
551 //                              }
552 //                      } else {
553 //                              IWorkingCopyManager manager= JavaUI.getWorkingCopyManager();
554 //                              ICompilationUnit unit= manager.getWorkingCopy(editorInput);
555 //                              if (unit != null) {
556 //                                      synchronized (unit) {
557 //                                              unit.reconcile(ICompilationUnit.NO_AST /*don't create ast*/, false/*don't force problem detection*/, null/*use primary owner*/, null/*no progress monitor*/);
558 //                                      }
559 //                                      IJavaElement e = unit.getElementAt(textSelection.getOffset());
560 //                                      if (e instanceof IMember) {
561 //                                              m= (IMember)e;
562 //                                      }
563 //                              }
564 //                      }
565 //                      if (m != null) {
566 //                              return new StructuredSelection(m);
567 //                      }
568 //              }
569 //              return selection;
570 //      }
571
572         /**
573          * Returns a list of matching types (IType - Java model) that correspond to the 
574          * declaring type (ReferenceType - JDI model) of the given variable.
575          */
576 //      protected static List searchForDeclaringType(IJavaFieldVariable variable) {
577 //              List types= new ArrayList();
578 //              ILaunch launch = variable.getDebugTarget().getLaunch();
579 //              if (launch == null) {
580 //                      return types;
581 //              }
582 //              
583 //              ILaunchConfiguration configuration= launch.getLaunchConfiguration();
584 //              IJavaProject[] javaProjects = null;
585 //              IWorkspace workspace= ResourcesPlugin.getWorkspace();
586 //              if (configuration != null) {
587 //                      // Launch configuration support
588 //                      try {
589 //                              String projectName= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
590 //                              if (projectName.length() != 0) {
591 //                                      javaProjects= new IJavaProject[] {JavaCore.create(workspace.getRoot().getProject(projectName))};
592 //                              } else {
593 //                                      IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
594 //                                      IProject project;
595 //                                      List projectList= new ArrayList();
596 //                                      for (int i= 0, numProjects= projects.length; i < numProjects; i++) {
597 //                                              project= projects[i];
598 //                                              if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
599 //                                                      projectList.add(JavaCore.create(project));
600 //                                              }
601 //                                      }
602 //                                      javaProjects= new IJavaProject[projectList.size()];
603 //                                      projectList.toArray(javaProjects);
604 //                              }
605 //                      } catch (CoreException e) {
606 //                              JDIDebugUIPlugin.log(e);
607 //                      }
608 //              }
609 //              if (javaProjects == null) {
610 //                      return types;
611 //              }
612 //
613 //              SearchEngine engine= new SearchEngine();
614 //              IJavaSearchScope scope= SearchEngine.createJavaSearchScope(javaProjects, true);
615 //              String declaringType= null;
616 //              try {
617 //                      declaringType= variable.getDeclaringType().getName();
618 //              } catch (DebugException x) {
619 //                      JDIDebugUIPlugin.log(x);
620 //                      return types;
621 //              }
622 //              ArrayList typeRefsFound= new ArrayList(3);
623 //              ITypeNameRequestor requestor= new TypeInfoRequestor(typeRefsFound);
624 //              try {
625 //                      engine.searchAllTypeNames( 
626 //                              getPackage(declaringType), 
627 //                              getTypeName(declaringType), 
628 //                              SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, 
629 //                              IJavaSearchConstants.CLASS, 
630 //                              scope, 
631 //                              requestor, 
632 //                              IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
633 //                              null);
634 //              } catch (JavaModelException x) {
635 //                      JDIDebugUIPlugin.log(x);
636 //                      return types;
637 //              }
638 //              Iterator iter= typeRefsFound.iterator();
639 //              TypeInfo typeInfo= null;
640 //              while (iter.hasNext()) {
641 //                      typeInfo= (TypeInfo)iter.next();
642 //                      try {
643 //                              types.add(typeInfo.resolveType(scope));
644 //                      } catch (JavaModelException jme) {
645 //                              JDIDebugUIPlugin.log(jme);
646 //                      }
647 //              }
648 //              return types;
649 //      }
650         
651         /**
652          * Returns the package name of the given fully qualified type name.
653          * The package name is assumed to be the dot-separated prefix of the 
654          * type name.
655          */
656 //      protected static char[] getPackage(String fullyQualifiedName) {
657 //              int index= fullyQualifiedName.lastIndexOf('.');
658 //              if (index == -1) {
659 //                      return new char[0];
660 //              }
661 //              return fullyQualifiedName.substring(0, index).toCharArray();
662 //      }
663 //      
664 //      /**
665 //       * Returns a simple type name from the given fully qualified type name.
666 //       * The type name is assumed to be the last contiguous segment of the 
667 //       * fullyQualifiedName not containing a '.' or '$'
668 //       */
669 //      protected static char[] getTypeName(String fullyQualifiedName) {
670 //              int index= fullyQualifiedName.lastIndexOf('.');
671 //              String typeName= fullyQualifiedName.substring(index + 1);
672 //              int lastInnerClass= typeName.lastIndexOf('$');
673 //              if (lastInnerClass != -1) {
674 //                      typeName= typeName.substring(lastInnerClass + 1);
675 //              }
676 //              return typeName.toCharArray();
677 //      }
678 //      
679 //      /**
680 //       * Return the associated IField (Java model) for the given
681 //       * IJavaFieldVariable (JDI model)
682 //       */
683 //      private IField getField(IJavaFieldVariable variable) {
684 //              String varName= null;
685 //              try {
686 //                      varName= variable.getName();
687 //              } catch (DebugException x) {
688 //                      JDIDebugUIPlugin.log(x);
689 //                      return null;
690 //              }
691 //              IField field;
692 //              List types= searchForDeclaringType(variable);
693 //              Iterator iter= types.iterator();
694 //              while (iter.hasNext()) {
695 //                      IType type= (IType)iter.next();
696 //                      field= type.getField(varName);
697 //                      if (field.exists()) {
698 //                              return field;
699 //                      }
700 //              }
701 //              return null;
702 //      }       
703 }