001: package com.xoetrope.editor.eclipse.visualizer;
002:
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005:
006: import javax.swing.Timer;
007:
008: import org.eclipse.core.runtime.IAdaptable;
009: import org.eclipse.debug.core.DebugEvent;
010: import org.eclipse.debug.core.DebugPlugin;
011: import org.eclipse.debug.core.IDebugEventSetListener;
012: import org.eclipse.debug.core.IExpressionManager;
013: import org.eclipse.debug.core.model.IDebugTarget;
014: import org.eclipse.debug.core.model.IValue;
015: import org.eclipse.debug.core.model.IWatchExpression;
016: import org.eclipse.debug.ui.DebugUITools;
017: import org.eclipse.jdt.debug.core.IJavaClassType;
018: import org.eclipse.jdt.debug.core.IJavaDebugTarget;
019: import org.eclipse.jdt.debug.core.IJavaObject;
020: import org.eclipse.jdt.debug.core.IJavaStackFrame;
021: import org.eclipse.jdt.debug.core.IJavaThread;
022: import org.eclipse.jdt.debug.core.IJavaType;
023:
024: import com.xoetrope.carousel.visualizer.ModelVisualiserPanel;
025: import com.xoetrope.carousel.visualizer.VisualiserDebuggerEngine;
026: import com.xoetrope.carousel.visualizer.VisualiserDebuggerModel;
027:
028: public class EclipseDebuggerEngine implements VisualiserDebuggerEngine,
029: IDebugEventSetListener {
030: private ModelVisualiserPanel visualiserPanel;
031:
032: /**
033: * Adds the "debugger" tab visualising the model in run-time
034: * @param vPanel the panel where the tab is to be added
035: */
036: public void setupDebuggerInterface(ModelVisualiserPanel vPanel) {
037: visualiserPanel = vPanel;
038: visualiserPanel.setDebuggerModel(null);
039:
040: DebugPlugin debugPlugin = DebugPlugin.getDefault();
041: debugPlugin.addDebugEventListener(this );
042: IAdaptable debugContext = DebugUITools.getDebugContext();
043: if (!(debugContext instanceof IJavaStackFrame)) {
044: visualiserPanel
045: .addMessageNode("no debugger session available");
046: return;
047: }
048:
049: IJavaStackFrame stackFrame = (IJavaStackFrame) debugContext;
050:
051: IJavaObject rootModel = getRootModel(stackFrame);
052: if (rootModel == null) {
053: visualiserPanel
054: .addMessageNode("no debugger session available");
055: return;
056: }
057:
058: VisualiserDebuggerModel debugRootModel = new EclipseDebuggerModel(
059: stackFrame, rootModel);
060: visualiserPanel.setDebuggerModel(debugRootModel);
061: visualiserPanel.setRootModel(debugRootModel);
062: }
063:
064: /**
065: * Gets the object referencing a root model on target VM
066: * @param stackFrame a thread on VM
067: * @return the root model representation
068: */
069: private IJavaObject getRootModel(IJavaStackFrame stackFrame) {
070: IJavaObject rootModel = null;
071: try {
072:
073: IJavaThread thread = (IJavaThread) stackFrame.getThread();
074: IJavaDebugTarget debugTarget = (IJavaDebugTarget) stackFrame
075: .getDebugTarget();
076:
077: IJavaType[] types = debugTarget
078: .getJavaTypes("net.xoetrope.xui.XProjectManager");
079: IJavaClassType projectManager = (IJavaClassType) types[0];
080:
081: rootModel = (IJavaObject) projectManager.sendMessage(
082: "getModel", "()Lnet/xoetrope/xui/data/XModel;",
083: null, thread);
084: } catch (Exception ex) {
085: rootModel = null;
086: }
087:
088: return rootModel;
089: }
090:
091: /**
092: * Invoked whenever a breakpoint is hit
093: */
094: private void breakpointReached() {
095: if (visualiserPanel.getDebuggerModel() == null) {
096: ActionListener task = (new ActionListener() {
097: public void actionPerformed(ActionEvent e) {
098: visualiserPanel.refresh(null);
099: }
100: });
101: Timer timer = new Timer(1000, task);
102: timer.setRepeats(false);
103: timer.setCoalesce(true);
104: timer.start();
105: }
106: }
107:
108: /**
109: * Invoked when the debugger session has been removed
110: */
111: private void sessionRemoved() {
112: visualiserPanel.setDebuggerModel(null);
113: visualiserPanel.refresh(null);
114: visualiserPanel.addMessageNode("No debugger session available");
115: }
116:
117: /**
118: * Handles breakpoint hits and "debugger session removed" event.
119: */
120: public void handleDebugEvents(DebugEvent[] events) {
121: for (int i = 0; i < events.length; i++) {
122: DebugEvent event = events[i];
123:
124: // TODO: not sure whether these conditions are sufficient
125: if (event.getKind() == DebugEvent.TERMINATE) {
126: if (event.getSource() instanceof IDebugTarget)
127: sessionRemoved();
128: } else if (event.getKind() == DebugEvent.SUSPEND) {
129: breakpointReached();
130: }
131:
132: }
133: }
134:
135: }
|