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.Color;
|
|
27 |
import java.awt.Component;
|
|
28 |
import java.awt.Font;
|
|
29 |
import java.util.HashMap;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import javax.swing.Action;
|
|
33 |
import javax.swing.ImageIcon;
|
|
34 |
import javax.swing.JLabel;
|
|
35 |
import javax.swing.JTable;
|
|
36 |
import javax.swing.UIManager;
|
|
37 |
import javax.swing.table.DefaultTableCellRenderer;
|
|
38 |
import javax.swing.table.TableModel;
|
|
39 |
|
|
40 |
/**
|
|
41 |
*
|
|
42 |
* @author aim
|
|
43 |
*/
|
|
44 |
public class OscarCellRenderers {
|
|
45 |
|
|
46 |
//<snip>Render table rows with alternating colors
|
|
47 |
public static class RowRenderer extends DefaultTableCellRenderer {
|
|
48 |
private Color rowColors[];
|
|
49 |
|
|
50 |
public RowRenderer() {
|
|
51 |
// initialize default colors from look-and-feel
|
|
52 |
rowColors = new Color[1];
|
|
53 |
rowColors[0] = UIManager.getColor("Table.background");
|
|
54 |
}
|
|
55 |
|
|
56 |
public RowRenderer(Color colors[]) {
|
|
57 |
super();
|
|
58 |
setRowColors(colors);
|
|
59 |
}
|
|
60 |
|
|
61 |
public void setRowColors(Color colors[]) {
|
|
62 |
rowColors = colors;
|
|
63 |
}
|
|
64 |
|
|
65 |
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
66 |
boolean isSelected, boolean hasFocus, int row, int column) {
|
|
67 |
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
|
68 |
setText(value != null ? value.toString() : "unknown");
|
|
69 |
if (!isSelected) {
|
|
70 |
setBackground(rowColors[row % rowColors.length]);
|
|
71 |
}
|
|
72 |
return this;
|
|
73 |
}
|
|
74 |
|
|
75 |
public boolean isOpaque() {
|
|
76 |
return true;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
//<snip>
|
|
80 |
|
|
81 |
//<snip>Render "year" table column with font representing style of decade
|
|
82 |
// currently only used on OS X because fonts are Mac centric.
|
|
83 |
|
|
84 |
public static class YearRenderer extends RowRenderer {
|
|
85 |
private HashMap<String, Font> eraFonts;
|
|
86 |
|
|
87 |
public YearRenderer() {
|
|
88 |
setHorizontalAlignment(JLabel.CENTER);
|
|
89 |
|
|
90 |
if (System.getProperty("os.name").equals("Mac OS X")) {
|
|
91 |
eraFonts = new HashMap<String, Font>();
|
|
92 |
eraFonts.put("192"/*1920's*/, new Font("Jazz LET", Font.PLAIN, 12));
|
|
93 |
eraFonts.put("193"/*1930's*/, new Font("Mona Lisa Solid ITC TT", Font.BOLD, 18));
|
|
94 |
eraFonts.put("194"/*1940's*/, new Font("American Typewriter", Font.BOLD, 12));
|
|
95 |
eraFonts.put("195"/*1950's*/, new Font("Britannic Bold", Font.PLAIN, 12));
|
|
96 |
eraFonts.put("196"/*1960's*/, new Font("Cooper Black", Font.PLAIN, 14));
|
|
97 |
eraFonts.put("197"/*1970's*/, new Font("Syncro LET", Font.PLAIN, 14));
|
|
98 |
eraFonts.put("198"/*1980's*/, new Font("Mistral", Font.PLAIN, 18));
|
|
99 |
eraFonts.put("199"/*1990's*/, new Font("Papyrus", Font.BOLD, 14));
|
|
100 |
eraFonts.put("200"/*2000's*/, new Font("Calisto MT", Font.PLAIN, 14));
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
public YearRenderer(Color colors[]) {
|
|
105 |
this();
|
|
106 |
setRowColors(colors);
|
|
107 |
}
|
|
108 |
|
|
109 |
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
110 |
boolean isSelected, boolean hasFocus, int row, int column) {
|
|
111 |
|
|
112 |
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
|
113 |
|
|
114 |
String year = table.getValueAt(row,
|
|
115 |
table.convertColumnIndexToView(OscarTableModel.YEAR_COLUMN)).toString();
|
|
116 |
if (eraFonts != null && year != null && year.length() == 4) {
|
|
117 |
String era = year.substring(0, 3);
|
|
118 |
Font eraFont = eraFonts.get(era);
|
|
119 |
setFont(eraFont);
|
|
120 |
}
|
|
121 |
return this;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
//</snip>
|
|
125 |
|
|
126 |
//<snip>Render "nominee" table column with special icon for winners
|
|
127 |
|
|
128 |
public static class NomineeRenderer extends RowRenderer {
|
|
129 |
private final ImageIcon winnerIcon;
|
|
130 |
private final ImageIcon nomineeIcon; // nice way of saying "loser" :)
|
|
131 |
|
|
132 |
public NomineeRenderer() {
|
|
133 |
winnerIcon = new ImageIcon(
|
|
134 |
getClass().getResource("resources/images/goldstar.png"));
|
|
135 |
nomineeIcon = new ImageIcon(
|
|
136 |
getClass().getResource("resources/images/nominee.png"));
|
|
137 |
setHorizontalTextPosition(JLabel.TRAILING);
|
|
138 |
}
|
|
139 |
|
|
140 |
public NomineeRenderer(Color colors[]) {
|
|
141 |
this();
|
|
142 |
setRowColors(colors);
|
|
143 |
}
|
|
144 |
|
|
145 |
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
146 |
boolean isSelected, boolean hasFocus, int row, int column) {
|
|
147 |
|
|
148 |
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
|
149 |
|
|
150 |
TableModel model = table.getModel();
|
|
151 |
boolean winner = ((Boolean) model.getValueAt(table.convertRowIndexToModel(row),
|
|
152 |
OscarTableModel.WINNER_COLUMN)).booleanValue();
|
|
153 |
|
|
154 |
List<String> persons = (List<String>) value;
|
|
155 |
String text = persons != null && !persons.isEmpty() ? persons.get(0) : "name unknown";
|
|
156 |
if (persons != null && persons.size() > 1) {
|
|
157 |
int personCount = persons.size();
|
|
158 |
setText(text + " + more...");
|
|
159 |
StringBuffer winners = new StringBuffer("");
|
|
160 |
for (int i = 0; i < personCount; i++) {
|
|
161 |
String person = persons.get(i);
|
|
162 |
winners.append(" " + person + (i < personCount - 1 ? ", " : ""));
|
|
163 |
}
|
|
164 |
setToolTipText((winner ? "Winners:" : "Nominees:") + winners);
|
|
165 |
} else {
|
|
166 |
setText(text);
|
|
167 |
setToolTipText(winner ? "Winner!" : "Nominee");
|
|
168 |
}
|
|
169 |
|
|
170 |
setIcon(winner ? winnerIcon : nomineeIcon);
|
|
171 |
|
|
172 |
return this;
|
|
173 |
}
|
|
174 |
}
|
|
175 |
//</snip>
|
|
176 |
|
|
177 |
public static class MovieRenderer extends HyperlinkCellRenderer {
|
|
178 |
public MovieRenderer(Action action, boolean underlineOnRollover, Color rowColors[]) {
|
|
179 |
super(action, underlineOnRollover);
|
|
180 |
setRowColors(rowColors);
|
|
181 |
}
|
|
182 |
|
|
183 |
public Component getTableCellRendererComponent(JTable table, Object value,
|
|
184 |
boolean isSelected, boolean hasFocus, int row, int column) {
|
|
185 |
super.getTableCellRendererComponent(table, value, isSelected,
|
|
186 |
hasFocus, row, column);
|
|
187 |
if (value != null) {
|
|
188 |
setToolTipText("http://www.imdb.com/" + "\"" + value + "\"");
|
|
189 |
}
|
|
190 |
return this;
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
}
|