001: package org.drools.eclipse.debug;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.drools.eclipse.DroolsEclipsePlugin;
010: import org.eclipse.core.runtime.CoreException;
011: import org.eclipse.debug.core.DebugException;
012: import org.eclipse.debug.core.DebugPlugin;
013: import org.eclipse.debug.core.ILogicalStructureType;
014: import org.eclipse.debug.core.model.IStackFrame;
015: import org.eclipse.debug.core.model.IValue;
016: import org.eclipse.debug.core.model.IVariable;
017: import org.eclipse.jface.viewers.ITreeContentProvider;
018: import org.eclipse.jface.viewers.Viewer;
019:
020: /**
021: * A generic Drools debug view content provider.
022: *
023: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
024: */
025: public class DroolsDebugViewContentProvider implements
026: ITreeContentProvider {
027:
028: private Map parentCache;
029:
030: public DroolsDebugViewContentProvider() {
031: parentCache = new HashMap(10);
032: }
033:
034: public Object[] getChildren(Object parent) {
035: return null;
036: }
037:
038: public Object[] getElements(Object parent) {
039: Object[] result = getChildren(parent);
040: if (result != null && result.length == 0) {
041: return new Object[] { getEmptyString() };
042: }
043: return result;
044: }
045:
046: protected String getEmptyString() {
047: return "Empty";
048: }
049:
050: protected void cache(Object parent, Object[] children) {
051: for (int i = 0; i < children.length; i++) {
052: parentCache.put(children[i], parent);
053: }
054: }
055:
056: public Object getParent(Object item) {
057: return parentCache.get(item);
058: }
059:
060: public void dispose() {
061: parentCache = null;
062: }
063:
064: protected void clearCache() {
065: if (parentCache != null) {
066: parentCache.clear();
067: }
068: }
069:
070: public void removeCache(Object[] children) {
071: if (parentCache == null) {
072: return;
073: }
074: for (int i = 0; i < children.length; i++) {
075: parentCache.remove(children[i]);
076: }
077: }
078:
079: public boolean hasChildren(Object element) {
080: try {
081: if (element instanceof IVariable) {
082: IValue v = ((IVariable) element).getValue();
083: return v != null && v.hasVariables();
084: }
085: if (element instanceof IValue) {
086: return ((IValue) element).hasVariables();
087: }
088: if (element instanceof IStackFrame) {
089: return ((IStackFrame) element).hasVariables();
090: }
091: } catch (DebugException e) {
092: DroolsEclipsePlugin.log(e);
093: return false;
094: }
095: return false;
096: }
097:
098: public void inputChanged(Viewer viewer, Object oldInput,
099: Object newInput) {
100: clearCache();
101: }
102:
103: public List getCachedDecendants(Object parent) {
104: Iterator children = parentCache.keySet().iterator();
105: List cachedChildren = new ArrayList(10);
106: while (children.hasNext()) {
107: Object child = children.next();
108: if (isCachedDecendant(child, parent)) {
109: cachedChildren.add(child);
110: }
111: }
112: return cachedChildren;
113: }
114:
115: protected boolean isCachedDecendant(Object child, Object parent) {
116: Object p = getParent(child);
117: while (p != null) {
118: if (p.equals(parent)) {
119: return true;
120: }
121: p = getParent(p);
122: }
123: return false;
124: }
125:
126: protected IValue getLogicalValue(IValue value,
127: List previousStructureIds) {
128: ILogicalStructureType[] types = DebugPlugin
129: .getLogicalStructureTypes(value);
130: if (types.length > 0) {
131: ILogicalStructureType type = DebugPlugin
132: .getDefaultStructureType(types);
133: if (type != null
134: && !previousStructureIds.contains(type.getId())) {
135: try {
136: value = type.getLogicalStructure(value);
137: previousStructureIds.add(type.getId());
138: return getLogicalValue(value, previousStructureIds);
139: } catch (CoreException e) {
140: // unable to display logical structure
141: }
142: }
143: }
144: return value;
145: }
146: }
|