KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > testscreen > TableTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive.testscreen;
31
32 import nextapp.echo2.app.Alignment;
33 import nextapp.echo2.app.Border;
34 import nextapp.echo2.app.Color;
35 import nextapp.echo2.app.Column;
36 import nextapp.echo2.app.Component;
37 import nextapp.echo2.app.Extent;
38 import nextapp.echo2.app.Insets;
39 import nextapp.echo2.app.Label;
40 import nextapp.echo2.app.SelectField;
41 import nextapp.echo2.app.SplitPane;
42 import nextapp.echo2.app.Table;
43 import nextapp.echo2.app.TextField;
44 import nextapp.echo2.app.event.ActionEvent;
45 import nextapp.echo2.app.event.ActionListener;
46 import nextapp.echo2.app.event.ChangeEvent;
47 import nextapp.echo2.app.event.ChangeListener;
48 import nextapp.echo2.app.layout.SplitPaneLayoutData;
49 import nextapp.echo2.app.layout.TableLayoutData;
50 import nextapp.echo2.app.list.AbstractListModel;
51 import nextapp.echo2.app.list.ListModel;
52 import nextapp.echo2.app.list.ListSelectionModel;
53 import nextapp.echo2.app.table.AbstractTableModel;
54 import nextapp.echo2.app.table.DefaultTableCellRenderer;
55 import nextapp.echo2.app.table.DefaultTableModel;
56 import nextapp.echo2.app.table.TableCellRenderer;
57 import nextapp.echo2.app.table.TableColumnModel;
58 import nextapp.echo2.app.table.TableModel;
59 import nextapp.echo2.testapp.interactive.ButtonColumn;
60 import nextapp.echo2.testapp.interactive.InteractiveApp;
61 import nextapp.echo2.testapp.interactive.StyleUtil;
62 import nextapp.echo2.testapp.interactive.Styles;
63
64 /**
65  * A test for <code>Tables</code>s.
66  */

