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  * FactTreeModel.java
26  *
27  * The underlying model for the Fact Hierarchy tree
28  *****************************************************************************/
29  
30  package zeus.ontology.facts;
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.ontology.*;
38  
39  
40  public class FactTreeModel extends DefaultTreeModel
41                             implements ChangeListener {
42  
43    static final String[] ERROR_MESSAGE = {
44       /* 0 */ "Rename failed"
45    };
46  
47    protected OntologyDb model;
48  
49    public FactTreeModel(OntologyDb model) {
50      super(new DefaultMutableTreeNode(model.getRoot()));
51      this.model = model;
52      createTree((DefaultMutableTreeNode)getRoot(),model.getRoot());
53      model.addChangeListener(this);
54    }
55  
56    protected void createTree(DefaultMutableTreeNode m_node,
57                              zeus.util.TreeNode a_node) {
58  
59      // takes contents of model and inserts them into the tree
60  
61      Vector children = a_node.getChildren();
62      zeus.util.TreeNode b_node;
63      DefaultMutableTreeNode n_node;
64  
65      for(int i = 0; i < children.size(); i++ ) {
66         b_node = (zeus.util.TreeNode)children.elementAt(i);
67         n_node = new DefaultMutableTreeNode(b_node);
68         m_node.add(n_node);
69         createTree(n_node,b_node);
70      }
71    }
72  
73    public void valueForPathChanged(TreePath path, Object aValue) {
74      String newname = (String)aValue;
75      DefaultMutableTreeNode node = (DefaultMutableTreeNode)
76        path.getLastPathComponent();
77      String name = node.getUserObject().toString();
78      if ( name.equals(newname) ) return;
79  
80      Object object;
81      if ( (object = model.renameFact(name,newname)) != null ) {
82         node.setUserObject(object);
83         nodeChanged(node);
84      }
85      else
86         errorMsg(0);
87    }
88  
89    void refresh() {
90       root = new DefaultMutableTreeNode(model.getRoot());
91       createTree((DefaultMutableTreeNode)root,model.getRoot());
92       reload();
93    }
94  
95    void errorMsg(int tag) {
96       JOptionPane.showMessageDialog(null,ERROR_MESSAGE[tag],
97                                     "Error", JOptionPane.ERROR_MESSAGE);
98    }
99  
100   // -- MANIPULATE INTERNAL STATE METHODS ---------------------------
101 
102   public boolean isEditable(String name) {
103      return model.isFactEditable(name);
104   }
105 
106   void addNewChild(DefaultMutableTreeNode parent) {
107      DefaultMutableTreeNode node;
108      zeus.util.TreeNode p_node = (zeus.util.TreeNode)parent.getUserObject();
109      zeus.util.TreeNode c_node = model.addChildFact(p_node);
110      node = new DefaultMutableTreeNode(c_node);
111      insertNodeInto(node,parent,parent.getChildCount());
112   }
113 
114   Object renameFact(String old_name, String new_name) {
115      return model.renameFact(old_name,new_name);
116   }
117 
118   void removeNode(DefaultMutableTreeNode node) {
119     if ( node != null && node != (DefaultMutableTreeNode)getRoot() ) {
120        model.removeFact((zeus.util.TreeNode)node.getUserObject());
121        DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent();
122        removeNodeFromParent(node);
123        nodeStructureChanged(parent);
124     }
125   }
126 
127   DefaultMutableTreeNode cutNode(DefaultMutableTreeNode node) {
128     if ( node != null && node != (DefaultMutableTreeNode)getRoot() ) {
129        model.removeFact((zeus.util.TreeNode)node.getUserObject());
130        DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent();
131        removeNodeFromParent(node);
132        nodeStructureChanged(parent);
133        return node;
134     }
135     return null;
136   }
137   DefaultMutableTreeNode copyNode(DefaultMutableTreeNode node) {
138      if ( node != null ) {
139         zeus.util.TreeNode o_node = (zeus.util.TreeNode)node.getUserObject();
140         zeus.util.TreeNode c_node = model.copyFactTree(o_node);
141         DefaultMutableTreeNode m_node = new DefaultMutableTreeNode(c_node);
142         createTree(m_node,c_node);
143         return m_node;
144      }
145      return null;
146   }
147   void pasteNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode node) {
148      if ( node != null ) {
149         zeus.util.TreeNode p_node = (zeus.util.TreeNode)parent.getUserObject();
150         zeus.util.TreeNode c_node = (zeus.util.TreeNode)node.getUserObject();
151         zeus.util.TreeNode x_node = model.pasteFactTree(p_node,c_node);
152         DefaultMutableTreeNode m_node = new DefaultMutableTreeNode(x_node);
153         parent.add(m_node);
154         createTree(m_node,x_node);
155         nodeStructureChanged(parent);
156      }
157      return;
158   }
159 
160   // -- CHANGE LISTENER METHODS -------------------------------------
161 
162   public void stateChanged(ChangeEvent e) {
163     OntologyDbChangeEvent evt = (OntologyDbChangeEvent)e;
164     if ( evt.getEventType() == OntologyDb.RELOAD )
165        refresh();
166   }
167 
168 }