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   *          ZEUS Agent Society Viewer with Video Tools             *
26   *        Allows the user to view the interaction of agents        *
27   *         and use video tool features to examine sessions         *
28   *******************************************************************/
29  
30  package zeus.visualiser.society;
31  
32  import java.io.*;
33  import java.util.*;
34  import java.awt.*;
35  import java.awt.event.*;
36  import javax.swing.*;
37  
38  import zeus.util.*;
39  import zeus.concepts.*;
40  import zeus.gui.*;
41  import zeus.gui.graph.*;
42  import zeus.gui.help.*;
43  import zeus.actors.*;
44  import zeus.visualiser.VisualiserModel;
45  import zeus.visualiser.basic.*;
46  
47  
48  public class SocietyTool extends VideoTool {
49     private static int count = 0;
50  
51     private EditableMultipleSelectionDialog update_dialog   = null;
52     private EditableMultipleSelectionDialog message_dialog  = null;
53     private NumberDialog                    number_dialog = null;
54  
55     private SocietyMenuBar menubar;
56  
57     protected SocietyModel     onlineModel, offlineModel;
58     protected AnimationManager animationManager;
59     protected Graph            graph;
60  
61     public SocietyTool(AgentContext context, VisualiserModel model) {
62       super(context,model);
63       setTitle(context.whoami() + " - SocietyTool:" + (count++));
64       String path = SystemProps.getProperty("gif.dir") + File.separator +
65                     "visualiser" + File.separator + "society-icon.gif";
66       ImageIcon icon = new ImageIcon(path);
67       setIconImage(icon.getImage());
68       setBackground(Color.lightGray);
69  
70       onlineModel = new SocietyModel();
71       offlineModel = new SocietyModel();
72  
73       SocietyPanel societyPanel = new SocietyPanel(onlineModel);
74       graph = societyPanel.getGraph();
75       animationManager = new AnimationManager(context,graph);
76  
77       // Create a panel to model the frame contents
78       Container pane = getContentPane();
79       pane.setLayout(new BorderLayout());
80       pane.add(societyPanel, BorderLayout.CENTER);
81       pane.add(videoToolbar,BorderLayout.NORTH);
82  
83       menubar = new SocietyMenuBar();
84       setJMenuBar(menubar);
85  
86       pack();
87       setVisible(true);
88       setMode(ONLINE);
89       initialRegistration();
90     }
91  
92     public void Exit() {
93       if ( animationManager != null )
94          animationManager.terminate();
95       super.Exit();
96     }
97  
98     public void UpdateRelations() {
99        if ( !hubOK() ) return;
100       if ( !stopPlayback() ) return;
101   
102       String[] agents = model.getAgents();
103       if ( update_dialog == null ) {
104          update_dialog = new EditableMultipleSelectionDialog(this,
105             "Select Agents", agents);
106          update_dialog.setLocationRelativeTo(this);
107       }
108       else {
109          Object[] chosen = update_dialog.getPriorSelection();
110          update_dialog.setListData(agents);
111          update_dialog.setSelection(chosen);
112       }
113 
114       Object[] data = update_dialog.getSelection();
115       model.addAgents(Misc.stringArray(update_dialog.getListData()));
116       if ( data != null && data.length > 0 )
117          query("your_relations",Misc.stringArray(data),"log_relations");
118    }
119 
120    public void StreamMessages(boolean mode) {
121       if ( !hubOK() ) return;
122       if ( !stopPlayback() ) return;
123 
124       String[] agents = model.getAgents();
125       if ( message_dialog == null ) {
126          message_dialog = new EditableMultipleSelectionDialog(this,
127             "Select Agents", agents);
128          message_dialog.setLocationRelativeTo(this);
129       }
130       else {
131          Object[] chosen = message_dialog.getPriorSelection();
132          message_dialog.setListData(agents);
133          message_dialog.setSelection(chosen);
134       }
135 
136       Object[] data = message_dialog.getSelection();
137       model.addAgents(Misc.stringArray(message_dialog.getListData()));
138       subscribe(mode,model.keys[VisualiserModel.MESSAGE_KEY],
139          Misc.stringArray(data),"log_message");
140    }
141 
142    protected void setMode(int mode) {
143      if ( mode == ONLINE ) {
144         menubar.update(ONLINE);
145         videoToolbar.setStatus(false);
146         graph.setModel(onlineModel);
147      }
148      else {
149         menubar.update(PLAYBACK);
150         videoToolbar.setStatus(true);
151         graph.setModel(offlineModel);
152      }
153    }
154 
155    protected void initialRegistration() {
156       String type = SystemProps.getProperty("agent.names.agent");
157       String[] agent = model.getAgents(type);
158       for(int i = 0; i < agent.length; i++ )
159          registerAgent(agent[i],type);
160 
161       type = SystemProps.getProperty("agent.names.facilitator");
162       agent = model.getAgents(type);
163       for(int i = 0; i <  agent.length; i++ )
164          registerAgent(agent[i],type);
165 
166       type = SystemProps.getProperty("agent.names.visualiser");
167       agent = model.getAgents(type);
168       for(int i = 0; i <  agent.length; i++ )
169          registerAgent(agent[i],type);
170 
171       type = SystemProps.getProperty("agent.names.dbProxy");
172       agent = model.getAgents(type);
173       for(int i = 0; i <  agent.length; i++ )
174          registerAgent(agent[i],type);
175 
176       type = SystemProps.getProperty("agent.names.nameserver");
177       agent = model.getAgents(type);
178       for(int i = 0; i <  agent.length; i++ )
179          registerAgent(agent[i],type);
180    }
181 
182    protected void registerAgent(String name, String type) {
183       onlineModel.addAgent(name,type);
184       debug ("added online name = " + name ); 
185    }
186 
187    protected void registerListOfPlaybackAgents(Vector List) {
188       offlineModel.addAgents(List);
189    }
190 
191    protected void  visualiseVideoData(int dir, Performative msg) {
192       if ( state.mode == PLAYBACK && state.animating && filterMsg(msg) ) {
193          // check if going forwards or backwards
194          if ( dir == BACKWARD ) {
195             String sender = msg.getReceiver();
196             msg.setReceiver(msg.getSender());
197             msg.setSender(sender);
198          }
199          animationManager.animate(msg);
200       }
201    }
202 
203    public void log_relations(Performative msg) {
204       if (!msg.getType().equals("inform") ) return;
205       Vector List = ZeusParser.relationshipList(msg.getContent());
206       String agent = msg.getSender();
207       onlineModel.addRelations(agent,List);
208    }
209 
210    public void log_message(Performative msg) {
211     debug("log_message 1"); 
212 try {
213       Performative imsg = ZeusParser.performative(msg.getContent());
214          debug("log_message 2"); 
215       if ( state.mode == ONLINE && state.animating && filterMsg(imsg) )
216          animationManager.animate(imsg);
217             debug("log_message 3"); 
218 
219       // save to persistent db
220       record_item(imsg);
221 }
222 catch(Exception e) {
223        e.printStackTrace();
224 }
225    }
226 
227    void Animation(boolean set) {
228       state.animating = set;
229       if ( !set ) animationManager.flushAnimator();
230    }
231 
232    void setAnimationMode(int mode) {
233       animationManager.setMode(mode);
234    }
235 
236    void AnimationSpeed() {
237       long speed = animationManager.getSpeed();
238       if ( number_dialog == null ) {
239          number_dialog = new NumberDialog(this,"Set Animation Speed",
240             "Enter speed:");
241          number_dialog.setLocationRelativeTo(this);
242       }
243       number_dialog.setValue(speed);
244       Long value = number_dialog.getValue();
245       if (value != null)
246          animationManager.setSpeed(value.longValue());
247    }
248 
249    void Help() {
250       Point pt = getLocation();
251       HelpWindow helpWin = new HelpWindow(this, pt, "visualiser", "Society Viewer");
252       helpWin.setSize(new Dimension(getWidth(), 500));
253       helpWin.setLocation(pt.x+24, pt.y+24);
254       helpWin.validate();
255    }
256 
257    public class SocietyMenuBar extends JMenuBar
258                                implements ActionListener, ItemListener {
259 
260      protected JMenuItem connect, disconnect, exit;
261      protected JMenuItem sessions, load, save, close, forward, rewind,
262                          fforward, frewind, stop, forward_step, rewind_step,
263                          forward_last, rewind_first, delete, purge;
264 
265      protected JMenuItem cc, update, un_cc;
266      protected JMenuItem filter, animation_speed, player_speed;
267      protected JMenuItem collapse, expand, recompute, redraw;
268      protected JMenuItem select, selectAll, hide, show;
269      protected JMenuItem help, about;
270      protected JMenuItem onOff;
271      protected JRadioButtonMenuItem letterAnim, arrowAnim;
272    
273      protected static final int CHECK = 0;
274      protected static final int PLAIN = 1;
275      protected static final int RADIO = 2;
276 
277      public SocietyMenuBar() {
278         add(fileMenu());
279         add(onlineMenu());
280         add(playbackMenu());
281         add(optionsMenu());
282         add(viewMenu());
283         add(helpMenu());
284      }
285    
286      private JMenu fileMenu() {
287         JMenu menu = new JMenu("File");
288         menu.setMnemonic('F');
289         connect = createMenuItem(menu,PLAIN,"Connect to namservers", 'C');
290         disconnect=createMenuItem(menu,PLAIN,"Disconnect from nameservers",'D');
291         exit = createMenuItem(menu, PLAIN, "Quit", 'Q');
292         return menu;
293      }
294    
295        private JMenu onlineMenu() {
296            JMenu menu = new JMenu("Online");
297            menu.setMnemonic('O');
298            update = createMenuItem(menu, PLAIN, "Update relations", 'U');
299            cc = createMenuItem(menu, PLAIN, "Request messages", 'R');
300            un_cc = createMenuItem(menu, PLAIN, "Un-request messages", 'N');
301            return menu;
302        }
303    
304        private JMenu playbackMenu() {
305            JMenu menu = new JMenu("Playback");
306            menu.setMnemonic('P');
307            sessions = createMenuItem(menu, PLAIN, "Request saved sessions", 0);
308            delete = createMenuItem(menu, PLAIN, "Delete session", 0);
309            purge = createMenuItem(menu, PLAIN, "Purge database", 0);
310            menu.addSeparator();
311            load = createMenuItem(menu, PLAIN, "Load session", 0);
312            save = createMenuItem(menu, PLAIN, "Save session", 0);
313            close = createMenuItem(menu, PLAIN, "Close session", 0);
314            menu.addSeparator();
315            forward = createMenuItem(menu, PLAIN, "Forward", 0);
316            rewind = createMenuItem(menu, PLAIN, "Rewind", 0);
317            fforward = createMenuItem(menu, PLAIN, "Fast forward", 0);
318            frewind = createMenuItem(menu, PLAIN, "Fast rewind", 0);
319            forward_step = createMenuItem(menu, PLAIN, "Step forward", 0);
320            rewind_step = createMenuItem(menu, PLAIN, "Step backward", 0);
321            forward_last = createMenuItem(menu, PLAIN, "Forward to end", 0);
322            rewind_first = createMenuItem(menu, PLAIN, "Rewind to beginning", 0);
323            stop = createMenuItem(menu, PLAIN, "Stop", 0);
324            return menu;
325        }
326    
327        private JMenu optionsMenu() {
328            JMenu menu = new JMenu("Options");
329            menu.setMnemonic('T');
330            filter = createMenuItem(menu, PLAIN, "Filter messages", 0);
331            player_speed = createMenuItem(menu, PLAIN, "Player speed...", 0);
332            animation_speed = createMenuItem(menu,PLAIN,"Animation speed...",0);
333            JMenu animMenu = new JMenu("Animation mode");
334            menu.add(animMenu);
335            int mode = SocietyTool.this.animationManager.getMode();
336            arrowAnim = new JRadioButtonMenuItem("Arrow",
337               mode == AnimationQueue.ARROWS);
338            arrowAnim.addActionListener(this);
339            letterAnim = new JRadioButtonMenuItem("Letter",
340               mode == AnimationQueue.LETTER);
341            letterAnim.addActionListener(this);
342            animMenu.add(letterAnim);
343            animMenu.add(arrowAnim);
344            ButtonGroup animGroup = new ButtonGroup();
345            animGroup.add(arrowAnim);
346            animGroup.add(letterAnim);
347            onOff = createMenuItem(menu, CHECK, "Animation", 0);
348            ((JCheckBoxMenuItem)onOff).setState(SocietyTool.this.state.animating);
349            return menu;
350        }
351    
352        private JMenu viewMenu() {
353            JMenu menu = new JMenu("View");
354            menu.setMnemonic('V');
355            collapse = createMenuItem(menu, PLAIN, "Collapse", 0);
356            expand = createMenuItem(menu, PLAIN, "Expand", 0);
357            recompute = createMenuItem(menu, PLAIN, "Recompute", 0);
358            redraw = createMenuItem(menu, PLAIN, "Redraw", 0);
359            menu.addSeparator();
360            select = createMenuItem(menu, PLAIN, "Select", 0);
361            selectAll = createMenuItem(menu, PLAIN, "Select all", 0);
362            menu.addSeparator();
363            hide = createMenuItem(menu, PLAIN, "Hide", 0);
364            show = createMenuItem(menu, PLAIN, "Show", 0);
365            return menu;
366        }
367    
368        private JMenu helpMenu() {
369            JMenu menu = new JMenu("Help");
370            menu.setMnemonic('H');
371            help  = createMenuItem(menu, PLAIN, "Using the society tool", 'U');
372            about = createMenuItem(menu, PLAIN, "About ZEUS...", 'A');
373            return menu;
374        }
375    
376        private JMenuItem createMenuItem(JMenu menu, int type,
377           String text, int accelKey) {
378 
379            JMenuItem item;
380            switch(type) {
381                case CHECK :
382                    item = new JCheckBoxMenuItem();
383                    ((JCheckBoxMenuItem) item).setState(false);
384                    item.addItemListener(this);
385                    break;
386                case RADIO :
387                    item = new JRadioButtonMenuItem();
388                    item.addActionListener(this);
389                default :
390                    item = new JMenuItem();
391                    item.addActionListener(this);
392                    break;
393            }
394            item.setText(text);
395            if(accelKey > 0)
396                item.setMnemonic(accelKey);
397            menu.add(item);
398            return item;
399      }
400 
401      public void actionPerformed(ActionEvent event) {
402        Object source = event.getSource();
403 
404        if ( source == connect )
405          Connect(true);
406        else if ( source == disconnect )
407          Connect(false);
408        else if ( source == exit )
409          Exit();
410 
411        else if ( source == cc)
412          StreamMessages(true);
413        else if ( source == update )
414          UpdateRelations();
415        else if ( source == un_cc )
416          StreamMessages(false);
417 
418        else if ( source == sessions)
419          Sessions();
420        else if ( source == delete)
421          Delete();
422        else if ( source == purge)
423          Purge();
424        else if ( source == load)
425          Load();
426        else if ( source == save )
427          Record();
428        else if ( source == close )
429          Close();
430        else if ( source == forward )
431          Forward();
432        else if ( source == rewind )
433          Rewind();
434        else if ( source == fforward )
435          FForward();
436        else if ( source == frewind )
437          FRewind();
438        else if ( source == stop )
439          Stop();
440        else if ( source == forward_step )
441          StepForward();
442        else if ( source == forward_last )
443          ForwardEnd();
444        else if ( source == rewind_step )
445          StepRewind();
446        else if ( source == rewind_first )
447          RewindBegin();
448 
449 
450        else if ( source == filter )
451          Filter();
452        else if ( source == player_speed )
453          PlayerSpeed();
454        else if ( source == animation_speed )
455          AnimationSpeed();
456 
457        // The animation choice radio buttons
458        else if( source == arrowAnim )
459          setAnimationMode(AnimationQueue.ARROWS);
460        else if( source == letterAnim )
461          setAnimationMode(AnimationQueue.LETTER);
462    
463        else if ( source == collapse )
464          graph.collapse();
465        else if ( source == expand )
466          graph.expand();
467        else if ( source == recompute )
468          graph.recompute();
469        else if ( source == redraw )
470          graph.redraw();
471        else if ( source == select )
472          graph.select();
473        else if ( source == selectAll )
474          graph.selectAll();
475        else if ( source == hide )
476          graph.hide();
477        else if ( source == show )
478          graph.show();
479    
480        else if ( source == help )
481          Help();
482        else if ( source == about )
483          About();
484      }
485 
486      public void itemStateChanged(ItemEvent event) {
487        Object source = event.getSource();
488 
489        if ( source == onOff ) {
490            boolean b = ((JCheckBoxMenuItem)onOff).getState();
491            animation_speed.setEnabled(b);
492            letterAnim.setEnabled(b);
493            arrowAnim.setEnabled(b);
494            Animation(b);
495        }
496      }
497 
498      public void update(int mode) {
499        boolean b = mode == SocietyTool.this.PLAYBACK;
500 
501        cc.setEnabled(!b);
502        update.setEnabled(!b);
503 
504        forward.setEnabled(b);
505        rewind.setEnabled(b);
506        fforward.setEnabled(b);
507        frewind.setEnabled(b);
508        stop.setEnabled(b);
509        forward_step.setEnabled(b);
510        rewind_step.setEnabled(b);
511        forward_last.setEnabled(b);
512        rewind_first.setEnabled(b);
513      }
514     }
515     
516     private void debug (String str) { 
517           //  System.out.println("SocietyTool>> " + str); 
518           }
519 }