67 public class TableTest extends SplitPane {
68     
69     private static class PayGrade {
70         
71         public int payGrade;
72         
73         public PayGrade(int payGrade) {
74             this.payGrade = payGrade;
75         }
76         
77         public String JavaDoc toString() {
78             return "Level " + payGrade;
79         }
80     }
81     
82     public static TableModel createEmployeeTableModel() {
83         DefaultTableModel model = new DefaultTableModel();
84         model.setColumnCount(4);
85         model.insertRow(0, new Object JavaDoc[]{"Bob Johnson", "bob.johnson@test.nextapp.com", "949.555.1234", new PayGrade(10)});
86         model.insertRow(0, new Object JavaDoc[]{"Laura Smith", "laura.smith@test.nextapp.com", "217.555.9343", new PayGrade(6)});
87         model.insertRow(0, new Object JavaDoc[]{"Jenny Roberts", "jenny.roberts@test.nextapp.com", "630.555.1987", new PayGrade(6)});
88         model.insertRow(0, new Object JavaDoc[]{"Thomas Albertson", "thomas.albertson@test.nextapp.com", "619.555.1233", new PayGrade(3)});
89         model.insertRow(0, new Object JavaDoc[]{"Albert Thomas", "albert.thomas@test.nextapp.com", "408.555.3232", new PayGrade(11)});
90         model.insertRow(0, new Object JavaDoc[]{"Sheila Simmons", "sheila.simmons@test.nextapp.com", "212.555.8700", new PayGrade(6)});
91         model.insertRow(0, new Object JavaDoc[]{"Mark Atkinson", "mark.atkinson@test.nextapp.com", "213.555.9456", new PayGrade(3)});
92         model.insertRow(0, new Object JavaDoc[]{"Linda Jefferson", "linda.jefferson@test.nextapp.com", "949.555.8925", new PayGrade(4)});
93         model.insertRow(0, new Object JavaDoc[]{"Yvonne Adams", "yvonne.adams@test.nextapp.com", "714.555.8543", new PayGrade(5)});
94         return model;
95     }
96     
97     private class MultiplicationTableModel extends AbstractTableModel {
98
99         /**
100          * @see nextapp.echo2.app.table.TableModel#getColumnCount()
101          */

102         public int getColumnCount() {
103             return 12;
104         }
105         
106         /**
107          * @see nextapp.echo2.app.table.TableModel#getRowCount()
108          */

109         public int getRowCount() {
110             return 12;
111         }
112         
113         /**
114          * @see nextapp.echo2.app.table.TableModel#getValueAt(int, int)
115          */

116         public Object JavaDoc getValueAt(int column, int row) {
117             return new Integer JavaDoc((column + 1) * (row + 1));
118         }
119     }
120     
121     private Table testTable;
122     
123     private TableCellRenderer editingTableCellRenderer = new TableCellRenderer() {
124         
125         /**
126          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
127          * java.lang.Object, int, int)
128          */

129         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
130             if (value instanceof PayGrade) {
131                 ListModel listModel = new AbstractListModel() {
132                     
133                     public Object JavaDoc get(int index) {
134                         return new Integer JavaDoc(index + 3);
135                     }
136     
137                     public int size() {
138                         return 10;
139                     }
140                 };
141                 final SelectField selectField = new SelectField(listModel);
142                 selectField.setSelectedIndex(((PayGrade) value).payGrade - 3);
143                 selectField.addActionListener(new ActionListener() {
144
145                     public void actionPerformed(ActionEvent e) {
146                         selectField.setBackground(StyleUtil.randomBrightColor());
147                     }
148                 });
149                 return selectField;
150             } else {
151                 TextField textField = new TextField();
152                 if (value != null) {
153                     textField.setText(value.toString());
154                 }
155                 return textField;
156             }
157         }
158     };
159     
160     private TableCellRenderer randomizingCellRenderer = new TableCellRenderer() {
161         
162         /**
163          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
164          * java.lang.Object, int, int)
165          */

166         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
167             Label label = new Label(value == null ? null : value.toString());
168             TableLayoutData layoutData = new TableLayoutData();
169             layoutData.setBackground(StyleUtil.randomBrightColor());
170             layoutData.setInsets(new Insets(StyleUtil.randomExtent(12), StyleUtil.randomExtent(12), StyleUtil.randomExtent(12),
171                     StyleUtil.randomExtent(12)));
172             layoutData.setAlignment(StyleUtil.randomAlignmentHV());
173             label.setLayoutData(layoutData);
174             return label;
175         }
176     };
177     
178     private TableCellRenderer backgroundImageCheckerCellRenderer = new TableCellRenderer() {
179         
180         /**
181          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
182          * java.lang.Object, int, int)
183          */

184         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
185             Label label = new Label(value == null ? null : value.toString());
186             TableLayoutData layoutData = new TableLayoutData();
187             layoutData.setInsets(new Insets(5));
188             if (row % 2 == column % 2) {
189                 layoutData.setBackgroundImage(Styles.BG_SHADOW_DARK_BLUE);
190             } else {
191                 layoutData.setBackgroundImage(Styles.BG_SHADOW_LIGHT_BLUE);
192             }
193             label.setLayoutData(layoutData);
194             return label;
195         }
196     };
197     
198     private TableCellRenderer visibilityCheckerCellRenderer = new TableCellRenderer() {
199         
200         /**
201          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
202          * java.lang.Object, int, int)
203          */

204         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
205             Label label = new Label(value == null ? null : value.toString());
206             if (row % 2 == column % 2) {
207                 label.setVisible(false);
208             }
209             return label;
210         }
211     };
212     
213     /**
214      * Writes <code>ActionEvent</code>s to console.
215      */

216     private ActionListener actionListener = new ActionListener() {
217
218         /**
219          * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
220          */

221         public void actionPerformed(ActionEvent e) {
222             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
223         }
224     };
225     
226     /**
227      * Writes <code>ChangeEvent</code>s to console.
228      */

