001: package wingset;
002:
003: import org.wingx.XTable;
004: import org.wingx.XScrollPane;
005: import org.wingx.table.*;
006: import org.wings.*;
007: import org.wings.table.STableColumnModel;
008: import org.wings.style.CSSProperty;
009: import org.wings.style.CSSStyleSheet;
010: import org.wings.plaf.css.TableCG;
011: import org.wings.event.SMouseListener;
012: import org.wings.event.SMouseEvent;
013:
014: import javax.swing.table.TableModel;
015: import java.awt.*;
016: import java.awt.event.*;
017: import java.util.*;
018:
019: /**
020: * @author Holger Engels
021: */
022: public class XScrollPaneExample extends XTableExample {
023: private XTable table;
024: private SLabel clicks = new SLabel();
025: private XScrollPaneExample.TableControls controls;
026: private boolean consume = false;
027:
028: protected SComponent createControls() {
029: controls = new TableControls();
030: return controls;
031: }
032:
033: public SComponent createExample() {
034: table = new XTable(new XScrollPaneExample.MyTableModel(7, 100));
035: table.setName("xScrollPaneExample");
036: table.setShowGrid(true);
037: table.setSelectionMode(STable.NO_SELECTION);
038: table.setDefaultRenderer(cellRenderer);
039: table.setShowAsFormComponent(true);
040: table.setEditable(false);
041:
042: ((XTableColumn) table.getColumnModel().getColumn(0))
043: .setSortable(true);
044: ((XTableColumn) table.getColumnModel().getColumn(1))
045: .setSortable(true);
046: ((XTableColumn) table.getColumnModel().getColumn(5))
047: .setSortable(true);
048: ((XTableColumn) table.getColumnModel().getColumn(6))
049: .setSortable(true);
050:
051: ((XTableColumn) table.getColumnModel().getColumn(0))
052: .setFilterRenderer(new StringFilterRenderer());
053: ((XTableColumn) table.getColumnModel().getColumn(0))
054: .setFilterable(true);
055:
056: ((XTableColumn) table.getColumnModel().getColumn(4))
057: .setFilterRenderer(new BooleanFilterRenderer());
058: ((XTableColumn) table.getColumnModel().getColumn(4))
059: .setCellRenderer(new BooleanEditableCellRenderer());
060: ((XTableColumn) table.getColumnModel().getColumn(4))
061: .setFilterable(true);
062:
063: table.getColumnModel().getColumn(0).setWidth("200px");
064: table.getColumnModel().getColumn(1).setWidth("*");
065: table.getColumnModel().getColumn(2).setWidth("100px");
066: table.getColumnModel().getColumn(3).setWidth("50px");
067: table.getColumnModel().getColumn(4).setWidth("50px");
068: table.getColumnModel().getColumn(5).setWidth("50px");
069: table.getColumnModel().getColumn(6).setWidth("100px");
070:
071: table.addMouseListener(new SMouseListener() {
072: public void mouseClicked(SMouseEvent e) {
073: if (consume && table.columnAtPoint(e.getPoint()) == 1)
074: e.consume();
075: clicks.setText("clicked " + e.getPoint());
076: }
077: });
078:
079: XScrollPane scrollPane = new XScrollPane(table);
080: scrollPane.setVerticalAlignment(SConstants.TOP_ALIGN);
081: controls.addControllable(scrollPane);
082:
083: SPanel panel = new SPanel(new SBorderLayout());
084: panel.add(scrollPane, SBorderLayout.CENTER);
085: panel.add(clicks, SBorderLayout.SOUTH);
086: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
087: return panel;
088: }
089:
090: class MyTableModel extends XTableModel {
091: int cols, rows;
092:
093: Object[][] origData;
094: Object[][] data;
095:
096: MyTableModel(int pCols, int pRows) {
097: this .cols = pCols > 6 ? pCols : 6;
098: this .rows = pRows;
099:
100: origData = new Object[rows][cols];
101:
102: for (int c = 0; c < cols; c++) {
103: for (int r = 0; r < rows; r++)
104: origData[r][c] = (c == 1 ? "stretched cell "
105: : "cell ")
106: + r + ":" + c;
107: }
108: for (int r = 0; r < rows; r++)
109: origData[r][2] = createColor(r);
110: for (int r = 0; r < rows; r++)
111: origData[r][3] = createImage(r);
112: for (int r = 0; r < rows; r++)
113: origData[r][4] = createBoolean(r);
114: for (int r = 0; r < rows; r++)
115: origData[r][5] = createInteger(r);
116:
117: refresh();
118: }
119:
120: public int getColumnCount() {
121: return cols;
122: }
123:
124: public String getColumnName(int col) {
125: return "col " + col;
126: }
127:
128: public int getRowCount() {
129: return data.length;
130: }
131:
132: public Object getValueAt(int row, int col) {
133: return data[row][col];
134: }
135:
136: public void setValueAt(Object value, int row, int col) {
137: if (value == null)
138: data[row][col] = null;
139: else if (getColumnClass(col).isAssignableFrom(String.class))
140: data[row][col] = value.toString();
141: else if (getColumnClass(col)
142: .isAssignableFrom(Boolean.class))
143: data[row][col] = new Boolean(((Boolean) value)
144: .booleanValue());
145: fireTableCellUpdated(row, col);
146: }
147:
148: public Class 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.class;
156: case 5:
157: return Integer.class;
158: }
159: return String.class;
160: }
161:
162: public Color createColor(int row) {
163: return colors[row % colors.length];
164: }
165:
166: public SIcon createImage(int row) {
167: return image;
168: }
169:
170: public Boolean createBoolean(int row) {
171: if (row % 2 == 1)
172: return new Boolean(false);
173: else
174: return new Boolean(true);
175: }
176:
177: public Integer createInteger(int row) {
178: return new Integer(row);
179: }
180:
181: public boolean isCellEditable(int row, int col) {
182: if (getColumnClass(col).isAssignableFrom(String.class)
183: || getColumnClass(col).isAssignableFrom(
184: Boolean.class))
185: return true;
186: else
187: return false;
188: }
189:
190: public void refresh() {
191: java.util.List list = new ArrayList(Arrays
192: .asList((Object[][]) origData.clone()));
193: for (Iterator iterator = list.iterator(); iterator
194: .hasNext();) {
195: Object[] r = (Object[]) iterator.next();
196: for (int i = 0; i < getColumnCount(); i++) {
197: Object filter = getFilter(i);
198: if (filter != null) {
199: if (getColumnClass(i) == String.class) {
200: if (r[i] == null
201: || !(((String) r[i])
202: .indexOf((String) filter) > 0))
203: iterator.remove();
204: } else if (getColumnClass(i) == Boolean.class) {
205: if (!filter.equals(r[i]))
206: iterator.remove();
207: }
208: }
209: }
210: }
211: data = (Object[][]) list.toArray(new Object[0][0]);
212:
213: Arrays.sort(data, new Comparator() {
214: public int compare(Object o1, Object o2) {
215: for (int i = 0; i < getColumnCount(); i++) {
216: Object[] r1 = (Object[]) o1;
217: Object[] r2 = (Object[]) o2;
218: int comparision;
219:
220: switch (getSort(i)) {
221: case SORT_ASCENDING: {
222: Comparable v1 = (Comparable) r1[i];
223: Comparable v2 = (Comparable) r2[i];
224: comparision = v1.compareTo(v2);
225: if (comparision != 0)
226: return comparision;
227: }
228: case SORT_DESCENDING: {
229: Comparable v1 = (Comparable) r1[i];
230: Comparable v2 = (Comparable) r2[i];
231: comparision = v2.compareTo(v1);
232: if (comparision != 0)
233: return comparision;
234: }
235: default:
236: comparision = 0;
237: }
238: }
239: return 0;
240: }
241: });
242:
243: fireTableDataChanged();
244: }
245: }
246:
247: /**
248: * Proof that we can do some really nice tables with j-wings.
249: */
250: public class MyTable extends STable {
251: private final/*static*/TableCG myTableCG = new TableCG();
252:
253: public MyTable(TableModel tm) {
254: super (tm);
255: myTableCG.setFixedTableBorderWidth("0");
256: setCG(myTableCG);
257: }
258:
259: /**
260: * Returns the CSS style for a row (<td style="xxx")
261: */
262: public String getRowStyle(int row) {
263: return isRowSelected(row) ? "table_selected_row"
264: : (row % 2 == 0 ? "table_row1" : "table_row2");
265: }
266: }
267:
268: class TableControls extends ComponentControls {
269: private final String[] SELECTION_MODES = new String[] { "no",
270: "single", "multiple" };
271:
272: public TableControls() {
273: formComponentCheckBox.setSelected(true);
274:
275: final SCheckBox editable = new SCheckBox("editable");
276: editable
277: .addActionListener(new java.awt.event.ActionListener() {
278: public void actionPerformed(ActionEvent e) {
279: table.setEditable(editable.isSelected());
280: }
281: });
282:
283: final SCheckBox consume = new SCheckBox("consume events");
284: consume
285: .setToolTipText("<html>A SMouseListener will intercept the mouse clicks.<br>"
286: + "It will consume events on col 1, so that they're not be processed by the table anymore");
287: consume
288: .addActionListener(new java.awt.event.ActionListener() {
289: public void actionPerformed(ActionEvent e) {
290: XScrollPaneExample.this .consume = consume
291: .isSelected();
292: }
293: });
294:
295: final SComboBox selectionMode = new SComboBox(
296: SELECTION_MODES);
297: selectionMode.addItemListener(new ItemListener() {
298: public void itemStateChanged(ItemEvent e) {
299: if ("no".equals(selectionMode.getSelectedItem()))
300: table.setSelectionMode(STable.NO_SELECTION);
301: else if ("single".equals(selectionMode
302: .getSelectedItem()))
303: table.setSelectionMode(STable.SINGLE_SELECTION);
304: else if ("multiple".equals(selectionMode
305: .getSelectedItem()))
306: table
307: .setSelectionMode(STable.MULTIPLE_SELECTION);
308: }
309: });
310:
311: addControl(editable);
312: addControl(new SLabel(""));
313: addControl(consume);
314: addControl(new SLabel(" selection"));
315: addControl(selectionMode);
316:
317: final SComboBox headerColor = new SComboBox(COLORS);
318: headerColor.addActionListener(new ActionListener() {
319: public void actionPerformed(ActionEvent e) {
320: Color color = (Color) ((Object[]) headerColor
321: .getSelectedItem())[1];
322: table.setAttribute(STable.SELECTOR_HEADER,
323: CSSProperty.BACKGROUND_COLOR, CSSStyleSheet
324: .getAttribute(color));
325: }
326: });
327: headerColor.setRenderer(new ObjectPairCellRenderer());
328: addControl(new SLabel(" header"));
329: addControl(headerColor);
330:
331: final SComboBox oddColor = new SComboBox(COLORS);
332: oddColor.addActionListener(new ActionListener() {
333: public void actionPerformed(ActionEvent e) {
334: Color color = (Color) ((Object[]) oddColor
335: .getSelectedItem())[1];
336: table.setAttribute(STable.SELECTOR_ODD_ROWS,
337: CSSProperty.BACKGROUND_COLOR, CSSStyleSheet
338: .getAttribute(color));
339: }
340: });
341: oddColor.setRenderer(new ObjectPairCellRenderer());
342: addControl(new SLabel(" odd"));
343: addControl(oddColor);
344:
345: final SComboBox evenColor = new SComboBox(COLORS);
346: evenColor.addActionListener(new ActionListener() {
347: public void actionPerformed(ActionEvent e) {
348: Color color = (Color) ((Object[]) evenColor
349: .getSelectedItem())[1];
350: table.setAttribute(STable.SELECTOR_EVEN_ROWS,
351: CSSProperty.BACKGROUND_COLOR, CSSStyleSheet
352: .getAttribute(color));
353: }
354: });
355: evenColor.setRenderer(new ObjectPairCellRenderer());
356: addControl(new SLabel(" even"));
357: addControl(evenColor);
358:
359: final SCheckBox reverseColumnOrder = new SCheckBox(
360: "reverse columns");
361: reverseColumnOrder.addActionListener(new ActionListener() {
362: public void actionPerformed(ActionEvent e) {
363: STableColumnModel columnModel = table
364: .getColumnModel();
365: Collections.reverse((java.util.List) columnModel
366: .getColumns());
367: table.reload();
368: }
369: });
370: addControl(reverseColumnOrder);
371:
372: final SCheckBox hideSomeColumns = new SCheckBox(
373: "Hide some Columns");
374: hideSomeColumns.addActionListener(new ActionListener() {
375: public void actionPerformed(ActionEvent e) {
376: boolean hide = hideSomeColumns.isSelected();
377: STableColumnModel columnModel = table
378: .getColumnModel();
379: for (int i = 0; i < columnModel.getColumnCount(); i++) {
380: columnModel.getColumn(i).setHidden(
381: hide && i % 3 == 0);
382: }
383: }
384: });
385: addControl(hideSomeColumns);
386: }
387: }
388:
389: private static class StringFilterRenderer extends STextField
390: implements EditableTableCellRenderer {
391: public StringFilterRenderer() {
392: addActionListener(new NoAction());
393: }
394:
395: public Object getValue() {
396: String s = getText();
397: if ("".equals(s))
398: return null;
399: return s;
400: }
401:
402: public SComponent getTableCellRendererComponent(STable table,
403: Object value, boolean isSelected, int row, int column) {
404: setText(value != null ? value.toString() : null);
405: return this ;
406: }
407:
408: public LowLevelEventListener getLowLevelEventListener(
409: STable table, int row, int column) {
410: return this ;
411: }
412: }
413:
414: private static class BooleanEditableCellRenderer extends SCheckBox
415: implements EditableTableCellRenderer {
416: public Object getValue() {
417: return isSelected() ? Boolean.TRUE : Boolean.FALSE;
418: }
419:
420: public SComponent getTableCellRendererComponent(STable table,
421: Object value, boolean isSelected, int row, int column) {
422: setSelected(Boolean.TRUE.equals(value));
423: return this ;
424: }
425:
426: public LowLevelEventListener getLowLevelEventListener(
427: STable table, int row, int column) {
428: return this ;
429: }
430: }
431:
432: private static class BooleanFilterRenderer extends SComboBox
433: implements EditableTableCellRenderer {
434: public BooleanFilterRenderer() {
435: super (new Boolean[] { null, Boolean.TRUE, Boolean.FALSE });
436: addActionListener(new NoAction());
437: }
438:
439: public Object getValue() {
440: return getSelectedItem();
441: }
442:
443: public SComponent getTableCellRendererComponent(STable table,
444: Object value, boolean isSelected, int row, int column) {
445: setSelectedItem(value);
446: return this ;
447: }
448:
449: public LowLevelEventListener getLowLevelEventListener(
450: STable table, int row, int column) {
451: return this ;
452: }
453: }
454:
455: static class NoAction implements ActionListener {
456: public void actionPerformed(ActionEvent e) {
457: }
458: }
459: }
|