org.chromium.debug.core/src/org/chromium/debug/core/model/LineBreakpointAdapter.java
changeset 2 e4420d2515f1
child 52 f577ea64429e
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.debug.core.model;
       
     6 
       
     7 import org.chromium.debug.core.ChromiumDebugPlugin;
       
     8 import org.chromium.debug.core.util.ChromiumDebugPluginUtil;
       
     9 import org.eclipse.core.resources.IResource;
       
    10 import org.eclipse.core.runtime.CoreException;
       
    11 import org.eclipse.debug.core.DebugPlugin;
       
    12 import org.eclipse.debug.core.model.IBreakpoint;
       
    13 import org.eclipse.debug.core.model.ILineBreakpoint;
       
    14 import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
       
    15 import org.eclipse.jface.text.ITextSelection;
       
    16 import org.eclipse.jface.viewers.ISelection;
       
    17 import org.eclipse.ui.IWorkbenchPart;
       
    18 import org.eclipse.ui.texteditor.ITextEditor;
       
    19 
       
    20 /**
       
    21  * Adapter to create breakpoints in JS files.
       
    22  */
       
    23 public class LineBreakpointAdapter implements IToggleBreakpointsTarget {
       
    24 
       
    25   public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection)
       
    26       throws CoreException {
       
    27     ITextEditor textEditor = getEditor(part);
       
    28     if (textEditor != null) {
       
    29       IResource resource = (IResource) textEditor.getEditorInput().getAdapter(IResource.class);
       
    30       ITextSelection textSelection = (ITextSelection) selection;
       
    31       int lineNumber = textSelection.getStartLine();
       
    32       IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(
       
    33           ChromiumDebugPlugin.DEBUG_MODEL_ID);
       
    34       for (int i = 0; i < breakpoints.length; i++) {
       
    35         IBreakpoint breakpoint = breakpoints[i];
       
    36         if (resource.equals(breakpoint.getMarker().getResource())) {
       
    37           if (((ILineBreakpoint) breakpoint).getLineNumber() == lineNumber + 1) {
       
    38             // remove
       
    39             breakpoint.delete();
       
    40             return;
       
    41           }
       
    42         }
       
    43       }
       
    44 
       
    45       // Line numbers start with 0 in V8, with 1 in Eclipse.
       
    46       ChromiumLineBreakpoint lineBreakpoint = new ChromiumLineBreakpoint(resource, lineNumber + 1);
       
    47       DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(lineBreakpoint);
       
    48     }
       
    49   }
       
    50 
       
    51   public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) {
       
    52     return getEditor(part) != null;
       
    53   }
       
    54 
       
    55   /**
       
    56    * Returns the editor being used to edit a PDA file, associated with the given
       
    57    * part, or <code>null</code> if none.
       
    58    * @param part workbench part
       
    59    * @return the editor being used to edit a PDA file, associated with the given
       
    60    *         part, or <code>null</code> if none
       
    61    */
       
    62   protected ITextEditor getEditor(IWorkbenchPart part) {
       
    63     if (part instanceof ITextEditor) {
       
    64       ITextEditor editorPart = (ITextEditor) part;
       
    65       IResource resource = (IResource) editorPart.getEditorInput().getAdapter(IResource.class);
       
    66       if (resource != null &&
       
    67           ChromiumDebugPluginUtil.CHROMIUM_EXTENSION.equals(resource.getFileExtension())) {
       
    68         return editorPart;
       
    69       }
       
    70     }
       
    71     return null;
       
    72   }
       
    73 
       
    74   public void toggleMethodBreakpoints(IWorkbenchPart part, ISelection selection)
       
    75       throws CoreException {
       
    76     // TODO(apavlov): Implement method breakpoints if feasible.
       
    77   }
       
    78 
       
    79   public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) {
       
    80     // TODO(apavlov): Implement method breakpoints if feasible.
       
    81     return true;
       
    82   }
       
    83 
       
    84   public void toggleWatchpoints(IWorkbenchPart part, ISelection selection)
       
    85       throws CoreException {
       
    86     // TODO(apavlov): Implement watchpoints if feasible.
       
    87   }
       
    88 
       
    89   public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) {
       
    90     // TODO(apavlov): Implement watchpoints if feasible.
       
    91     return false;
       
    92   }
       
    93 }