001: package com.xoetrope.editor.netbeans.visualizer;
002:
003: import com.xoetrope.carousel.visualizer.ModelVisualiserPanel;
004: import com.xoetrope.carousel.visualizer.VisualiserDebuggerEngine;
005: import com.xoetrope.carousel.visualizer.VisualiserDebuggerModel;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.beans.PropertyChangeEvent;
009: import java.util.List;
010: import javax.swing.Timer;
011: import org.netbeans.api.debugger.Breakpoint;
012: import org.netbeans.api.debugger.DebuggerEngine;
013: import org.netbeans.api.debugger.DebuggerManager;
014: import org.netbeans.api.debugger.DebuggerManagerListener;
015: import org.netbeans.api.debugger.Session;
016: import org.netbeans.api.debugger.Watch;
017: import org.netbeans.api.debugger.jpda.JPDABreakpoint;
018: import org.netbeans.api.debugger.jpda.JPDADebugger;
019: import org.netbeans.api.debugger.jpda.ObjectVariable;
020: import org.netbeans.api.debugger.jpda.Variable;
021: import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
022: import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
023:
024: public class NbDebuggerEngine implements VisualiserDebuggerEngine,
025: DebuggerManagerListener, JPDABreakpointListener {
026:
027: private Breakpoint[] breakPoints;
028: private ModelVisualiserPanel visualiserPanel;
029:
030: /**
031: * Adds the "debugger" tab visualizing the model in run-time.
032: * @param vPanel the panel where the tab is to be added
033: */
034: public void setupDebuggerInterface(ModelVisualiserPanel vPanel) {
035: visualiserPanel = vPanel;
036: visualiserPanel.setDebuggerModel(null);
037: DebuggerManager debugManager = DebuggerManager
038: .getDebuggerManager();
039: debugManager.addDebuggerListener(this );
040: if (breakPoints == null) {
041: breakPoints = debugManager.getBreakpoints();
042: int numBreakpoints = 0;
043: if (breakPoints != null)
044: numBreakpoints = breakPoints.length;
045: for (int i = 0; i < numBreakpoints; i++) {
046: if (breakPoints[i] instanceof JPDABreakpoint)
047: ((JPDABreakpoint) breakPoints[i])
048: .addJPDABreakpointListener(this );
049: }
050: }
051:
052: DebuggerEngine debugEngine = debugManager.getCurrentEngine();
053: try {
054: Session session = debugManager.getCurrentSession();
055: if (session == null) {
056: visualiserPanel
057: .addMessageNode("No debugger session available");
058: return;
059: }
060: debugEngine = session.getCurrentEngine();
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064:
065: try {
066: List debuggerInstances = debugEngine.lookup(null,
067: JPDADebugger.class);
068: JPDADebugger jpdaDebugger = (JPDADebugger) debuggerInstances
069: .get(0);
070: int suspendState = jpdaDebugger.getSuspend();
071: if (/*( suspendState == jpdaDebugger.STATE_STOPPED ) &&*/(jpdaDebugger
072: .getCurrentCallStackFrame() != null)) {
073: Variable varModel = jpdaDebugger
074: .evaluate("net.xoetrope.xui.XProjectManager.getModel()");
075:
076: if (varModel instanceof ObjectVariable) {
077: ObjectVariable objVar = (ObjectVariable) varModel;
078: VisualiserDebuggerModel debuggerModel = new NbDebuggerModel(
079: jpdaDebugger, objVar);
080: visualiserPanel.setDebuggerModel(debuggerModel);
081: visualiserPanel.setRootModel(debuggerModel);
082: }
083: varModel = jpdaDebugger
084: .evaluate("net.xoetrope.xui.XProjectManager.getCurrentProject().getStartupParam(\"ProjectPath\")");
085: if (varModel instanceof ObjectVariable) {
086: ObjectVariable objVar = (ObjectVariable) varModel;
087: String projectPath = objVar.getValue();
088: projectPath = projectPath.substring(1, projectPath
089: .length() - 1);
090: visualiserPanel.setProjectPath(projectPath);
091: }
092: } else {
093: visualiserPanel.addMessageNode("Debugger running");
094: }
095: } catch (Exception e) {
096: visualiserPanel.addMessageNode("Debuggers lookup failure");
097: e.printStackTrace();
098: }
099: }
100:
101: public Breakpoint[] initBreakpoints() {
102: return new Breakpoint[0];
103: }
104:
105: /**
106: * Called when some breakpoint is removed.
107: */
108: public void breakpointRemoved(Breakpoint breakpoint) {
109: if (breakpoint instanceof JPDABreakpoint) {
110: JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint;
111: jpdaBreakpoint.removeJPDABreakpointListener(this );
112: }
113: }
114:
115: /**
116: * Called when some breakpoint is added.
117: */
118: public void breakpointAdded(Breakpoint breakpoint) {
119: if (breakpoint instanceof JPDABreakpoint) {
120: JPDABreakpoint jpdaBreakpoint = (JPDABreakpoint) breakpoint;
121: jpdaBreakpoint.addJPDABreakpointListener(this );
122: }
123: }
124:
125: public void initWatches() {
126: }
127:
128: public void watchAdded(Watch arg0) {
129: }
130:
131: public void watchRemoved(Watch arg0) {
132: }
133:
134: public void sessionAdded(Session arg0) {
135: }
136:
137: /**
138: * Called when some session is removed.
139: */
140: public void sessionRemoved(Session session) {
141: visualiserPanel.setDebuggerModel(null);
142: visualiserPanel.refresh(null);
143: visualiserPanel.addMessageNode("No debugger session available");
144:
145: int numBreakpoints = 0;
146: if (breakPoints != null)
147: numBreakpoints = breakPoints.length;
148: for (int i = 0; i < numBreakpoints; i++) {
149: if (breakPoints[i] instanceof JPDABreakpoint)
150: ((JPDABreakpoint) breakPoints[i])
151: .removeJPDABreakpointListener(this );
152: }
153: breakPoints = null;
154: }
155:
156: public void engineAdded(DebuggerEngine arg0) {
157: }
158:
159: public void engineRemoved(DebuggerEngine arg0) {
160: }
161:
162: public void propertyChange(PropertyChangeEvent evt) {
163: }
164:
165: public void breakpointReached(JPDABreakpointEvent event) {
166: if (visualiserPanel.getDebuggerModel() == null) {
167: ActionListener task = (new ActionListener() {
168: public void actionPerformed(ActionEvent e) {
169: visualiserPanel.refresh(null);
170: }
171: });
172: Timer timer = new Timer(1000, task);
173: timer.setRepeats(false);
174: timer.setCoalesce(true);
175: timer.start();
176: }
177: }
178:
179: }
|