001: package org.jdesktop.test;
002:
003: import java.awt.Color;
004:
005: import javax.swing.table.AbstractTableModel;
006:
007: /**
008: * Standard Table with class-Infos. Taken from some old
009: * SwingSet...
010: * Can remove/add rows.
011: */
012: public class AncientSwingTeam extends AbstractTableModel {
013:
014: protected final String[] names = { "First Name", "Last Name",
015: "Favorite Color", "No.", "Vegetarian" };
016: NamedColor aqua = new NamedColor(new Color(127, 255, 212), "Aqua");
017: NamedColor beige = new NamedColor(new Color(245, 245, 220),
018: ("Beige"));
019: NamedColor black = new NamedColor(Color.black, "Black");
020: NamedColor blue = new NamedColor(new Color(0, 0, 222), "Blue");
021: NamedColor eblue = new NamedColor(Color.blue, "Electric Blue");
022: NamedColor jfcblue = new NamedColor(new Color(204, 204, 255),
023: "JFC Primary");
024: NamedColor jfcblue2 = new NamedColor(new Color(153, 153, 204),
025: "JFC SEcondary");
026: NamedColor cybergreen = new NamedColor(Color.green.darker()
027: .brighter(), "Cyber Green");
028: NamedColor darkgreen = new NamedColor(new Color(0, 100, 75),
029: "darkgreen");
030: NamedColor forestgreen = new NamedColor(Color.green.darker(),
031: "Forest Green");
032: NamedColor gray = new NamedColor(Color.gray, "Gray");
033: NamedColor green = new NamedColor(Color.green, "Green");
034: NamedColor orange = new NamedColor(new Color(255, 165, 0), "Orange");
035: NamedColor purple = new NamedColor(new Color(160, 32, 240),
036: "Purple");
037: NamedColor red = new NamedColor(Color.red, "Red");
038: NamedColor rustred = new NamedColor(Color.red.darker(), "Rust Red");
039: NamedColor sunpurple = new NamedColor(new Color(100, 100, 255),
040: "Sun Purple");
041: NamedColor suspectpink = new NamedColor(new Color(255, 105, 180),
042: "Suspect Pink");
043: NamedColor turquoise = new NamedColor(new Color(0, 255, 255),
044: "Turquoise");
045: NamedColor violet = new NamedColor(new Color(238, 130, 238),
046: "Violet");
047: NamedColor yellow = new NamedColor(Color.yellow, "Yellow");
048:
049: protected final Object[][] data = {
050: { "Mark", "Andrews", red, new Integer(2), new Boolean(true) },
051: { "Tom", "Ball", blue, new Integer(99), new Boolean(false) },
052: { "Alan", "Chung", green, new Integer(838),
053: new Boolean(false) },
054: { "Jeff", "Dinkins", turquoise, new Integer(8),
055: new Boolean(true) },
056: { "Amy", "Fowler", yellow, new Integer(3),
057: new Boolean(false) },
058: { "Brian", "Gerhold", green, new Integer(0),
059: new Boolean(false) },
060: { "James", "Gosling", suspectpink, new Integer(21),
061: new Boolean(false) },
062: { "David", "Karlton", red, new Integer(1),
063: new Boolean(false) },
064: { "Dave", "Kloba", yellow, new Integer(14),
065: new Boolean(false) },
066: { "Peter", "Korn", purple, new Integer(12),
067: new Boolean(false) },
068: { "Phil", "Milne", purple, new Integer(3),
069: new Boolean(false) },
070: { "Dave", "Moore", green, new Integer(88),
071: new Boolean(false) },
072: { "Hans", "Muller", rustred, new Integer(5),
073: new Boolean(false) },
074:
075: { "Rick", "Levenson", blue, new Integer(2),
076: new Boolean(false) },
077: { "Tim", "Prinzing", blue, new Integer(22),
078: new Boolean(false) },
079: { "Chester", "Rose", black, new Integer(0),
080: new Boolean(false) },
081: { "Ray", "Ryan", gray, new Integer(77), new Boolean(false) },
082: { "Georges", "Saab", red, new Integer(4),
083: new Boolean(false) },
084: { "Willie", "Walker", jfcblue, new Integer(4),
085: new Boolean(false) },
086:
087: { "Kathy", "Walrath", blue, new Integer(8),
088: new Boolean(false) },
089: { "Arnaud", "Weber", green, new Integer(44),
090: new Boolean(false) } };
091:
092: protected int rowCount = data.length;
093:
094: public AncientSwingTeam() {
095:
096: }
097:
098: public AncientSwingTeam(int count) {
099: rowCount = count;
100: }
101:
102: public int getColumnCount() {
103: return names.length;
104: }
105:
106: public int getRowCount() {
107: return rowCount;
108: }
109:
110: /** reuses values internally */
111: public Object getValueAt(int row, int col) {
112: // following shows only every second value
113: // if ((row + col) % 2 == 0) return null;
114: return data[row % data.length][col];
115: }
116:
117: public void setValueAt(Object value, int row, int col) {
118: data[row % data.length][col] = value;
119: fireTableCellUpdated(row, col);
120: }
121:
122: // The default implementations of these methods in
123: // AbstractTableModel would work, but we can refine them.
124: public String getColumnName(int column) {
125: return names[column];
126: }
127:
128: /** returns class of column by asking class of value in first row. */
129: public Class getColumnClass(int c) {
130: Object value = null;
131: if (getRowCount() > 0) {
132: value = getValueAt(0, c);
133: }
134: if (value == null) {
135: return Object.class;
136: }
137: return value.getClass();
138: }
139:
140: /** everything is editable. */
141: public boolean isCellEditable(int row, int col) {
142: return true;
143: }
144:
145: /**
146: * insert length rows at rowIndex. PRE: rowIndex <= getRowCount()
147: */
148: public void insertRows(int rowIndex, int length) {
149: rowCount += length;
150: fireTableRowsInserted(rowIndex, rowIndex + length - 1);
151: }
152:
153: /**
154: * remove rows. NOTE: not tested
155: */
156: public void removeRows(int rowIndex, int length) {
157: rowCount -= length;
158: if (rowCount < 0) {
159: length -= rowCount;
160: rowCount = 0;
161: }
162: fireTableRowsDeleted(rowIndex, rowIndex + length - 1);
163: }
164:
165: public static class NamedColor extends Color {
166: String name;
167:
168: public NamedColor(Color color, String name) {
169: super (color.getRGB());
170: this .name = name;
171: }
172:
173: public Color getTextColor() {
174: int r = getRed();
175: int g = getGreen();
176: int b = getBlue();
177: if (r > 240 || g > 240) {
178: return Color.black;
179: } else {
180: return Color.white;
181: }
182: }
183:
184: public String toString() {
185: return name;
186: }
187:
188: }
189: } // end class SwingTeam
|