001: /*
002: * Copyright 2005-2008 Kirill Grouchnikov, based on work by
003: * Sun Microsystems, Inc. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018: */
019: package test;
020:
021: import java.awt.*;
022: import java.util.Calendar;
023: import java.util.Date;
024:
025: import javax.swing.*;
026: import javax.swing.table.*;
027:
028: import org.jdesktop.swingx.JXTable;
029: import org.jdesktop.swingx.decorator.ColorHighlighter;
030: import org.jdesktop.swingx.decorator.HighlightPredicate;
031: import org.jvnet.substance.SubstanceImageCreator;
032: import org.jvnet.substance.SubstanceLookAndFeel;
033: import org.jvnet.substance.theme.SubstanceTheme;
034: import org.jvnet.substance.theme.ThemeInfo;
035: import org.jvnet.substance.utils.SubstanceColorUtilities;
036:
037: public class TablePanel extends JPanel {
038: private JXTable table;
039:
040: private Object[][] data;
041:
042: private Class[] columns;
043:
044: private static class MyColorTableRenderer extends JLabel implements
045: TableCellRenderer {
046: public Component getTableCellRendererComponent(JTable table,
047: Object value, boolean isSelected, boolean hasFocus,
048: int row, int column) {
049: Color color = (Color) value;
050: this .setForeground(color);
051: this .setBackground(SubstanceColorUtilities
052: .invertColor(color));
053: this .setText("row " + row);
054: return this ;
055: }
056: }
057:
058: private static class MyFloatTableRenderer extends JLabel implements
059: TableCellRenderer {
060: public Component getTableCellRendererComponent(JTable table,
061: Object value, boolean isSelected, boolean hasFocus,
062: int row, int column) {
063: int c = (10 * row) % 255;
064: Color color = new Color(c, c, c);
065: this .setForeground(new Color(255 - c, 0, 0));
066: this .setBackground(color);
067: this .setText(value.toString());
068: return this ;
069: }
070: }
071:
072: public TablePanel() {
073: final int rows = 400;
074: final int cols = 10;
075: this .data = new Object[rows][cols];
076: this .columns = new Class[] { String.class, JComboBox.class,
077: Boolean.class, Byte.class, Float.class, Double.class,
078: String.class, Date.class, ImageIcon.class, Color.class };
079:
080: for (int i = 0; i < rows; i++) {
081: this .data[i][0] = "cell " + i + ":" + 0;
082: this .data[i][1] = "predef";
083: this .data[i][2] = new Boolean(i % 2 == 0);
084: this .data[i][3] = new Byte((byte) i);
085: this .data[i][4] = new Float(i);
086: this .data[i][5] = new Double(i);
087: this .data[i][6] = "cell " + i + ":" + 6;
088: Calendar cal = Calendar.getInstance();
089: cal.set(2000 + i, 1 + i, 1 + i);
090: this .data[i][7] = cal.getTime();
091: int count = 0;
092: if (UIManager.getLookAndFeel() instanceof SubstanceLookAndFeel) {
093: for (ThemeInfo themeInfo : SubstanceLookAndFeel
094: .getAllThemes().values()) {
095: if (count++ == i) {
096: try {
097: this .data[i][8] = SubstanceImageCreator
098: .getHexaMarker(
099: 6,
100: (SubstanceTheme) Class
101: .forName(
102: themeInfo
103: .getClassName())
104: .newInstance());
105: } catch (Exception exc) {
106: }
107: }
108: }
109: }
110:
111: int comp = i * 20;
112: int red = (comp / 3) % 255;
113: int green = (comp / 2) % 255;
114: int blue = comp % 255;
115: this .data[i][9] = new Color(red, green, blue);
116: }
117:
118: TableModel dataModel = new AbstractTableModel() {
119: @Override
120: public Class<?> getColumnClass(int columnIndex) {
121: return columns[columnIndex];
122: }
123:
124: public int getColumnCount() {
125: return cols;
126: }
127:
128: public int getRowCount() {
129: return rows;
130: }
131:
132: public Object getValueAt(int row, int col) {
133: return data[row][col];
134: }
135:
136: @Override
137: public String getColumnName(int column) {
138: return this .getColumnClass(column).getSimpleName();
139: }
140:
141: @Override
142: public boolean isCellEditable(int rowIndex, int columnIndex) {
143: return (rowIndex % 2 == 0);
144: }
145:
146: @Override
147: public void setValueAt(Object value, int row, int col) {
148: data[row][col] = value;
149: fireTableCellUpdated(row, col);
150: }
151: };
152: this .table = new JXTable(dataModel);
153: this .table.setDefaultRenderer(Color.class,
154: new MyColorTableRenderer());
155: this .table.setDefaultRenderer(Float.class,
156: new MyFloatTableRenderer());
157: final JScrollPane tableScrollpane = new JScrollPane(this .table);
158:
159: this .table.setColumnControlVisible(true);
160: this .table.setHighlighters(new ColorHighlighter(
161: HighlightPredicate.EVEN));
162:
163: JComboBox combo = new JComboBox(
164: new Object[] { "aa", "bb", "cc" });
165: combo.setBorder(null);
166: this .table.getColumnModel().getColumn(1).setCellEditor(
167: new DefaultCellEditor(combo));
168:
169: this .table
170: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
171: this .table.setCellSelectionEnabled(true);
172: this .table.setColumnSelectionAllowed(true);
173: this .table.setShowGrid(true);
174: this .table.setDragEnabled(false);
175:
176: this .setLayout(new BorderLayout());
177: this.add(tableScrollpane, BorderLayout.CENTER);
178:
179: }
180: }
|