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  * ReportGraph.java
26  *
27  * Panel through which summary task nodes are entered
28  ***************************************************************************/
29  
30  package zeus.visualiser.report;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  import javax.swing.*;
36  import javax.swing.border.*;
37  import javax.swing.text.*;
38  import javax.swing.event.*;
39  
40  import zeus.util.*;
41  import zeus.concepts.*;
42  import zeus.actors.PlanRecord;
43  import zeus.gui.graph.*;
44  
45  public class ReportGraph extends JPanel {
46    protected Graph       graph;
47    protected ReportModel model;
48  
49    public ReportGraph(ReportModel model)  {
50      this.model = model;
51      graph = new Graph(Graph.HORIZONTAL_CHILD_PARENT,model,true,false);
52      graph.setNodeRenderer(new ReportNodeRenderer());
53      graph.setNodeEditor(new ReportNodeEditor());
54  
55      GridBagLayout gridBagLayout = new GridBagLayout();
56      GridBagConstraints gbc = new GridBagConstraints();
57      setLayout(gridBagLayout);
58      setBackground(Color.lightGray);
59      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
60  
61      // Add the panel that will contain the task's nodes
62      JPanel dataPanel = new JPanel();
63      gbc.gridwidth = GridBagConstraints.REMAINDER;
64      gbc.anchor = GridBagConstraints.NORTHWEST;
65      gbc.fill = GridBagConstraints.BOTH;
66      gbc.weightx = gbc.weighty = 1;
67      gbc.insets = new Insets(8,8,8,8);
68      gridBagLayout.setConstraints(dataPanel,gbc);
69      add(dataPanel);
70  
71      // Data panel info
72      gridBagLayout = new GridBagLayout();
73      dataPanel.setLayout(gridBagLayout);
74      dataPanel.setBackground(Color.lightGray);
75  
76      JScrollPane scrollPane = new JScrollPane(graph);
77      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
78      scrollPane.setPreferredSize(new Dimension(600,600));
79      graph.setPreferredSize(new Dimension(2000,2000));
80      graph.setBackground(new Color(245,230,140));
81  
82      TitledBorder border = (BorderFactory.createTitledBorder("Task Graph"));
83      border.setTitlePosition(TitledBorder.TOP);
84      border.setTitleJustification(TitledBorder.RIGHT);
85      border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
86      border.setTitleColor(Color.blue);
87      dataPanel.setBorder(border);
88  
89      JToolBar toolbar = new NodesToolBar();
90  
91      gbc = new GridBagConstraints();
92      gbc.gridwidth = GridBagConstraints.REMAINDER;
93      gbc.anchor = GridBagConstraints.WEST;
94      gbc.fill = GridBagConstraints.NONE;
95      gbc.weightx = gbc.weighty = 0;
96      gbc.insets = new Insets(0,8,0,0);
97      gridBagLayout.setConstraints(toolbar,gbc);
98      dataPanel.add(toolbar);
99  
100     gbc.gridwidth = GridBagConstraints.REMAINDER;
101     gbc.anchor = GridBagConstraints.WEST;
102     gbc.fill = GridBagConstraints.BOTH;
103     gbc.weightx = gbc.weighty = 1;
104     gbc.insets = new Insets(8,8,8,8);
105     gridBagLayout.setConstraints(scrollPane,gbc);
106     dataPanel.add(scrollPane);
107   }
108 
109   Graph       getGraph() { return graph; }
110   ReportModel getModel() { return model; }
111 
112   protected class ReportNodeRenderer implements GraphNodeRenderer {
113      public Component getNodeRendererComponent(Graph g, GraphNode node) {
114         ReportRec obj = (ReportRec)node.getUserObject();
115         return new NodeIcon(obj);
116      }
117 
118      class NodeIcon extends JPanel {
119         public NodeIcon(ReportRec rec) {
120            Color color = PlanRecord.color[rec.getState()];
121 
122            GridBagLayout gridBagLayout = new GridBagLayout();
123            GridBagConstraints gbc = new GridBagConstraints();
124            setLayout(gridBagLayout);
125            setBackground(color);
126 
127            KeyLabel label;
128            label = new KeyLabel("Agent: " + rec.getAgent());
129            label.setBackground(color);
130            gbc.gridwidth = GridBagConstraints.REMAINDER;
131            gbc.anchor = GridBagConstraints.NORTHWEST;
132            gbc.fill = GridBagConstraints.BOTH;
133            gbc.weightx = gbc.weighty = 1;
134            gbc.insets = new Insets(2,2,0,2);
135            gridBagLayout.setConstraints(label,gbc);
136            add(label);
137 
138            label = new KeyLabel("Task: " + rec.getTask());
139            label.setBackground(color);
140            gridBagLayout.setConstraints(label,gbc);
141            add(label);
142 
143 	   label = new KeyLabel("Goal: " + rec.getGoal());
144            label.setBackground(color);
145            gbc.insets = new Insets(2,2,2,2);
146            gridBagLayout.setConstraints(label,gbc);
147            add(label);
148         }
149      }
150 
151      class KeyLabel extends JLabel{
152         public KeyLabel(String text) {
153            super(text,JLabel.LEFT);
154         }
155         public void paint(Graphics g){
156            Color c = g.getColor();
157            g.setColor(getBackground());
158            g.fillRect(0,0,getWidth(),getHeight()-1);
159            g.setColor(c);
160            super.paint(g);
161         }
162      }
163   }
164 
165   class ReportNodeEditor extends AbstractGraphNodeEditor
166                           implements ActionListener {
167 
168      protected JButton button = new JButton("Click to edit");
169      protected GraphNode node;
170      protected ReportDialog dialog = null;
171 
172      public ReportNodeEditor() {
173         button.setBackground(Color.lightGray);
174         button.setHorizontalAlignment(JButton.CENTER);
175         button.setBorderPainted(true);
176         button.addActionListener(this);
177         button.setOpaque(true);
178         button.setMinimumSize(new Dimension(120,30));
179         button.setPreferredSize(new Dimension(120,30));
180         button.setSize(120,30);
181      }
182 
183      public void actionPerformed(ActionEvent e) {
184         Object src = e.getSource();
185         if ( src == button ) {
186            // Note: create dialog (using button) before canceling editing
187            // which will remove the button from the visible component hierachy
188            ReportRec rec = (ReportRec)node.getUserObject();
189            dialog = new ReportDialog(rec);
190 	   dialog.setLocationRelativeTo(button);
191            dialog.pack();
192            dialog.setVisible(true);
193            fireEditAction(EDITING_CANCELLED,this.node,null);
194         }
195      }
196      public Component getNodeEditorComponent(Graph graph, GraphNode gnode) {
197         this.node = gnode;
198         return button;
199      }
200 
201      class ReportDialog extends JDialog implements ActionListener {
202        String[] attr = { 
203          "Goal",
204          "Task",
205          "Agent",
206          "State",
207          "Owner",
208          "Start-Time",
209          "End-Time",
210          "Cost"
211        };
212      
213        protected JTextField[] textfield;
214        protected JButton      okButton;
215        
216        public ReportDialog(ReportRec rec) {
217          super((JFrame)SwingUtilities.getRoot(ReportGraph.this),
218                rec.getGoal() + "/" + rec.getAgent(),false);
219          
220          JPanel p1 = new JPanel();
221          p1.setLayout(new GridLayout(1,1,10,10));
222          okButton = new JButton("OK");
223          p1.add(okButton);
224          
225          GridBagLayout gb = new GridBagLayout();
226          GridBagConstraints gbc = new GridBagConstraints();
227          gbc.insets = new Insets(10,0,10,0);
228          gbc.anchor = GridBagConstraints.NORTHWEST;
229          gbc.fill = GridBagConstraints.HORIZONTAL;
230          gbc.gridwidth = GridBagConstraints.REMAINDER;
231          gbc.weightx = 1;
232          
233          JPanel p0 = new JPanel();
234          JSeparator s1 = new JSeparator();
235          p0.setLayout(gb);
236          gb.setConstraints(s1,gbc);
237          p0.add(s1);
238          
239          gbc.anchor = GridBagConstraints.CENTER;
240          gbc.fill = GridBagConstraints.NONE;
241          gbc.insets = new Insets(0,0,10,0);
242          gb.setConstraints(p1,gbc);
243          p0.add(p1);
244          
245          getContentPane().add("South",p0);
246          
247          JPanel panel = new JPanel();
248          panel.setLayout(gb);
249          
250          JLabel[] label = new JLabel[attr.length];
251          textfield = new JTextField[attr.length];
252          
253          gbc.anchor = GridBagConstraints.WEST;
254          gbc.weightx = gbc.weighty = 0;
255          for(int i = 0; i < attr.length; i++ ) {
256            label[i] = new JLabel(attr[i],Label.LEFT);
257            gbc.gridwidth = 1;
258            gbc.fill = GridBagConstraints.NONE;
259            gbc.weightx = 0;
260            gbc.insets = new Insets(10,10,0,0);
261            gb.setConstraints(label[i],gbc);
262            panel.add(label[i]);
263            
264            textfield[i] = new JTextField();
265            textfield[i].setEditable(false);
266            gbc.gridwidth = GridBagConstraints.REMAINDER;
267            gbc.fill = GridBagConstraints.HORIZONTAL;
268            gbc.weightx = 1;
269            gbc.insets = new Insets(10,10,0,10);
270            gb.setConstraints(textfield[i],gbc);
271            panel.add(textfield[i]);
272          }
273          getContentPane().add("Center",panel);
274          
275          okButton.addActionListener(this);
276          this.addWindowListener( new WindowAdapter() {
277                public void windowClosing(WindowEvent evt) {
278                   setVisible(false);
279                }
280             }
281          );
282          update(rec);
283        }
284      
285        protected void update(ReportRec report) {
286          if ( report == null ) return;
287          textfield[0].setText(report.getGoal());
288          textfield[1].setText(report.getTask());
289          textfield[2].setText(report.getAgent());
290          textfield[3].setText(PlanRecord.state_string[report.getState()]);
291          textfield[4].setText(report.getOwner());
292          textfield[5].setText(Integer.toString(report.getStartTime()));
293          textfield[6].setText(Integer.toString(report.getEndTime()));
294          textfield[7].setText(Double.toString(report.getCost()));
295        }
296        
297        public void actionPerformed(ActionEvent evt) {
298          setVisible(false);
299        }
300      }
301   }
302 
303 
304   protected class NodesToolBar extends JToolBar implements ActionListener {
305      protected JButton       selectBtn;
306      protected JButton       selectAllBtn;
307      protected JButton       hideBtn;
308      protected JButton       showBtn;
309      protected JButton       collapseBtn;
310      protected JButton       expandBtn;
311      protected JButton       deleteBtn;
312      protected JButton       recomputeBtn;
313      protected JButton       redrawBtn;
314 
315      public NodesToolBar() {
316         setBackground(Color.lightGray);
317         setBorder(new BevelBorder(BevelBorder.LOWERED));
318         setFloatable(false);
319 
320         String sep = System.getProperty("file.separator");
321         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
322 
323         // Draw Buttons
324         recomputeBtn = new JButton(new ImageIcon(path + "recompute.gif"));
325 	recomputeBtn.setMargin(new Insets(0,0,0,0));
326         add(recomputeBtn);
327         recomputeBtn.setToolTipText("Recompute node positions");
328         recomputeBtn.addActionListener(this);
329 
330         redrawBtn = new JButton(new ImageIcon(path + "redraw.gif"));
331 	redrawBtn.setMargin(new Insets(0,0,0,0));
332         add(redrawBtn);
333         redrawBtn.setToolTipText("Redraw");
334         redrawBtn.addActionListener(this);
335 
336 	addSeparator();
337 
338         selectBtn = new JButton(new ImageIcon(path + "select.gif"));
339 	selectBtn.setMargin(new Insets(0,0,0,0));
340         add(selectBtn);
341         selectBtn.setToolTipText("Select nodes");
342         selectBtn.addActionListener(this);
343 
344         selectAllBtn = new JButton(new ImageIcon(path + "selectAll.gif"));
345 	selectAllBtn.setMargin(new Insets(0,0,0,0));
346         add(selectAllBtn);
347         selectAllBtn.setToolTipText("Select all nodes");
348         selectAllBtn.addActionListener(this);
349 
350         addSeparator();
351 
352         collapseBtn = new JButton(new ImageIcon(path + "collapse.gif"));
353 	collapseBtn.setMargin(new Insets(0,0,0,0));
354         add(collapseBtn);
355         collapseBtn.setToolTipText("Collapse nodes");
356         collapseBtn.addActionListener(this);
357 
358         expandBtn = new JButton(new ImageIcon(path + "expand.gif"));
359 	expandBtn.setMargin(new Insets(0,0,0,0));
360         add(expandBtn);
361         expandBtn.setToolTipText("Expand nodes");
362         expandBtn.addActionListener(this);
363 
364         addSeparator();
365 
366         hideBtn = new JButton(new ImageIcon(path + "hide.gif"));
367 	hideBtn.setMargin(new Insets(0,0,0,0));
368         add(hideBtn);
369         hideBtn.setToolTipText("Hide nodes");
370         hideBtn.addActionListener(this);
371 
372         showBtn = new JButton(new ImageIcon(path + "show.gif"));
373 	showBtn.setMargin(new Insets(0,0,0,0));
374         add(showBtn);
375         showBtn.setToolTipText("Show nodes");
376         showBtn.addActionListener(this);
377      }
378 
379      public void actionPerformed(ActionEvent e)  {
380         Object src = e.getSource();
381         if ( src == recomputeBtn )
382            graph.recompute();
383         else if ( src == redrawBtn )
384            graph.redraw();
385         else if ( src == selectBtn )
386            graph.select();
387         else if ( src == selectAllBtn )
388            graph.selectAll();
389         else if ( src == collapseBtn )
390            graph.collapse();
391         else if ( src == expandBtn )
392            graph.expand();
393         else if ( src == hideBtn )
394            graph.hide();
395         else if ( src == showBtn )
396            graph.show();
397      }
398   }
399 }