49298
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package com.sun.swingset3.demos.table;
|
|
25 |
|
|
26 |
import java.awt.BorderLayout;
|
|
27 |
import java.awt.Color;
|
|
28 |
import java.awt.Container;
|
|
29 |
import java.awt.Dimension;
|
|
30 |
import java.awt.GridBagConstraints;
|
|
31 |
import java.awt.GridBagLayout;
|
|
32 |
import java.awt.Insets;
|
|
33 |
import java.awt.event.ActionEvent;
|
|
34 |
import java.beans.PropertyChangeEvent;
|
|
35 |
import java.beans.PropertyChangeListener;
|
|
36 |
import java.net.URI;
|
|
37 |
import java.net.URL;
|
|
38 |
import java.util.ArrayList;
|
|
39 |
import java.util.List;
|
|
40 |
import java.util.MissingResourceException;
|
|
41 |
import java.util.ResourceBundle;
|
|
42 |
import java.util.logging.Level;
|
|
43 |
import java.util.logging.Logger;
|
|
44 |
import java.util.regex.Pattern;
|
|
45 |
|
|
46 |
import javax.swing.AbstractAction;
|
|
47 |
import javax.swing.Box;
|
|
48 |
import javax.swing.JCheckBox;
|
|
49 |
import javax.swing.JFrame;
|
|
50 |
import javax.swing.JLabel;
|
|
51 |
import javax.swing.JOptionPane;
|
|
52 |
import javax.swing.JPanel;
|
|
53 |
import javax.swing.JProgressBar;
|
|
54 |
import javax.swing.JScrollPane;
|
|
55 |
import javax.swing.JTable;
|
|
56 |
import javax.swing.JTextField;
|
|
57 |
import javax.swing.ListSelectionModel;
|
|
58 |
import javax.swing.RowFilter;
|
|
59 |
import javax.swing.UIManager;
|
|
60 |
import javax.swing.border.CompoundBorder;
|
|
61 |
import javax.swing.border.EmptyBorder;
|
|
62 |
import javax.swing.border.TitledBorder;
|
|
63 |
import javax.swing.event.ChangeEvent;
|
|
64 |
import javax.swing.event.ChangeListener;
|
|
65 |
import javax.swing.event.DocumentEvent;
|
|
66 |
import javax.swing.event.DocumentListener;
|
|
67 |
import javax.swing.event.TableModelEvent;
|
|
68 |
import javax.swing.event.TableModelListener;
|
|
69 |
import javax.swing.table.DefaultTableColumnModel;
|
|
70 |
import javax.swing.table.JTableHeader;
|
|
71 |
import javax.swing.table.TableCellRenderer;
|
|
72 |
import javax.swing.table.TableColumn;
|
|
73 |
import javax.swing.table.TableColumnModel;
|
|
74 |
import javax.swing.table.TableRowSorter;
|
|
75 |
import javax.swing.text.Document;
|
|
76 |
|
|
77 |
import com.sun.swingset3.DemoProperties;
|
|
78 |
import com.sun.swingset3.demos.DemoUtilities;
|
|
79 |
|
|
80 |
/**
|
|
81 |
*
|
|
82 |
* @author aim
|
|
83 |
*/
|
|
84 |
@DemoProperties(
|
|
85 |
value = "JTable Demo",
|
|
86 |
category = "Data",
|
|
87 |
description = "Demonstrates use of Swing's data grid component, JTable, including asynchronous loading and sorting/filtering.",
|
|
88 |
sourceFiles = {
|
|
89 |
"com/sun/swingset3/demos/table/TableDemo.java",
|
|
90 |
"com/sun/swingset3/demos/table/HyperlinkCellRenderer.java",
|
|
91 |
"com/sun/swingset3/demos/table/IMDBLink.java",
|
|
92 |
"com/sun/swingset3/demos/table/Link.java",
|
|
93 |
"com/sun/swingset3/demos/table/OscarCandidate.java",
|
|
94 |
"com/sun/swingset3/demos/table/OscarCellRenderers.java",
|
|
95 |
"com/sun/swingset3/demos/table/OscarDataParser.java",
|
|
96 |
"com/sun/swingset3/demos/table/OscarTableModel.java",
|
|
97 |
"com/sun/swingset3/demos/DemoUtilities.java",
|
|
98 |
"com/sun/swingset3/demos/JHyperlink.java",
|
|
99 |
"com/sun/swingset3/demos/table/resources/bestpicture",
|
|
100 |
//"com/sun/swingset3/demos/table/resources/oscars.xml", file too large!!
|
|
101 |
"com/sun/swingset3/demos/table/resources/TableDemo.properties",
|
|
102 |
"com/sun/swingset3/demos/table/resources/images/goldstar.png",
|
|
103 |
"com/sun/swingset3/demos/table/resources/images/nominee.png",
|
|
104 |
"com/sun/swingset3/demos/table/resources/images/TableDemo.gif"
|
|
105 |
}
|
|
106 |
)
|
|
107 |
public class TableDemo extends JPanel {
|
|
108 |
static final Logger logger = Logger.getLogger(TableDemo.class.getName());
|
|
109 |
public static final String DEMO_TITLE = TableDemo.class.getAnnotation(DemoProperties.class).value();
|
|
110 |
public static final int ROW_HEIGHT = 26;
|
|
111 |
public static final String COLUMN1_NAME = "Year";
|
|
112 |
public static final String COLUMN2_NAME = "Award Category";
|
|
113 |
public static final String COLUMN3_NAME = "Movie Title";
|
|
114 |
public static final String COLUMN4_NAME = "Nominees";
|
|
115 |
|
|
116 |
private OscarTableModel oscarModel;
|
|
117 |
|
|
118 |
private JPanel controlPanel;
|
|
119 |
private JTable oscarTable;
|
|
120 |
private JCheckBox winnersCheckbox;
|
|
121 |
private JTextField filterField;
|
|
122 |
private Box statusBarLeft;
|
|
123 |
private JLabel actionStatus;
|
|
124 |
private JLabel tableStatus;
|
|
125 |
|
|
126 |
private Color[] rowColors;
|
|
127 |
private String statusLabelString;
|
|
128 |
private String searchLabelString;
|
|
129 |
|
|
130 |
private boolean showOnlyWinners = false;
|
|
131 |
private String filterString = null;
|
|
132 |
|
|
133 |
private TableRowSorter sorter;
|
|
134 |
private RowFilter<OscarTableModel, Integer> winnerFilter;
|
|
135 |
private RowFilter<OscarTableModel, Integer> searchFilter;
|
|
136 |
|
|
137 |
// Resource bundle for internationalized and accessible text
|
|
138 |
private ResourceBundle bundle = null;
|
|
139 |
|
|
140 |
public TableDemo() {
|
|
141 |
initModel();
|
|
142 |
initComponents();
|
|
143 |
initSortingFiltering();
|
|
144 |
}
|
|
145 |
|
|
146 |
protected void initModel() {
|
|
147 |
oscarModel = new OscarTableModel();
|
|
148 |
}
|
|
149 |
|
|
150 |
protected void initComponents() {
|
|
151 |
setLayout(new BorderLayout());
|
|
152 |
|
|
153 |
controlPanel = createControlPanel();
|
|
154 |
add(controlPanel, BorderLayout.NORTH);
|
|
155 |
|
|
156 |
//<snip>Create JTable
|
|
157 |
oscarTable = new JTable(oscarModel);
|
|
158 |
//</snip>
|
|
159 |
|
|
160 |
//</snip>Set JTable display properties
|
|
161 |
oscarTable.setColumnModel(createColumnModel());
|
|
162 |
oscarTable.setAutoCreateRowSorter(true);
|
|
163 |
oscarTable.setRowHeight(ROW_HEIGHT);
|
|
164 |
oscarTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
|
|
165 |
oscarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
166 |
oscarTable.setIntercellSpacing(new Dimension(0, 0));
|
|
167 |
//</snip>
|
|
168 |
|
|
169 |
//<snip>Initialize preferred size for table's viewable area
|
|
170 |
Dimension viewSize = new Dimension();
|
|
171 |
viewSize.width = oscarTable.getColumnModel().getTotalColumnWidth();
|
|
172 |
viewSize.height = 10 * oscarTable.getRowHeight();
|
|
173 |
oscarTable.setPreferredScrollableViewportSize(viewSize);
|
|
174 |
//</snip>
|
|
175 |
|
|
176 |
//<snip>Customize height and alignment of table header
|
|
177 |
JTableHeader header = oscarTable.getTableHeader();
|
|
178 |
header.setPreferredSize(new Dimension(30, 26));
|
|
179 |
TableCellRenderer headerRenderer = header.getDefaultRenderer();
|
|
180 |
if (headerRenderer instanceof JLabel) {
|
|
181 |
((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
|
|
182 |
}
|
|
183 |
//</snip>
|
|
184 |
|
|
185 |
JScrollPane scrollpane = new JScrollPane(oscarTable);
|
|
186 |
add(scrollpane, BorderLayout.CENTER);
|
|
187 |
|
|
188 |
add(createStatusBar(), BorderLayout.SOUTH);
|
|
189 |
|
|
190 |
}
|
|
191 |
|
|
192 |
protected JPanel createControlPanel() {
|
|
193 |
JPanel controlPanel = new JPanel();
|
|
194 |
GridBagLayout gridbag = new GridBagLayout();
|
|
195 |
GridBagConstraints c = new GridBagConstraints();
|
|
196 |
controlPanel.setLayout(gridbag);
|
|
197 |
|
|
198 |
c.gridx = 0;
|
|
199 |
c.gridy = 1;
|
|
200 |
c.gridheight = 1;
|
|
201 |
c.insets = new Insets(20, 10, 0, 10);
|
|
202 |
c.anchor = GridBagConstraints.SOUTHWEST;
|
|
203 |
JLabel searchLabel = new JLabel(getString("TableDemo.searchLabel",
|
|
204 |
"Search Titles and Recipients"));
|
|
205 |
controlPanel.add(searchLabel, c);
|
|
206 |
|
|
207 |
c.gridx = 0;
|
|
208 |
c.gridy = 2;
|
|
209 |
c.weightx = 1.0;
|
|
210 |
c.insets.top = 0;
|
|
211 |
c.insets.bottom = 12;
|
|
212 |
c.anchor = GridBagConstraints.SOUTHWEST;
|
|
213 |
//c.fill = GridBagConstraints.HORIZONTAL;
|
|
214 |
filterField = new JTextField(24);
|
|
215 |
filterField.getDocument().addDocumentListener(new SearchFilterListener());
|
|
216 |
controlPanel.add(filterField, c);
|
|
217 |
|
|
218 |
c.gridx = 1;
|
|
219 |
c.gridy = 2;
|
|
220 |
c.gridwidth = GridBagConstraints.REMAINDER;
|
|
221 |
//c.insets.right = 24;
|
|
222 |
//c.insets.left = 12;
|
|
223 |
c.weightx = 0.0;
|
|
224 |
c.anchor = GridBagConstraints.EAST;
|
|
225 |
c.fill = GridBagConstraints.NONE;
|
|
226 |
winnersCheckbox = new JCheckBox(getString("TableDemo.winnersLabel",
|
|
227 |
"Show Only Winners"));
|
|
228 |
winnersCheckbox.addChangeListener(new ShowWinnersListener());
|
|
229 |
controlPanel.add(winnersCheckbox, c);
|
|
230 |
|
|
231 |
return controlPanel;
|
|
232 |
}
|
|
233 |
|
|
234 |
protected Container createStatusBar() {
|
|
235 |
statusLabelString = getString("TableDemo.rowCountLabel",
|
|
236 |
"Showing ");
|
|
237 |
searchLabelString = getString("TableDemo.searchCountLabel",
|
|
238 |
"Search found ");
|
|
239 |
|
|
240 |
Box statusBar = Box.createHorizontalBox();
|
|
241 |
|
|
242 |
// Left status area
|
|
243 |
statusBar.add(Box.createRigidArea(new Dimension(10, 22)));
|
|
244 |
statusBarLeft = Box.createHorizontalBox();
|
|
245 |
statusBar.add(statusBarLeft);
|
|
246 |
actionStatus = new JLabel(getString("TableDemo.noDataStatusLabel",
|
|
247 |
"No data loaded"));
|
|
248 |
actionStatus.setHorizontalAlignment(JLabel.LEADING);
|
|
249 |
statusBarLeft.add(actionStatus);
|
|
250 |
|
|
251 |
// Middle (should stretch)
|
|
252 |
statusBar.add(Box.createHorizontalGlue());
|
|
253 |
statusBar.add(Box.createHorizontalGlue());
|
|
254 |
statusBar.add(Box.createVerticalGlue());
|
|
255 |
|
|
256 |
// Right status area
|
|
257 |
tableStatus = new JLabel(statusLabelString + "0");
|
|
258 |
statusBar.add(tableStatus);
|
|
259 |
statusBar.add(Box.createHorizontalStrut(12));
|
|
260 |
|
|
261 |
//<snip>Track number of rows currently displayed
|
|
262 |
oscarModel.addTableModelListener(new TableModelListener() {
|
|
263 |
public void tableChanged(TableModelEvent e) {
|
|
264 |
// Get rowCount from *table*, not model, as the view row count
|
|
265 |
// may be different from the model row count due to filtering
|
|
266 |
tableStatus.setText((hasFilterString() ? searchLabelString : statusLabelString) +
|
|
267 |
oscarTable.getRowCount());
|
|
268 |
}
|
|
269 |
});
|
|
270 |
//</snip>
|
|
271 |
|
|
272 |
return statusBar;
|
|
273 |
}
|
|
274 |
|
|
275 |
private Color[] getTableRowColors() {
|
|
276 |
if (rowColors == null) {
|
|
277 |
rowColors = new Color[2];
|
|
278 |
rowColors[0] = UIManager.getColor("Table.background");
|
|
279 |
rowColors[1] = new Color((int) (rowColors[0].getRed() * .9),
|
|
280 |
(int) (rowColors[0].getGreen() * .9),
|
|
281 |
(int) (rowColors[0].getBlue() * .9));
|
|
282 |
}
|
|
283 |
return rowColors;
|
|
284 |
}
|
|
285 |
|
|
286 |
// returns appropriate string from resource bundle
|
|
287 |
protected String getString(String key, String fallback) {
|
|
288 |
String value = fallback;
|
|
289 |
if (bundle == null) {
|
|
290 |
String bundleName = getClass().getPackage().getName() + ".resources." + getClass().getSimpleName();
|
|
291 |
bundle = ResourceBundle.getBundle(bundleName);
|
|
292 |
}
|
|
293 |
try {
|
|
294 |
value = bundle != null ? bundle.getString(key) : key;
|
|
295 |
|
|
296 |
} catch (MissingResourceException ex) {
|
|
297 |
logger.log(Level.WARNING, "couldn't find resource value for: " + key, ex);
|
|
298 |
}
|
|
299 |
return value;
|
|
300 |
}
|
|
301 |
|
|
302 |
public void start() {
|
|
303 |
if (oscarModel.getRowCount() == 0) {
|
|
304 |
loadData("resources/oscars.xml");
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
//<snip>Initialize table columns
|
|
309 |
protected TableColumnModel createColumnModel() {
|
|
310 |
DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
|
|
311 |
|
|
312 |
TableCellRenderer cellRenderer = new OscarCellRenderers.RowRenderer(getTableRowColors());
|
|
313 |
|
|
314 |
TableColumn column = new TableColumn();
|
|
315 |
column.setModelIndex(OscarTableModel.YEAR_COLUMN);
|
|
316 |
column.setHeaderValue(COLUMN1_NAME);
|
|
317 |
column.setPreferredWidth(26);
|
|
318 |
column.setCellRenderer(new OscarCellRenderers.YearRenderer(getTableRowColors()));
|
|
319 |
columnModel.addColumn(column);
|
|
320 |
|
|
321 |
column = new TableColumn();
|
|
322 |
column.setModelIndex(OscarTableModel.CATEGORY_COLUMN);
|
|
323 |
column.setHeaderValue(COLUMN2_NAME);
|
|
324 |
column.setPreferredWidth(100);
|
|
325 |
column.setCellRenderer(cellRenderer);
|
|
326 |
columnModel.addColumn(column);
|
|
327 |
|
|
328 |
column = new TableColumn();
|
|
329 |
column.setModelIndex(OscarTableModel.MOVIE_COLUMN);
|
|
330 |
column.setHeaderValue(COLUMN3_NAME);
|
|
331 |
column.setPreferredWidth(180);
|
|
332 |
column.setCellRenderer(cellRenderer);
|
|
333 |
columnModel.addColumn(column);
|
|
334 |
|
|
335 |
column = new TableColumn();
|
|
336 |
column.setModelIndex(OscarTableModel.PERSONS_COLUMN);
|
|
337 |
column.setHeaderValue(COLUMN4_NAME);
|
|
338 |
column.setPreferredWidth(120);
|
|
339 |
column.setCellRenderer(new OscarCellRenderers.NomineeRenderer(getTableRowColors()));
|
|
340 |
columnModel.addColumn(column);
|
|
341 |
|
|
342 |
return columnModel;
|
|
343 |
}
|
|
344 |
//</snip>
|
|
345 |
|
|
346 |
protected void initSortingFiltering() {
|
|
347 |
//<snip>Setup filtering for winners
|
|
348 |
sorter = new TableRowSorter(oscarModel);
|
|
349 |
oscarTable.setRowSorter(sorter);
|
|
350 |
winnerFilter = new RowFilter<OscarTableModel, Integer>() {
|
|
351 |
public boolean include(Entry<? extends OscarTableModel, ? extends Integer> entry) {
|
|
352 |
OscarTableModel oscarModel = entry.getModel();
|
|
353 |
OscarCandidate candidate = oscarModel.getCandidate(entry.getIdentifier().intValue());
|
|
354 |
if (candidate.isWinner()) {
|
|
355 |
// Returning true indicates this row should be shown.
|
|
356 |
return true;
|
|
357 |
}
|
|
358 |
// loser
|
|
359 |
return false;
|
|
360 |
}
|
|
361 |
};
|
|
362 |
//</snip>
|
|
363 |
|
|
364 |
//<snip>Setup search filter
|
|
365 |
searchFilter = new RowFilter<OscarTableModel, Integer>() {
|
|
366 |
public boolean include(Entry<? extends OscarTableModel, ? extends Integer> entry) {
|
|
367 |
OscarTableModel oscarModel = entry.getModel();
|
|
368 |
OscarCandidate candidate = oscarModel.getCandidate(entry.getIdentifier().intValue());
|
|
369 |
boolean matches = false;
|
|
370 |
Pattern p = Pattern.compile(filterString + ".*", Pattern.CASE_INSENSITIVE);
|
|
371 |
|
|
372 |
String movie = candidate.getMovieTitle();
|
|
373 |
if (movie != null) {
|
|
374 |
if (movie.startsWith("The ")) {
|
|
375 |
movie = movie.replace("The ", "");
|
|
376 |
} else if (movie.startsWith("A ")) {
|
|
377 |
movie = movie.replace("A ", "");
|
|
378 |
}
|
|
379 |
// Returning true indicates this row should be shown.
|
|
380 |
matches = p.matcher(movie).matches();
|
|
381 |
}
|
|
382 |
List<String> persons = candidate.getPersons();
|
|
383 |
for (String person : persons) {
|
|
384 |
if (p.matcher(person).matches()) {
|
|
385 |
matches = true;
|
|
386 |
}
|
|
387 |
}
|
|
388 |
return matches;
|
|
389 |
}
|
|
390 |
};
|
|
391 |
//</snip>
|
|
392 |
}
|
|
393 |
|
|
394 |
public void setShowOnlyWinners(boolean showOnlyWinners) {
|
|
395 |
boolean oldShowOnlyWinners = this.showOnlyWinners;
|
|
396 |
this.showOnlyWinners = showOnlyWinners;
|
|
397 |
configureFilters();
|
|
398 |
tableStatus.setText(statusLabelString + oscarTable.getRowCount());
|
|
399 |
firePropertyChange("showOnlyWinners", oldShowOnlyWinners, showOnlyWinners);
|
|
400 |
}
|
|
401 |
|
|
402 |
public boolean getShowOnlyWinners() {
|
|
403 |
return showOnlyWinners;
|
|
404 |
}
|
|
405 |
|
|
406 |
public void setFilterString(String filterString) {
|
|
407 |
String oldFilterString = this.filterString;
|
|
408 |
this.filterString = filterString;
|
|
409 |
configureFilters();
|
|
410 |
firePropertyChange("filterString", oldFilterString, filterString);
|
|
411 |
}
|
|
412 |
|
|
413 |
protected boolean hasFilterString() {
|
|
414 |
return filterString != null && !filterString.equals("");
|
|
415 |
}
|
|
416 |
|
|
417 |
protected void configureFilters() {
|
|
418 |
if (showOnlyWinners && hasFilterString()) {
|
|
419 |
List<RowFilter<OscarTableModel, Integer>> filters =
|
|
420 |
new ArrayList<RowFilter<OscarTableModel, Integer>>(2);
|
|
421 |
filters.add(winnerFilter);
|
|
422 |
filters.add(searchFilter);
|
|
423 |
RowFilter<OscarTableModel, Integer> comboFilter = RowFilter.andFilter(filters);
|
|
424 |
sorter.setRowFilter(comboFilter);
|
|
425 |
} else if (showOnlyWinners) {
|
|
426 |
sorter.setRowFilter(winnerFilter);
|
|
427 |
} else if (hasFilterString()) {
|
|
428 |
sorter.setRowFilter(searchFilter);
|
|
429 |
} else {
|
|
430 |
sorter.setRowFilter(null);
|
|
431 |
}
|
|
432 |
tableStatus.setText((hasFilterString() ? searchLabelString : statusLabelString)
|
|
433 |
+ oscarTable.getRowCount());
|
|
434 |
|
|
435 |
}
|
|
436 |
|
|
437 |
private class ShowWinnersListener implements ChangeListener {
|
|
438 |
public void stateChanged(ChangeEvent event) {
|
|
439 |
setShowOnlyWinners(winnersCheckbox.isSelected());
|
|
440 |
}
|
|
441 |
}
|
|
442 |
|
|
443 |
//<snip>Setup search filter
|
|
444 |
protected class SearchFilterListener implements DocumentListener {
|
|
445 |
protected void changeFilter(DocumentEvent event) {
|
|
446 |
Document document = event.getDocument();
|
|
447 |
try {
|
|
448 |
setFilterString(document.getText(0, document.getLength()));
|
|
449 |
|
|
450 |
} catch (Exception ex) {
|
|
451 |
ex.printStackTrace();
|
|
452 |
System.err.println(ex);
|
|
453 |
}
|
|
454 |
}
|
|
455 |
|
|
456 |
public void changedUpdate(DocumentEvent e) {
|
|
457 |
changeFilter(e);
|
|
458 |
}
|
|
459 |
|
|
460 |
public void insertUpdate(DocumentEvent e) {
|
|
461 |
changeFilter(e);
|
|
462 |
}
|
|
463 |
|
|
464 |
public void removeUpdate(DocumentEvent e) {
|
|
465 |
changeFilter(e);
|
|
466 |
}
|
|
467 |
}
|
|
468 |
//</snip>
|
|
469 |
|
|
470 |
//<snip>Use SwingWorker to asynchronously load the data
|
|
471 |
|
|
472 |
public void loadData(String dataPath) {
|
|
473 |
// create SwingWorker which will load the data on a separate thread
|
|
474 |
OscarDataLoader loader = new OscarDataLoader(
|
|
475 |
TableDemo.class.getResource(dataPath), oscarModel);
|
|
476 |
|
|
477 |
actionStatus.setText(getString("TableDemo.loadingStatusLabel",
|
|
478 |
"Loading data: "));
|
|
479 |
|
|
480 |
// display progress bar while data loads
|
|
481 |
final JProgressBar progressBar = new JProgressBar();
|
|
482 |
statusBarLeft.add(progressBar);
|
|
483 |
loader.addPropertyChangeListener(new PropertyChangeListener() {
|
|
484 |
public void propertyChange(PropertyChangeEvent event) {
|
|
485 |
if (event.getPropertyName().equals("progress")) {
|
|
486 |
int progress = ((Integer) event.getNewValue()).intValue();
|
|
487 |
progressBar.setValue(progress);
|
|
488 |
|
|
489 |
if (progress == 100) {
|
|
490 |
statusBarLeft.remove(progressBar);
|
|
491 |
actionStatus.setText("");
|
|
492 |
revalidate();
|
|
493 |
}
|
|
494 |
}
|
|
495 |
}
|
|
496 |
});
|
|
497 |
loader.execute();
|
|
498 |
|
|
499 |
}
|
|
500 |
//</snip>
|
|
501 |
|
|
502 |
protected void showMessage(String title, String message) {
|
|
503 |
JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
|
|
504 |
}
|
|
505 |
|
|
506 |
//<snip>Use SwingWorker to asynchronously load the data
|
|
507 |
private class OscarDataLoader extends javax.swing.SwingWorker<List<OscarCandidate>, OscarCandidate> {
|
|
508 |
private final URL oscarData;
|
|
509 |
private final OscarTableModel oscarModel;
|
|
510 |
private final List<OscarCandidate> candidates = new ArrayList<OscarCandidate>();
|
|
511 |
private JLabel credits;
|
|
512 |
|
|
513 |
private OscarDataLoader(URL oscarURL, OscarTableModel oscarTableModel) {
|
|
514 |
this.oscarData = oscarURL;
|
|
515 |
this.oscarModel = oscarTableModel;
|
|
516 |
}
|
|
517 |
|
|
518 |
@Override
|
|
519 |
public List<OscarCandidate> doInBackground() {
|
|
520 |
OscarDataParser parser = new OscarDataParser() {
|
|
521 |
@Override
|
|
522 |
protected void addCandidate(OscarCandidate candidate) {
|
|
523 |
candidates.add(candidate);
|
|
524 |
if (candidates.size() % 3 == 0) {
|
|
525 |
try { // slow it down so we can see progress :-)
|
|
526 |
Thread.sleep(1);
|
|
527 |
} catch (Exception ex) {
|
|
528 |
}
|
|
529 |
}
|
|
530 |
publish(candidate);
|
|
531 |
setProgress(100 * candidates.size() / 524);
|
|
532 |
}
|
|
533 |
};
|
|
534 |
parser.parseDocument(oscarData);
|
|
535 |
return candidates;
|
|
536 |
}
|
|
537 |
|
|
538 |
// for OS X older Java 6
|
|
539 |
/*
|
|
540 |
protected void process(List<OscarCandidate>... moreCandidates) {
|
|
541 |
for(List<OscarCandidate> newCandidates: moreCandidates) {
|
|
542 |
oscarModel.add(newCandidates);
|
|
543 |
}
|
|
544 |
}*/
|
|
545 |
|
|
546 |
//@Override
|
|
547 |
|
|
548 |
protected void process(List<OscarCandidate> moreCandidates) {
|
|
549 |
if (credits == null) {
|
|
550 |
showCredits();
|
|
551 |
}
|
|
552 |
oscarModel.add(moreCandidates);
|
|
553 |
}
|
|
554 |
|
|
555 |
// For older Java 6 on OS X
|
|
556 |
protected void process(OscarCandidate... moreCandidates) {
|
|
557 |
for (OscarCandidate candidate : moreCandidates) {
|
|
558 |
oscarModel.add(candidate);
|
|
559 |
}
|
|
560 |
}
|
|
561 |
|
|
562 |
private void showCredits() {
|
|
563 |
credits = new JLabel(getString("TableDemo.credits",
|
|
564 |
"<html><p align=\"center\">Academy Award data<br>courtesy of Howard Katz</p></html>"));
|
|
565 |
credits.setFont(UIManager.getFont("Table.font").deriveFont(24f));
|
|
566 |
credits.setHorizontalAlignment(JLabel.CENTER);
|
|
567 |
credits.setBorder(new CompoundBorder(new TitledBorder(""),
|
|
568 |
new EmptyBorder(20,20,20,20)));
|
|
569 |
}
|
|
570 |
@Override
|
|
571 |
protected void done() {
|
|
572 |
setProgress(100);
|
|
573 |
}
|
|
574 |
|
|
575 |
}
|
|
576 |
//</snip>
|
|
577 |
|
|
578 |
private class IMDBLinkAction extends AbstractAction {
|
|
579 |
|
|
580 |
public void actionPerformed(ActionEvent event) {
|
|
581 |
int row = ((Integer) event.getSource()).intValue();
|
|
582 |
OscarCandidate candidate = oscarModel.getCandidate(oscarTable.convertRowIndexToModel(row));
|
|
583 |
|
|
584 |
try {
|
|
585 |
URI imdbURI = candidate.getIMDBMovieURI();
|
|
586 |
if (imdbURI == null) {
|
|
587 |
String imdbString = IMDBLink.getMovieURIString(candidate.getMovieTitle(),
|
|
588 |
candidate.getYear());
|
|
589 |
if (imdbString != null) {
|
|
590 |
imdbURI = new URI(imdbString);
|
|
591 |
candidate.setIMDBMovieURI(imdbURI);
|
|
592 |
}
|
|
593 |
}
|
|
594 |
if (imdbURI != null) {
|
|
595 |
DemoUtilities.browse(imdbURI);
|
|
596 |
} else {
|
|
597 |
showMessage("IMDB Link",
|
|
598 |
getString("TableDemo.imdbLinkNotFound",
|
|
599 |
"Unable to locate IMDB URL for") + "\n" +
|
|
600 |
candidate.getMovieTitle());
|
|
601 |
}
|
|
602 |
|
|
603 |
} catch (Exception ex) {
|
|
604 |
ex.printStackTrace();
|
|
605 |
}
|
|
606 |
}
|
|
607 |
}
|
|
608 |
|
|
609 |
public static void main(String args[]) {
|
|
610 |
|
|
611 |
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
|
612 |
public void run() {
|
|
613 |
JFrame frame = new JFrame("JTable Demo");
|
|
614 |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
615 |
TableDemo demo = new TableDemo();
|
|
616 |
frame.add(demo);
|
|
617 |
frame.setSize(700, 400);
|
|
618 |
frame.setVisible(true);
|
|
619 |
demo.start();
|
|
620 |
}
|
|
621 |
});
|
|
622 |
}
|
|
623 |
}
|