View Javadoc

1   /*
2    * @(#) LoggerSelectFrame.java Nov 18, 2003 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.logger.gui;
8   
9   import java.awt.BorderLayout;
10  import java.awt.Dimension;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  
14  import javax.swing.JButton;
15  import javax.swing.JComboBox;
16  import javax.swing.JFrame;
17  import javax.swing.JPanel;
18  
19  import nl.tudelft.simulation.event.EventInterface;
20  import nl.tudelft.simulation.event.EventListenerInterface;
21  import nl.tudelft.simulation.event.util.EventProducingMap;
22  import nl.tudelft.simulation.logger.Logger;
23  
24  /***
25   * A LoggerSelectFrame <br>
26   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a
29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
31   * General Public License (LGPL) </a>, no warranty.
32   * 
33   * @version $Revision: 1.6 $ $Date: 2005/07/04 12:22:38 $
34   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>,, <a
35   *         href="mailto:nlang@fbk.eur.nl">Niels Lang </a>
36   */
37  public class LoggerSelectFrame extends JFrame implements EventListenerInterface
38  {
39      /*** the loggerChooser */
40      private JComboBox loggerChooser;
41  
42      /*** the openButton */
43      private JButton openButton = new JButton("Open");
44  
45      /***
46       * constructs a new LoggerSelectFrame
47       */
48      public LoggerSelectFrame()
49      {
50          super("Logger selection Frame");
51          this.initialize();
52      }
53  
54      /***
55       * initializes the frame
56       */
57      private void initialize()
58      {
59          JPanel contentPane = new JPanel(new BorderLayout());
60          this.loggerChooser = new JComboBox(Logger.getLoggerNames());
61          contentPane.setPreferredSize(new Dimension(300,
62                  (int) this.loggerChooser.getPreferredSize().getHeight()));
63          Logger.LOGGERS.addListener(this, EventProducingMap.OBJECT_ADDED_EVENT);
64          Logger.LOGGERS
65                  .addListener(this, EventProducingMap.OBJECT_REMOVED_EVENT);
66          this.openButton.addActionListener(new MyActionListener(
67                  this.loggerChooser));
68          contentPane.add(this.loggerChooser, BorderLayout.CENTER);
69          contentPane.add(this.openButton, BorderLayout.EAST);
70          this.setContentPane(contentPane);
71          pack();
72          setVisible(true);
73      }
74  
75      /***
76       * @see nl.tudelft.simulation.event.EventListenerInterface
77       *      #notify(nl.tudelft.simulation.event.EventInterface)
78       */
79      public synchronized void notify(final EventInterface event)
80      {
81          if (event.getType().equals(EventProducingMap.OBJECT_ADDED_EVENT)
82                  || event.getType().equals(
83                          EventProducingMap.OBJECT_REMOVED_EVENT))
84          {
85              String[] names = Logger.getLoggerNames();
86              this.loggerChooser.removeAllItems();
87              for (int i = 0; i < names.length; i++)
88              {
89                  this.loggerChooser.addItem(names[i]);
90              }
91              this.repaint();
92          }
93      }
94  
95      /***
96       * A MyActionListener
97       */
98      private class MyActionListener implements ActionListener
99      {
100         /*** the loggerChooser */
101         private JComboBox loggerChooser = null;
102 
103         /***
104          * creates a new MyActionListener
105          * 
106          * @param loggerChooser the chooser
107          */
108         public MyActionListener(final JComboBox loggerChooser)
109         {
110             this.loggerChooser = loggerChooser;
111         }
112 
113         /***
114          * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
115          */
116         public void actionPerformed(final ActionEvent actionEvent)
117         {
118             if (this.loggerChooser.getSelectedItem() != null)
119             {
120                 String logger = (String) this.loggerChooser.getSelectedItem();
121                 new LoggerFrame(java.util.logging.Logger.getLogger(logger));
122             } else
123             {
124                 Logger.warning(this, "actionPerformed", actionEvent
125                         .getActionCommand()
126                         + " on empty logger");
127             }
128         }
129     }
130 }