KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > TableExample


1 /*
2  * $Id: TableExample.java,v 1.7 2005/04/08 17:42:26 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.plaf.css.TableCG;
18 import org.wings.table.SDefaultTableCellRenderer;
19
20 import javax.swing.table.AbstractTableModel JavaDoc;
21 import javax.swing.table.TableModel JavaDoc;
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.ItemEvent JavaDoc;
26 import java.awt.event.ItemListener JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
30  * @version $Revision: 1.7 $
31  */

32 public class TableExample
33         extends WingSetPane {
34     static final SIcon image = new SResourceIcon("org/wings/icons/JavaCup.gif");
35
36     public final MyCellRenderer cellRenderer = new MyCellRenderer();
37
38     final static Color[] colors = {
39         Color.black,
40         Color.cyan,
41         Color.yellow,
42         Color.magenta,
43         Color.orange,
44         Color.pink,
45         Color.red,
46         Color.darkGray,
47         Color.gray,
48         Color.green,
49         Color.lightGray,
50         Color.white,
51         Color.blue
52     };
53
54     private STable table;
55     private TableControls controls;
56
57     public SComponent createExample() {
58         controls = new TableControls();
59
60         table = new STable(new MyTableModel(7, 5));
61         table.setName("table");
62         table.setShowGrid(true);
63         table.setSelectionMode(STable.NO_SELECTION);
64         table.setDefaultRenderer(cellRenderer);
65         table.setShowAsFormComponent(false);
66         table.setEditable(false);
67         controls.addSizable(table);
68
69         SForm panel = new SForm(new SBorderLayout());
70         panel.add(controls, SBorderLayout.NORTH);
71         panel.add(table, SBorderLayout.CENTER);
72         return panel;
73     }
74
75
76     static class MyCellRenderer extends SDefaultTableCellRenderer {
77
78         public MyCellRenderer() {
79             setEditIcon(getSession().getCGManager().getIcon("TableCG.editIcon"));
80         }
81
82         public SComponent getTableCellRendererComponent(STable table,
83                                                         Object JavaDoc value,
84                                                         boolean selected,
85                                                         int row,
86                                                         int col) {
87             if (value instanceof Color) {
88                 Color c = (Color)value;
89                 setText("<html><span style=\"color:" + colorToHex(c) + "\">" + colorToHex(c) + "</span>");
90                 setIcon(null);
91                 return this;
92             }
93             else
94                 return super.getTableCellRendererComponent(table, value, selected, row, col);
95         }
96     }
97
98     static class MyTableModel extends AbstractTableModel JavaDoc {
99         int cols, rows;
100
101         Object JavaDoc[][] data;
102         boolean asc[];
103
104         MyTableModel(int cols, int rows) {
105             this.cols = cols;
106             this.rows = rows;
107
108             data = new Object JavaDoc[rows][cols];
109             asc = new boolean[cols];
110
111             for (int c = 0; c < cols; c++) {
112                 for (int r = 0; r < rows; r++)
113                     data[r][c] = "cell " + r + ":" + c;
114             }
115             for (int r = 0; r < rows; r++)
116                 data[r][2] = createColor(r);
117             for (int r = 0; r < rows; r++)
118                 data[r][3] = createImage(r);
119             for (int r = 0; r < rows; r++)
120                 data[r][4] = createBoolean(r);
121         }
122
123         public int getColumnCount() {
124             return cols;
125         }
126
127         public String JavaDoc getColumnName(int col) {
128             return "col " + col;
129         }
130
131         public int getRowCount() {
132             return rows;
133         }
134
135         public Object JavaDoc getValueAt(int row, int col) {
136             return data[row][col];
137         }
138
139         public void setValueAt(Object JavaDoc value, int row, int col) {
140             if (value == null)
141                 data[row][col] = null;
142             else if (getColumnClass(col).isAssignableFrom(String JavaDoc.class))
143                 data[row][col] = value.toString();
144             else if (getColumnClass(col).isAssignableFrom(Boolean JavaDoc.class))
145                 data[row][col] = new Boolean JavaDoc(((Boolean JavaDoc) value).booleanValue());
146         }
147
148         public Class JavaDoc getColumnClass(int columnIndex) {
149             switch (columnIndex) {
150                 case 2:
151                     return Color.class;
152                 case 3:
153                     return SIcon.class;
154                 case 4:
155                     return Boolean JavaDoc.class;
156             }
157             return String JavaDoc.class;
158         }
159
160         public Color createColor(int row) {
161             return colors[row % colors.length];
162         }
163
164         public SIcon createImage(int row) {
165             return image;
166         }
167
168         public Boolean JavaDoc createBoolean(int row) {
169             if (row % 2 == 1)
170                 return new Boolean JavaDoc(false);
171             else
172                 return new Boolean JavaDoc(true);
173         }
174
175         public void sort(int col, boolean ascending) {
176             log.debug("sort");
177             if (col < asc.length)
178                 asc[col] = !ascending;
179         }
180
181         public boolean isCellEditable(int row, int col) {
182             if (getColumnClass(col).isAssignableFrom(String JavaDoc.class) ||
183                     getColumnClass(col).isAssignableFrom(Boolean JavaDoc.class))
184                 return true;
185             else
186                 return false;
187         }
188     }
189
190     static class ROTableModel extends MyTableModel {
191         public ROTableModel(int cols, int rows) {
192             super(cols, rows);
193         }
194
195         public boolean isCellEditable(int row, int col) { return false; }
196     }
197
198     static String JavaDoc colorToHex(Color color) {
199         String JavaDoc colorstr = new String JavaDoc("#");
200
201         // Red
202
String JavaDoc str = Integer.toHexString(color.getRed());
203         if (str.length() > 2)
204             str = str.substring(0, 2);
205         else if (str.length() < 2)
206             colorstr += "0" + str;
207         else
208             colorstr += str;
209
210         // Green
211
str = Integer.toHexString(color.getGreen());
212         if (str.length() > 2)
213             str = str.substring(0, 2);
214         else if (str.length() < 2)
215             colorstr += "0" + str;
216         else
217             colorstr += str;
218
219         // Blue
220
str = Integer.toHexString(color.getBlue());
221         if (str.length() > 2)
222             str = str.substring(0, 2);
223         else if (str.length() < 2)
224             colorstr += "0" + str;
225         else
226             colorstr += str;
227
228         return colorstr;
229     }
230
231
232     /**
233      * Proof that we can do some really nice tables with j-wings.
234      */

235     public class MyTable extends STable {
236         private final /*static*/ TableCG myTableCG = new TableCG();
237
238         public MyTable(TableModel JavaDoc tm) {
239             super(tm);
240             myTableCG.setFixedTableBorderWidth("0");
241             setCG(myTableCG);
242         }
243
244         /**
245          * Returns the CSS style for a row (<td style="xxx")
246          */

247         public String JavaDoc getRowStyle(int row) {
248             return isRowSelected(row) ? "table_selected_row" : (row % 2 == 0 ? "table_row1" : "table_row2");
249         }
250     }
251
252     class TableControls extends ComponentControls {
253         private final String JavaDoc[] SELECTION_MODES = new String JavaDoc[]{"no", "single", "multiple"};
254
255         public TableControls() {
256             final SCheckBox showAsFormComponent = new SCheckBox("<html>Show as Form Component&nbsp;&nbsp;&nbsp;");
257             showAsFormComponent.addActionListener(new ActionListener JavaDoc() {
258                 public void actionPerformed(ActionEvent JavaDoc e) {
259                     table.setShowAsFormComponent(showAsFormComponent.isSelected());
260                 }
261             });
262
263             final SCheckBox editable = new SCheckBox("<html>Editable&nbsp;&nbsp;&nbsp;");
264             editable.addActionListener(new ActionListener JavaDoc() {
265                 public void actionPerformed(ActionEvent JavaDoc e) {
266                     table.setEditable(editable.isSelected());
267                 }
268             });
269
270             final SComboBox selectionMode = new SComboBox(SELECTION_MODES);
271             selectionMode.addItemListener(new ItemListener JavaDoc() {
272                 public void itemStateChanged(ItemEvent JavaDoc e) {
273                     if ("no".equals(selectionMode.getSelectedItem()))
274                         table.setSelectionMode(STable.NO_SELECTION);
275                     else if ("single".equals(selectionMode.getSelectedItem()))
276                         table.setSelectionMode(STable.SINGLE_SELECTION);
277                     else if ("multiple".equals(selectionMode.getSelectedItem()))
278                         table.setSelectionMode(STable.MULTIPLE_SELECTION);
279                 }
280             });
281
282             add(showAsFormComponent);
283             add(editable);
284             add(new SLabel(" selection mode "));
285             add(selectionMode);
286         }
287     }
288 }
289
Popular Tags