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   * @(#)EngineUI.java 1.03b
24   */
25  
26  
27  package zeus.agentviewer.engine;
28  
29  
30  import javax.swing.*;
31  import javax.swing.table.*;
32  import javax.swing.tree.*;
33  import javax.swing.event.*;
34  import javax.swing.border.*;
35  import java.awt.*;
36  import java.awt.event.*;
37  import java.util.*;
38  
39  import zeus.agentviewer.*;
40  import zeus.util.*;
41  import zeus.gui.graph.*;
42  import zeus.gui.help.*;
43  
44  
45  public class EngineUI extends ZeusInternalFrame
46     implements ListSelectionListener {
47  
48     private JPanel  contentPane;
49     private JTable  table;
50     private JScrollPane tablePanel;
51  
52     final int TOP_PANE_MIN_HEIGHT = 120;
53     final int TOP_PANE_MIN_WIDTH = 500;
54     final int BOTTOM_PANE_MIN_WIDTH = 50;
55     final int BOTTOM_PANE_MIN_HEIGHT = 50;
56  
57     private static int NUMBER_DISPLAYED = 0;
58     private InternalFramesPanel deskTop;
59     EngineTableModel engineBuffer;
60     private JScrollPane treeSP;
61     zeus.gui.graph.Graph graph = null;
62  
63  
64  
65  //--------------------------------------------------------------------------
66     public EngineUI(InternalFramesPanel deskTop,EngineTableModel engineBuffer) 
67     {
68          super("Coordination Engine",true,true,true,true);
69          setTitle("Coordination Engine:" + (++NUMBER_DISPLAYED));
70          String sep = System.getProperty("file.separator");
71          String gifpath = SystemProps.getProperty("gif.dir") + "agentviewer" + sep;
72          ImageIcon icon = new ImageIcon(gifpath + ViewerNames.COORDENG_IMG);
73          setFrameIcon(icon);
74          this.deskTop = deskTop;
75          this.engineBuffer = engineBuffer;
76          buildUI();
77          deskTop.addInternalFrame(this);
78          setVisible(true);
79     }
80  //--------------------------------------------------------------------------
81        class KeyLabel extends JPanel {
82             public KeyLabel(String text, Color bColor) {
83                GridBagLayout gridBagLayout = new GridBagLayout();
84                GridBagConstraints gbc = new GridBagConstraints();
85                setLayout(gridBagLayout);
86            
87                // Add the colour panel
88                JPanel colorPanel = new JPanel();
89                colorPanel.setPreferredSize(new Dimension(10,10));
90                colorPanel.setBackground(bColor);
91                gbc.gridwidth = 1;
92                gbc.anchor = GridBagConstraints.WEST;
93                gbc.fill = GridBagConstraints.NONE;
94                gbc.insets = new Insets(0,0,0,0);
95                gbc.weightx = gbc.weighty = 0;
96                gridBagLayout.setConstraints(colorPanel,gbc);
97                add(colorPanel);
98  
99                // Add the text 
100               JLabel label = new JLabel(text);
101               gbc.gridwidth = GridBagConstraints.REMAINDER;
102               gbc.anchor = GridBagConstraints.WEST;
103               gbc.fill = GridBagConstraints.HORIZONTAL;
104               gbc.insets = new Insets(0,5,0,0);
105               gbc.weightx = gbc.weighty = 1;
106               gridBagLayout.setConstraints(label,gbc);
107               add(label);
108            }
109       }
110 //--------------------------------------------------------------------------
111       private JPanel getColorPanel() {
112 
113          KeyLabel lb;
114          JPanel skeyPanel = new JPanel();
115          skeyPanel.setLayout(new GridLayout(2,3,5,2));
116          for( int i = 0; i < EngineNodeRenderer.state_string.length; i++ ) {
117             lb = new KeyLabel(EngineNodeRenderer.state_string[i],
118                               EngineNodeRenderer.color[i]) ;
119      skeyPanel.add(lb);
120          }
121    
122          JPanel keyPanel = new JPanel();
123          GridBagLayout gb = new GridBagLayout();
124          GridBagConstraints gbc = new GridBagConstraints();
125          keyPanel.setLayout(gb);
126    
127          gbc.insets = new Insets(10,5,0,0);
128          gbc.anchor = GridBagConstraints.NORTHWEST;
129          gbc.fill = GridBagConstraints.NONE;
130          gbc.gridwidth = GridBagConstraints.REMAINDER;
131          JLabel keyLb = new JLabel("Key",SwingConstants.LEFT);
132          keyLb.setFont(new Font("Courier",Font.BOLD,14));
133          keyLb.setForeground(Color.black);
134          gb.setConstraints(keyLb,gbc);
135          keyPanel.add(keyLb);
136          gbc.insets = new Insets(5,5,5,5);
137          gbc.weightx = gbc.weighty = 1;
138          gb.setConstraints(skeyPanel,gbc);
139          keyPanel.add(skeyPanel);
140    
141          return keyPanel;
142    }
143 
144 //--------------------------------------------------------------------------
145       private void buildUI() {
146           table = new JTable(engineBuffer);
147           table.setPreferredScrollableViewportSize(
148              new Dimension(TOP_PANE_MIN_WIDTH, TOP_PANE_MIN_HEIGHT));
149           tablePanel = new JScrollPane(table);
150           tablePanel.setPreferredSize(
151              new Dimension(TOP_PANE_MIN_WIDTH, TOP_PANE_MIN_HEIGHT));
152           table.getSelectionModel().addListSelectionListener(this );
153           table.getSelectionModel().setSelectionMode(
154              ListSelectionModel.SINGLE_SELECTION);
155           table.setShowGrid(false);
156 
157           treeSP = new JScrollPane(graph);
158           treeSP.setBackground(Color.white);
159 
160           treeSP.setPreferredSize(
161              new Dimension(BOTTOM_PANE_MIN_WIDTH, BOTTOM_PANE_MIN_HEIGHT));
162 
163           treeSP.setBorder(new BevelBorder(BevelBorder.LOWERED));
164 
165           JPanel centerPanel = new JPanel(new BorderLayout());
166           centerPanel.add(BorderLayout.NORTH, new NodesToolBar());
167           centerPanel.add(BorderLayout.CENTER, treeSP);
168 
169           contentPane = (JPanel) getContentPane();
170           contentPane.setLayout(new BorderLayout());
171           contentPane.add(BorderLayout.NORTH,tablePanel);
172           contentPane.add(BorderLayout.CENTER,centerPanel);
173           contentPane.add(BorderLayout.SOUTH,getColorPanel());
174           pack();
175       }
176 
177 //--------------------------------------------------------------------------
178       public void valueChanged(ListSelectionEvent e) {
179           int row;
180 
181           if (e.getSource() == table.getSelectionModel() ) {
182 
183                row = table.getSelectedRow();
184                if (row >= 0  && row < engineBuffer.getRowCount()) {
185                   if (treeSP.getViewport().getComponentCount() > 0)
186                      treeSP.getViewport().removeAll();
187                   EngineGraphModel engineGraphModel = engineBuffer.getGraph(row);
188 
189                   graph = new Graph(Graph.HORIZONTAL_PARENT_CHILD,
190                    engineGraphModel,false,false);
191                   graph.setNodeRenderer(new TreeNodeRenderer());
192                   graph.setBackground(Color.white);
193                   graph.setPreferredSize(new Dimension(2000,2000));
194                   treeSP.getViewport().add(graph);
195                   treeSP.validate();
196                }
197           }
198 
199       }
200 //--------------------------------------------------------------------------
201      void reSize() {
202         setSize(getWidth()-2,getHeight());
203         setSize(getWidth()+2,getHeight());
204      }
205 //--------------------------------------------------------------------------
206      protected class NodesToolBar extends JToolBar implements ActionListener {
207          protected HelpWindow    helpWin;
208          protected JToggleButton helpBtn;
209          protected JButton       newBtn;
210          protected JButton       selectBtn;
211          protected JButton       selectAllBtn;
212          protected JButton       hideBtn;
213          protected JButton       showBtn;
214          protected JButton       collapseBtn;
215          protected JButton       expandBtn;
216          protected JButton       deleteBtn;
217          protected JButton       cutBtn;
218          protected JButton       copyBtn;
219          protected JButton       pasteBtn;
220          protected JButton       recomputeBtn;
221          protected JButton       redrawBtn;
222 
223          public NodesToolBar() {
224            setBackground(Color.lightGray);
225            setBorder(new BevelBorder(BevelBorder.LOWERED));
226            setFloatable(false);
227 
228            String sep = System.getProperty("file.separator");
229            String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
230 
231 
232           // Draw Buttons
233           recomputeBtn = new JButton(new ImageIcon(path + "recompute.gif"));
234     recomputeBtn.setMargin(new Insets(0,0,0,0));
235           add(recomputeBtn);
236           recomputeBtn.setToolTipText("Recompute node positions");
237           recomputeBtn.addActionListener(this);
238 
239           redrawBtn = new JButton(new ImageIcon(path + "redraw.gif"));
240     redrawBtn.setMargin(new Insets(0,0,0,0));
241           add(redrawBtn);
242           redrawBtn.setToolTipText("Redraw");
243           redrawBtn.addActionListener(this);
244 
245     addSeparator();
246 
247           selectBtn = new JButton(new ImageIcon(path + "select.gif"));
248     selectBtn.setMargin(new Insets(0,0,0,0));
249           add(selectBtn);
250           selectBtn.setToolTipText("Select nodes");
251           selectBtn.addActionListener(this);
252 
253           selectAllBtn = new JButton(new ImageIcon(path + "selectAll.gif"));
254     selectAllBtn.setMargin(new Insets(0,0,0,0));
255           add(selectAllBtn);
256           selectAllBtn.setToolTipText("Select all nodes");
257           selectAllBtn.addActionListener(this);
258 
259           addSeparator();
260 
261           collapseBtn = new JButton(new ImageIcon(path + "collapse.gif"));
262     collapseBtn.setMargin(new Insets(0,0,0,0));
263           add(collapseBtn);
264           collapseBtn.setToolTipText("Collapse nodes");
265           collapseBtn.addActionListener(this);
266 
267           expandBtn = new JButton(new ImageIcon(path + "expand.gif"));
268     expandBtn.setMargin(new Insets(0,0,0,0));
269           add(expandBtn);
270           expandBtn.setToolTipText("Expand nodes");
271           expandBtn.addActionListener(this);
272 
273           addSeparator();
274 
275           hideBtn = new JButton(new ImageIcon(path + "hide.gif"));
276     hideBtn.setMargin(new Insets(0,0,0,0));
277           add(hideBtn);
278           hideBtn.setToolTipText("Hide nodes");
279           hideBtn.addActionListener(this);
280 
281           showBtn = new JButton(new ImageIcon(path + "show.gif"));
282     showBtn.setMargin(new Insets(0,0,0,0));
283           add(showBtn);
284           showBtn.setToolTipText("Show nodes");
285           showBtn.addActionListener(this);
286 
287           addSeparator();
288 
289    // Help Button
290           helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
291     helpBtn.setMargin(new Insets(0,0,0,0));
292           add(helpBtn);
293           helpBtn.setToolTipText("Help");
294           helpBtn.addActionListener(this);
295          }
296 
297          public void actionPerformed(ActionEvent e)  {
298            Object src = e.getSource();
299 
300            if (graph == null ) return;
301 
302            if ( src == recomputeBtn )
303             graph.recompute();
304            else if ( src == redrawBtn )
305             graph.redraw();
306            else if ( src == selectBtn )
307             graph.select();
308            else if ( src == selectAllBtn )
309             graph.selectAll();
310            else if ( src == collapseBtn )
311             graph.collapse();
312            else if ( src == expandBtn )
313             graph.expand();
314            else if ( src == hideBtn )
315             graph.hide();
316            else if ( src == showBtn )
317             graph.show();
318            else if ( src == helpBtn ) {
319             if ( helpBtn.isSelected() ) {
320               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
321                  getLocation(), "generator", "Coordination Engine");
322               helpWin.setSource(helpBtn);
323             }
324             else
325               helpWin.dispose();
326            }
327          }
328      }
329 
330 //--------------------------------------------------------------------------
331 }
332 
333 
334 
335 //--------------------------------------------------------------------------
336 class TreeNodeRenderer implements GraphNodeRenderer {
337      public Component getNodeRendererComponent(Graph g, GraphNode node) {
338         zeus.actors.rtn.Node obj = (zeus.actors.rtn.Node)node.getUserObject();
339         return new EngineNodeRenderer(obj);
340      }
341   }
342 
343 
344 class EngineNodeRenderer extends JLabel  {
345 
346       zeus.actors.rtn.Node node;
347       public static final Color[] color = {
348        Color.blue,
349        Color.yellow,
350        Color.orange,
351        Color.green,
352        Color.cyan,
353        Color.red
354      };
355 
356      public static final String[] state_string = {
357       "NOT_READY",
358       "READY",
359       "WAITING",
360       "RUNNING",
361       "DONE",
362       "FAILED"
363    };
364 
365 //--------------------------------------------------------------------------
366    public EngineNodeRenderer(zeus.actors.rtn.Node node) {
367         super(node.getDescription(), JLabel.CENTER);
368         // LL 040500 1.03b
369         if ( node.getDesc() != null ) setToolTipText(node.getDesc());
370         this.node =  node;
371         repaint();
372    }
373 
374  //-------------------------------------------------------------------------
375    public void paint(Graphics g) {
376          String text = node.getDescription();
377          Color bColor = color[node.getState()];
378 
379    if (bColor == Color.black)
380             setForeground(Color.white);
381          else
382           setForeground(Color.black);
383          setText(text);
384          g.setColor(bColor);
385          g.fillRect(0,0,getWidth(),getHeight()-1);
386          super.paint(g);
387    }
388 }