001: package org.drools.eclipse.debug;
002:
003: import java.io.ObjectInputStream;
004: import java.io.StringReader;
005: import java.util.List;
006:
007: import org.drools.eclipse.DroolsEclipsePlugin;
008: import org.eclipse.debug.core.DebugException;
009: import org.eclipse.debug.core.model.IValue;
010: import org.eclipse.debug.core.model.IVariable;
011: import org.eclipse.debug.ui.IDebugUIConstants;
012: import org.eclipse.jdt.debug.core.IJavaObject;
013: import org.eclipse.jface.action.IToolBarManager;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.Viewer;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.ui.ISelectionListener;
019: import org.eclipse.ui.IWorkbenchPart;
020:
021: import com.thoughtworks.xstream.XStream;
022:
023: /**
024: * An audit view that shows the contents of the selected logger when debugging.
025: *
026: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
027: */
028: public class RealtimeAuditView extends AuditView implements
029: ISelectionListener {
030:
031: public void dispose() {
032: getSite().getPage().removeSelectionListener(
033: IDebugUIConstants.ID_VARIABLE_VIEW, this );
034: super .dispose();
035: }
036:
037: protected Viewer createViewer(Composite parent) {
038: getSite().getPage().addSelectionListener(
039: IDebugUIConstants.ID_VARIABLE_VIEW, this );
040: return super .createViewer(parent);
041: }
042:
043: protected void setViewerInput(Object context) {
044: // if an in memory logger has been explicitly selected as variable
045: if (context instanceof IVariable) {
046: IVariable variable = (IVariable) context;
047: try {
048: IValue value = ((IVariable) context).getValue();
049: if (value != null
050: && value instanceof IJavaObject
051: && "org.drools.audit.WorkingMemoryInMemoryLogger"
052: .equals(variable.getValue()
053: .getReferenceTypeName())) {
054: setAuditEvents((IJavaObject) value);
055: }
056: } catch (Throwable t) {
057: DroolsEclipsePlugin.log(t);
058: }
059: }
060: }
061:
062: protected void becomesHidden() {
063: setViewerInput(null);
064: super .becomesHidden();
065: }
066:
067: protected void becomesVisible() {
068: super .becomesVisible();
069: ISelection selection = getSite().getPage().getSelection(
070: IDebugUIConstants.ID_VARIABLE_VIEW);
071: if (selection instanceof IStructuredSelection) {
072: setViewerInput(((IStructuredSelection) selection)
073: .getFirstElement());
074: }
075: }
076:
077: public void selectionChanged(IWorkbenchPart part,
078: ISelection selection) {
079: if (!isAvailable()) {
080: return;
081: }
082: if (selection == null) {
083: setViewerInput(null);
084: } else if (selection instanceof IStructuredSelection) {
085: setViewerInput(((IStructuredSelection) selection)
086: .getFirstElement());
087: }
088: }
089:
090: protected void createActions() {
091: }
092:
093: protected void configureToolBar(IToolBarManager tbm) {
094: }
095:
096: private void setAuditEvents(IJavaObject inMemoryLogger)
097: throws DebugException {
098: IValue eventString = DebugUtil.getValueByExpression(
099: "return getEvents();", inMemoryLogger);
100: String s = eventString.getValueString();
101: if (s != null) {
102: try {
103: XStream xstream = new XStream();
104: ObjectInputStream in = xstream
105: .createObjectInputStream(new StringReader(s));
106: getViewer().setInput(
107: createEventList((List) in.readObject()));
108: } catch (Throwable t) {
109: DroolsEclipsePlugin.log(t);
110: getViewer().setInput(null);
111: }
112: } else {
113: getViewer().setInput(null);
114: }
115: }
116: }
|