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  * TaskLinkBaseTreeModel.java
26  *
27  * The underlying model for the Fact Hierarchy tree
28  *****************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.event.*;
35  import javax.swing.tree.*;
36  
37  import zeus.concepts.*;
38  import zeus.util.HSet;
39  import zeus.util.Core;
40  import zeus.generator.event.*;
41  import zeus.generator.util.*;
42  
43  public class TaskLinkBaseTreeModel extends DefaultTreeModel
44                                  implements ChangeListener, RenameListener {
45  
46    static final String PRECONDITIONS   = "PRECONDITIONS";
47    static final String EFFECTS         = "EFFECTS";
48    static final String BOTH            = "BOTH";
49    static final String LEFT_SEPARATOR  = "[";
50    static final String RIGHT_SEPARATOR = "] ";
51  
52    static final String[] INVISIBLE_ITEMS = {
53       PRECONDITIONS, EFFECTS
54    };
55  
56    protected GroupManager   leftGroupManager = null;
57    protected GroupManager   rightGroupManager = null;
58    protected String         mode = BOTH;
59    protected String         nodeName = "--undefined--";
60  
61    public TaskLinkBaseTreeModel(GroupManager leftGroupManager,
62                                 GroupManager rightGroupManager) {
63  
64      super(new DefaultMutableTreeNode("--undefined--"));
65  
66      this.leftGroupManager = leftGroupManager;
67      this.rightGroupManager = rightGroupManager;
68  
69      leftGroupManager.addChangeListener(this);
70      rightGroupManager.addChangeListener(this);
71      leftGroupManager.addRenameListener(this);
72      rightGroupManager.addRenameListener(this);
73  
74      refresh();
75    }
76  
77    void reset(String mode, String name) {
78      this.nodeName = name;
79      setMode(mode);
80    }
81  
82    String getNodeName() {
83      return nodeName;
84    }
85  
86    void refresh(String mode) {
87      setMode(mode);
88    }
89  
90    void refresh() {
91      DefaultMutableTreeNode base, prec, post, xnode, gnode;
92      Vector items;
93      String name, group;
94      Enumeration enum;
95      Fact f1;
96      Hashtable input;
97  
98      root = new DefaultMutableTreeNode(nodeName);
99      base = (DefaultMutableTreeNode)root;
100 
101     if ( mode.equals(BOTH) || mode.equals(PRECONDITIONS) ) {
102        prec = new DefaultMutableTreeNode(PRECONDITIONS);
103        base.add(prec);
104 
105        input = leftGroupManager.getManagerData();
106        enum = input.keys();
107        while( enum.hasMoreElements() ) {
108           group = (String)enum.nextElement();
109 
110           gnode = new DefaultMutableTreeNode(group);
111           prec.add(gnode);
112 
113           items = (Vector)input.get(group);
114           for(int i = 0; i < items.size(); i++ ) {
115              f1 = (Fact)items.elementAt(i);
116              name = compoundName(f1.getType(),f1.getId());
117              xnode = new DefaultMutableTreeNode(name);
118              gnode.add(xnode);
119           }
120        }
121     }
122     if ( mode.equals(BOTH) || mode.equals(EFFECTS) ) {
123        post = new DefaultMutableTreeNode(EFFECTS);
124        base.add(post);
125 
126        input = rightGroupManager.getManagerData();
127        enum = input.keys();
128        while( enum.hasMoreElements() ) {
129           group = (String)enum.nextElement();
130 
131           gnode = new DefaultMutableTreeNode(group);
132           post.add(gnode);
133 
134           items = (Vector)input.get(group);
135           for(int i = 0; i < items.size(); i++ ) {
136              f1 = (Fact)items.elementAt(i);
137              name = compoundName(f1.getType(),f1.getId());
138              xnode = new DefaultMutableTreeNode(name);
139              gnode.add(xnode);
140           }
141        }
142     }
143 
144     reload();
145   }
146 
147   public void setMode(String mode) {
148      // if ( mode.equals(this.mode) ) return;
149      Core.ERROR(mode.equals(BOTH) || mode.equals(PRECONDITIONS) ||
150                 mode.equals(EFFECTS), 1, this);
151      this.mode = mode;
152      refresh();
153   }
154 
155   public static String getFactType(String name) {
156      StringTokenizer st = new StringTokenizer(name,LEFT_SEPARATOR+RIGHT_SEPARATOR);
157      return st.nextToken();
158   }
159   public static String getFactId(String name) {
160      StringTokenizer st = new StringTokenizer(name,LEFT_SEPARATOR+RIGHT_SEPARATOR);
161      st.nextToken(); // ignore fact type
162      return st.nextToken();
163   }
164 
165   public static String compoundName(String type, String id) {
166      return LEFT_SEPARATOR + type + RIGHT_SEPARATOR + id;
167   }
168 
169   public void stateChanged(ChangeEvent e) {
170      refresh();
171   }
172   public void nameChanged(RenameEvent e) {
173      // nodeName has changed
174      String prev = (String)e.getOriginal();
175      String curr = (String)e.getCurrent();
176      if ( prev.equals(curr) ) return;
177      
178      if ( nodeName.equals(prev) ) {
179         nodeName = curr;
180         DefaultMutableTreeNode xnode = (DefaultMutableTreeNode)root;
181         xnode.setUserObject(nodeName);
182         nodeChanged(root);
183      }
184      else
185         refresh();
186   }
187 }