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  * TaskLinkMainTreeModel.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  
42  public class TaskLinkMainTreeModel extends DefaultTreeModel
43                                     implements RenameListener {
44  
45    static final String DEFAULT_ROOT  = "#Root#";
46    static final String PRECONDITIONS = TaskLinkBaseTreeModel.PRECONDITIONS;
47    static final String EFFECTS       = TaskLinkBaseTreeModel.EFFECTS;
48  
49    protected TaskNode[] nodes = new TaskNode[0];
50    protected String     ignoreNode;
51    protected String     mode = PRECONDITIONS;
52  
53    public TaskLinkMainTreeModel() {
54      super(new DefaultMutableTreeNode(DEFAULT_ROOT));
55    }
56  
57    public TaskLinkMainTreeModel(TaskNode[] nodes, String ignoreNode) {
58      this();
59      reset(nodes,ignoreNode);
60    }
61  
62    void reset(TaskNode[] nodes, String ignoreNode) {
63      this.nodes = nodes;
64      this.ignoreNode = ignoreNode;
65      mode = PRECONDITIONS;
66      refresh();
67    }
68  
69    void refresh(String mode) {
70      setMode(mode);
71    }
72  
73    void refresh() {
74      DefaultMutableTreeNode base, xnode, gnode, leaf;
75      Vector items;
76      Hashtable input;
77      String name, group;
78      Enumeration enum;
79      Fact f1;
80  
81      root = new DefaultMutableTreeNode(DEFAULT_ROOT);
82      base = (DefaultMutableTreeNode)root;
83  
84      for(int k = 0; k < nodes.length; k++ ) {
85         if ( !((nodes[k].getName()).equals(ignoreNode)) ) {
86            xnode = new DefaultMutableTreeNode(nodes[k].getName());
87            base.add(xnode);
88  
89            input = null;
90            if ( mode.equals(PRECONDITIONS) )
91               input = nodes[k].getAllPreconditions();
92            else if ( mode.equals(EFFECTS) )
93               input = nodes[k].getAllPostconditions();
94            else
95               Core.ERROR(null,2,this);
96  
97            enum = input.keys();
98            while( enum.hasMoreElements() ) {
99               group = (String)enum.nextElement();
100 
101              gnode = new DefaultMutableTreeNode(group);
102              xnode.add(gnode);
103 
104              items = (Vector)input.get(group);
105              for(int i = 0; i < items.size(); i++ ) {
106                 f1 = (Fact)items.elementAt(i);
107                 name = TaskLinkBaseTreeModel.compoundName(f1.getType(),f1.getId());
108                 leaf = new DefaultMutableTreeNode(name);
109                 gnode.add(leaf);
110              }
111           }
112        }
113     }
114     reload();
115   }
116 
117   public void setMode(String mode) {
118      if ( mode.equals(this.mode) ) return;
119      Core.ERROR(mode.equals(PRECONDITIONS) || mode.equals(EFFECTS), 1, this);
120      this.mode = mode;
121      refresh();
122   }
123 
124   public void setInverseMode(String inverse) {
125      String new_mode = null;
126 
127      if ( inverse.equals(PRECONDITIONS) )
128         new_mode = EFFECTS;
129      else if ( inverse.equals(EFFECTS) )
130         new_mode = PRECONDITIONS;
131      else
132         Core.ERROR(null, 1, this);
133 
134      if ( mode != null && new_mode.equals(mode) ) return;
135      this.mode = new_mode;
136      refresh();
137   }
138 
139   public String getMode() { return mode; }
140 
141   public static String getFactType(String name) {
142      return TaskLinkBaseTreeModel.getFactType(name);
143   }
144 
145   public static String getFactId(String name) {
146      return TaskLinkBaseTreeModel.getFactId(name);
147   }
148   public void nameChanged(RenameEvent e) {
149      // ignoreNode has changed
150      String prev = (String)e.getOriginal();
151      String curr = (String)e.getCurrent();
152      if ( prev.equals(curr) ) return;
153      if ( ignoreNode == null || ignoreNode.equals(prev) ) ignoreNode = curr;
154   }
155 }