1 4 package org.terracotta.dso.editors; 5 6 import org.apache.xmlbeans.XmlObject; 7 import org.w3c.dom.Node ; 8 import org.w3c.dom.NodeList ; 9 10 import com.tc.admin.common.XObjectTableModel; 11 import com.terracottatech.config.ClassExpression; 12 import com.terracottatech.config.Include; 13 import com.terracottatech.config.InstrumentedClasses; 14 15 import java.util.ArrayList ; 16 17 public class RuleModel extends XObjectTableModel { 18 private InstrumentedClasses m_instrumentedClasses; 19 20 private static final String [] INCLUDE_FIELDS = { 21 "Type", "Expression", "Details" 22 }; 23 24 private static final String [] INCLUDE_HEADERS = { 25 "Rule", "Expression", "Details" 26 }; 27 28 public RuleModel() { 29 super(RuleHolder.class, INCLUDE_FIELDS, INCLUDE_HEADERS); 30 } 31 32 public void setInstrumentedClasses(InstrumentedClasses instrumentedClasses) { 33 clear(); 34 if((m_instrumentedClasses = instrumentedClasses) != null) { 35 setRules(m_instrumentedClasses.selectPath("*")); 36 } else { 37 fireTableDataChanged(); 38 } 39 } 40 41 public void setRules(XmlObject[] objects) { 42 ArrayList <RuleHolder> list = new ArrayList <RuleHolder>(); 43 for(int i = 0; i < objects.length; i++) { 44 list.add(new RuleHolder(m_instrumentedClasses, Rule.create(objects[i]))); 45 } 46 set(list.toArray(new RuleHolder[0])); 47 } 48 49 private void updateRules() { 50 clear(); 51 setRules(m_instrumentedClasses.selectPath("*")); 52 } 53 54 public void setValueAt(Object value, int row, int col) { 55 if(col == 0) { 56 getRuleHolderAt(row).toggleRuleType(); 57 updateRules(); 58 return; 59 } 60 61 super.setValueAt(value, row, col); 62 } 63 64 public RuleHolder getRuleHolderAt(int i) { 65 return (RuleHolder)getObjectAt(i); 66 } 67 68 public Rule getRuleAt(int i) { 69 return ((RuleHolder)getObjectAt(i)).getRule(); 70 } 71 72 public void removeRule(int index) { 73 Rule rule = getRuleAt(index); 74 Node ruleNode = rule.getXmlObject().getDomNode(); 75 Node topNode = m_instrumentedClasses.getDomNode(); 76 77 topNode.removeChild(ruleNode); 78 updateRules(); 79 } 80 81 public void moveRuleUp(int index) { 82 Rule rule = getRuleAt(index); 83 Node ruleNode = rule.getXmlObject().getDomNode(); 84 Node topNode = m_instrumentedClasses.getDomNode(); 85 NodeList topNodeList = topNode.getChildNodes(); 86 int listSize = topNodeList.getLength(); 87 Node prevNode; 88 89 for(int i = 0; i < listSize; i++) { 90 if(ruleNode == topNodeList.item(i)) { 91 while(--i >= 0) { 92 prevNode = topNodeList.item(i); 93 if(prevNode.getNodeType() == Node.ELEMENT_NODE) { 94 topNode.removeChild(ruleNode); 95 topNode.insertBefore(ruleNode, prevNode); 96 updateRules(); 97 return; 98 } 99 } 100 } 101 } 102 } 103 104 public void moveRuleDown(int index) { 105 Rule rule = getRuleAt(index); 106 Node ruleNode = rule.getXmlObject().getDomNode(); 107 Node topNode = m_instrumentedClasses.getDomNode(); 108 NodeList topNodeList = topNode.getChildNodes(); 109 int listSize = topNodeList.getLength(); 110 Node nextNode; 111 112 for(int i = 0; i < listSize; i++) { 113 if(ruleNode == topNodeList.item(i)) { 114 while(++i < listSize) { 115 nextNode = topNodeList.item(i); 116 if(nextNode.getNodeType() == Node.ELEMENT_NODE) { 117 while(++i < listSize) { 118 nextNode = topNodeList.item(i); 119 if(nextNode.getNodeType() == Node.ELEMENT_NODE) { 120 topNode.removeChild(ruleNode); 121 topNode.insertBefore(ruleNode, nextNode); 122 updateRules(); 123 return; 124 } 125 } 126 topNode.removeChild(ruleNode); 127 topNode.appendChild(ruleNode); 128 updateRules(); 129 return; 130 } 131 } 132 } 133 } 134 } 135 136 public int size() { 137 return getRowCount(); 138 } 139 140 public void addExclude(String expr) { 141 ClassExpression classExpr = m_instrumentedClasses.addNewExclude(); 142 143 classExpr.setStringValue(expr); 144 add(new RuleHolder(m_instrumentedClasses, new ExcludeRule(classExpr))); 145 } 146 147 public void removeRuleAt(int i) { 148 removeRule(i); 149 } 150 151 public void addInclude(String expr) { 152 Include include = m_instrumentedClasses.addNewInclude(); 153 154 include.setClassExpression(expr); 155 add(new RuleHolder(m_instrumentedClasses, new IncludeRule(include))); 156 } 157 158 public boolean hasEditor(Class type) { 159 return type.equals(RuleDetail.class); 160 } 161 } 162 | Popular Tags |