View Javadoc

1   /*
2    * @(#) IntroSpectionDialog.java Apr 15, 2004 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   
8   package nl.tudelft.simulation.introspection.gui;
9   
10  import java.awt.BorderLayout;
11  import java.awt.Dimension;
12  import java.awt.FlowLayout;
13  import java.awt.Frame;
14  import java.awt.Toolkit;
15  import java.awt.Window;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.ActionListener;
18  
19  import javax.swing.JButton;
20  import javax.swing.JDialog;
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  import javax.swing.JTable;
24  import javax.swing.ScrollPaneConstants;
25  import javax.swing.WindowConstants;
26  
27  import nl.tudelft.simulation.introspection.table.DynamicTableModel;
28  
29  /***
30   * A GUI element for presentation and manipulation of an introspected object.
31   * The dialog is 'powered' by an instance of {see ObjectJTable}. The dialog is
32   * positioned to a 'parent' window, or displayed centered if no parent window is
33   * available.
34   * <p>
35   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
36   * University of Technology </a>, the Netherlands. <br>
37   * See for project information <a
38   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
39   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
40   * General Public License (LGPL) </a>, no warranty.
41   * 
42   * @author <a
43   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
44   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
45   *         Jacobs </a>
46   * @version 1.1 Apr 15, 2004
47   * @since 1.5
48   */
49  public class IntroSpectionDialog extends JDialog
50  {
51      /*** the table, set during initialization */
52      private JTable table;
53  
54      /*** the parent window, set during initialization */
55      private Window parent;
56  
57      /***
58       * Constructs a new IntroSpectionDialog.
59       * 
60       * @param introspected The introspected object
61       */
62      public IntroSpectionDialog(final Object introspected)
63      {
64          this(null, introspected);
65      }
66  
67      /***
68       * Constructs a new IntroSpectionDialog.
69       * 
70       * @param parent The parent window, used for locating the dialog
71       * @param introspected The introspected object
72       */
73      public IntroSpectionDialog(final Window parent, final Object introspected)
74      {
75          this(parent, introspected.toString(), new ObjectJTable(
76                  new ObjectTableModel(introspected)));
77      }
78  
79      /***
80       * Constructs a new IntroSpectionDialog.
81       * 
82       * @param title The title of the frame
83       * @param introspected The introspected object
84       */
85      public IntroSpectionDialog(final Object introspected, final String title)
86      {
87          this(null, title, new ObjectJTable(new ObjectTableModel(introspected)));
88      }
89  
90      /***
91       * Constructs a new IntroSpectionDialog.
92       * 
93       * @param title The title of the dialog
94       * @param content The object table-model containing the data of the
95       *        introspected object
96       */
97      public IntroSpectionDialog(final String title,
98              final IntrospectingTableModelInterface content)
99      {
100         this(null, title, content);
101     }
102 
103     /***
104      * Constructs a new IntroSpectionDialog.
105      * 
106      * @param parent The parent window, used for locating the dialog
107      * @param title The title of the dialog
108      * @param content The object table-model containing the data of the
109      *        introspected object
110      */
111     public IntroSpectionDialog(final Window parent, final String title,
112             final IntrospectingTableModelInterface content)
113     {
114         this(parent, title, new ObjectJTable(content));
115     }
116 
117     /***
118      * Constructs a new IntroSpectionDialog.
119      * 
120      * @param parent The parent window, used for locating the dialog
121      * @param title The title of the dialog
122      * @param introspected The introspected object
123      */
124     public IntroSpectionDialog(final Frame parent, final Object introspected,
125             final String title)
126     {
127         this(parent, title,
128                 new ObjectJTable(new ObjectTableModel(introspected)));
129     }
130 
131     /***
132      * Constructs a new IntroSpectionDialog.
133      * 
134      * @param title The title of the dialog
135      * @param content The table displaying the data of the introspected object
136      */
137     public IntroSpectionDialog(final String title, final JTable content)
138     {
139         this(null, title, content);
140     }
141 
142     /***
143      * Constructs a new IntroSpectionDialog.
144      * 
145      * @param parent The parent window, used for locating the dialog
146      * @param title The title of the dialog
147      * @param content The table displaying the data of the introspected object
148      */
149     public IntroSpectionDialog(final Window parent, final String title,
150             final JTable content)
151     {
152         super();
153         this.parent = parent;
154         this.init(title, content);
155     }
156 
157     /***
158      * initializes the dialog
159      * 
160      * @param title the title of the dialog
161      * @param table the table to display
162      */
163     private void init(final String title, final JTable table)
164     {
165         this.table = table;
166         this.setModal(false);
167         this.setTitle(title);
168         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
169         this.getContentPane().setLayout(new BorderLayout());
170         JScrollPane pane = new JScrollPane(table,
171                 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
172                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
173         this.getContentPane().add(pane, BorderLayout.CENTER);
174         if (table instanceof ObjectJTableInterface)
175         {
176             if (((ObjectJTableInterface) table).getIntrospectingTableModel() instanceof DynamicTableModel)
177             {
178                 DynamicTableModel model = (DynamicTableModel) ((ObjectJTableInterface) table)
179                         .getIntrospectingTableModel();
180                 this.getContentPane().add(new ButtonPanel(model, table),
181                         BorderLayout.SOUTH);
182             }
183         }
184         this.pack();
185         setRelativeLocation();
186         this.setVisible(true);
187     }
188 
189     /***
190      * Reformats this dialog to reflect changes in the table displayed.
191      */
192     protected void formatDialog()
193     {
194         // NB PropertyChanges of height are not broadcasted by the dialog!
195         // Therefore, a static check is used instead.
196         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
197         if (this.table.getPreferredSize().height >= 0.5 * d.height
198                 || this.table.getPreferredSize().height + getLocation().y >= 0.9 * d.height)
199         {
200             return;
201         }
202         this.table.setPreferredScrollableViewportSize(this.table
203                 .getPreferredSize());
204         pack();
205     }
206 
207     /***
208      * Initializes the location of this dialog relative to its parent window if
209      * any.
210      */
211     protected void setRelativeLocation()
212     {
213         setLocationRelativeTo(this.parent);
214     }
215 
216     /***
217      * The ButtonPanel adds functionality for adding and removing rows in a
218      * table.
219      */
220     class ButtonPanel extends JPanel
221     {
222         /*** model */
223         private DynamicTableModel model;
224 
225         /*** the viewer */
226         private JTable viewer;
227 
228         /***
229          * Constructs a new ButtonPanel
230          * 
231          * @param model the model to control
232          * @param viewer the viewer to control
233          */
234         public ButtonPanel(final DynamicTableModel model, final JTable viewer)
235         {
236             this.model = model;
237             this.viewer = viewer;
238             this.setLayout(new BorderLayout());
239             JPanel buttons = new JPanel();
240             FlowLayout manager = new FlowLayout();
241             manager.setHgap(0);
242             manager.setVgap(0);
243             buttons.setLayout(manager);
244             add(buttons, BorderLayout.CENTER);
245             JButton addButton = new JButton("Add row");
246             JButton delButton = new JButton("Delete rows");
247             if (!model.isRowEditable())
248             {
249                 addButton.setEnabled(false);
250                 delButton.setEnabled(false);
251             }
252             buttons.add(addButton);
253             buttons.add(delButton);
254             addButton.addActionListener(new ActionListener()
255             {
256                 public void actionPerformed(ActionEvent e)
257                 {
258                     ButtonPanel.this.addRow();
259                     formatDialog();
260                 }
261             });
262             delButton.addActionListener(new ActionListener()
263             {
264                 public void actionPerformed(ActionEvent e)
265                 {
266                     ButtonPanel.this.delRows();
267                     formatDialog();
268                 }
269             });
270         }
271 
272         /***
273          * Adds a row
274          */
275         protected void addRow()
276         {
277             this.model.createRow();
278         }
279 
280         /***
281          * Deletes the rows currently selected from the table model.
282          */
283         protected void delRows()
284         {
285             this.model.deleteRows(this.viewer.getSelectedRows());
286         }
287     }
288 }