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  * TaskLinkMainTreePanel.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 TaskLinkMainTreePanel extends JPanel 
44                                     implements LinkRootSelectionListener {
45  
46    protected EventListenerList selectionListeners = new EventListenerList();
47  
48    protected JTree                 tree;
49    protected TaskLinkMainTreeModel model;
50    protected JTextField            field;
51  
52  
53    public TaskLinkMainTreePanel(TaskLinkMainTreeModel model) {
54      this.model = model;
55  
56      setBorder(new EmptyBorder(10,10,10,10));
57      setBackground(Color.lightGray);
58      setLayout(new BorderLayout());
59  
60      tree = new JTree(model);
61      tree.setEditable(false);
62      tree.setRootVisible(false);
63  
64      String sep = System.getProperty("file.separator");
65      String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
66      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
67      renderer.setLeafIcon(new ImageIcon(path + "cloud.gif"));
68      tree.setCellRenderer(renderer);
69  
70      tree.putClientProperty( "JTree.lineStyle", "Angled" );
71  
72      TreeSelectionModel selectionModel = tree.getSelectionModel();
73      selectionModel.addTreeSelectionListener(new SymSelectAction());
74      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
75  
76      JScrollPane scrollpane = new JScrollPane();
77      scrollpane.setPreferredSize(new Dimension(200,200));
78      scrollpane.getViewport().add(tree);
79      add(scrollpane,BorderLayout.CENTER);
80  
81      field = new JTextField(20);
82      CompoundBorder cbr = new CompoundBorder(new EtchedBorder(),
83        new EmptyBorder(5,5,5,5));
84      field.setBorder(cbr);
85      field.setEditable(false);
86  
87      add(field,BorderLayout.SOUTH);
88    }
89  
90    class SymSelectAction implements TreeSelectionListener {
91       public void valueChanged(TreeSelectionEvent e) {
92         String name = getSelectedItem();
93         if ( name != null ) {
94            String node = getSelectedNode();
95            String group = getSelectedItemGroup();
96            field.setText(node + " " + group + " " + name);
97            String type = model.getMode();
98            String fact = model.getFactType(name);
99            String factId = model.getFactId(name);
100           fireSelectionAction(node,group,type,fact,factId);
101        }
102        else
103           field.setText(null);
104      }
105   }
106 
107   public void linkRootSelected(LinkRootSelectionEvent e) {
108      field.setText(null);
109      String mode = e.getType();
110      model.setInverseMode(mode);
111   }
112   protected String getSelectedNode() {
113      TreePath path = tree.getSelectionPath();
114      if ( path != null && path.getPathCount() == 4 )
115 	return path.getPathComponent(1).toString();
116      return null;
117   }
118   protected String getSelectedItem() {
119      TreePath path = tree.getSelectionPath();
120      if ( path != null && path.getPathCount() == 4 )
121         return path.getPathComponent(3).toString();
122      return null;
123   }
124   protected String getSelectedItemGroup() {
125      TreePath path = tree.getSelectionPath();
126      if ( path != null && path.getPathCount() == 4 )
127         return path.getPathComponent(2).toString();
128      return null;
129   }
130 
131    public void addLinkNodeSelectionListener(LinkNodeSelectionListener x) {
132       selectionListeners.add(LinkNodeSelectionListener.class, x);
133    }
134    public void removeLinkNodeSelectionListener(LinkNodeSelectionListener x) {
135       selectionListeners.remove(LinkNodeSelectionListener.class, x);
136    }
137 
138    protected void fireSelectionAction(String node, String group,
139       String type, String fact, String factId) {
140 
141       LinkNodeSelectionEvent evt;
142       evt = new LinkNodeSelectionEvent(this,node,group,type,fact,factId);
143       Object[] listeners = selectionListeners.getListenerList();
144       for(int i = listeners.length-2; i >= 0; i -= 2) {
145          if (listeners[i] == LinkNodeSelectionListener.class) {
146             ((LinkNodeSelectionListener)listeners[i+1]).linkNodeSelected(evt);
147          }
148       }
149    }
150 }