KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > editors > RuleHolder


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.editors;
5
6 import org.apache.xmlbeans.XmlObject;
7 import org.w3c.dom.Node JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9
10 import com.terracottatech.config.ClassExpression;
11 import com.terracottatech.config.Include;
12 import com.terracottatech.config.InstrumentedClasses;
13
14 public class RuleHolder {
15   private InstrumentedClasses m_instrumentedClasses;
16   private Rule m_rule;
17   
18   public RuleHolder(InstrumentedClasses instrumentedClasses, Rule rule) {
19     m_instrumentedClasses = instrumentedClasses;
20     m_rule = rule;
21   }
22   
23   public Rule getRule() {
24     return m_rule;
25   }
26   
27   public void replace(Rule newRule) {
28     Node JavaDoc newRuleNode = newRule.getXmlObject().getDomNode();
29     Node JavaDoc ruleNode = m_rule.getXmlObject().getDomNode();
30     Node JavaDoc topNode = m_instrumentedClasses.getDomNode();
31     NodeList JavaDoc topNodeList = topNode.getChildNodes();
32     int listSize = topNodeList.getLength();
33
34     m_rule = newRule;
35     topNode.removeChild(newRuleNode);
36     
37     for(int i = 0; i < listSize; i++) {
38       if(ruleNode == topNodeList.item(i)) {
39         topNode.insertBefore(newRuleNode, ruleNode);
40         topNode.removeChild(ruleNode);
41         return;
42       }
43     }
44   }
45   
46   public int getType() {
47     return m_rule.getType();
48   }
49   
50   public void toggleRuleType() {
51     String JavaDoc expr = getExpression();
52     XmlObject xmlObject;
53     
54     if(m_rule.isIncludeRule()) {
55       ClassExpression ce = m_instrumentedClasses.addNewExclude();
56       ce.setStringValue(expr);
57       xmlObject = ce;
58     }
59     else {
60       Include include = m_instrumentedClasses.addNewInclude();
61       include.setClassExpression(expr);
62       xmlObject = include;
63     }
64     
65     replace(Rule.create(xmlObject));
66   }
67
68   public void setType(int type) {
69     if(type != getType()) {
70       toggleRuleType();
71     }
72   }
73   
74   public String JavaDoc getExpression() {
75     return m_rule.getExpression();
76   }
77   
78   public void setExpression(String JavaDoc expr) {
79     m_rule.setExpression(expr);
80   }
81   
82   public RuleDetail getDetails() {
83     return m_rule.getDetails();
84   }
85   
86   public void setDetails(RuleDetail details) {
87     m_rule.setDetails(details);
88   }
89 }
90
Popular Tags