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  * SocietyPanel.java
26  *
27  ***************************************************************************/
28  
29  package zeus.visualiser.society;
30  
31  import java.awt.*;
32  import java.awt.event.*;
33  import java.util.*;
34  import javax.swing.*;
35  import javax.swing.border.*;
36  import javax.swing.text.*;
37  import javax.swing.event.*;
38  import java.io.File;
39  
40  import zeus.util.*;
41  import zeus.concepts.*;
42  import zeus.generator.util.*;
43  import zeus.generator.event.*;
44  import zeus.generator.agent.AcquaintanceModel;
45  import zeus.gui.graph.*;
46  import zeus.gui.help.*;
47  
48  public class SocietyPanel extends JPanel {
49    protected Graph graph;
50  
51    public SocietyPanel(SocietyModel model) {
52      graph = new Graph(Graph.VERTICAL_PARENT_CHILD,model,true,false);
53      graph.setNodeRenderer(new SocietyNodeRenderer());
54      graph.setNodeEditor(new SocietyNodeEditor());
55  
56      GridBagLayout gridBagLayout = new GridBagLayout();
57      GridBagConstraints gbc = new GridBagConstraints();
58  
59      JPanel lhsPanel = new JPanel();
60      lhsPanel.setLayout(gridBagLayout);
61      lhsPanel.setBackground(Color.lightGray);
62  
63      // Add the lhs panel
64      SoceityControlPanel controlPane = new SoceityControlPanel();
65      gbc.gridwidth = GridBagConstraints.REMAINDER;
66      gbc.anchor = GridBagConstraints.NORTHWEST;
67      gbc.fill = GridBagConstraints.NONE;
68      gbc.insets = new Insets(8,8,8,8);
69      gbc.weightx = gbc.weighty = 0;
70      gridBagLayout.setConstraints(controlPane,gbc);
71      lhsPanel.add(controlPane);
72      lhsPanel.setBackground(Color.lightGray);
73  
74      gridBagLayout = new GridBagLayout();
75      gbc = new GridBagConstraints();
76      setLayout(gridBagLayout);
77      setBackground(Color.lightGray);
78      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
79  
80      gbc.gridwidth = 1;
81      gbc.anchor = GridBagConstraints.NORTHWEST;
82      gbc.fill = GridBagConstraints.NONE;
83      gbc.insets = new Insets(0,0,0,0);
84      gbc.weightx = gbc.weighty = 0;
85      gridBagLayout.setConstraints(lhsPanel,gbc);
86      add(lhsPanel);
87  
88      // Add the panel that will contain the nodes
89      JPanel dataPanel = new JPanel();
90      gbc.gridwidth = GridBagConstraints.REMAINDER;
91      gbc.anchor = GridBagConstraints.NORTHWEST;
92      gbc.fill = GridBagConstraints.BOTH;
93      gbc.weightx = gbc.weighty = 1;
94      gbc.insets = new Insets(8,8,8,8);
95      gridBagLayout.setConstraints(dataPanel,gbc);
96      add(dataPanel);
97  
98      // Data panel info
99      gridBagLayout = new GridBagLayout();
100     dataPanel.setLayout(gridBagLayout);
101     dataPanel.setBackground(Color.lightGray);
102 
103     JScrollPane scrollPane = new JScrollPane(graph);
104     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
105     scrollPane.setPreferredSize(new Dimension(600,600));
106     graph.setPreferredSize(new Dimension(2000,2000));
107     graph.setBackground(new Color(245,230,140));
108 
109     TitledBorder border = (BorderFactory.createTitledBorder("Agent Society"));
110     border.setTitlePosition(TitledBorder.TOP);
111     border.setTitleJustification(TitledBorder.RIGHT);
112     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
113     border.setTitleColor(Color.blue);
114     dataPanel.setBorder(border);
115 
116     JToolBar toolbar = new NodesToolBar();
117 
118     gbc = new GridBagConstraints();
119     gbc.gridwidth = GridBagConstraints.REMAINDER;
120     gbc.anchor = GridBagConstraints.WEST;
121     gbc.fill = GridBagConstraints.NONE;
122     gbc.weightx = gbc.weighty = 0;
123     gbc.insets = new Insets(0,8,0,0);
124     gridBagLayout.setConstraints(toolbar,gbc);
125     dataPanel.add(toolbar);
126 
127     gbc.gridwidth = GridBagConstraints.REMAINDER;
128     gbc.anchor = GridBagConstraints.WEST;
129     gbc.fill = GridBagConstraints.BOTH;
130     gbc.weightx = gbc.weighty = 1;
131     gbc.insets = new Insets(8,8,8,8);
132     gridBagLayout.setConstraints(scrollPane,gbc);
133     dataPanel.add(scrollPane);
134   }
135 
136   Graph getGraph() { return graph; }
137 
138   protected class SocietyNodeRenderer implements GraphNodeRenderer {
139      public Component getNodeRendererComponent(Graph g, GraphNode node) {
140         SocietyModelEntry obj = (SocietyModelEntry)node.getUserObject();
141         String name = obj.getName();
142         String icon_file = obj.getIcon();
143         if ( icon_file == null )
144 	   return new JLabel(name,JLabel.CENTER);
145         else {
146            JLabel label;
147            label = new JLabel(name,new ImageIcon(icon_file),JLabel.CENTER);
148            label.setVerticalTextPosition(JLabel.BOTTOM);
149            label.setHorizontalTextPosition(JLabel.CENTER);
150            return label;
151         }
152      }
153   }
154 
155   protected class NodesToolBar extends JToolBar implements ActionListener {
156      protected JButton       selectBtn;
157      protected JButton       selectAllBtn;
158      protected JButton       hideBtn;
159      protected JButton       showBtn;
160      protected JButton       collapseBtn;
161      protected JButton       expandBtn;
162      protected JButton       deleteBtn;
163      protected JButton       recomputeBtn;
164      protected JButton       redrawBtn;
165 
166      public NodesToolBar() {
167         setBackground(Color.lightGray);
168         setBorder(new BevelBorder(BevelBorder.LOWERED));
169         setFloatable(false);
170 
171         String sep = System.getProperty("file.separator");
172         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
173 
174         // Draw Buttons
175         recomputeBtn = new JButton(new ImageIcon(path + "recompute.gif"));
176 	recomputeBtn.setMargin(new Insets(0,0,0,0));
177         add(recomputeBtn);
178         recomputeBtn.setToolTipText("Recompute node positions");
179         recomputeBtn.addActionListener(this);
180 
181         redrawBtn = new JButton(new ImageIcon(path + "redraw.gif"));
182 	redrawBtn.setMargin(new Insets(0,0,0,0));
183         add(redrawBtn);
184         redrawBtn.setToolTipText("Redraw");
185         redrawBtn.addActionListener(this);
186 
187 	addSeparator();
188 
189         selectBtn = new JButton(new ImageIcon(path + "select.gif"));
190 	selectBtn.setMargin(new Insets(0,0,0,0));
191         add(selectBtn);
192         selectBtn.setToolTipText("Select nodes");
193         selectBtn.addActionListener(this);
194 
195         selectAllBtn = new JButton(new ImageIcon(path + "selectAll.gif"));
196 	selectAllBtn.setMargin(new Insets(0,0,0,0));
197         add(selectAllBtn);
198         selectAllBtn.setToolTipText("Select all nodes");
199         selectAllBtn.addActionListener(this);
200 
201         addSeparator();
202 
203         collapseBtn = new JButton(new ImageIcon(path + "collapse.gif"));
204 	collapseBtn.setMargin(new Insets(0,0,0,0));
205         add(collapseBtn);
206         collapseBtn.setToolTipText("Collapse nodes");
207         collapseBtn.addActionListener(this);
208 
209         expandBtn = new JButton(new ImageIcon(path + "expand.gif"));
210 	expandBtn.setMargin(new Insets(0,0,0,0));
211         add(expandBtn);
212         expandBtn.setToolTipText("Expand nodes");
213         expandBtn.addActionListener(this);
214 
215         addSeparator();
216 
217         hideBtn = new JButton(new ImageIcon(path + "hide.gif"));
218 	hideBtn.setMargin(new Insets(0,0,0,0));
219         add(hideBtn);
220         hideBtn.setToolTipText("Hide nodes");
221         hideBtn.addActionListener(this);
222 
223         showBtn = new JButton(new ImageIcon(path + "show.gif"));
224 	showBtn.setMargin(new Insets(0,0,0,0));
225         add(showBtn);
226         showBtn.setToolTipText("Show nodes");
227         showBtn.addActionListener(this);
228      }
229 
230      public void actionPerformed(ActionEvent e)  {
231         Object src = e.getSource();
232         if ( src == recomputeBtn )
233            graph.recompute();
234         else if ( src == redrawBtn )
235            graph.redraw();
236         else if ( src == selectBtn )
237            graph.select();
238         else if ( src == selectAllBtn )
239            graph.selectAll();
240         else if ( src == collapseBtn )
241            graph.collapse();
242         else if ( src == expandBtn )
243            graph.expand();
244         else if ( src == hideBtn )
245            graph.hide();
246         else if ( src == showBtn )
247            graph.show();
248      }
249   }
250 
251   class SocietyNodeEditor extends AbstractGraphNodeEditor
252                           implements ActionListener {
253 
254      protected JButton button = new JButton("Click to edit");
255      protected GraphNode node;
256      protected Graph graph;
257      protected FileDialog dialog = null;
258  
259      public SocietyNodeEditor() {
260         button.setBackground(Color.lightGray);
261         button.setHorizontalAlignment(JButton.CENTER);
262         button.setBorderPainted(true);
263         button.addActionListener(this);
264         button.setOpaque(true);
265         button.setMinimumSize(new Dimension(120,30));
266         button.setPreferredSize(new Dimension(120,30));
267         button.setSize(120,30);
268      }
269  
270      public void actionPerformed(ActionEvent e) {
271         Object src = e.getSource();
272         if ( src == button ) {
273            // Note: create dialog (using button) before stopping editing
274            // which will remove the button from the visible component hierachy
275            if ( dialog == null )
276               dialog = new FileDialog((Frame)SwingUtilities.getRoot(button),
277   	       "Select Icon File",FileDialog.LOAD);
278   
279            SocietyModelEntry agent = (SocietyModelEntry)node.getUserObject();
280            String icon_file = agent.getIcon();
281   
282            if ( icon_file != null ) {
283   	    File f1 = new File(icon_file);
284               dialog.setFile(f1.getName());
285               dialog.setDirectory(f1.getParent());
286            }
287            else
288               dialog.setFile("*.gif");
289   
290            dialog.pack();
291            dialog.setVisible(true);
292   
293            String path = (dialog.getFile() == null) ? null 
294               : dialog.getDirectory() + File.separator + dialog.getFile();
295            if ( path == null )
296               fireEditAction(EDITING_CANCELLED,this.node,null);
297            else
298               fireEditAction(EDITING_STOPPED,this.node,path);
299         }
300      }
301      public Component getNodeEditorComponent(Graph graph, GraphNode gnode) {
302         this.graph = graph;
303         this.node = gnode;
304         return button;
305      }
306   }
307 
308 
309   protected class SoceityControlPanel extends JPanel implements ItemListener {
310      protected JCheckBox[]    checkbox;
311      protected JRadioButton[] view;
312 
313      public SoceityControlPanel()  {
314        String[] RELATIONS = Misc.stringArray(AcquaintanceModel.RELATIONS_LIST);
315 
316        checkbox = new JCheckBox[RELATIONS.length];
317        view = new JRadioButton[RELATIONS.length];
318 
319        JPanel topPanel = new JPanel();
320        TitledBorder border = (BorderFactory.createTitledBorder("View Style"));
321        border.setTitlePosition(TitledBorder.TOP);
322        border.setTitleJustification(TitledBorder.RIGHT);
323        border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
324        border.setTitleColor(Color.blue);
325        topPanel.setBorder(border);
326        topPanel.setBackground(Color.lightGray);
327 
328        GridBagLayout gb = new GridBagLayout();
329        GridBagConstraints gbc = new GridBagConstraints();
330        gbc.insets = new Insets(4,4,0,4);
331        gbc.gridwidth = GridBagConstraints.REMAINDER;
332        gbc.anchor = GridBagConstraints.NORTHWEST;
333        topPanel.setLayout(gb);
334 
335        ButtonGroup alignGroup = new ButtonGroup();
336        for(int i = 0; i < view.length; i++ ) {
337           if ( i == 0 )
338              view[i] = new JRadioButton("vertical",
339                 ((SocietyModel)graph.getModel()).getView() == i);
340           else if ( i == 1 )
341              view[i] = new JRadioButton("centered",
342                 ((SocietyModel)graph.getModel()).getView() == i);
343           else
344              view[i] = new JRadioButton(RELATIONS[i],
345                 ((SocietyModel)graph.getModel()).getView() == i);
346           view[i].setBackground(Color.lightGray);
347           alignGroup.add(view[i]);
348           gb.setConstraints(view[i], gbc);
349           topPanel.add(view[i]);
350        }
351 
352        JPanel bottomPanel = new JPanel();
353        border = (BorderFactory.createTitledBorder("Relational Links"));
354        border.setTitlePosition(TitledBorder.TOP);
355        border.setTitleJustification(TitledBorder.RIGHT);
356        border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
357        border.setTitleColor(Color.blue);
358        bottomPanel.setBorder(border);
359        bottomPanel.setBackground(Color.lightGray);
360 
361        gb = new GridBagLayout();
362        gbc = new GridBagConstraints();
363        bottomPanel.setLayout(gb);
364 
365        JPanel colorPanel;
366        for(int i = 0; i < checkbox.length; i++ ) {
367           checkbox[i] = new JCheckBox(RELATIONS[i]);
368           checkbox[i].setBackground(Color.lightGray);
369           colorPanel = new JPanel();
370           colorPanel.setSize(10,10);
371           colorPanel.setBackground(
372              ((SocietyModel)graph.getModel()).getColor(i)
373           );
374 
375           gbc.fill = GridBagConstraints.NONE;
376           gbc.weightx = 0;
377           gbc.anchor = GridBagConstraints.NORTHWEST;
378           gbc.insets = new Insets(4,4,0,0);
379           gbc.gridwidth = 1;
380           gb.setConstraints(checkbox[i],gbc);
381           bottomPanel.add(checkbox[i]);
382 
383           gbc.anchor = GridBagConstraints.EAST;
384           gbc.insets = new Insets(4,0,0,4);
385           gbc.gridwidth = GridBagConstraints.REMAINDER;
386           gb.setConstraints(colorPanel,gbc);
387           bottomPanel.add(colorPanel);
388        }
389 
390        JPanel lowerBottomPanel = new JPanel();
391        border = (BorderFactory.createTitledBorder("Message codes"));
392        border.setTitlePosition(TitledBorder.TOP);
393        border.setTitleJustification(TitledBorder.RIGHT);
394        border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
395        border.setTitleColor(Color.blue);
396        lowerBottomPanel.setBorder(border);
397        lowerBottomPanel.setBackground(Color.lightGray);
398 
399        gb = new GridBagLayout();
400        gbc = new GridBagConstraints();
401        lowerBottomPanel.setLayout(gb);
402 
403        JLabel label;
404        String msg_type;
405        Color color;
406        Enumeration enum = AnimationManager.MessageColor.keys();
407        while( enum.hasMoreElements() ) {
408           msg_type = (String)enum.nextElement();
409           color = (Color)AnimationManager.MessageColor.get(msg_type);
410           label = new JLabel(msg_type,JLabel.LEFT);
411           label.setBackground(Color.lightGray);
412           colorPanel = new JPanel();
413           colorPanel.setSize(10,10);
414           colorPanel.setBackground(color);
415 
416           gbc.fill = GridBagConstraints.NONE;
417           gbc.weightx = 0;
418           gbc.anchor = GridBagConstraints.NORTHWEST;
419           gbc.insets = new Insets(4,4,0,0);
420           gbc.gridwidth = 1;
421           gb.setConstraints(label,gbc);
422           lowerBottomPanel.add(label);
423 
424           gbc.anchor = GridBagConstraints.EAST;
425           gbc.insets = new Insets(4,0,0,4);
426           gbc.gridwidth = GridBagConstraints.REMAINDER;
427           gb.setConstraints(colorPanel,gbc);
428           lowerBottomPanel.add(colorPanel);
429        }
430 
431        gb = new GridBagLayout();
432        gbc = new GridBagConstraints();
433        setLayout(gb);
434        gbc.insets = new Insets(0,0,0,0);
435        gbc.gridwidth = GridBagConstraints.REMAINDER;
436        gbc.anchor = GridBagConstraints.NORTHWEST;
437        gbc.fill = GridBagConstraints.BOTH;
438        gbc.weightx = gbc.weighty = 0;
439        gb.setConstraints(topPanel,gbc);
440        add(topPanel);
441 
442        gbc.weightx = gbc.weighty = 0;
443        gb.setConstraints(bottomPanel,gbc);
444        add(bottomPanel);
445 
446        gbc.weightx = gbc.weighty = 0;
447        gb.setConstraints(lowerBottomPanel,gbc);
448        add(lowerBottomPanel);
449 
450        for(int i = 0; i < checkbox.length; i++ ) {
451           checkbox[i].setSelected(
452              ((SocietyModel)graph.getModel()).isLinkVisible(i)
453           );
454           checkbox[i].addItemListener(this);
455           view[i].addItemListener(this);
456        }
457      }
458 
459      public void itemStateChanged(ItemEvent evt) {
460        Object source = evt.getSource();
461 
462        for(int i = 0; i < checkbox.length; i++ ) {
463           if ( source == checkbox[i] ) {
464              ((SocietyModel)graph.getModel()).showLinks(i,
465                 checkbox[i].isSelected());
466              graph.redraw();
467              return;
468           }
469           if ( source == view[i] ) {
470              switch( i ) {
471                 case 0:
472                      graph.setViewMode(Graph.VERTICAL_PARENT_CHILD);
473                      break;
474                 case 1:
475                      graph.setViewMode(Graph.CENTRED);
476                      break;
477                 default:
478                      graph.setViewMode(Graph.CIRCLES);
479                      break;
480              }
481              ((SocietyModel)graph.getModel()).setView(i);
482              graph.recompute();
483              return;
484           }
485        }
486      }
487   }
488 
489 }