229     private ChangeListener changeListener = new ChangeListener() {
230
231         /**
232          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
233          */

234         public void stateChanged(ChangeEvent e) {
235             ((InteractiveApp) getApplicationInstance()).consoleWrite(e.toString());
236         }
237     };
238     
239     public TableTest() {
240         super(SplitPane.ORIENTATION_HORIZONTAL, new Extent(250, Extent.PX));
241         setStyleName("DefaultResizable");
242         
243         Column groupContainerColumn = new Column();
244         groupContainerColumn.setCellSpacing(new Extent(5));
245         groupContainerColumn.setStyleName("TestControlsColumn");
246         add(groupContainerColumn);
247         
248         Column testColumn = new Column();
249         SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
250         splitPaneLayoutData.setInsets(new Insets(10, 5));
251         testColumn.setLayoutData(splitPaneLayoutData);
252         add(testColumn);
253
254         ButtonColumn controlsColumn;
255         
256         controlsColumn = new ButtonColumn();
257         groupContainerColumn.add(controlsColumn);
258
259         controlsColumn.add(new Label("TableModel"));
260         
261         controlsColumn.addButton("Multiplication Model", new ActionListener() {
262             public void actionPerformed(ActionEvent e) {
263                 testTable.setModel(new MultiplicationTableModel());
264             }
265         });
266         
267         controlsColumn.addButton("DefaultTableModel (Empty)", new ActionListener() {
268             public void actionPerformed(ActionEvent e) {
269                 testTable.setModel(new DefaultTableModel());
270             }
271         });
272         
273         controlsColumn.addButton("DefaultTableModel (Employees)", new ActionListener() {
274             public void actionPerformed(ActionEvent e) {
275                 testTable.setModel(createEmployeeTableModel());
276             }
277         });
278         
279         testTable = new Table();
280         testTable.setBorder(new Border(new Extent(1), Color.BLUE, Border.STYLE_SOLID));
281         testColumn.add(testTable);
282
283         controlsColumn.add(new Label("Appearance"));
284         
285         controlsColumn.addButton("Change Foreground", new ActionListener() {
286             public void actionPerformed(ActionEvent e) {
287                 testTable.setForeground(StyleUtil.randomColor());
288             }
289         });
290         controlsColumn.addButton("Change Background", new ActionListener() {
291             public void actionPerformed(ActionEvent e) {
292                 testTable.setBackground(StyleUtil.randomColor());
293             }
294         });
295         controlsColumn.addButton("Change Border (All Attributes)", new ActionListener() {
296             public void actionPerformed(ActionEvent e) {
297                 testTable.setBorder(StyleUtil.randomBorder());
298             }
299         });
300         controlsColumn.addButton("Change Border Color", new ActionListener() {
301             public void actionPerformed(ActionEvent e) {
302                 Border border = testTable.getBorder();
303                 testTable.setBorder(new Border(border.getSize(), StyleUtil.randomColor(), border.getStyle()));
304             }
305         });
306         controlsColumn.addButton("Change Border Size", new ActionListener() {
307             public void actionPerformed(ActionEvent e) {
308                 testTable.setBorder(StyleUtil.nextBorderSize(testTable.getBorder()));
309             }
310         });
311         controlsColumn.addButton("Change Border Style", new ActionListener() {
312             public void actionPerformed(ActionEvent e) {
313                 testTable.setBorder(StyleUtil.nextBorderStyle(testTable.getBorder()));
314             }
315         });
316         
317         controlsColumn.addButton("Set Insets 0px", new ActionListener() {
318             public void actionPerformed(ActionEvent e) {
319                 testTable.setInsets(new Insets(0));
320             }
321         });
322         controlsColumn.addButton("Set Insets 2px", new ActionListener() {
323             public void actionPerformed(ActionEvent e) {
324                 testTable.setInsets(new Insets(2));
325             }
326         });
327         controlsColumn.addButton("Set Insets 10/5px", new ActionListener() {
328             public void actionPerformed(ActionEvent e) {
329                 testTable.setInsets(new Insets(10, 5));
330             }
331         });
332         controlsColumn.addButton("Set Insets 10/20/30/40px", new ActionListener() {
333             public void actionPerformed(ActionEvent e) {
334                 testTable.setInsets(new Insets(10, 20, 30, 40));
335             }
336         });
337         controlsColumn.addButton("Set Width = null", new ActionListener() {
338             public void actionPerformed(ActionEvent e) {
339                 testTable.setWidth(null);
340             }
341         });
342         controlsColumn.addButton("Set Width = 500px", new ActionListener() {
343             public void actionPerformed(ActionEvent e) {
344                 testTable.setWidth(new Extent(500));
345             }
346         });
347         controlsColumn.addButton("Set Width = 100%", new ActionListener() {
348             public void actionPerformed(ActionEvent e) {
349                 testTable.setWidth(new Extent(100, Extent.PERCENT));
350             }
351         });
352         controlsColumn.addButton("Set ColumnWidths Equal", new ActionListener() {
353             public void actionPerformed(ActionEvent e) {
354                 TableColumnModel columnModel = testTable.getColumnModel();
355                 int columnCount = columnModel.getColumnCount();
356                     if (columnCount > 0) {
357                     Extent width = new Extent(100 / columnCount, Extent.PERCENT);
358                     for (int i = 0; i < columnCount; ++i) {
359                         columnModel.getColumn(i).setWidth(width);
360                     }
361                 }
362             }
363         });
364         controlsColumn.addButton("Toggle Header Visible", new ActionListener() {
365             public void actionPerformed(ActionEvent e) {
366                 testTable.setHeaderVisible(!testTable.isHeaderVisible());
367             }
368         });
369         controlsColumn.addButton("Toggle Enabled State", new ActionListener() {
370             public void actionPerformed(ActionEvent e) {
371                 testTable.setEnabled(!testTable.isEnabled());
372             }
373         });
374         
375         // Rollover Effect Settings
376

377         controlsColumn = new ButtonColumn();
378         groupContainerColumn.add(controlsColumn);
379         
380         controlsColumn.add(new Label("Rollover Effects"));
381
382         controlsColumn.addButton("Enable Rollover Effects", new ActionListener() {
383             public void actionPerformed(ActionEvent e) {
384                 testTable.setRolloverEnabled(true);
385             }
386         });
387         controlsColumn.addButton("Disable Rollover Effects", new ActionListener() {
388             public void actionPerformed(ActionEvent e) {
389                 testTable.setRolloverEnabled(false);
390             }
391         });
392         controlsColumn.addButton("Set Rollover Foreground", new ActionListener() {
393             public void actionPerformed(ActionEvent e) {
394                 testTable.setRolloverForeground(StyleUtil.randomColor());
395             }
396         });
397         controlsColumn.addButton("Clear Rollover Foreground", new ActionListener() {
398             public void actionPerformed(ActionEvent e) {
399                 testTable.setRolloverForeground(null);
400             }
401         });
402         controlsColumn.addButton("Set Rollover Background", new ActionListener() {
403             public void actionPerformed(ActionEvent e) {
404                  testTable.setRolloverBackground(StyleUtil.randomColor());
405             }
406         });
407         controlsColumn.addButton("Clear Rollover Background", new ActionListener() {
408             public void actionPerformed(ActionEvent e) {
409                 testTable.setRolloverBackground(null);
410             }
411         });
412         controlsColumn.addButton("Set Rollover Font", new ActionListener() {
413             public void actionPerformed(ActionEvent e) {
414                 testTable.setRolloverFont(StyleUtil.randomFont());
415             }
416         });
417         controlsColumn.addButton("Clear Rollover Font", new ActionListener() {
418             public void actionPerformed(ActionEvent e) {
419                 testTable.setRolloverFont(null);
420             }
421         });
422         controlsColumn.addButton("Set Rollover Background Image", new ActionListener() {
423             public void actionPerformed(ActionEvent e) {
424                 testTable.setRolloverBackgroundImage(Styles.BG_SHADOW_LIGHT_BLUE);
425             }
426         });
427         controlsColumn.addButton("Clear Rollover Background Image", new ActionListener() {
428             public void actionPerformed(ActionEvent e) {
429                 testTable.setRolloverBackgroundImage(null);
430             }
431         });
432         
433         // Selection Settings
434

435         controlsColumn = new ButtonColumn();
436         groupContainerColumn.add(controlsColumn);
437         
438         controlsColumn.add(new Label("Selection"));
439
440         controlsColumn.addButton("Enable Selection", new ActionListener() {
441             public void actionPerformed(ActionEvent e) {
442                 testTable.setSelectionEnabled(true);
443             }
444         });
445         controlsColumn.addButton("Disable Selection", new ActionListener() {
446             public void actionPerformed(ActionEvent e) {
447                 testTable.setSelectionEnabled(false);
448             }
449         });
450         controlsColumn.addButton("Set SelectionMode = Single", new ActionListener() {
451             public void actionPerformed(ActionEvent e) {
452                 testTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
453             }
454         });
455         controlsColumn.addButton("Set SelectionMode = Multiple", new ActionListener() {
456             public void actionPerformed(ActionEvent e) {
457                 testTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION);
458             }
459         });
460         controlsColumn.addButton("Toggle Selection of Row #2", new ActionListener() {
461             public void actionPerformed(ActionEvent e) {
462                 ListSelectionModel selectionModel = testTable.getSelectionModel();
463                 selectionModel.setSelectedIndex(2, !selectionModel.isSelectedIndex(2));
464             }
465         });
466         controlsColumn.addButton("Toggle Selection of Row #500 (there isn't one)", new ActionListener() {
467             public void actionPerformed(ActionEvent e) {
468                 ListSelectionModel selectionModel = testTable.getSelectionModel();
469                 selectionModel.setSelectedIndex(500, !selectionModel.isSelectedIndex(500));
470             }
471         });
472         controlsColumn.addButton("Set Selection Foreground", new ActionListener() {
473             public void actionPerformed(ActionEvent e) {
474                 testTable.setSelectionForeground(StyleUtil.randomColor());
475             }
476         });
477         controlsColumn.addButton("Clear Selection Foreground", new ActionListener() {
478             public void actionPerformed(ActionEvent e) {
479                 testTable.setSelectionForeground(null);
480             }
481         });
482         controlsColumn.addButton("Set Selection Background", new ActionListener() {
483             public void actionPerformed(ActionEvent e) {
484                  testTable.setSelectionBackground(StyleUtil.randomColor());
485             }
486         });
487         controlsColumn.addButton("Clear Selection Background", new ActionListener() {
488             public void actionPerformed(ActionEvent e) {
489                 testTable.setSelectionBackground(null);
490             }
491         });
492         controlsColumn.addButton("Set Selection Font", new ActionListener() {
493             public void actionPerformed(ActionEvent e) {
494                 testTable.setSelectionFont(StyleUtil.randomFont());
495             }
496         });
497         controlsColumn.addButton("Clear Selection Font", new ActionListener() {
498             public void actionPerformed(ActionEvent e) {
499                 testTable.setSelectionFont(null);
500             }
501         });
502         controlsColumn.addButton("Set Selection Background Image", new ActionListener() {
503             public void actionPerformed(ActionEvent e) {
504                 testTable.setSelectionBackgroundImage(Styles.BUTTON_PRESSED_BACKGROUND_IMAGE);
505             }
506         });
507         controlsColumn.addButton("Clear Selection Background Image", new ActionListener() {
508             public void actionPerformed(ActionEvent e) {
509                 testTable.setSelectionBackgroundImage(null);
510             }
511         });
512         
513         // Listener Settings
514

