View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * AttributeTreeModel.java
26  *
27  * The underlying model for the Fact Hierarchy tree
28  *****************************************************************************/
29  
30  package zeus.generator.util;
31  
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.event.*;
35  import javax.swing.tree.*;
36  import zeus.concepts.*;
37  import zeus.util.HSet;
38  
39  
40  public class AttributeTreeModel extends DefaultTreeModel
41                                  implements ChangeListener {
42  
43    static final String PRECONDITIONS = "PRECONDITIONS";
44    static final String EFFECTS       = "EFFECTS";
45    static final String DEFAULT_ROOT  = "#Root";
46  
47    static final String[] INVISIBLE_ITEMS = {
48       DEFAULT_ROOT, PRECONDITIONS, EFFECTS
49    };
50  
51    protected AttributeModel model = null;
52    protected BasicFactModel preconditionsModel = null;
53    protected BasicFactModel postconditionsModel = null;
54    protected BasicFactModel singletonModel = null;
55    protected boolean        changed;
56  
57    public AttributeTreeModel(AttributeModel model) {
58      this();
59      this.model = model;
60    }
61    public AttributeTreeModel() {
62      super(new DefaultMutableTreeNode(DEFAULT_ROOT));
63      changed = true;
64    }
65  
66    public void setFactModels(BasicFactModel preconditionsModel,
67                              BasicFactModel postconditionsModel) {
68  
69      if ( this.preconditionsModel != null ) {
70         this.preconditionsModel.removeChangeListener(this);
71         this.postconditionsModel.removeChangeListener(this);
72      }
73  
74      this.preconditionsModel = preconditionsModel;
75      this.postconditionsModel = postconditionsModel;
76  
77      preconditionsModel.addChangeListener(this);
78      postconditionsModel.addChangeListener(this);
79  
80      if ( singletonModel != null ) {
81         singletonModel.removeChangeListener(this);
82         singletonModel = null;
83      }
84      changed = true;
85    }
86  
87    public void setFactModel(BasicFactModel singletonModel) {
88      if ( this.preconditionsModel != null ) {
89         this.preconditionsModel.removeChangeListener(this);
90         this.postconditionsModel.removeChangeListener(this);
91         this.preconditionsModel = null;
92         this.postconditionsModel = null;
93      }
94  
95      if ( this.singletonModel != null )
96         this.singletonModel.removeChangeListener(this);
97  
98      this.singletonModel = singletonModel;
99      this.singletonModel.addChangeListener(this);
100     changed = true;
101   }
102 
103   void refresh() {
104     if ( !changed ) return;
105 
106     DefaultMutableTreeNode base, prec, post, xnode;
107     Fact[] items;
108 
109     root = new DefaultMutableTreeNode(DEFAULT_ROOT);
110     base = (DefaultMutableTreeNode)root;
111 
112     zeus.util.TreeNode node;
113     if ( preconditionsModel != null ) {
114        String this_id = ( model != null ) ? model.getData().getId() : "";
115        prec = new DefaultMutableTreeNode(PRECONDITIONS);
116        post = new DefaultMutableTreeNode(EFFECTS);
117        base.add(prec);
118        base.add(post);
119 
120        items = preconditionsModel.getData();
121        for(int i = 0; i < items.length; i++ ) {
122           node = items[i].createAttributeTree(items[i].getId().equals(this_id));
123           xnode = new DefaultMutableTreeNode(node);
124           createTree(xnode,node);
125           prec.add(xnode);
126        }
127 
128        items = postconditionsModel.getData();
129        for(int i = 0; i < items.length; i++ ) {
130           node = items[i].createAttributeTree(items[i].getId().equals(this_id));
131           xnode = new DefaultMutableTreeNode(node);
132           createTree(xnode,node);
133           post.add(xnode);
134        }
135     }
136     else if ( singletonModel != null ) {
137        String this_id = ( model != null ) ? model.getData().getId() : "";
138        items = singletonModel.getData();
139        for(int i = 0; i < items.length; i++ ) {
140           node = items[i].createAttributeTree(items[i].getId().equals(this_id));
141           xnode = new DefaultMutableTreeNode(node);
142           createTree(xnode,node);
143           base.add(xnode);
144        }
145     }
146     else if ( model != null ) {
147        node = model.getData().createAttributeTree(true);
148        xnode = new DefaultMutableTreeNode(node);
149        createTree(xnode,node);
150        base.add(xnode);
151     }
152 
153     changed = false;
154     reload();
155   }
156 
157   protected void createTree(DefaultMutableTreeNode m_node,
158                             zeus.util.TreeNode a_node) {
159 
160     // takes contents of model and inserts them into the tree
161 
162     Vector children = a_node.getChildren();
163     zeus.util.TreeNode b_node;
164     DefaultMutableTreeNode n_node;
165 
166     for(int i = 0; i < children.size(); i++ ) {
167        b_node = (zeus.util.TreeNode)children.elementAt(i);
168        n_node = new DefaultMutableTreeNode(b_node);
169        m_node.add(n_node);
170        createTree(n_node,b_node);
171     }
172   }
173 
174   public void stateChanged(ChangeEvent e) {
175      changed = true;
176   }
177 }