/******************************************************************************* * Copyright (c) 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * Bjorn Freeman-Benson - initial API and implementation *******************************************************************************/ package rhogenwizard.debugger.model; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.model.IBreakpoint; import org.eclipse.debug.core.model.ILineBreakpoint; import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.texteditor.ITextEditor; import rhogenwizard.constants.DebugConstants; import rhogenwizard.editors.RubyEditor; /** * Adapter to create breakpoints in PDA files. */ public class DebugLineBreakpointAdapter implements IToggleBreakpointsTarget { public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException { ITextEditor textEditor = getEditor(part); if (textEditor != null) { IResource resource = (IResource) textEditor.getEditorInput().getAdapter(IResource.class); ITextSelection textSelection = (ITextSelection) selection; int lineNumber = textSelection.getStartLine(); IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(DebugConstants.debugModelId); for (int i = 0; i < breakpoints.length; i++) { IBreakpoint breakpoint = breakpoints[i]; if (resource.equals(breakpoint.getMarker().getResource())) { if (((ILineBreakpoint)breakpoint).getLineNumber() == (lineNumber + 1)) { // remove breakpoint.delete(); return; } } } // create line breakpoint (doc line numbers start at 0) DebugLineBreakpoint lineBreakpoint = new DebugLineBreakpoint(resource, lineNumber + 1); DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(lineBreakpoint); } } public boolean canToggleLineBreakpoints(IWorkbenchPart part, ISelection selection) { return getEditor(part) != null; } /** * Returns the editor being used to edit a PDA file, associated with the * given part, or <code>null</code> if none. * * @param part workbench part * @return the editor being used to edit a PDA file, associated with the * given part, or <code>null</code> if none */ private ITextEditor getEditor(IWorkbenchPart part) { if (part instanceof RubyEditor) { RubyEditor editorPart = (RubyEditor) part; IResource resource = (IResource) editorPart.getEditorInput().getAdapter(IResource.class); if (resource != null) { String extension = resource.getFileExtension(); if (extension != null && extension.equals("rb")) { return (ITextEditor) editorPart.getTextEditor(); } } } return null; } public void toggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException { } public boolean canToggleMethodBreakpoints(IWorkbenchPart part, ISelection selection) { return false; } public void toggleWatchpoints(IWorkbenchPart part, ISelection selection) throws CoreException { } public boolean canToggleWatchpoints(IWorkbenchPart part, ISelection selection) { return false; } }