515         controlsColumn = new ButtonColumn();
516         groupContainerColumn.add(controlsColumn);
517         
518         controlsColumn.add(new Label("Listeners"));
519
520         controlsColumn.addButton("Add ActionListener", new ActionListener() {
521             public void actionPerformed(ActionEvent e) {
522                 testTable.addActionListener(actionListener);
523             }
524         });
525         controlsColumn.addButton("Remove ActionListener", new ActionListener() {
526             public void actionPerformed(ActionEvent e) {
527                 testTable.removeActionListener(actionListener);
528             }
529         });
530         controlsColumn.addButton("Add ChangeListener", new ActionListener() {
531             public void actionPerformed(ActionEvent e) {
532                 testTable.getSelectionModel().addChangeListener(changeListener);
533             }
534         });
535         controlsColumn.addButton("Remove ChangeListener", new ActionListener() {
536             public void actionPerformed(ActionEvent e) {
537                 testTable.getSelectionModel().removeChangeListener(changeListener);
538             }
539         });
540         
541         // Cell Settings
542

543         controlsColumn = new ButtonColumn();
544         groupContainerColumn.add(controlsColumn);
545         
546         controlsColumn.add(new Label("Cell Renderer"));
547
548         controlsColumn.addButton("Default Cell Renderer", new ActionListener() {
549             public void actionPerformed(ActionEvent e) {
550                 testTable.setDefaultRenderer(Object JavaDoc.class, new DefaultTableCellRenderer());
551             }
552         });
553         controlsColumn.addButton("Randomizing Cell Renderer", new ActionListener() {
554             public void actionPerformed(ActionEvent e) {
555                 testTable.setDefaultRenderer(Object JavaDoc.class, randomizingCellRenderer);
556             }
557         });
558         controlsColumn.addButton("BackgroundImage Checker Cell Renderer", new ActionListener() {
559             public void actionPerformed(ActionEvent e) {
560                 testTable.setDefaultRenderer(Object JavaDoc.class, backgroundImageCheckerCellRenderer);
561             }
562         });
563         controlsColumn.addButton("Visibility Checker Cell Renderer", new ActionListener() {
564             public void actionPerformed(ActionEvent e) {
565                 testTable.setDefaultRenderer(Object JavaDoc.class, visibilityCheckerCellRenderer);
566             }
567         });
568         controlsColumn.addButton("Editing Cell Renderer (not bound to model)", new ActionListener() {
569             public void actionPerformed(ActionEvent e) {
570                 testTable.setDefaultRenderer(Object JavaDoc.class, editingTableCellRenderer);
571             }
572         });
573         controlsColumn.addButton("Alignment = Leading/Top", new ActionListener() {
574             public void actionPerformed(ActionEvent e) {
575                 testTable.setDefaultRenderer(Object JavaDoc.class,
576                         createTableCellRenderer(new Alignment(Alignment.LEADING, Alignment.TOP)));
577             }
578         });
579         controlsColumn.addButton("Alignment = Trailing/Bottom", new ActionListener() {
580             public void actionPerformed(ActionEvent e) {
581                 testTable.setDefaultRenderer(Object JavaDoc.class,
582                         createTableCellRenderer(new Alignment(Alignment.TRAILING, Alignment.BOTTOM)));
583             }
584         });
585         controlsColumn.addButton("Alignment = Left/Top", new ActionListener() {
586             public void actionPerformed(ActionEvent e) {
587                 testTable.setDefaultRenderer(Object JavaDoc.class,
588                         createTableCellRenderer(new Alignment(Alignment.LEFT, Alignment.TOP)));
589             }
590         });
591         controlsColumn.addButton("Alignment = Right/Bottom", new ActionListener() {
592             public void actionPerformed(ActionEvent e) {
593                 testTable.setDefaultRenderer(Object JavaDoc.class,
594                         createTableCellRenderer(new Alignment(Alignment.RIGHT, Alignment.BOTTOM)));
595             }
596         });
597     }
598
599     private TableCellRenderer createTableCellRenderer(final Alignment alignment) {
600         return new TableCellRenderer() {
601
602             /**
603              * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
604              * java.lang.Object, int, int)
605              */

606             public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
607                 Label label = new Label(value == null ? null : value.toString());
608                 TableLayoutData layoutData = new TableLayoutData();
609                 layoutData.setAlignment(alignment);
610                 label.setLayoutData(layoutData);
611                 return label;
612             }
613         };
614     }
615     
616 }
617
Popular Tags