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  * FactTreeUI.java
26  *
27  * The Viewer/Controller for displaying the Fact Hierarchy
28  *****************************************************************************/
29  
30  package zeus.ontology.facts;
31  
32  import java.util.*;
33  import java.awt.*;
34  import java.awt.event.*;
35  import javax.swing.*;
36  import javax.swing.event.*;
37  import javax.swing.tree.*;
38  import javax.swing.border.*;
39  
40  
41  import zeus.util.SystemProps;
42  import zeus.concepts.*;
43  import zeus.ontology.*;
44  import zeus.ontology.attributes.*;
45  import zeus.gui.fields.*;
46  
47  
48  public class FactTreeUI extends JTree {
49  
50    protected AttributeTablePanel     dataPanel;
51    protected FactTreeModel           model;
52    protected DefaultMutableTreeNode  clipboard;
53  
54    static final String[] ERROR_MESSAGE = {
55       /* 0 */ "You cannot add a peer node to the root fact",
56       /* 1 */ "Please select a node before\ncalling this operation",
57       /* 2 */ "You cannot delete this fact",
58       /* 3 */ "Renaming fact to a name that already\nexists in fact hierarchy",
59       /* 4 */ "You cannot rename this fact"
60    };
61  
62    public FactTreeUI(OntologyDb ontologyDb) {
63      super(new FactTreeModel(ontologyDb));
64      model = (FactTreeModel)getModel();
65      setEditable(false);
66  
67      String sep = System.getProperty("file.separator");
68      String path = SystemProps.getProperty("gif.dir") + "ontology" + sep;
69      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
70      renderer.setLeafIcon(new ImageIcon(path + "cloud.gif"));
71      setCellRenderer(renderer);
72  
73      putClientProperty( "JTree.lineStyle", "Angled" );
74    }
75  
76  
77    public FactTreeUI(OntologyDb ontologyDb, AttributeTablePanel panel) {
78      this(ontologyDb);
79  
80      dataPanel = panel;
81      setEditable(true);
82      SymSelectAction listener = new SymSelectAction();
83  
84      FactCellEditor editor = new FactCellEditor();
85      editor.addCellEditorListener(listener);
86  
87      setCellEditor(new DefaultTreeCellEditor(this,(DefaultTreeCellRenderer)
88         this.getCellRenderer(),editor));
89  
90      TreeSelectionModel selectionModel = getSelectionModel();
91      selectionModel.addTreeSelectionListener(listener);
92      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
93    }
94  
95    class FactCellEditor extends DefaultCellEditor {
96       public FactCellEditor() {
97          super(new NameField());
98       }
99       public boolean isCellEditable(EventObject event) {
100         if ( event instanceof MouseEvent ) {
101            MouseEvent e = (MouseEvent)event;
102            TreePath path = getPathForLocation(e.getX(),e.getY());
103            DefaultMutableTreeNode node =
104 	      (DefaultMutableTreeNode)path.getLastPathComponent();
105            if ( model.isEditable(node.getUserObject().toString()) )
106               return super.isCellEditable(event);
107            else {
108               // cancelCellEditing();
109               errorMsg(4);
110               return false;
111            }
112         }
113         return super.isCellEditable(event);
114      }
115   }
116 
117   class SymSelectAction implements TreeSelectionListener,
118                                    CellEditorListener {
119      public void valueChanged(TreeSelectionEvent e) {
120        dataPanel.displayAttributes(getSelectedNodeName());
121      }
122      public void editingStopped(ChangeEvent e) {
123        dataPanel.displayAttributes(getSelectedNodeName());
124      }
125      public void editingCanceled(ChangeEvent e) {
126      }
127   }
128 
129   public void refresh() { model.refresh(); }
130 
131   // -- UPDATE METHODS ----------------------------------------------
132 
133   void addPeerNode() {
134     DefaultMutableTreeNode lastItem = getSelectedNode();
135     if ( lastItem == null )  {
136        errorMsg(1);
137        return;
138     }
139     if ( lastItem.isRoot() ) {
140        errorMsg(0);
141        return;
142     }
143 
144     DefaultMutableTreeNode   parent;
145 
146     // Determine where to create the new node
147     if ( lastItem != null )
148        parent = (DefaultMutableTreeNode)lastItem.getParent();
149     else
150        parent = (DefaultMutableTreeNode)model.getRoot();
151 
152     model.addNewChild(parent);
153   }
154 
155   void addSubNode() {
156     DefaultMutableTreeNode parent = getSelectedNode();
157     if ( parent == null )  {
158        errorMsg(1);
159        return;
160     }
161     model.addNewChild(parent);
162     expandPath(getSelectionPath());
163   }
164 
165   void removeNode() {
166     DefaultMutableTreeNode lastItem = getSelectedNode();
167     if ( lastItem == null )  {
168        errorMsg(1);
169        return;
170     }
171     else if ( !model.isEditable(lastItem.getUserObject().toString()) )  {
172        errorMsg(2);
173        return;
174     }
175     model.removeNode(lastItem);
176     dataPanel.clear();
177   }
178   void cutNode() {
179     DefaultMutableTreeNode lastItem = getSelectedNode();
180     if ( lastItem == null )  {
181        errorMsg(1);
182        return;
183     }
184     else if ( !model.isEditable(lastItem.getUserObject().toString()) )  {
185        errorMsg(2);
186        return;
187     }
188     clipboard = model.cutNode(lastItem);
189     dataPanel.clear();
190   }
191   void copyNode() {
192     DefaultMutableTreeNode lastItem = getSelectedNode();
193     if ( lastItem == null )  {
194        errorMsg(1);
195        return;
196     }
197     clipboard = model.copyNode(lastItem);
198   }
199   void pasteNode() {
200     DefaultMutableTreeNode lastItem = getSelectedNode();
201     if ( lastItem == null )  {
202        errorMsg(1);
203        return;
204     }
205     model.pasteNode(lastItem,clipboard);
206     expandPath(getSelectionPath());
207   }
208 
209   void errorMsg(int tag) {
210      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
211                                    "Error", JOptionPane.ERROR_MESSAGE);
212   }
213 
214   void expandRow() {
215      expandRow(getLeadSelectionRow());
216   }
217   void collapseRow() {
218      collapseRow(getLeadSelectionRow());
219   }
220 
221   // -- RETURN VIEW'S PROPERTIES METHODS ----------------------------
222 
223   DefaultMutableTreeNode getSelectedNode() {
224     TreePath path = getSelectionPath();
225     if (path != null)
226        return (DefaultMutableTreeNode)path.getLastPathComponent();
227     return null;
228   }
229 
230   public String getSelectedNodeName() {
231     String name = null;
232     DefaultMutableTreeNode node = getSelectedNode();
233     if (node != null)
234        name = node.getUserObject().toString();
235     return name;
236   }
237 
238   public String[] getSelectedNodeNames() {
239     TreePath[] paths = getSelectionPaths();
240     if (paths == null || paths.length == 0 ) return new String[0];
241 
242     String[] names = new String[paths.length];
243     DefaultMutableTreeNode node;
244     for(int i = 0; i < names.length; i++ ) {
245        node = (DefaultMutableTreeNode)paths[i].getLastPathComponent();
246        names[i] = node.getUserObject().toString();
247     }
248     return names;
249   }
250 
251 }