001: package org.drools.eclipse.debug;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.eclipse.DroolsEclipsePlugin;
007: import org.eclipse.debug.core.DebugException;
008: import org.eclipse.debug.core.ILaunch;
009: import org.eclipse.debug.core.model.IDebugTarget;
010: import org.eclipse.debug.core.model.IValue;
011: import org.eclipse.debug.core.model.IVariable;
012: import org.eclipse.jdt.debug.core.IJavaArray;
013: import org.eclipse.jdt.debug.core.IJavaModifiers;
014: import org.eclipse.jdt.debug.core.IJavaObject;
015: import org.eclipse.jdt.debug.core.IJavaType;
016: import org.eclipse.jdt.debug.core.IJavaValue;
017: import org.eclipse.jdt.debug.core.IJavaVariable;
018:
019: /**
020: * The Working Memory view content provider.
021: *
022: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
023: */
024: public class WorkingMemoryViewContentProvider extends
025: DroolsDebugViewContentProvider {
026:
027: private DroolsDebugEventHandlerView view;
028:
029: public WorkingMemoryViewContentProvider(
030: DroolsDebugEventHandlerView view) {
031: this .view = view;
032: }
033:
034: protected String getEmptyString() {
035: return "The selected working memory is empty.";
036: }
037:
038: public Object[] getChildren(Object obj) {
039: try {
040: IVariable[] variables = null;
041: if (obj != null
042: && obj instanceof IJavaObject
043: && "org.drools.reteoo.ReteooStatefulSession"
044: .equals(((IJavaObject) obj)
045: .getReferenceTypeName())) {
046: variables = getWorkingMemoryElements((IJavaObject) obj);
047: } else if (obj instanceof IVariable) {
048: if (view.isShowLogicalStructure()) {
049: IValue value = getLogicalValue(((IVariable) obj)
050: .getValue(), new ArrayList());
051: variables = value.getVariables();
052: }
053: if (variables == null) {
054: variables = ((IVariable) obj).getValue()
055: .getVariables();
056: }
057: }
058: if (variables == null) {
059: return new Object[0];
060: } else {
061: cache(obj, variables);
062: return variables;
063: }
064: } catch (DebugException e) {
065: DroolsEclipsePlugin.log(e);
066: return new Object[0];
067: }
068: }
069:
070: private IVariable[] getWorkingMemoryElements(IJavaObject stackObj)
071: throws DebugException {
072: IValue objects = DebugUtil.getValueByExpression(
073: "return iterateObjectsToList().toArray();", stackObj);
074: if (objects instanceof IJavaArray) {
075: IJavaArray array = (IJavaArray) objects;
076: List result = new ArrayList();
077:
078: IJavaValue[] vals = array.getValues();
079:
080: for (int i = 0; i < vals.length; i++) {
081: result.add(new MyJavaVariable("[" + i + "]", vals[i]));
082: }
083:
084: return (IVariable[]) result.toArray(new IVariable[0]);
085: }
086: return null;
087: }
088:
089: public class MyJavaVariable implements IJavaVariable {
090:
091: private String name;
092: private IJavaValue value;
093:
094: public MyJavaVariable(String name, IJavaValue value) {
095: this .name = name;
096: this .value = value;
097: }
098:
099: public String getSignature() throws DebugException {
100: return ((IJavaValue) getValue()).getSignature();
101: }
102:
103: public String getGenericSignature() throws DebugException {
104: return ((IJavaValue) getValue()).getGenericSignature();
105: }
106:
107: public IJavaType getJavaType() throws DebugException {
108: return ((IJavaValue) getValue()).getJavaType();
109: }
110:
111: public boolean isLocal() {
112: return false;
113: }
114:
115: public IValue getValue() {
116: return value;
117: }
118:
119: public String getName() {
120: return name;
121: }
122:
123: public String getReferenceTypeName() throws DebugException {
124: return ((IJavaValue) getValue()).getReferenceTypeName();
125: }
126:
127: public boolean hasValueChanged() {
128: return false;
129: }
130:
131: public boolean isPublic() {
132: return false;
133: }
134:
135: public boolean isPrivate() {
136: return false;
137: }
138:
139: public boolean isProtected() {
140: return false;
141: }
142:
143: public boolean isPackagePrivate() {
144: return false;
145: }
146:
147: public boolean isFinal() {
148: return false;
149: }
150:
151: public boolean isStatic() {
152: return false;
153: }
154:
155: public boolean isSynthetic() {
156: return false;
157: }
158:
159: public String getModelIdentifier() {
160: return getValue().getModelIdentifier();
161: }
162:
163: public IDebugTarget getDebugTarget() {
164: return ((IJavaValue) getValue()).getDebugTarget();
165: }
166:
167: public ILaunch getLaunch() {
168: return getValue().getLaunch();
169: }
170:
171: public void setValue(String expression) {
172: }
173:
174: public void setValue(IValue value) {
175: }
176:
177: public boolean supportsValueModification() {
178: return false;
179: }
180:
181: public boolean verifyValue(String expression) {
182: return false;
183: }
184:
185: public boolean verifyValue(IValue value) {
186: return false;
187: }
188:
189: public Object getAdapter(Class adapter) {
190: if (IJavaVariable.class.equals(adapter)
191: || IJavaModifiers.class.equals(adapter)) {
192: return this ;
193: }
194: return null;
195: }
196:
197: public boolean equals(Object obj) {
198: if (obj instanceof MyJavaVariable) {
199: MyJavaVariable var = (MyJavaVariable) obj;
200: return var.getName().equals(getName())
201: && var.getValue().equals(getValue());
202: }
203: return false;
204: }
205:
206: public int hashCode() {
207: return name.hashCode() + value.hashCode();
208: }
209: }
210:
211: }
|