001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc;
005:
006: import org.apache.xmlbeans.XmlObject;
007: import org.w3c.dom.Node;
008: import org.w3c.dom.NodeList;
009:
010: import com.tc.admin.common.XObjectTableModel;
011: import com.tc.Rule;
012: import com.terracottatech.config.ClassExpression;
013: import com.terracottatech.config.Include;
014: import com.terracottatech.config.InstrumentedClasses;
015:
016: import java.util.ArrayList;
017:
018: public class RuleModel extends XObjectTableModel {
019: private InstrumentedClasses m_instrumentedClasses;
020:
021: private static final String[] INCLUDE_FIELDS = { "Type",
022: "Expression", "Details" };
023:
024: private static final String[] INCLUDE_HEADERS = { "Rule",
025: "Expression", "Details" };
026:
027: public RuleModel() {
028: super (RuleHolder.class, INCLUDE_FIELDS, INCLUDE_HEADERS);
029: }
030:
031: public void setInstrumentedClasses(
032: InstrumentedClasses instrumentedClasses) {
033: clear();
034: if ((m_instrumentedClasses = instrumentedClasses) != null) {
035: setRules(m_instrumentedClasses.selectPath("*"));
036: }
037: }
038:
039: public void setRules(XmlObject[] objects) {
040: ArrayList list = new ArrayList();
041: for (int i = 0; i < objects.length; i++) {
042: list.add(new RuleHolder(m_instrumentedClasses, Rule
043: .create(objects[i])));
044: }
045:
046: set(list.toArray(new RuleHolder[0]));
047: }
048:
049: private void updateRules() {
050: clear();
051: setRules(m_instrumentedClasses.selectPath("*"));
052: }
053:
054: public void setValueAt(Object value, int row, int col) {
055: if (col == 0) {
056: getRuleHolderAt(row).toggleRuleType();
057: updateRules();
058: return;
059: }
060:
061: super .setValueAt(value, row, col);
062: }
063:
064: public RuleHolder getRuleHolderAt(int i) {
065: return (RuleHolder) getObjectAt(i);
066: }
067:
068: public Rule getRuleAt(int i) {
069: return ((RuleHolder) getObjectAt(i)).getRule();
070: }
071:
072: public void removeRule(int index) {
073: Rule rule = getRuleAt(index);
074: Node ruleNode = rule.getXmlObject().getDomNode();
075: Node topNode = m_instrumentedClasses.getDomNode();
076:
077: topNode.removeChild(ruleNode);
078: updateRules();
079: }
080:
081: public void moveRuleUp(int index) {
082: Rule rule = getRuleAt(index);
083: Node ruleNode = rule.getXmlObject().getDomNode();
084: Node topNode = m_instrumentedClasses.getDomNode();
085: NodeList topNodeList = topNode.getChildNodes();
086: int listSize = topNodeList.getLength();
087: Node prevNode;
088:
089: for (int i = 0; i < listSize; i++) {
090: if (ruleNode == topNodeList.item(i)) {
091: while (--i >= 0) {
092: prevNode = topNodeList.item(i);
093: if (prevNode.getNodeType() == Node.ELEMENT_NODE) {
094: topNode.removeChild(ruleNode);
095: topNode.insertBefore(ruleNode, prevNode);
096: updateRules();
097: return;
098: }
099: }
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
122: .insertBefore(ruleNode,
123: nextNode);
124: updateRules();
125: return;
126: }
127: }
128: topNode.removeChild(ruleNode);
129: topNode.appendChild(ruleNode);
130: updateRules();
131: return;
132: }
133: }
134: }
135: }
136: }
137:
138: public int size() {
139: return getRowCount();
140: }
141:
142: public void addExclude(String expr) {
143: ClassExpression classExpr = m_instrumentedClasses
144: .addNewExclude();
145:
146: classExpr.setStringValue(expr);
147: add(new RuleHolder(m_instrumentedClasses, new ExcludeRule(
148: classExpr)));
149: }
150:
151: public void removeRuleAt(int i) {
152: removeRule(i);
153: }
154:
155: public void addInclude(String expr) {
156: Include include = m_instrumentedClasses.addNewInclude();
157:
158: include.setClassExpression(expr);
159: add(new RuleHolder(m_instrumentedClasses, new IncludeRule(
160: include)));
161: }
162:
163: public boolean hasEditor(Class type) {
164: return type.equals(RuleDetail.class);
165: }
166: }
|