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  * AttributeDialog.java
26  *
27  * A pop-up dialog that prompts selection of a fact attribute
28  *****************************************************************************/
29  
30  package zeus.generator.util;
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.concepts.*;
39  import zeus.util.SystemProps;
40  import zeus.util.Misc;
41  
42  public class AttributeDialog extends JDialog implements ActionListener {
43    protected JTree               tree;
44    protected AttributeTreeModel  model;
45    protected JButton             okBtn, cancelBtn;
46    protected AttributeSelector   caller = null;
47  
48    public AttributeDialog(Frame parent, AttributeTreeModel model) {
49      super(parent,"Select Attribute");
50      initialize(model);
51    }
52  
53    public AttributeDialog(Dialog parent, AttributeTreeModel model) {
54      super(parent,"Select Attribute");
55      initialize(model);
56    }
57  
58    protected void initialize(AttributeTreeModel model) {
59      this.model = model;
60  
61      JPanel pane = (JPanel)getContentPane();
62      pane.setBorder(new EmptyBorder(10,10,10,10));
63      pane.setBackground(Color.lightGray);
64      pane.setLayout(new BorderLayout());
65  
66      tree = new JTree(model);
67      tree.setEditable(false);
68      tree.setRootVisible(false);
69  
70      String sep = System.getProperty("file.separator");
71      String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
72      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
73      renderer.setLeafIcon(new ImageIcon(path + "cloud.gif"));
74      tree.setCellRenderer(renderer);
75  
76      tree.putClientProperty( "JTree.lineStyle", "Angled" );
77  
78      TreeSelectionModel selectionModel = tree.getSelectionModel();
79      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
80  
81      JScrollPane scrollpane = new JScrollPane();
82      scrollpane.setPreferredSize(new Dimension(200,200));
83      scrollpane.getViewport().add(tree);
84      pane.add(scrollpane,BorderLayout.CENTER);
85  
86      JPanel controlpane = new JPanel();
87      controlpane.setLayout(new GridLayout(1,2,10,10));
88      pane.add(controlpane,BorderLayout.SOUTH);
89  
90      okBtn = new JButton("OK");
91      okBtn.addActionListener(this);
92      cancelBtn = new JButton("Cancel");
93      cancelBtn.addActionListener(this);
94      controlpane.add(okBtn);
95      controlpane.add(cancelBtn);
96  
97      setModal(true);
98      setVisible(false);
99    }
100 
101   public void actionPerformed(ActionEvent evt) {
102     if ( evt.getSource() == okBtn ) {
103        setVisible(false);
104        String name = null;
105        int length;
106        TreePath path = tree.getSelectionPath();
107        if ( path != null && (length = path.getPathCount()) != 0 ) {
108           int i = 0;
109 	  do {
110 	     name = path.getPathComponent(i++).toString();
111           }
112           while( Misc.member(name,AttributeTreeModel.INVISIBLE_ITEMS) &&
113 	         i < length );
114 
115 	  if ( Misc.member(name,AttributeTreeModel.INVISIBLE_ITEMS) )
116              return;
117 
118 	  for(; i < length; i++ )
119              name += Fact.A_STR + path.getPathComponent(i);
120           caller.attributeSelected(name);
121        }
122     }
123     else if ( evt.getSource() == cancelBtn ) {
124        setVisible(false);
125     }
126   }
127 
128   public void display(AttributeSelector caller) {
129     this.caller = caller;
130     model.refresh();
131     pack();
132     setVisible(true);
133   }
134 }