1 /*
2 J-Bird net/sourceforge/jbird/swing/JTableDialog.java
3
4 Copyright 2001, 2002, 2003 Dick Repasky
5 */
6 package net.sourceforge.jbird.swing;
7
8 import java.awt.Container;
9 import java.awt.Dialog;
10 import java.awt.Dimension;
11 import java.awt.Frame;
12 import java.awt.Toolkit;
13 import java.awt.event.WindowAdapter;
14 import java.awt.event.WindowEvent;
15
16 import javax.swing.JDialog;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.table.TableModel;
20
21 import net.sourceforge.jbird.iface.Nullify;
22
23 /**
24 * A simple class for instantly obtaining a JDialog that presents
25 * a JTable.
26 * <p>
27 * The class provides a mechanism for nullifying objects referenced
28 * by the class when the dialog is dismissed. Call setNullifyOnClose
29 * to cause the class' nullify method to be invoked upon closing.
30 * That will nullify fields held within the class. No effort is
31 * made to nullify the current table or its table data model.
32 * To nullify objects that implement the Nullify interface, call
33 * setToNullify. The objects will be nullified whenever the nullify
34 * method is invoked.
35 * @author Dick Repasky
36 * @since J-Bird 0.1.2
37 */
38
39 public class JTableDialog extends JDialog {
40
41 protected JScrollPane scroller;
42 protected JTable table;
43
44 /** Boolean flag that indicates whether nullify method will
45 be called automatically on window close.
46 @since J-Bird 0.3.0 */
47 protected boolean nullify_on_close = false;
48 /** An array of objects that implement the Nullify interface
49 to be nullified when the nullify method of
50 this class is invoked. Use setToNullify
51 to populate. @since J-Bird 0.3.0 */
52 Nullify[] to_nullify;
53
54 /** Basic instantiation. Configure with either
55 setTable or setModel (for which
56 a JTable object will automatically be
57 generated. @since J-Bird 0.3.0 */
58 public JTableDialog(Frame owner) {
59 super(owner);
60 setupDefaultTable();
61 setupCommon();
62 }
63
64 /** Basic instantiation. Configure with either
65 setTable or setModel (for which
66 a JTable object will automatically be
67 generated. @since J-Bird 0.3.0 */
68 public JTableDialog(Dialog owner) {
69 super(owner);
70 setupDefaultTable();
71 setupCommon();
72 }
73
74 public JTableDialog(Frame owner, JTable table) {
75 super(owner);
76 setupContent();
77 setTable(table);
78 setupCommon();
79 }
80
81 /** @since J-Bird 0.3.0 */
82 public JTableDialog(Dialog owner, JTable table) {
83 super(owner);
84 setupContent();
85 setTable(table);
86 setupCommon();
87 }
88
89 public JTableDialog(Frame owner, TableModel model) {
90 super(owner);
91 table = new JTable(model);
92 scroller = new JScrollPane(table);
93 Container contain = getContentPane();
94 contain.add(scroller);
95 setupCommon();
96 }
97
98 /** @since J-Bird 0.3.0 */
99 public JTableDialog(Dialog owner, TableModel model) {
100 super(owner);
101 table = new JTable(model);
102 scroller = new JScrollPane(table);
103 Container contain = getContentPane();
104 contain.add(scroller);
105 setupCommon();
106 }
107
108 /** Use for tables that are already in a JScrollPane. Pass
109 the table itself, too, so that the table can be contacted.
110 @since J-Bird 0.1.3 */
111 public JTableDialog(Frame owner, JScrollPane scrollpane, JTable table) {
112 super(owner);
113 this.table = table;
114 scroller = scrollpane;
115 Container contain = getContentPane();
116 contain.add(scroller);
117 setupCommon();
118 }
119
120 /** Use for tables that are already in a JScrollPane. Pass
121 the table itself, too, so that the table can be contacted.
122 @since J-Bird 0.3.0 */
123 public JTableDialog(Dialog owner, JScrollPane scrollpane, JTable table){
124 super(owner);
125 this.table = table;
126 scroller = scrollpane;
127 Container contain = getContentPane();
128 contain.add(scroller);
129 setupCommon();
130 }
131
132 /**
133 * Indicates whether nullify is called when the window
134 * closes. By default it will not.
135 * @since J-Bird 0.3.0
136 */
137
138 public final boolean isNullifyOnClose() {
139 return nullify_on_close;
140 }
141
142 /**
143 * Set the dialog to the given size and center
144 * it on the the screen.
145 *
146 */
147
148 public final void center(int width, int height) {
149 Dimension screendim = Toolkit.getDefaultToolkit()
150 .getScreenSize();
151 int xoffset = (screendim.width - width) / 2;
152 int yoffset = (screendim.height - height) / 2;
153 setSize(new Dimension(width, height));
154 setLocation(xoffset, yoffset);
155 }
156
157 public final void center(Dimension dim) {
158 Dimension screendim = Toolkit.getDefaultToolkit()
159 .getScreenSize();
160 int xoffset = (screendim.width - dim.width) / 2;
161 int yoffset = (screendim.height - dim.height) / 2;
162 setSize(dim);
163 setLocation(xoffset, yoffset);
164 }
165
166 public TableModel getModel() {
167 return table.getModel();
168 }
169
170 public JTable getTable() {
171 return table;
172 }
173
174 /**
175 * Called automatically when window-close icon pushed if
176 * setNullifyOnClose has been called with argument true.
177 * Can also be called manually.
178 * @since J-Bird 0.3.0
179 */
180
181 public void nullify() {
182 scroller = null;
183 table = null;
184 if (to_nullify != null) {
185 for (int idx = 0; idx < to_nullify.length; idx ++) {
186 to_nullify[idx].nullify();
187 to_nullify[idx] = null;
188 }
189 to_nullify = null;
190 }
191 }
192
193 /**
194 * Launch nullify method as a separate thread.
195 * @since J-Bird 0.3.0
196 */
197
198 public void nullifyThread() {
199 Thread thread = new Thread() {
200 public void run() {
201 nullify();
202 System.gc();
203 }
204 };
205 thread.setPriority(Thread.MIN_PRIORITY);
206 thread.run();
207 }
208
209 /**
210 * Use the TableModel provided. Note that
211 * JTableDialog does not yet have JTable
212 * object, it will generate one to display
213 * the model.
214 */
215
216 public final void setModel(TableModel model) {
217 if (table == null) {
218 setTable(new JTable(model));
219 } else {
220 table.setModel(model);
221 }
222 }
223
224 /**
225 * If true the nullify method will be called
226 * automatically when when close icon is pushed.
227 * @since J-Bird 0.3.0
228 *
229 */
230
231 public final void setNullifyOnClose(boolean to) {
232 nullify_on_close = to;
233 }
234
235 /**
236 * Replace the current scroller that also contains the
237 * given table with that given. OK to use if no scroller
238 * and table have been set.
239 * @since J-Bird 0.3.0
240 */
241
242 public final void setScrollerTable(JScrollPane newscroller,
243 JTable newtable) {
244 Container contain = getContentPane();
245 if (scroller != null) {
246 contain.remove(scroller);
247 }
248 table = newtable;
249 scroller = newscroller;
250 contain.add(scroller);
251 }
252
253 /**
254 * Sets the viewport view of the current scroller to
255 * be the table that is passed.
256 *
257 */
258
259 public final void setTable(JTable table) {
260 this.table = table;
261 scroller.setViewportView(table);
262 }
263
264 /**
265 * Provide an array of objects that implement the Nullify
266 * interface to nullify when the nullify method is called.
267 * That will happen automatically only if setNullifyOnClose
268 * has been called with the argument true.
269 * @since J-Bird 0.3.0
270 */
271
272 public void setToNullify(Nullify[] tonullify) {
273 to_nullify = tonullify;
274 }
275
276 /////////////// protected methods below //////////////////////
277
278 protected final void setupContent() {
279 Container contain = getContentPane();
280 scroller = new JScrollPane();
281 contain.add(scroller);
282 }
283
284 protected final void setupDefaultTable() {
285 setupContent();
286 table = new JTable();
287 scroller.setViewportView(table);
288 }
289
290 /////////////// protected methods below //////////////////////
291
292 /** Common to all constructors. @since J-Bird 0.3.0 */
293 private final void setupCommon() {
294 addWindowListener(new WindowAdapter() {
295 public void windowClosing(WindowEvent e) {
296 if (nullify_on_close) {
297 nullifyThread();
298 dispose();
299 }
300 }
301 });
302 }
303
304 }