001: package org.drools.eclipse.editors;
002:
003: import org.drools.eclipse.DRLInfo;
004: import org.drools.eclipse.DroolsEclipsePlugin;
005: import org.drools.eclipse.DRLInfo.FunctionInfo;
006: import org.drools.eclipse.DRLInfo.RuleInfo;
007: import org.drools.eclipse.debug.core.DroolsLineBreakpoint;
008: import org.drools.eclipse.debug.core.IDroolsDebugConstants;
009: import org.eclipse.core.resources.IResource;
010: import org.eclipse.core.runtime.CoreException;
011: import org.eclipse.debug.core.DebugPlugin;
012: import org.eclipse.debug.core.model.IBreakpoint;
013: import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
014: import org.eclipse.jface.text.ITextSelection;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.ui.IEditorPart;
017: import org.eclipse.ui.IWorkbenchPart;
018:
019: public class DroolsLineBreakpointAdapter implements
020: IToggleBreakpointsTarget {
021:
022: public boolean canToggleLineBreakpoints(IWorkbenchPart part,
023: ISelection selection) {
024: if (part instanceof IEditorPart
025: && selection instanceof ITextSelection) {
026: IEditorPart editor = (IEditorPart) part;
027: IResource resource = (IResource) editor.getEditorInput()
028: .getAdapter(IResource.class);
029: ITextSelection textSelection = (ITextSelection) selection;
030: int lineNumber = textSelection.getStartLine();
031: try {
032: DRLInfo drlInfo = DroolsEclipsePlugin.getDefault()
033: .parseResource(resource, false);
034: if (drlInfo != null) {
035: RuleInfo ruleInfo = drlInfo.getRuleInfo(lineNumber);
036: if (ruleInfo != null) {
037: if (ruleInfo.getConsequenceDrlLineNumber() <= lineNumber) {
038: return true;
039: }
040: }
041: FunctionInfo functionInfo = drlInfo
042: .getFunctionInfo(lineNumber);
043: if (functionInfo != null) {
044: if (functionInfo.getDrlLineNumber() <= lineNumber) {
045: return true;
046: }
047: }
048: }
049: } catch (Throwable t) {
050: DroolsEclipsePlugin.log(t);
051: }
052: }
053: return false;
054: }
055:
056: public boolean canToggleMethodBreakpoints(IWorkbenchPart part,
057: ISelection selection) {
058: return false;
059: }
060:
061: public boolean canToggleWatchpoints(IWorkbenchPart part,
062: ISelection selection) {
063: return false;
064: }
065:
066: public void toggleLineBreakpoints(IWorkbenchPart part,
067: ISelection selection) throws CoreException {
068: if (part instanceof IEditorPart) {
069: IEditorPart editor = (IEditorPart) part;
070: IResource resource = (IResource) editor.getEditorInput()
071: .getAdapter(IResource.class);
072: ITextSelection textSelection = (ITextSelection) selection;
073: int lineNumber = textSelection.getStartLine();
074: IBreakpoint[] breakpoints = DebugPlugin
075: .getDefault()
076: .getBreakpointManager()
077: .getBreakpoints(
078: IDroolsDebugConstants.ID_DROOLS_DEBUG_MODEL);
079: for (int i = 0; i < breakpoints.length; i++) {
080: IBreakpoint breakpoint = breakpoints[i];
081: if (resource.equals(breakpoint.getMarker()
082: .getResource())) {
083: if (breakpoint.getMarker().getType().equals(
084: IDroolsDebugConstants.DROOLS_MARKER_TYPE)) {
085: if (((DroolsLineBreakpoint) breakpoint)
086: .getDRLLineNumber() == (lineNumber + 1)) {
087: breakpoint.delete();
088: return;
089: }
090: }
091: }
092: }
093: // TODO: drools breakpoints can only be created in functions and consequences
094: DroolsLineBreakpoint lineBreakpoint = new DroolsLineBreakpoint(
095: resource, lineNumber + 1);
096: DebugPlugin.getDefault().getBreakpointManager()
097: .addBreakpoint(lineBreakpoint);
098: }
099: }
100:
101: public void toggleMethodBreakpoints(IWorkbenchPart part,
102: ISelection selection) throws CoreException {
103: // do nothing
104: }
105:
106: public void toggleWatchpoints(IWorkbenchPart part,
107: ISelection selection) throws CoreException {
108: // do nothing
109: }
110: }
|