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   *   ReportTool for the visualiser
26   ********************************************************************/
27  
28  
29  package zeus.visualiser.report;
30  
31  import java.io.*;
32  import java.util.*;
33  import java.awt.*;
34  import java.awt.event.*;
35  import javax.swing.*;
36  import javax.swing.event.*;
37  import javax.swing.border.*;
38  
39  import zeus.util.*;
40  import zeus.concepts.*;
41  import zeus.gui.*;
42  import zeus.gui.help.*;
43  import zeus.gui.graph.*;
44  import zeus.actors.*;
45  import zeus.visualiser.*;
46  import zeus.visualiser.basic.*;
47  
48  public class ReportTool extends VideoTool
49                          implements ListSelectionListener {
50  
51    private static int count = 0;
52  
53    protected ReportMenuBar                   menubar;
54    protected EditableMultipleSelectionDialog report_dialog = null;
55    protected DeleteReportDialog              del_report = null;
56    protected Graph                           graph = null;
57    protected JList                           agentList, taskList;
58    protected ReportModel                     onlineModel, offlineModel;
59  
60  
61    public ReportTool(AgentContext context, VisualiserModel model) {
62      super(context, model);
63      this.setTitle(context.whoami() + " - Reports Tool:" + (count++));
64      ImageIcon icon = new ImageIcon(SystemProps.getProperty("gif.dir") +
65         File.separator + "visualiser" + File.separator + "report-icon.gif");
66      setIconImage(icon.getImage());
67      this.setBackground(Color.lightGray);
68  
69      onlineModel = new ReportModel();
70      offlineModel = new ReportModel();
71      ReportGraph graphPanel = new ReportGraph(onlineModel);
72      graph = graphPanel.getGraph();
73  
74      // Create and Add Toolbars
75      getContentPane().setLayout(new BorderLayout());
76      getContentPane().add(videoToolbar,BorderLayout.NORTH);
77      getContentPane().add(graphPanel,BorderLayout.CENTER);
78  
79      // Create Lists
80      agentList = new JList(onlineModel.getAgentListModel());
81      taskList = new JList(onlineModel.getTaskListModel());
82  
83      agentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
84      taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
85      agentList.addListSelectionListener(this);
86      taskList.addListSelectionListener(this);
87  
88      JPanel lhsPanel = new JPanel();
89      lhsPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
90  
91      GridBagLayout gb = new GridBagLayout();
92      GridBagConstraints gbc = new GridBagConstraints();
93      lhsPanel.setLayout(gb);
94      setBackground(Color.lightGray);
95  
96  
97      JScrollPane scrollpane = new JScrollPane(agentList);
98      scrollpane.setPreferredSize(new Dimension(80,180));
99  
100     TitledBorder border = BorderFactory.createTitledBorder("Agents");
101     border.setTitlePosition(TitledBorder.TOP);
102     border.setTitleJustification(TitledBorder.RIGHT);
103     border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
104     border.setTitleColor(Color.blue);
105     scrollpane.setBorder(border);
106 
107     gbc.insets = new Insets(4,4,0,4);
108     gbc.anchor = GridBagConstraints.NORTHWEST;
109     gbc.fill = GridBagConstraints.BOTH;
110     gbc.gridwidth = GridBagConstraints.REMAINDER;
111     gbc.weightx = gbc.weighty = 1;
112     gb.setConstraints(scrollpane,gbc);
113     lhsPanel.add(scrollpane);
114 
115     scrollpane = new JScrollPane(taskList);
116     scrollpane.setPreferredSize(new Dimension(80,180));
117 
118     border = BorderFactory.createTitledBorder("Current Tasks");
119     border.setTitlePosition(TitledBorder.TOP);
120     border.setTitleJustification(TitledBorder.RIGHT);
121     border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
122     border.setTitleColor(Color.blue);
123     scrollpane.setBorder(border);
124 
125     gbc.insets = new Insets(4,4,0,4);
126     gbc.anchor = GridBagConstraints.NORTHWEST;
127     gbc.fill = GridBagConstraints.BOTH;
128     gbc.gridwidth = GridBagConstraints.REMAINDER;
129     gbc.weightx = gbc.weighty = 1;
130     gb.setConstraints(scrollpane,gbc);
131     lhsPanel.add(scrollpane);
132 
133     JPanel keyPanel = new JPanel();
134     border = BorderFactory.createTitledBorder("Task Codes");
135     border.setTitlePosition(TitledBorder.TOP);
136     border.setTitleJustification(TitledBorder.RIGHT);
137     border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
138     border.setTitleColor(Color.blue);
139     keyPanel.setBorder(border);
140 
141     gbc.insets = new Insets(4,4,4,4);
142     gbc.anchor = GridBagConstraints.NORTHWEST;
143     gbc.fill = GridBagConstraints.HORIZONTAL;
144     gbc.gridwidth = GridBagConstraints.REMAINDER;
145     gbc.weightx = gbc.weighty = 0;
146     gb.setConstraints(keyPanel,gbc);
147     lhsPanel.add(keyPanel);
148 
149     gb = new GridBagLayout();
150     gbc = new GridBagConstraints();
151     keyPanel.setBackground(Color.lightGray);
152     keyPanel.setLayout(gb);
153 
154     JLabel taskLabel;
155     JPanel taskCode;
156     // first possible 3 states are highly unlikely to be shown
157     for(int i = 3; i < PlanRecord.state_string.length; i++) {
158        taskLabel = new JLabel(PlanRecord.state_string[i],JLabel.LEFT);
159        gbc.insets = new Insets(4,4,0,0);
160        gbc.anchor = GridBagConstraints.WEST;
161        gbc.fill = GridBagConstraints.NONE;
162        gbc.gridwidth = 1;
163        gbc.weightx = gbc.weighty = 0;
164        gb.setConstraints(taskLabel,gbc);
165        keyPanel.add(taskLabel);
166 
167        taskCode = new JPanel();
168        taskCode.setSize(10,10);
169        taskCode.setBackground(PlanRecord.color[i]);
170        gbc.insets = new Insets(4,4,0,4);
171        gbc.anchor = GridBagConstraints.WEST;
172        gbc.fill = GridBagConstraints.NONE;
173        gbc.gridwidth = GridBagConstraints.REMAINDER;
174        gbc.weightx = gbc.weighty = 0;
175        gb.setConstraints(taskCode,gbc);
176        keyPanel.add(taskCode);
177     }
178 
179     getContentPane().add(lhsPanel,BorderLayout.WEST);
180     
181     menubar = new ReportMenuBar();
182     setJMenuBar(menubar);
183 
184     pack();
185     setVisible(true);
186     
187     /* initialisation sequence */
188     setMode(ONLINE);
189   }
190 
191   public void valueChanged(ListSelectionEvent evt) {
192      if ( evt.getValueIsAdjusting() ) return;
193      JList list = (JList)evt.getSource();
194      String value = (String)list.getSelectedValue();
195      if ( value == null ) return;
196 
197      if ( list == agentList )
198         ((ReportModel)graph.getModel()).showAgent(value);
199      else if ( list == taskList )
200         ((ReportModel)graph.getModel()).showTask(value);
201   }
202 
203   public void StreamReports(boolean mode) {
204       if ( !hubOK() ) return;
205       if ( !stopPlayback() ) return;
206 
207       String type = SystemProps.getProperty("agent.names.agent");
208       String[] agents = model.getAgents(type);
209       if ( report_dialog == null ) {
210          report_dialog = new EditableMultipleSelectionDialog(this,
211             "Select Agents", agents);
212          report_dialog.setLocationRelativeTo(this);
213       }
214       else {
215          Object[] chosen = report_dialog.getPriorSelection();
216          report_dialog.setListData(agents);
217          report_dialog.setSelection(chosen);
218       }
219 
220       Object[] data = report_dialog.getSelection();
221       model.addAgents(Misc.stringArray(report_dialog.getListData()));
222       agents = Misc.stringArray(data);
223       subscribe(mode,model.keys[VisualiserModel.REPORT_KEY],agents,"log_report");
224 
225       if ( mode )
226          onlineModel.addAgents(agents);
227       else
228          onlineModel.removeAgents(agents);
229   }
230 
231   public void registerAgent(String name, String type) {
232   }
233 
234   public void log_report(Performative msg) {
235 try {
236      ReportRec rec = ZeusParser.reportRec(context.OntologyDb(),msg.getContent());
237      Core.DEBUG(2,"log_report\n" + rec);
238      if ( filterMsg(msg) ) {
239         // for efficiency only
240         Fact[] pc = null;
241         rec.setPreconditions(pc);
242         rec.setPostconditions(pc);
243         onlineModel.addReport(rec);
244      }
245 
246      // save to persistent db
247      record_item(msg);
248 }
249 catch(Exception e) {
250 //   e.printStackTrace();
251 }
252   }
253 
254   public void Help()  {
255     Point pt = getLocation();
256     HelpWindow helpWin = new HelpWindow(this, pt, "visualiser", "Report Tool");
257     helpWin.setSize(new Dimension(getWidth(), 440));
258     helpWin.setLocation(pt.x+24, pt.y+24);
259     helpWin.validate();
260   }
261 
262   public void DeleteReport() {
263      String task = (String)taskList.getSelectedValue();
264      String agent = (String)agentList.getSelectedValue();
265      if ( task == null )
266         JOptionPane.showMessageDialog(this,
267 	   "Select a task from the task list before calling this function",
268 	   "Error", JOptionPane.ERROR_MESSAGE);
269      else
270         ((ReportModel)graph.getModel()).removeTask(agent,task);
271   }
272 
273   public void DeleteReports() {
274      if ( del_report == null ) {
275         del_report = new DeleteReportDialog(this, "Delete Reports");
276         del_report.setLocationRelativeTo(this);
277      }
278      if ( del_report.isShowing() )
279         del_report.toFront();
280      else {
281         del_report.display((ReportModel)graph.getModel());
282      }
283   }
284 
285   public void setAutoDeleteReport(boolean set) {
286      onlineModel.setAutoDelete(set);
287      offlineModel.setAutoDelete(set);
288   }
289 
290   public void setShowJointGraphs(boolean set) {
291      onlineModel.setShowJointGraphs(set);
292      offlineModel.setShowJointGraphs(set);
293   }
294 
295   protected void setMode(int mode) {
296     agentList.clearSelection();
297     taskList.clearSelection();
298 
299     if ( mode == ONLINE ) {
300        menubar.update(ONLINE);
301        videoToolbar.setStatus(false);
302        graph.setModel(onlineModel);
303        agentList.setModel(onlineModel.getAgentListModel());
304        taskList.setModel(onlineModel.getTaskListModel());
305 
306        String agent = onlineModel.getCurrentAgent();
307        String task = onlineModel.getCurrentTask();
308        if ( agent != null ) agentList.setSelectedValue(agent,true);
309        if ( task != null )  taskList.setSelectedValue(task,true);
310  
311     }
312     else {
313        menubar.update(PLAYBACK);
314        videoToolbar.setStatus(true);
315        offlineModel.reset();
316        graph.setModel(offlineModel);
317        agentList.setModel(offlineModel.getAgentListModel());
318        taskList.setModel(offlineModel.getTaskListModel());
319 
320        String agent = onlineModel.getCurrentAgent();
321        String task = onlineModel.getCurrentTask();
322        if ( agent != null ) agentList.setSelectedValue(agent,true);
323        if ( task != null )  taskList.setSelectedValue(task,true);
324     }
325   }
326 
327   protected void registerListOfPlaybackAgents(Vector List) {
328      offlineModel.addAgents(List);
329   }
330 
331   protected void  visualiseVideoData(int dir, Performative msg) {
332      if ( state.mode == PLAYBACK ) {
333         ReportRec rec;
334         rec = ZeusParser.reportRec(context.OntologyDb(),msg.getContent());
335         // System.out.println(rec.pprint());
336         // for efficiency only
337         Fact[] pc = new Fact[0];
338         rec.setPreconditions(pc);
339         rec.setPostconditions(pc);
340         offlineModel.addReport(rec);
341      }
342   }
343 
344 protected class ReportMenuBar extends JMenuBar
345                               implements ActionListener, ItemListener {
346   protected JMenu fileMenu;
347   protected JMenu requestMenu;
348   protected JMenu helpMenu;
349   protected JMenu viewMenu;
350   protected JMenu playMenu;
351   protected JMenu optionsMenu;
352 
353   protected JMenuItem connect;
354   protected JMenuItem disconnect;
355   protected JMenuItem exit;
356   protected JMenuItem cc;
357   protected JMenuItem un_cc;
358   protected JMenuItem delete_one;
359   protected JMenuItem delete_all;
360   protected JMenuItem help;
361   protected JMenuItem about;
362 
363   protected JMenuItem filter;
364   protected JMenuItem player_speed;
365   protected JMenuItem collapse;
366   protected JMenuItem expand;
367   protected JMenuItem recompute;
368   protected JMenuItem redraw;
369   protected JMenuItem select;
370   protected JMenuItem selectAll;
371   protected JMenuItem hide;
372   protected JMenuItem show;
373 
374   protected JMenuItem sessions;
375   protected JMenuItem load;
376   protected JMenuItem save;
377   protected JMenuItem close;
378   protected JMenuItem forward;
379   protected JMenuItem rewind;
380   protected JMenuItem fforward;
381   protected JMenuItem frewind;
382   protected JMenuItem stop;
383   protected JMenuItem forward_step;
384   protected JMenuItem rewind_step;
385   protected JMenuItem forward_last;
386   protected JMenuItem rewind_first;
387   protected JMenuItem delete;
388   protected JMenuItem purge;
389 
390   protected JCheckBoxMenuItem auto_delete;
391   protected JCheckBoxMenuItem joint_graph;
392 
393   protected static final int CHECK = 0;
394   protected static final int PLAIN = 1;
395   protected static final int RADIO = 2;
396 
397   public ReportMenuBar() {
398     fileMenu = new JMenu("File");
399     fileMenu.setMnemonic('F');
400     connect    = createMenuItem(fileMenu, PLAIN, "Connect to namservers", 'C');
401     disconnect = createMenuItem(fileMenu, PLAIN, "Disconnect from nameservers",'D');
402     exit       = createMenuItem(fileMenu, PLAIN, "Quit", 'Q');
403     add(fileMenu);
404 
405     requestMenu = new JMenu("Reports");
406     requestMenu.setMnemonic('R');
407     cc = createMenuItem(requestMenu, PLAIN, "Request reports", 0);
408     un_cc = createMenuItem(requestMenu, PLAIN, "Unrequest reports", 0);
409     delete_one = createMenuItem(requestMenu, PLAIN, "Delete current report", 0);
410     delete_all = createMenuItem(requestMenu, PLAIN, "Delete reports...", 0);
411     auto_delete = (JCheckBoxMenuItem)createMenuItem(requestMenu, CHECK,
412 		               "Auto-delete reports", 0);
413     joint_graph = (JCheckBoxMenuItem)createMenuItem(requestMenu, CHECK,
414 		               "Show connected reports", 0);
415 
416     auto_delete.setState(onlineModel.getAutoDelete());
417     joint_graph.setState(onlineModel.getShowJointGraphs());
418     add(requestMenu);
419 
420     JMenu playMenu = new JMenu("Playback");
421     playMenu.setMnemonic('P');
422     sessions = createMenuItem(playMenu, PLAIN, "Request saved sessions", 0);
423     delete   = createMenuItem(playMenu, PLAIN, "Delete session", 0);
424     purge    = createMenuItem(playMenu, PLAIN, "Purge database", 0);
425     playMenu.addSeparator();
426     load  = createMenuItem(playMenu, PLAIN, "Load session", 0);
427     save  = createMenuItem(playMenu, PLAIN, "Record session", 0);
428     close = createMenuItem(playMenu, PLAIN, "Close session", 0);
429     playMenu.addSeparator();
430     forward  = createMenuItem(playMenu, PLAIN, "Forward", 0);
431     rewind   = createMenuItem(playMenu, PLAIN, "Rewind", 0);
432     fforward = createMenuItem(playMenu, PLAIN, "Fast forward", 0);
433     frewind  = createMenuItem(playMenu, PLAIN, "Fast rewind", 0);
434     forward_step = createMenuItem(playMenu, PLAIN, "Step forward", 0);
435     rewind_step  = createMenuItem(playMenu, PLAIN, "Step backward", 0);
436     forward_last = createMenuItem(playMenu, PLAIN, "Forward to end", 0);
437     rewind_first = createMenuItem(playMenu, PLAIN, "Rewind to beginning", 0);
438     stop         = createMenuItem(playMenu, PLAIN, "Stop", 0);
439     add(playMenu);
440 
441     optionsMenu = new JMenu("Options");
442     optionsMenu.setMnemonic('O');
443     filter       = createMenuItem(optionsMenu, PLAIN, "Filter messages...", 0);
444     player_speed = createMenuItem(optionsMenu, PLAIN, "Player speed...", 0);
445     add(optionsMenu);
446 
447     viewMenu = new JMenu("View");
448     viewMenu.setMnemonic('V');
449     collapse  = createMenuItem(viewMenu, PLAIN, "Collapse", 0);
450     expand    = createMenuItem(viewMenu, PLAIN, "Expand", 0);
451     recompute = createMenuItem(viewMenu, PLAIN, "Recompute", 0);
452     redraw    = createMenuItem(viewMenu, PLAIN, "Redraw", 0);
453     viewMenu.addSeparator();
454     select    = createMenuItem(viewMenu, PLAIN, "Select", 0);
455     selectAll = createMenuItem(viewMenu, PLAIN, "Select all", 0);
456     viewMenu.addSeparator();
457     hide      = createMenuItem(viewMenu, PLAIN, "Hide", 0);
458     show      = createMenuItem(viewMenu, PLAIN, "Show", 0);
459 
460     helpMenu = new JMenu("Help");
461     helpMenu.setMnemonic('H');
462     help  = createMenuItem(helpMenu, PLAIN, "Using the control tool", 'U');
463     about = createMenuItem(helpMenu, PLAIN, "About ZEUS", 'A');
464     add(helpMenu);
465   }
466 
467   private JMenuItem createMenuItem(JMenu menu, int type, String text, int accelKey) {
468     JMenuItem item;
469     switch(type) {
470       case CHECK:
471         item = new JCheckBoxMenuItem(text);
472         ((JCheckBoxMenuItem)item).setState(false);
473         item.addItemListener(this);
474         break;
475       case RADIO:
476         item = new JRadioButtonMenuItem(text);
477         item.addActionListener(this);
478       default:
479         item = new JMenuItem(text);
480         item.addActionListener(this);
481         break;
482     }
483     if(accelKey > 0)
484       item.setMnemonic(accelKey);
485     menu.add(item);
486     return item;
487   }
488 
489   public void actionPerformed(ActionEvent actionevent)  {
490         Object obj = actionevent.getSource();
491         if(obj == connect)
492             Connect(true);
493         else if(obj == disconnect)
494             Connect(false);
495         else if(obj == exit)
496             Exit();
497         else if(obj == cc)
498             StreamReports(true);
499         else if(obj == un_cc)
500             StreamReports(false);
501         else if(obj == delete_one)
502             DeleteReport();
503         else if(obj == delete_all)
504             DeleteReports();
505         else if(obj == sessions)
506             Sessions();
507         else if(obj == delete)
508             Delete();
509         else if(obj == purge)
510             Purge();
511         else if(obj == load)
512             Load();
513         else if(obj == save)
514             Record();
515         else if(obj == close)
516             Close();
517         else if(obj == forward)
518             Forward();
519         else if(obj == rewind)
520             Rewind();
521         else if(obj == fforward)
522             FForward();
523         else if(obj == frewind)
524             FRewind();
525         else if(obj == stop)
526             Stop();
527         else if(obj == forward_step)
528             StepForward();
529         else if(obj == forward_last)
530             ForwardEnd();
531         else if(obj == rewind_step)
532             StepRewind();
533         else if(obj == rewind_first)
534             RewindBegin();
535         else if(obj == player_speed)
536             PlayerSpeed();
537        else if (obj == filter )
538             Filter();
539 
540 
541         else if(obj == collapse)
542             graph.collapse();
543         else if(obj == expand)
544             graph.expand();
545         else if(obj == recompute)
546             graph.recompute();
547         else if(obj == redraw)
548             graph.redraw();
549         else if(obj == select)
550             graph.select();
551         else if(obj == selectAll)
552             graph.selectAll();
553         else if(obj == hide)
554             graph.hide();
555         else if(obj == show)
556             graph.show();
557 
558         else if(obj == help)
559             Help();
560         else if(obj == about)
561             About();
562   }
563 
564   public void itemStateChanged(ItemEvent itemevent)  {
565     Object obj = itemevent.getSource();
566     if(obj == auto_delete)
567       setAutoDeleteReport(auto_delete.getState());
568     else if(obj == joint_graph)
569       setShowJointGraphs(joint_graph.getState());
570   }
571 
572   public void update(int mode)  {
573     boolean flag = mode == ReportTool.this.PLAYBACK;
574     cc.setEnabled(!flag);
575     forward.setEnabled(flag);
576     rewind.setEnabled(flag);
577     fforward.setEnabled(flag);
578     frewind.setEnabled(flag);
579     stop.setEnabled(flag);
580     forward_step.setEnabled(flag);
581     rewind_step.setEnabled(flag);
582     forward_last.setEnabled(flag);
583     rewind_first.setEnabled(flag);
584   }
585 }
586 
587 }