001: package org.drools.eclipse.debug;
002:
003: import org.drools.eclipse.DroolsEclipsePlugin;
004: import org.drools.eclipse.debug.actions.ShowLogicalStructureAction;
005: import org.eclipse.debug.core.DebugException;
006: import org.eclipse.debug.core.model.IStackFrame;
007: import org.eclipse.debug.core.model.IValue;
008: import org.eclipse.debug.core.model.IVariable;
009: import org.eclipse.debug.internal.ui.VariablesViewModelPresentation;
010: import org.eclipse.debug.internal.ui.contexts.DebugContextManager;
011: import org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener;
012: import org.eclipse.debug.ui.AbstractDebugView;
013: import org.eclipse.debug.ui.IDebugModelPresentation;
014: import org.eclipse.debug.ui.IDebugUIConstants;
015: import org.eclipse.jdt.debug.core.IJavaObject;
016: import org.eclipse.jdt.debug.core.IJavaStackFrame;
017: import org.eclipse.jdt.debug.core.IJavaThread;
018: import org.eclipse.jface.action.GroupMarker;
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.IToolBarManager;
022: import org.eclipse.jface.resource.JFaceResources;
023: import org.eclipse.jface.viewers.IColorProvider;
024: import org.eclipse.jface.viewers.IContentProvider;
025: import org.eclipse.jface.viewers.ILabelProvider;
026: import org.eclipse.jface.viewers.ILabelProviderListener;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.TreeViewer;
030: import org.eclipse.jface.viewers.Viewer;
031: import org.eclipse.swt.graphics.Color;
032: import org.eclipse.swt.graphics.Image;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.ui.ISelectionListener;
035: import org.eclipse.ui.IWorkbenchActionConstants;
036: import org.eclipse.ui.IWorkbenchPart;
037:
038: /**
039: * A generic Drools debug view.
040: *
041: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
042: */
043: public abstract class DroolsDebugEventHandlerView extends
044: AbstractDebugView implements IDebugContextListener,
045: ISelectionListener {
046:
047: private VariablesViewModelPresentation modelPresentation;
048: private boolean showLogical = true;
049: private Object[] oldExpandedElements = new Object[0];
050:
051: public void dispose() {
052: DebugContextManager.getDefault().removeDebugContextListener(
053: this , getSite().getWorkbenchWindow());
054: getSite().getPage().removeSelectionListener(
055: IDebugUIConstants.ID_VARIABLE_VIEW, this );
056: super .dispose();
057: }
058:
059: public boolean isShowLogicalStructure() {
060: return showLogical;
061: }
062:
063: public void setShowLogicalStructure(boolean showLogical) {
064: this .showLogical = showLogical;
065: }
066:
067: protected void setViewerInput(Object context) {
068: Object input = null;
069:
070: // if a working memory has been explicitly selected as variable, use this
071: if (context instanceof IVariable) {
072: IVariable variable = (IVariable) context;
073: try {
074: IValue value = ((IVariable) context).getValue();
075: if (value != null
076: && value instanceof IJavaObject
077: && "org.drools.reteoo.ReteooStatefulSession"
078: .equals(variable.getValue()
079: .getReferenceTypeName())) {
080: input = value;
081: }
082: } catch (Throwable t) {
083: DroolsEclipsePlugin.log(t);
084: }
085: }
086: // else get selected thread and determine if any of the stack frames
087: // is executing in a working memory, if so, use that one
088: if (input == null) {
089: ISelection stackSelection = DebugContextManager
090: .getDefault().getActiveContext(
091: getSite().getWorkbenchWindow());
092: if (stackSelection instanceof IStructuredSelection) {
093: Object selection = ((IStructuredSelection) stackSelection)
094: .getFirstElement();
095: if (selection instanceof IJavaStackFrame) {
096: try {
097: IJavaThread thread = (IJavaThread) ((IJavaStackFrame) selection)
098: .getThread();
099: IStackFrame[] frames = thread.getStackFrames();
100: for (int i = 0; i < frames.length; i++) {
101: IJavaObject stackObj = ((IJavaStackFrame) frames[i])
102: .getThis();
103: if ((stackObj != null)
104: && (stackObj.getJavaType() != null)
105: && ("org.drools.reteoo.ReteooStatefulSession"
106: .equals(stackObj
107: .getJavaType()
108: .getName()))) {
109: input = stackObj;
110: break;
111: }
112: }
113: } catch (Throwable t) {
114: DroolsEclipsePlugin.log(t);
115: }
116: }
117: }
118: }
119:
120: Object current = getViewer().getInput();
121:
122: if (current == null && input == null) {
123: return;
124: }
125:
126: Object[] newExpandedElements = ((TreeViewer) getViewer())
127: .getExpandedElements();
128: if (newExpandedElements.length != 0) {
129: oldExpandedElements = newExpandedElements;
130: }
131: getViewer().setInput(input);
132: if (input != null) {
133: ((TreeViewer) getViewer())
134: .setExpandedElements(oldExpandedElements);
135: ((TreeViewer) getViewer())
136: .expandToLevel(getAutoExpandLevel());
137: }
138: }
139:
140: protected Viewer createViewer(Composite parent) {
141: TreeViewer variablesViewer = new TreeViewer(parent);
142: variablesViewer.setContentProvider(createContentProvider());
143: variablesViewer
144: .setLabelProvider(new VariablesViewLabelProvider(
145: getModelPresentation()));
146: variablesViewer.setUseHashlookup(true);
147: DebugContextManager.getDefault().addDebugContextListener(this ,
148: getSite().getWorkbenchWindow());
149: getSite().getPage().addSelectionListener(
150: IDebugUIConstants.ID_VARIABLE_VIEW, this );
151: return variablesViewer;
152: }
153:
154: protected int getAutoExpandLevel() {
155: return 0;
156: }
157:
158: protected abstract IContentProvider createContentProvider();
159:
160: protected String getHelpContextId() {
161: return null;
162: }
163:
164: protected void becomesHidden() {
165: setViewerInput(null);
166: super .becomesHidden();
167: }
168:
169: protected void becomesVisible() {
170: super .becomesVisible();
171: ISelection selection = getSite().getPage().getSelection(
172: IDebugUIConstants.ID_VARIABLE_VIEW);
173: if (selection instanceof IStructuredSelection) {
174: setViewerInput(((IStructuredSelection) selection)
175: .getFirstElement());
176: }
177: }
178:
179: protected void createActions() {
180: IAction action = new ShowLogicalStructureAction(this );
181: setAction("ShowLogicalStructure", action);
182: }
183:
184: protected void configureToolBar(IToolBarManager tbm) {
185: tbm.add(getAction("ShowLogicalStructure"));
186: }
187:
188: protected void fillContextMenu(IMenuManager menu) {
189: menu
190: .add(new GroupMarker(
191: IWorkbenchActionConstants.MB_ADDITIONS));
192: }
193:
194: public void contextActivated(ISelection selection,
195: IWorkbenchPart part) {
196: if (!isAvailable() || !isVisible()) {
197: return;
198: }
199:
200: if (selection instanceof IStructuredSelection) {
201: setViewerInput(((IStructuredSelection) selection)
202: .getFirstElement());
203: }
204: showViewer();
205: }
206:
207: public void contextChanged(ISelection selection, IWorkbenchPart part) {
208: }
209:
210: public void selectionChanged(IWorkbenchPart part,
211: ISelection selection) {
212: if (!isAvailable()) {
213: return;
214: }
215: if (selection == null) {
216: setViewerInput(null);
217: } else if (selection instanceof IStructuredSelection) {
218: setViewerInput(((IStructuredSelection) selection)
219: .getFirstElement());
220: }
221: }
222:
223: protected void initActionState(IAction action) {
224: // The show logical structure action is always enabled by default
225: // when (re)starting the view
226: String id = action.getId();
227: if (id.endsWith("ShowLogicalStructureAction")) {
228: action.setChecked(true);
229: } else {
230: super .initActionState(action);
231: }
232: }
233:
234: protected IDebugModelPresentation getModelPresentation() {
235: if (modelPresentation == null) {
236: modelPresentation = new VariablesViewModelPresentation();
237: }
238: return modelPresentation;
239: }
240:
241: private class VariablesViewLabelProvider implements ILabelProvider,
242: IColorProvider {
243:
244: private IDebugModelPresentation presentation;
245:
246: public VariablesViewLabelProvider(
247: IDebugModelPresentation presentation) {
248: this .presentation = presentation;
249: }
250:
251: public IDebugModelPresentation getPresentation() {
252: return presentation;
253: }
254:
255: public Image getImage(Object element) {
256: return presentation.getImage(element);
257: }
258:
259: public String getText(Object element) {
260: return presentation.getText(element);
261: }
262:
263: public void addListener(ILabelProviderListener listener) {
264: presentation.addListener(listener);
265: }
266:
267: public void dispose() {
268: presentation.dispose();
269: }
270:
271: public boolean isLabelProperty(Object element, String property) {
272: return presentation.isLabelProperty(element, property);
273: }
274:
275: public void removeListener(ILabelProviderListener listener) {
276: presentation.removeListener(listener);
277: }
278:
279: public Color getForeground(Object element) {
280: if (element instanceof IVariable) {
281: IVariable variable = (IVariable) element;
282: try {
283: if (variable.hasValueChanged()) {
284: return JFaceResources
285: .getColorRegistry()
286: .get(
287: IDebugUIConstants.PREF_CHANGED_DEBUG_ELEMENT_COLOR);
288: }
289: } catch (DebugException e) {
290: DroolsEclipsePlugin.log(e);
291: }
292: }
293: return null;
294: }
295:
296: public Color getBackground(Object element) {
297: return null;
298: }
299:
300: }
301: }
|