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  package zeus.agentviewer;
25  
26  import javax.swing.*;
27  import javax.swing.event.*;
28  import java.awt.*;
29  import java.awt.event.*;
30  import java.util.*;
31  
32  
33  import zeus.util.*;
34  import zeus.concepts.*;
35  import zeus.agents.ZeusAgentUI;
36  import zeus.gui.help.*;
37  import zeus.agents.ZeusAgent;
38  import zeus.actors.event.*;
39  import zeus.actors.*;
40  import zeus.agentviewer.mail.*;
41  import zeus.agentviewer.msghandler.*;
42  
43  
44  
45  public class BasicAgentViewer extends JFrame implements ActionListener,
46                                              ComponentListener,
47                                              ZeusAgentUI
48  {
49  
50  
51     JPanel contentPane;
52     public JPanel topPanel;
53     InternalFramesPanel bottomPanel;
54     String fsep = System.getProperty("file.separator");
55     String IMAGEPATH = SystemProps.getProperty("gif.dir") + "agentviewer" + fsep;
56     final int ICONh  = 40;
57     final int ICONw =35;
58     final int HEADERw = 165;
59     final int HEADERh = 43;
60  
61  
62  
63      private JMenuBar           menuBar;
64      private JMenu              fileMenu, helpMenu;
65      private JMenu              viewMenu;
66      private JMenuItem          exit;
67      private JMenuItem          about, aboutZeus;
68      private JMenuItem          tile, cascade;
69      private JMenuItem          nativeLF, metalLF;
70      private JToolBar toolBar;
71  
72      private AgentButton mailInBtn,mailOutBtn, msgHandlerBtn;
73      AgentContext context = null;
74      Thread agentViewerThread;
75      MailInTableModel mailInBuffer;
76      MailOutTableModel mailOutBuffer;
77      MsgHandlerTableModel msgHandlerBuffer;
78  
79  
80  //------------------------------------------------------------------------------
81        public BasicAgentViewer() {
82  
83            super("Basic Zeus Agent Viewer");
84  
85  
86            createMenus();
87            ImageIcon icon = new ImageIcon(IMAGEPATH + ViewerNames.VIEWER_IMG);
88            setIconImage(icon.getImage());
89            setJMenuBar(menuBar);
90            setContentPane();
91            getNativeUI();
92            addWindowListener(
93             new WindowAdapter() {
94                public void windowClosing(WindowEvent evt) { exitBtnFn(); }
95               }
96            );
97  
98            addComponentListener(this);
99  
100           setVisible(true);
101           setSize(600,500);
102           pack();
103 
104       }
105 //------------------------------------------------------------------------------
106 
107      public void showHelp()
108      {
109         HelpWindow helpWin;
110         Point dispos = getLocation();
111         helpWin = new HelpWindow(this, dispos, "visualiser", "Agent Viewer");
112         helpWin.setSize(new Dimension(600, getHeight()));
113         helpWin.setLocation(dispos.x+24, dispos.y+24);
114         helpWin.validate();
115      }
116 //------------------------------------------------------------------------------
117      public void About()
118      {
119        HelpWindow helpWin;
120        Point dispos = getLocation();
121        helpWin = new HelpWindow(this, dispos, "visualiser", "About");
122        helpWin.setSize(new Dimension(440, 440));
123        helpWin.setLocation(dispos.x+24, dispos.y+24);
124        helpWin.setTitle("About ZEUS ...");
125 		   helpWin.validate();
126      }
127 
128 
129 //------------------------------------------------------------------------------
130      void setMailInBox(){
131        mailInBuffer = new MailInTableModel(context);
132      }
133 //------------------------------------------------------------------------------
134      void setMailOutBox(){
135        mailOutBuffer = new MailOutTableModel(context);
136      }
137 //------------------------------------------------------------------------------
138      void setMsgHandlerBox(){
139        msgHandlerBuffer = new MsgHandlerTableModel(context);
140      }
141 //------------------------------------------------------------------------------
142        public void showMsg(String message) {
143        }
144 //------------------------------------------------------------------------------
145       public void set(AgentContext context) {
146         Assert.notNull(context);
147         this.context = context;
148         this.setTitle(context.whoami());
149         setMailInBox();
150         setMailOutBox();
151         setMsgHandlerBox();
152       }
153 //------------------------------------------------------------------------------
154     private void getNativeUI(){
155 
156          String pathsep = System.getProperty("path.separator");
157          System.out.println("Choosing native look & feel ....");
158          try {
159            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
160            SwingUtilities.updateComponentTreeUI(this);
161          }
162          catch (Exception exc) {
163            System.out.println("Error getting UI");
164          }
165 
166      }
167 //------------------------------------------------------------------------------
168      public void componentResized(ComponentEvent e) {
169 
170         if (bottomPanel.tileOn())
171           bottomPanel.tile();
172         else
173           bottomPanel.cascade();
174 
175      }
176 //------------------------------------------------------------------------------
177       public void componentMoved(ComponentEvent e) { }
178 //------------------------------------------------------------------------------
179       public void componentShown(ComponentEvent e) { }
180 //------------------------------------------------------------------------------
181       public void componentHidden(ComponentEvent e) { }
182 //------------------------------------------------------------------------------
183       private void setContentPane(){
184 
185           bottomPanel = new InternalFramesPanel(this);
186 
187           toolBar = new JToolBar(SwingConstants.VERTICAL);
188           toolBar.setFloatable(false);
189           JPanel leftPanel = new JPanel(new GridLayout(1,1));
190           leftPanel.add(toolBar);
191           leftPanel.setBorder(BorderFactory.createEtchedBorder(Color.red,Color.black));
192           addButtonsToToolBar();
193 
194           contentPane = (JPanel) getContentPane();
195           contentPane.setLayout(new BorderLayout());
196           contentPane.add(BorderLayout.WEST,leftPanel);
197           contentPane.add(BorderLayout.CENTER,bottomPanel);
198 
199 
200       }
201 //------------------------------------------------------------------------------
202       private void createMenus(){
203 
204           menuBar = new JMenuBar();
205 
206           fileMenu = new JMenu("File");
207 
208           exit = new JMenuItem("Exit");
209           exit.addActionListener(this);
210           fileMenu.add(exit);
211           menuBar.add(fileMenu);
212 
213 
214           viewMenu = new JMenu("View");
215           tile = new JMenuItem("Tile");
216           tile.addActionListener(this);
217           viewMenu.add(tile);
218           cascade = new JMenuItem("Cascade");
219           cascade.addActionListener(this);
220           viewMenu.add(cascade);
221           viewMenu.addSeparator();
222           nativeLF = new JMenuItem("Native Look & Feel");
223           nativeLF.addActionListener(this);
224           viewMenu.add(nativeLF);
225           metalLF = new JMenuItem("Metal Look & Feel");
226           metalLF.addActionListener(this);
227           viewMenu.add(metalLF);
228           menuBar.add(viewMenu);
229 
230           menuBar.add(Box.createHorizontalGlue());
231 
232           helpMenu = new JMenu("Help");
233           about = new JMenuItem("Using the Agent Viewer");
234           about.addActionListener(this);
235           helpMenu.add(about);
236           aboutZeus = new JMenuItem("About Zeus");
237           aboutZeus.addActionListener(this);
238           helpMenu.add(aboutZeus);
239           menuBar.add(helpMenu);
240       }
241 //------------------------------------------------------------------------------
242       public void actionPerformed(ActionEvent evt) {
243          Object source = evt.getSource();
244 
245          // --- look and feel
246          if ( source == nativeLF   ) {
247            getNativeUI();
248            return;
249          }
250          else if ( source == metalLF   ) {
251             try {
252                System.out.println("Metal look & feel chosen");
253                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
254                SwingUtilities.updateComponentTreeUI(this);
255             }
256             catch (Exception exc) {
257                System.out.println("Error getting UI");
258             }
259            return;
260          }
261 
262          // others
263          if (context != null) {
264             if ( source == exit ) exitBtnFn();
265             else if ( source == tile   ) bottomPanel.tile();
266             else if ( source == cascade   ) bottomPanel.cascade();
267             else if ( source == about   ) {
268             }
269             else if (source == mailInBtn) {
270                 new MailInTableUI(bottomPanel,mailInBuffer);
271             }
272             else if (source == mailOutBtn) {
273                new MailOutTableUI(bottomPanel,mailOutBuffer);
274             }
275             else if (source == msgHandlerBtn) {
276                 new MsgHandlerTableUI(bottomPanel,msgHandlerBuffer);
277             }
278          }
279          else {
280            JOptionPane.showMessageDialog(this,"No associated agent","Error Message",
281                                          JOptionPane.OK_OPTION);
282          }
283 
284 
285       }
286 
287 //------------------------------------------------------------------------------
288       private void exitBtnFn() { System.exit(0); }
289 
290 
291 //------------------------------------------------------------------------------
292      Icon getIcon(String imgFile, int w, int h){
293 
294        String  imgStr = new String(IMAGEPATH + imgFile);
295        Image aImg = Toolkit.getDefaultToolkit().getImage(imgStr);
296        aImg = aImg.getScaledInstance(w,h,Image.SCALE_SMOOTH);
297        Icon aIcon = new ImageIcon(aImg);
298 
299        return aIcon;
300      }
301 
302 //------------------------------------------------------------------------------
303     private void addButtonsToToolBar(){
304 
305       JLabel header = new JLabel(getIcon(ViewerNames.HEADER_IMG, HEADERw, HEADERh));
306       toolBar.add(header);
307 
308       toolBar.add(Box.createRigidArea(new Dimension(10,30)));
309       mailInBtn = new AgentButton(ViewerNames.MAIL_IN,
310                                   getIcon(ViewerNames.MAILIN_IMG,ICONw,ICONh));
311       mailInBtn.addActionListener(this);
312       toolBar.add(mailInBtn);
313 
314 
315       mailOutBtn = new AgentButton(ViewerNames.MAIL_OUT,
316                                    getIcon(ViewerNames.MAILOUT_IMG,ICONw,ICONh));
317       mailOutBtn.addActionListener(this);
318       toolBar.add(mailOutBtn);
319 
320      }
321 //------------------------------------------------------------------------------
322       public static void main(String[] args) {
323            new BasicAgentViewer();
324       }
325 
326 }