KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TableFormDemo


1 /*
2  * $Id: TableFormDemo.java,v 1.4 2005/01/27 14:40:31 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 /**
9  * JavaOne 2004 JDNC demo which loads bug records into a DefaultTableModelExt
10  * from a .tsv file, displays a summary view of those records in a JNTable
11  * and enables the selected bug record to be edited in a JNForm.
12  *
13  * @author Amy Fowler
14  * @version 1.0
15  */

16
17 import java.net.*;
18 import java.text.*;
19 import java.util.*;
20 import java.util.regex.*;
21
22 import java.awt.*;
23 import javax.swing.*;
24 import javax.swing.table.*;
25
26 import org.jdesktop.jdnc.*;
27 import org.jdesktop.swing.*;
28 import org.jdesktop.swing.binding.BindException;
29 import org.jdesktop.swing.data.*;
30 import org.jdesktop.swing.decorator.*;
31 import org.jdesktop.swing.form.*;
32 import org.jdesktop.swing.table.*;
33
34 public class TableFormDemo {
35     public static final Color BACKGROUND = new Color(238, 238, 238);
36     public static final Color BACKGROUND2 = new Color(236, 236, 237);
37     public static final Color BACKGROUND3 = new Color(254, 254, 254);
38
39     protected Application app = Application.getInstance();
40     protected DefaultTableModelExt bugData = null;
41
42     public TableFormDemo() {
43         this(null);
44     }
45
46     public TableFormDemo(String JavaDoc urlString) {
47         try {
48             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
49         }
50         catch (Exception JavaDoc e) {
51         }
52
53         try {
54             bugData = initializeData(urlString);
55         }
56         catch (MalformedURLException e) {
57             JOptionPane.showMessageDialog( (Window) app.getWindows().next(),
58                                           e.getMessage(),
59                                           "Data Initialization Error",
60                                           JOptionPane.ERROR_MESSAGE);
61         }
62         initializeGUI(bugData);
63     }
64
65     protected DefaultTableModelExt initializeData(String JavaDoc urlString) throws
66         MalformedURLException {
67
68         DefaultTableModelExt data = new DefaultTableModelExt(getDataURL(urlString));
69
70         /******************************************************
71          * Explicitly initialize metadata *before* loading data
72          ******************************************************/

73         data.setColumnCount(8);
74
75         // column0:"bugid"
76
MetaData metaData = new MetaData("bugid", String JavaDoc.class, "Bug ID");
77         metaData.setReadOnly(true);
78         data.setColumnMetaData(0, metaData);
79
80         // column1:"priority"
81
NumberMetaData numberMetaData = new NumberMetaData("priority",
82             Integer JavaDoc.class, "Priority");
83         numberMetaData.setMinimum(new Integer JavaDoc(1));
84         numberMetaData.setMaximum(new Integer JavaDoc(5));
85         numberMetaData.setRequired(true);
86         data.setColumnMetaData(1, numberMetaData);
87
88         // column2:"severity"
89
numberMetaData = new NumberMetaData("severity", Integer JavaDoc.class,
90                                             "Severity");
91         numberMetaData.setReadOnly(true);
92         data.setColumnMetaData(2, numberMetaData);
93
94         // column3:"engineer"
95
StringMetaData stringMetaData = new StringMetaData("engineer",
96             "Engineer");
97         stringMetaData.setMaxLength(36);
98         data.setColumnMetaData(3, stringMetaData);
99
100         // column4:"dispatchdate"
101
metaData = new MetaData("dispatchdate", Date.class, "Dispatched");
102         metaData.setReadOnly(true);
103         metaData.setDecodeFormat(new SimpleDateFormat(
104             "EEE MMM dd HH:mm:ss z yyyy"));
105         metaData.setEncodeFormat(new SimpleDateFormat("MMM dd, yyyy"));
106         data.setColumnMetaData(4, metaData);
107
108         // column5:"jdcvotes"
109
numberMetaData = new NumberMetaData("jdcvotes", Integer JavaDoc.class,
110                                             "JDC Votes");
111         numberMetaData.setReadOnly(true);
112         data.setColumnMetaData(5, numberMetaData);
113
114         // column6:"synopsis"
115
stringMetaData = new StringMetaData("synopsis", "Synopsis");
116         stringMetaData.setRequired(true);
117         stringMetaData.setMaxLength(64);
118         stringMetaData.setDisplayWidth(64);
119         data.setColumnMetaData(6, stringMetaData);
120
121         // column7:"state"
122
EnumeratedMetaData enumMetaData = new EnumeratedMetaData("state",
123             String JavaDoc.class, "State");
124         enumMetaData.setRequired(true);
125         String JavaDoc states[] = {
126             "dispatched", "accepted", "evaluated", "fixed", "integrated",
127             "verified", "closed"};
128         enumMetaData.setEnumeration(states);
129         data.setColumnMetaData(7, enumMetaData);
130
131         /******************************
132          * Kickoff asynchronous loading
133          ******************************/

134         startLoadingData(data);
135
136         return data;
137     }
138
139     protected URL getDataURL(String JavaDoc urlString) throws MalformedURLException {
140         if (urlString != null) {
141             return new URL(urlString);
142         }
143         // fallback to hardcoded url values
144
URL baseURL = app.getBaseURL(); // will be non-null if deployed from applet or webstart
145
URL dataURL = dataURL = baseURL != null ?
146             new URL(baseURL, "resources/bugs.tsv") :
147             new URL("file:" + System.getProperty("user.dir") +
148                     "/src/demo/resources/bugs.tsv");
149         return dataURL;
150     }
151
152     protected void startLoadingData(DefaultTableModelExt data) {
153         try {
154             data.startLoading();
155         }
156         catch (Exception JavaDoc e) {
157             JOptionPane.showMessageDialog( (Window) app.getWindows().next(),
158                                           e.getMessage(),
159                                           "Data Loading Error",
160                                           JOptionPane.ERROR_MESSAGE);
161         }
162     }
163
164     protected void initializeGUI(DefaultTableModelExt data) {
165
166         JXFrame frame = new JXFrame("Bug Editor");
167         app.registerWindow(frame);
168
169         JXRootPane rootPane = frame.getRootPaneExt();
170         // note: JXFrame should have a setToolBar() method
171
JToolBar toolbar = new JToolBar();
172         rootPane.setToolBar(toolbar);
173
174         JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
175         splitPane.setBackground(BACKGROUND);
176         frame.getContentPane().add(BorderLayout.CENTER, splitPane);
177
178         JNTable table = initializeTable(data);
179         splitPane.setTopComponent(table);
180         setupFiltering(table, toolbar);
181
182         JNForm form = initializeForm(table, data);
183         splitPane.setBottomComponent(form);
184
185         frame.pack();
186         frame.show();
187     }
188
189     protected JNTable initializeTable(DefaultTableModelExt data) {
190
191         JNTable table = new JNTable();
192         table.setBackground(BACKGROUND);
193         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
194         table.setEvenRowBackground(BACKGROUND3);
195         table.setShowHorizontalLines(false);
196         table.setHasColumnControl(true);
197         table.setPreferredVisibleRowCount(12);
198
199         table.setHeaderBackground(BACKGROUND2);
200
201         // need to create our own column objects because we want a *re-ordered subset*
202
// of those in the model; this is cumbersome -- we need something cleaner
203
// to avoid forcing the jtable access and direct TableColumn creation.
204
JTable jtable = table.getTable();
205         jtable.setAutoCreateColumnsFromModel(false);
206         table.setModel(data);
207
208         // "BugID"
209
TableColumnExt column = new TableColumnExt(0);
210         column.setIdentifier("bugid");
211         table.addColumn(column);
212         table.setColumnHorizontalAlignment("bugid", JLabel.CENTER);
213
214         // "State"
215
column = new TableColumnExt(7);
216         column.setIdentifier("state");
217         table.addColumn(column);
218         table.setColumnHorizontalAlignment("state", JLabel.CENTER);
219         table.setColumnPrototypeValue("state", "evaluated");
220
221         // "Priority"
222
column = new TableColumnExt(1);
223         column.setIdentifier("priority");
224         table.addColumn(column);
225         table.setColumnPrototypeValue("priority", new Integer JavaDoc(1));
226
227         HashMap enumMap = new HashMap(); // map priority values to rendering properties
228
enumMap.put("1", createLabelProperties(" ", "resources/p1.gif",
229                                                JLabel.CENTER, JLabel.LEADING));
230         enumMap.put("2", createLabelProperties(" ", "resources/p2.gif",
231                                                JLabel.CENTER, JLabel.LEADING));
232         enumMap.put("3", createLabelProperties(" ", "resources/p3.gif",
233                                                JLabel.CENTER, JLabel.LEADING));
234         enumMap.put("4", createLabelProperties(" ", "resources/p4.gif",
235                                                JLabel.CENTER, JLabel.LEADING));
236         enumMap.put("5", createLabelProperties(" ", "resources/p5.gif",
237                                                JLabel.CENTER, JLabel.LEADING));
238         setupEnumRenderer(column, enumMap);
239
240         // "Severity"
241
column = new TableColumnExt(2);
242         column.setIdentifier("severity");
243         table.addColumn(column);
244         table.setColumnHorizontalAlignment("severity", JLabel.CENTER);
245         table.setColumnPrototypeValue("severity", new Integer JavaDoc(5));
246
247         enumMap = new HashMap(); // map severity values to rendering properties
248
enumMap.put("1", createLabelProperties("severe", "resources/s1.gif",
249                                                JLabel.TRAILING, JLabel.LEADING));
250         enumMap.put("2", createLabelProperties("serious", "resources/s2.gif",
251                                                JLabel.TRAILING, JLabel.LEADING));
252         enumMap.put("3", createLabelProperties("significant", "resources/s3.gif",
253                                                JLabel.TRAILING, JLabel.LEADING));
254         enumMap.put("4", createLabelProperties("normal", "resources/s4.gif",
255                                                JLabel.TRAILING, JLabel.LEADING));
256         enumMap.put("5", createLabelProperties("insignificant", "resources/s5.gif",
257                                                JLabel.TRAILING, JLabel.LEADING));
258         setupEnumRenderer(column, enumMap);
259
260         // "Synopsis"
261
column = new TableColumnExt(6);
262         column.setIdentifier("synopsis");
263         table.addColumn(column);
264         table.setColumnPrototypeValue("synopsis",
265             "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
266
267         // "Engineer"
268
column = new TableColumnExt(3);
269         column.setIdentifier("engineer");
270         table.addColumn(column);
271         table.setColumnPrototypeValue("engineer",
272                                       "xxxxxxxxxxxxxxxxx");
273
274         return table;
275     }
276
277     protected LabelProperties createLabelProperties(String JavaDoc text, String JavaDoc iconPath,
278                                                  int alignment, int textPosition) {
279         LabelProperties props = new LabelProperties();
280         props.setHorizontalAlignment(alignment);
281         props.setText(text);
282         props.setHorizontalTextPosition(textPosition);
283         props.setIcon(new ImageIcon(TableFormDemo.class.getResource(iconPath)));
284         return props;
285     }
286
287     protected void setupEnumRenderer(TableColumnExt column,
288                                      final HashMap enumMap) {
289         // need to make this easier in either JNTable and possibly JXTable
290
column.setCellRenderer(new DefaultTableCellRenderer() {
291             public Component getTableCellRendererComponent(JTable table,
292                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
293                 int column) {
294                 JLabel label = (JLabel)super.getTableCellRendererComponent(
295                     table, value, isSelected, hasFocus, row, column);
296                 // RG: value could be null
297
LabelProperties props = value == null ? null :
298                     (LabelProperties) enumMap.get(value.toString());
299                 if (props != null) {
300                     props.applyPropertiesTo(label);
301                 }
302                 return label;
303             }
304         });
305     }
306
307     protected void setupFiltering(JNTable table, JToolBar toolbar) {
308
309         Filter filters[] = new Filter[1];
310         filters[0] = new PatternFilter("", Pattern.CASE_INSENSITIVE, 6);
311         table.setFilters(new FilterPipeline(filters));
312
313         JXSearchPanel searchPanel = new JXSearchPanel();
314         searchPanel.setPatternFilter( (PatternFilter) filters[0]);
315         searchPanel.setTargetComponent(table.getTable());
316         toolbar.add(searchPanel);
317     }
318
319     protected JNForm initializeForm(JNTable table, DefaultTableModelExt data) {
320
321         JNForm form = new JNForm();
322         form.setBackground(BACKGROUND3);
323
324         // ensure table selection will automatically load selected record into form
325
TableModelExtAdapter adapter = new TableModelExtAdapter(data);
326         new RowSelector(table.getTable(), adapter);
327
328         try {
329             form.bind(adapter);
330         }
331         catch (BindException e) {
332             e.printStackTrace();
333         }
334         return form;
335     }
336
337     public static void main(String JavaDoc args[]) {
338         TableFormDemo demo = new TableFormDemo();
339     }
340
341 }
Popular Tags