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  * TaskLinkBaseTreePanel.java
26  *
27  *
28  *****************************************************************************/
29  
30  package zeus.generator.task;
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  import javax.swing.event.*;
38  
39  import zeus.util.SystemProps;
40  import zeus.util.Misc;
41  import zeus.generator.event.*;
42  
43  public class TaskLinkBaseTreePanel extends JPanel {
44    protected EventListenerList selectionListeners = new EventListenerList();
45  
46    protected JTree                 tree;
47    protected TaskLinkBaseTreeModel model;
48    protected JTextField            field;
49  
50    public TaskLinkBaseTreePanel(TaskLinkBaseTreeModel model) {
51      this.model = model;
52  
53      setBorder(new EmptyBorder(10,10,10,10));
54      setBackground(Color.lightGray);
55      setLayout(new BorderLayout());
56  
57      tree = new JTree(model);
58      tree.setEditable(false);
59      tree.setRootVisible(true);
60  
61      String sep = System.getProperty("file.separator");
62      String path = SystemProps.getProperty("gif.dir") + "generator" + 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.addTreeSelectionListener(new SymSelectAction());
71      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
72  
73      JScrollPane scrollpane = new JScrollPane();
74      scrollpane.setPreferredSize(new Dimension(200,200));
75      scrollpane.getViewport().add(tree);
76      add(scrollpane,BorderLayout.CENTER);
77  
78      field = new JTextField(20);
79      CompoundBorder cbr = new CompoundBorder(new EtchedBorder(),
80        new EmptyBorder(5,5,5,5));
81      field.setBorder(cbr);
82      field.setEditable(false);
83  
84      add(field,BorderLayout.SOUTH);
85    }
86  
87    class SymSelectAction implements TreeSelectionListener {
88       public void valueChanged(TreeSelectionEvent e) {
89         String type = getSelectedNodeType();
90         String node = model.getNodeName();
91         if ( type != null ) {
92            fireRootSelectionAction(node,type);
93            String name = getSelectedNodeName();
94            if ( name != null ) {
95               String group = getSelectedNodeGroup();
96               field.setText(node + " " + group + " " + name);
97               String fact = model.getFactType(name);
98               String factId = model.getFactId(name);
99               fireNodeSelectionAction(node,group,type,fact,factId);
100           }
101           else
102              field.setText(null);
103        }
104      }
105   }
106 
107   protected String getSelectedNodeName() {
108      String name = null;
109      int length;
110      TreePath path = tree.getSelectionPath();
111      if ( path != null && (length = path.getPathCount()) != 0 ) {
112 	for(int i = 1; i < length; i++ ) {
113 	   name = path.getPathComponent(i).toString();
114            if ( !Misc.member(name,TaskLinkBaseTreeModel.INVISIBLE_ITEMS) ) {
115 	      // group found
116 	      return (i < length-1)
117 	             ? path.getPathComponent(length-1).toString() : null;
118            }
119         }
120         return null;
121      }
122      return null;
123   }
124   protected String getSelectedNodeType() {
125      String name = null;
126      int length;
127      TreePath path = tree.getSelectionPath();
128      if ( path != null && (length = path.getPathCount()) != 0 ) {
129 	for(int i = 1; i < length; i++ ) {
130 	   name = path.getPathComponent(i).toString();
131            if ( name.equals(TaskLinkBaseTreeModel.PRECONDITIONS) ||
132                 name.equals(TaskLinkBaseTreeModel.EFFECTS) )
133               return name;
134         }
135         return null;
136      }
137      return null;
138   }
139   protected String getSelectedNodeGroup() {
140      String name = null;
141      int length;
142      TreePath path = tree.getSelectionPath();
143      if ( path != null && (length = path.getPathCount()) != 0 ) {
144 	for(int i = 1; i < length; i++ ) {
145 	   name = path.getPathComponent(i).toString();
146            if ( !Misc.member(name,TaskLinkBaseTreeModel.INVISIBLE_ITEMS) )
147               return name;
148         }
149         return null;
150      }
151      return null;
152   }
153 
154   public void addLinkNodeSelectionListener(LinkNodeSelectionListener x) {
155      selectionListeners.add(LinkNodeSelectionListener.class, x);
156   }
157   public void removeLinkNodeSelectionListener(LinkNodeSelectionListener x) {
158      selectionListeners.remove(LinkNodeSelectionListener.class, x);
159   }
160   public void addLinkRootSelectionListener(LinkRootSelectionListener x) {
161      selectionListeners.add(LinkRootSelectionListener.class, x);
162   }
163   public void removeLinkRootSelectionListener(LinkRootSelectionListener x) {
164      selectionListeners.remove(LinkRootSelectionListener.class, x);
165   }
166 
167   protected void fireNodeSelectionAction(String node, String group,
168      String type, String fact, String factId) {
169 
170      LinkNodeSelectionEvent evt;
171      evt = new LinkNodeSelectionEvent(this,node,group,type,fact,factId);
172      Object[] listeners = selectionListeners.getListenerList();
173      for(int i = listeners.length-2; i >= 0; i -= 2) {
174         if (listeners[i] == LinkNodeSelectionListener.class) {
175            ((LinkNodeSelectionListener)listeners[i+1]).linkNodeSelected(evt);
176         }
177      }
178   }
179   protected void fireRootSelectionAction(String node, String type) {
180      LinkRootSelectionEvent evt;
181      evt = new LinkRootSelectionEvent(this,node,type);
182      Object[] listeners = selectionListeners.getListenerList();
183      for(int i = listeners.length-2; i >= 0; i -= 2) {
184         if (listeners[i] == LinkRootSelectionListener.class) {
185            ((LinkRootSelectionListener)listeners[i+1]).linkRootSelected(evt);
186         }
187      }
188   }
189    
190 }