001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.style.CSSProperty;
017: import org.wings.style.CSSStyleSheet;
018: import org.wings.event.SMouseEvent;
019: import org.wings.event.SMouseListener;
020: import org.wings.plaf.css.TableCG;
021: import org.wings.table.*;
022:
023: import javax.swing.table.AbstractTableModel;
024: import javax.swing.table.TableModel;
025: import java.awt.*;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ItemEvent;
028: import java.awt.event.ItemListener;
029: import java.util.*;
030:
031: /**
032: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
033: */
034: public class TableExample extends WingSetPane {
035: static final SIcon image = new SResourceIcon(
036: "org/wings/icons/JavaCup.gif");
037:
038: public final MyCellRenderer cellRenderer = new MyCellRenderer();
039:
040: final static Color[] colors = { Color.black, Color.cyan,
041: Color.yellow, Color.magenta, Color.orange, Color.pink,
042: Color.red, Color.darkGray, Color.gray, Color.green,
043: Color.lightGray, Color.white, Color.blue };
044:
045: private STable table;
046: private SLabel clicks = new SLabel();
047: private TableControls controls;
048: private boolean consume = false;
049:
050: protected SComponent createControls() {
051: controls = new TableControls();
052: return controls;
053: }
054:
055: public SComponent createExample() {
056: table = new STable(new MyTableModel(7, 5));
057: table.setName("tableExample");
058: table.setShowGrid(true);
059: table.setSelectionMode(STable.NO_SELECTION);
060: table.setDefaultRenderer(cellRenderer);
061: table.setShowAsFormComponent(false);
062: table.setEditable(false);
063: controls.addControllable(table);
064:
065: table.getColumnModel().getColumn(0).setWidth("200px");
066: table.getColumnModel().getColumn(1).setWidth("*");
067: table.getColumnModel().getColumn(2).setWidth("100px");
068: table.getColumnModel().getColumn(3).setWidth("50px");
069: table.getColumnModel().getColumn(4).setWidth("50px");
070: table.getColumnModel().getColumn(5).setWidth("50px");
071: table.getColumnModel().getColumn(6).setWidth("100px");
072:
073: table.addMouseListener(new SMouseListener() {
074: public void mouseClicked(SMouseEvent e) {
075: if (consume && table.columnAtPoint(e.getPoint()) == 1)
076: e.consume();
077: clicks.setText("clicked " + e.getPoint());
078: }
079: });
080: table.setVerticalAlignment(SConstants.TOP_ALIGN);
081:
082: SPanel panel = new SPanel(new SBorderLayout());
083: panel.add(table, SBorderLayout.CENTER);
084: panel.add(clicks, SBorderLayout.SOUTH);
085: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
086: return panel;
087: }
088:
089: static class MyCellRenderer extends SDefaultTableCellRenderer {
090: private static final SFont MONOSPACE = new SFont("monospace",
091: SFont.BOLD, SFont.DEFAULT_SIZE);
092:
093: public MyCellRenderer() {
094: setEditIcon(getSession().getCGManager().getIcon(
095: "TableCG.editIcon"));
096: }
097:
098: public SComponent getTableCellRendererComponent(STable table,
099: Object value, boolean selected, int row, int col) {
100: setHorizontalAlignment(SConstants.LEFT);
101: setIcon(null);
102: setFont(null);
103: setForeground(null);
104: setComponentPopupMenu(null);
105:
106: if (value instanceof Color) {
107: Color c = (Color) value;
108: setFont(MONOSPACE);
109: setText(colorToHex(c));
110: setForeground(c);
111: return this ;
112: } else if (value instanceof Boolean && row != -1) {
113: setText("" + value);
114: return this ;
115: } else if (value instanceof Integer && row != -1) {
116: setHorizontalAlignment(SConstants.RIGHT);
117: return super .getTableCellRendererComponent(table,
118: value, selected, row, col);
119: } else
120: return super .getTableCellRendererComponent(table,
121: value, selected, row, col);
122: }
123: }
124:
125: static class MyRowSelectionRenderer extends
126: SDefaultTableRowSelectionRenderer {
127: public SComponent getTableCellRendererComponent(STable table,
128: Object value, boolean selected, int row, int col) {
129: return super .getTableCellRendererComponent(table, value,
130: selected, row, col); //To change body of overridden methods use File | Settings | File Templates.
131: }
132: }
133:
134: static class MyTableModel extends AbstractTableModel {
135: int cols, rows;
136:
137: Object[][] data;
138: boolean asc[];
139:
140: MyTableModel(int pCols, int pRows) {
141: this .cols = pCols > 6 ? pCols : 6;
142: this .rows = pRows;
143:
144: data = new Object[rows][cols];
145: asc = new boolean[cols];
146:
147: for (int c = 0; c < cols; c++) {
148: for (int r = 0; r < rows; r++)
149: data[r][c] = (c == 1 ? "stretched cell " : "cell ")
150: + r + ":" + c;
151: }
152: for (int r = 0; r < rows; r++)
153: data[r][2] = createColor(r);
154: for (int r = 0; r < rows; r++)
155: data[r][3] = createImage(r);
156: for (int r = 0; r < rows; r++)
157: data[r][4] = createBoolean(r);
158: for (int r = 0; r < rows; r++)
159: data[r][5] = createInteger(r);
160: }
161:
162: public int getColumnCount() {
163: return cols;
164: }
165:
166: public String getColumnName(int col) {
167: return "col " + col;
168: }
169:
170: public int getRowCount() {
171: return rows;
172: }
173:
174: public Object getValueAt(int row, int col) {
175: return data[row][col];
176: }
177:
178: public void setValueAt(Object value, int row, int col) {
179: if (value == null)
180: data[row][col] = null;
181: else if (getColumnClass(col).isAssignableFrom(String.class))
182: data[row][col] = value.toString();
183: else if (getColumnClass(col)
184: .isAssignableFrom(Boolean.class))
185: data[row][col] = new Boolean(((Boolean) value)
186: .booleanValue());
187: }
188:
189: public Class getColumnClass(int columnIndex) {
190: switch (columnIndex) {
191: case 2:
192: return Color.class;
193: case 3:
194: return SIcon.class;
195: case 4:
196: return Boolean.class;
197: case 5:
198: return Integer.class;
199: }
200: return String.class;
201: }
202:
203: public Color createColor(int row) {
204: return colors[row % colors.length];
205: }
206:
207: public SIcon createImage(int row) {
208: return image;
209: }
210:
211: public Boolean createBoolean(int row) {
212: if (row % 2 == 1)
213: return new Boolean(false);
214: else
215: return new Boolean(true);
216: }
217:
218: public Integer createInteger(int row) {
219: return new Integer(row);
220: }
221:
222: public void sort(int col, boolean ascending) {
223: log.debug("sort");
224: if (col < asc.length)
225: asc[col] = !ascending;
226: }
227:
228: public boolean isCellEditable(int row, int col) {
229: if (getColumnClass(col).isAssignableFrom(String.class)
230: || getColumnClass(col).isAssignableFrom(
231: Boolean.class))
232: return true;
233: else
234: return false;
235: }
236: }
237:
238: static class ROTableModel extends MyTableModel {
239: public ROTableModel(int cols, int rows) {
240: super (cols, rows);
241: }
242:
243: public boolean isCellEditable(int row, int col) {
244: return false;
245: }
246: }
247:
248: static String colorToHex(Color color) {
249: String colorstr = "#";
250:
251: // Red
252: String str = Integer.toHexString(color.getRed());
253: if (str.length() < 2)
254: colorstr += "0" + str;
255: else
256: colorstr += str;
257:
258: // Green
259: str = Integer.toHexString(color.getGreen());
260: if (str.length() < 2)
261: colorstr += "0" + str;
262: else
263: colorstr += str;
264:
265: // Blue
266: str = Integer.toHexString(color.getBlue());
267: if (str.length() < 2)
268: colorstr += "0" + str;
269: else
270: colorstr += str;
271:
272: return colorstr;
273: }
274:
275: /**
276: * Proof that we can do some really nice tables with j-wings.
277: */
278: public class MyTable extends STable {
279: private final/*static*/TableCG myTableCG = new TableCG();
280:
281: public MyTable(TableModel tm) {
282: super (tm);
283: myTableCG.setFixedTableBorderWidth("0");
284: setCG(myTableCG);
285: }
286:
287: /**
288: * Returns the CSS style for a row (<td style="xxx")
289: */
290: public String getRowStyle(int row) {
291: return isRowSelected(row) ? "table_selected_row"
292: : (row % 2 == 0 ? "table_row1" : "table_row2");
293: }
294: }
295:
296: class TableControls extends ComponentControls {
297: private final String[] SELECTION_MODES = new String[] { "no",
298: "single", "multiple" };
299:
300: public TableControls() {
301: widthTextField.setText("100%");
302:
303: final SCheckBox editable = new SCheckBox("editable");
304: editable
305: .addActionListener(new java.awt.event.ActionListener() {
306: public void actionPerformed(ActionEvent e) {
307: if (!editable.isSelected()) {
308: table.removeEditor();
309: }
310: table.setEditable(editable.isSelected());
311: }
312: });
313:
314: final SCheckBox consume = new SCheckBox(
315: "consume events on col 1");
316: consume
317: .setToolTipText("<html>A SMouseListener will intercept the mouse clicks.<br>"
318: + "Consumed events will not be processed by the table anymore");
319: consume
320: .addActionListener(new java.awt.event.ActionListener() {
321: public void actionPerformed(ActionEvent e) {
322: TableExample.this .consume = consume
323: .isSelected();
324: }
325: });
326:
327: final SComboBox selectionMode = new SComboBox(
328: SELECTION_MODES);
329: selectionMode.addItemListener(new ItemListener() {
330: public void itemStateChanged(ItemEvent e) {
331: if ("no".equals(selectionMode.getSelectedItem()))
332: table.setSelectionMode(STable.NO_SELECTION);
333: else if ("single".equals(selectionMode
334: .getSelectedItem()))
335: table.setSelectionMode(STable.SINGLE_SELECTION);
336: else if ("multiple".equals(selectionMode
337: .getSelectedItem()))
338: table
339: .setSelectionMode(STable.MULTIPLE_SELECTION);
340: }
341: });
342:
343: addControl(editable);
344: addControl(new SLabel(""));
345: addControl(consume);
346: addControl(new SLabel(" selection mode"));
347: addControl(selectionMode);
348:
349: final SComboBox headerColor = new SComboBox(COLORS);
350: headerColor
351: .addActionListener(new java.awt.event.ActionListener() {
352: public void actionPerformed(ActionEvent e) {
353: Color color = (Color) ((Object[]) headerColor
354: .getSelectedItem())[1];
355: table.setAttribute(STable.SELECTOR_HEADER,
356: CSSProperty.BACKGROUND_COLOR,
357: CSSStyleSheet.getAttribute(color));
358: }
359: });
360: headerColor.setRenderer(new ObjectPairCellRenderer());
361: addControl(new SLabel(" header"));
362: addControl(headerColor);
363:
364: final SComboBox oddColor = new SComboBox(COLORS);
365: oddColor
366: .addActionListener(new java.awt.event.ActionListener() {
367: public void actionPerformed(ActionEvent e) {
368: Color color = (Color) ((Object[]) oddColor
369: .getSelectedItem())[1];
370: table.setAttribute(
371: STable.SELECTOR_ODD_ROWS,
372: CSSProperty.BACKGROUND_COLOR,
373: CSSStyleSheet.getAttribute(color));
374: }
375: });
376: oddColor.setRenderer(new ObjectPairCellRenderer());
377: addControl(new SLabel(" odd"));
378: addControl(oddColor);
379:
380: final SComboBox evenColor = new SComboBox(COLORS);
381: evenColor
382: .addActionListener(new java.awt.event.ActionListener() {
383: public void actionPerformed(ActionEvent e) {
384: Color color = (Color) ((Object[]) evenColor
385: .getSelectedItem())[1];
386: table.setAttribute(
387: STable.SELECTOR_EVEN_ROWS,
388: CSSProperty.BACKGROUND_COLOR,
389: CSSStyleSheet.getAttribute(color));
390: }
391: });
392: evenColor.setRenderer(new ObjectPairCellRenderer());
393: addControl(new SLabel(" even"));
394: addControl(evenColor);
395:
396: final SCheckBox reverseColumnOrder = new SCheckBox(
397: "reverse columns");
398: reverseColumnOrder
399: .addActionListener(new java.awt.event.ActionListener() {
400: public void actionPerformed(ActionEvent e) {
401: STableColumnModel columnModel = table
402: .getColumnModel();
403: Collections
404: .reverse((java.util.List) columnModel
405: .getColumns());
406: table.removeEditor();
407: table.reload();
408: }
409: });
410: addControl(reverseColumnOrder);
411:
412: final SCheckBox hideSomeColumns = new SCheckBox(
413: "hide some columns");
414: hideSomeColumns
415: .addActionListener(new java.awt.event.ActionListener() {
416: public void actionPerformed(ActionEvent e) {
417: SDefaultTableColumnModel columnModel = (SDefaultTableColumnModel) table
418: .getColumnModel();
419: for (int i = 0; i < columnModel
420: .getColumnCount(); ++i) {
421: if (i % 3 == 0) {
422: STableColumn column = columnModel
423: .getColumn(i);
424: columnModel.setColumnHidden(column,
425: hideSomeColumns
426: .isSelected());
427: }
428: }
429: }
430: });
431: addControl(hideSomeColumns);
432: }
433: }
434: }
|