/machinelearning/5.0/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/debug/DroolsDebugViewContentProvider.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 140 lines · 116 code · 18 blank · 6 comment · 24 complexity · cf4a37eef95dc24a2c576fa5eeb419f8 MD5 · raw file

  1. package org.drools.eclipse.debug;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.drools.eclipse.DroolsEclipsePlugin;
  8. import org.eclipse.core.runtime.CoreException;
  9. import org.eclipse.debug.core.DebugException;
  10. import org.eclipse.debug.core.DebugPlugin;
  11. import org.eclipse.debug.core.ILogicalStructureType;
  12. import org.eclipse.debug.core.model.IStackFrame;
  13. import org.eclipse.debug.core.model.IValue;
  14. import org.eclipse.debug.core.model.IVariable;
  15. import org.eclipse.jface.viewers.ITreeContentProvider;
  16. import org.eclipse.jface.viewers.Viewer;
  17. /**
  18. * A generic Drools debug view content provider.
  19. *
  20. * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
  21. */
  22. public class DroolsDebugViewContentProvider implements ITreeContentProvider {
  23. private Map parentCache;
  24. public DroolsDebugViewContentProvider() {
  25. parentCache = new HashMap(10);
  26. }
  27. public Object[] getChildren(Object parent) {
  28. return null;
  29. }
  30. public Object[] getElements(Object parent) {
  31. Object[] result = getChildren(parent);
  32. if (result != null && result.length == 0) {
  33. return new Object[] { getEmptyString() };
  34. }
  35. return result;
  36. }
  37. protected String getEmptyString() {
  38. return "Empty";
  39. }
  40. protected void cache(Object parent, Object[] children) {
  41. for (int i = 0; i < children.length; i++) {
  42. parentCache.put(children[i], parent);
  43. }
  44. }
  45. public Object getParent(Object item) {
  46. return parentCache.get(item);
  47. }
  48. public void dispose() {
  49. parentCache= null;
  50. }
  51. protected void clearCache() {
  52. if (parentCache != null) {
  53. parentCache.clear();
  54. }
  55. }
  56. public void removeCache(Object[] children) {
  57. if (parentCache == null) {
  58. return;
  59. }
  60. for (int i = 0; i < children.length; i++) {
  61. parentCache.remove(children[i]);
  62. }
  63. }
  64. public boolean hasChildren(Object element) {
  65. try {
  66. if (element instanceof IVariable) {
  67. IValue v = ((IVariable)element).getValue();
  68. return v != null && v.hasVariables();
  69. }
  70. if (element instanceof IValue) {
  71. return ((IValue)element).hasVariables();
  72. }
  73. if (element instanceof IStackFrame) {
  74. return ((IStackFrame)element).hasVariables();
  75. }
  76. } catch (DebugException e) {
  77. DroolsEclipsePlugin.log(e);
  78. return false;
  79. }
  80. return false;
  81. }
  82. public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  83. clearCache();
  84. }
  85. public List getCachedDecendants(Object parent) {
  86. Iterator children = parentCache.keySet().iterator();
  87. List cachedChildren = new ArrayList(10);
  88. while (children.hasNext()) {
  89. Object child = children.next();
  90. if (isCachedDecendant(child, parent)) {
  91. cachedChildren.add(child);
  92. }
  93. }
  94. return cachedChildren;
  95. }
  96. protected boolean isCachedDecendant(Object child, Object parent) {
  97. Object p = getParent(child);
  98. while (p != null) {
  99. if (p.equals(parent)) {
  100. return true;
  101. }
  102. p = getParent(p);
  103. }
  104. return false;
  105. }
  106. protected IValue getLogicalValue(IValue value, List previousStructureIds) {
  107. ILogicalStructureType[] types = DebugPlugin.getLogicalStructureTypes(value);
  108. if (types.length > 0) {
  109. ILogicalStructureType type = DebugPlugin.getDefaultStructureType(types);
  110. if (type != null && !previousStructureIds.contains(type.getId())) {
  111. try {
  112. value = type.getLogicalStructure(value);
  113. previousStructureIds.add(type.getId());
  114. return getLogicalValue(value, previousStructureIds);
  115. } catch (CoreException e) {
  116. // unable to display logical structure
  117. }
  118. }
  119. }
  120. return value;
  121. }
  122. }