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  * TypeDialog.java
26  *
27  * A pop-up dialog that prompts selection of a fact
28  *****************************************************************************/
29  
30  package zeus.ontology;
31  
32  import javax.swing.*;
33  import javax.swing.border.*;
34  import javax.swing.tree.*;
35  import java.awt.*;
36  import java.awt.event.*;
37  
38  import zeus.util.SystemProps;
39  import zeus.concepts.*;
40  import zeus.ontology.facts.*;
41  
42  public class TypeDialog extends JDialog implements ActionListener {
43    protected JTree          tree;
44    protected TypeTreeModel  model;
45    protected JButton        okBtn, cancelBtn;
46    protected TypeSelector   caller = null;
47  
48    public TypeDialog(Frame parent, OntologyDb ontologyDb, int mode) {
49      super(parent,"Select Type");
50  
51      JPanel pane = (JPanel)getContentPane();
52      pane.setBorder(new EmptyBorder(10,10,10,10));
53      pane.setBackground(Color.lightGray);
54      pane.setLayout(new BorderLayout());
55  
56      model = new TypeTreeModel(ontologyDb,mode);
57      tree = new JTree(model);
58      tree.setEditable(false);
59      tree.setRootVisible(false);
60  
61      String sep = System.getProperty("file.separator");
62      String path = SystemProps.getProperty("gif.dir") + "ontology" + sep;
63      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
64      renderer.setLeafIcon(new ImageIcon(path + "cloud.gif"));
65      tree.setCellRenderer(renderer);
66  
67      tree.putClientProperty( "JTree.lineStyle", "Angled" );
68  
69      TreeSelectionModel selectionModel = tree.getSelectionModel();
70      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
71  
72      JScrollPane scrollpane = new JScrollPane();
73      scrollpane.setPreferredSize(new Dimension(200,150));
74      scrollpane.getViewport().add(tree);
75      pane.add(scrollpane,BorderLayout.CENTER);
76  
77      JPanel controlpane = new JPanel();
78      controlpane.setLayout(new GridLayout(1,2,10,10));
79      pane.add(controlpane,BorderLayout.SOUTH);
80  
81      okBtn = new JButton("OK");
82      okBtn.addActionListener(this);
83      cancelBtn = new JButton("Cancel");
84      cancelBtn.addActionListener(this);
85      controlpane.add(okBtn);
86      controlpane.add(cancelBtn);
87  
88      setModal(true);
89      setVisible(false);
90    }
91  
92    public void actionPerformed(ActionEvent evt) {
93      if ( evt.getSource() == okBtn ) {
94         setVisible(false);
95         String name = getSelectedNodeName();
96         if ( name != null && !name.equals(TypeTreeModel.BASIC_TYPES) &&
97              !name.equals(TypeTreeModel.RESTRICTIONS) &&
98              !name.equals(TypeTreeModel.FACTS) )
99            caller.typeSelected(name);
100     }
101     else if ( evt.getSource() == cancelBtn ) {
102        setVisible(false);
103     }
104   }
105 
106   public void display(TypeSelector caller) {
107     this.caller = caller;
108     model.refresh();
109     pack();
110     setVisible(true);
111   }
112 
113   protected String getSelectedNodeName() {
114     TreePath path = tree.getSelectionPath();
115     if ( path != null ) {
116        DefaultMutableTreeNode node =
117           (DefaultMutableTreeNode)path.getLastPathComponent();
118        if ( node == null ) return null;
119        return node.getUserObject().toString();
120     }
121     return null;
122   }
123 }