KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > XObjectTable


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import com.tc.util.runtime.Os;
7
8 import java.awt.BorderLayout JavaDoc;
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.awt.event.MouseAdapter JavaDoc;
14 import java.awt.event.MouseEvent JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.prefs.Preferences JavaDoc;
17
18 import javax.swing.JComponent JavaDoc;
19 import javax.swing.JTable JavaDoc;
20 import javax.swing.UIManager JavaDoc;
21 import javax.swing.border.BevelBorder JavaDoc;
22 import javax.swing.border.Border JavaDoc;
23 import javax.swing.table.JTableHeader JavaDoc;
24 import javax.swing.table.TableCellEditor JavaDoc;
25 import javax.swing.table.TableColumn JavaDoc;
26 import javax.swing.table.TableColumnModel JavaDoc;
27 import javax.swing.table.TableModel JavaDoc;
28
29 public class XObjectTable extends XTable {
30   protected int sortColumn;
31   protected int sortDirection;
32
33   private TableColumnRenderer columnRenderer;
34
35   public static final int UP = XObjectTableModel.UP;
36   public static final int DOWN = XObjectTableModel.DOWN;
37
38   private ArrowLabel arrowLabel;
39
40   private static final String JavaDoc SORT_COLUMN_PREF_KEY = "SortColumn";
41   private static final String JavaDoc SORT_DIRECTION_PREF_KEY = "SortDirection";
42   
43   public XObjectTable() {
44     super();
45     init();
46   }
47
48   public XObjectTable(TableModel JavaDoc model) {
49     super(model);
50     init();
51   }
52
53   private void init() {
54     sortColumn = -1;
55     sortDirection = DOWN;
56
57     arrowLabel = new ArrowLabel();
58
59     setDefaultRenderer(Method JavaDoc.class, new MethodRenderer());
60     setDefaultEditor(Method JavaDoc.class, new MethodEditor());
61
62     getTableHeader().addMouseListener(new MouseAdapter JavaDoc() {
63         public void mouseClicked(MouseEvent JavaDoc me) {
64           if(me.getClickCount() == 2) {
65             int col = columnAtPoint(me.getPoint());
66             
67             if(((XObjectTableModel)getModel()).isColumnSortable(col)) {
68               setSortDirection(toggleSortDirection());
69               setSortColumn(col);
70             }
71           }
72         }
73     });
74     columnRenderer = new TableColumnRenderer();
75
76     getTableHeader().setDefaultRenderer(columnRenderer);
77   }
78
79   public void createDefaultColumnsFromModel() {
80     super.createDefaultColumnsFromModel();
81
82     XObjectTableModel tableModel = (XObjectTableModel)getModel();
83     TableColumnModel JavaDoc colModel = getColumnModel();
84     TableColumn JavaDoc column;
85
86     for(int i = 0; i < colModel.getColumnCount(); i++) {
87       column = colModel.getColumn(i);
88       column.setHeaderRenderer(columnRenderer);
89       column.setIdentifier(tableModel.getFieldName(i));
90     }
91   }
92
93   public void addNotify() {
94     super.addNotify();
95     loadSortPrefs();
96   }
97   
98   private class TableColumnRenderer extends XTableCellRenderer {
99     private JComponent JavaDoc sortView;
100     private Border JavaDoc border;
101
102     public TableColumnRenderer() {
103       super();
104       sortView = new JComponent JavaDoc(){/**/};
105       sortView.setLayout(new BorderLayout JavaDoc());
106
107       border = Os.isMac() ? new BevelBorder JavaDoc(BevelBorder.RAISED) :
108         UIManager.getBorder("TableHeader.cellBorder");
109     }
110
111     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table,
112                                                    Object JavaDoc value,
113                                                    boolean isSelected,
114                                                    boolean hasFocus,
115                                                    int row,
116                                                    int column)
117     {
118       if(table != null) {
119         JTableHeader JavaDoc header = table.getTableHeader();
120
121         if(header != null) {
122           setForeground(header.getForeground());
123           setBackground(header.getBackground());
124           setFont(header.getFont());
125         }
126       }
127
128       String JavaDoc text = (value == null) ? "" : value.toString();
129       setText(text);
130
131       if(sortColumn != -1) {
132         if(column == sortColumn) {
133           arrowLabel.setDirection(sortDirection);
134
135           setBorder(null);
136           setOpaque(false);
137
138           sortView.setForeground(null);
139           sortView.setBackground(null);
140           sortView.setFont(null);
141
142           sortView.add(this, BorderLayout.WEST);
143           sortView.add(arrowLabel, BorderLayout.EAST);
144           sortView.setBorder(border);
145           return sortView;
146         }
147       }
148
149       setBorder(border);
150       return this;
151     }
152   }
153
154   private void internalSetSortColumn(int columnIndex) {
155     if((sortColumn = columnIndex) != -1) {
156       sort();
157     }
158   }
159   
160   public void setSortColumn(int columnIndex) {
161     internalSetSortColumn(columnIndex);
162     storeSortPrefs();
163   }
164
165   public int getSortColumn() {
166     return sortColumn;
167   }
168   
169   public void sort() {
170     if(sortColumn != -1) {
171       TableModel JavaDoc model = getModel();
172       JTableHeader JavaDoc header = getTableHeader();
173       
174       if(model != null && model instanceof XObjectTableModel) {
175         Object JavaDoc[] selection = getSelection();
176         
177         ((XObjectTableModel)model).sortColumn(sortColumn, sortDirection);
178
179         if(header != null) {
180           header.repaint();
181         }
182         
183         setSelection(selection);
184       }
185     }
186   }
187
188   private void internalSetSortDirection(int direction) {
189     sortDirection = direction;
190   }
191   
192   public void setSortDirection(int direction) {
193     internalSetSortDirection(direction);
194     storeSortPrefs();
195   }
196
197   public int getSortDirection() {
198     return sortDirection;
199   }
200   
201   public int toggleSortDirection() {
202     return sortDirection == UP ? DOWN : UP;
203   }
204
205   public Object JavaDoc[] getSelection() {
206     XObjectTableModel model = (XObjectTableModel)getModel();
207     int[] rows = getSelectedRows();
208     Object JavaDoc[] result = new Object JavaDoc[rows.length];
209     
210     for(int i = 0; i < rows.length; i++) {
211       result[i] = model.getObjectAt(rows[i]);
212     }
213     
214     return result;
215   }
216   
217   public void setSelection(Object JavaDoc[] selection) {
218     XObjectTableModel model = (XObjectTableModel)getModel();
219     int index;
220
221     clearSelection();
222     
223     for(int i = 0; i < selection.length; i++) {
224       index = model.getObjectIndex(selection[i]);
225       addRowSelectionInterval(index, index);
226     }
227   }
228   
229   protected TableModel JavaDoc createDefaultDataModel() {
230     return new XObjectTableModel();
231   }
232
233   public void setModel(TableModel JavaDoc model) {
234     super.setModel(model);
235     sort();
236   }
237
238   public void showColumnsExclusive(String JavaDoc[] fieldNames) {
239     Object JavaDoc[] selection = getSelection();
240     XObjectTableModel model = (XObjectTableModel)getModel();
241    
242     model.showColumnsExclusive(fieldNames);
243     setSelection(selection);
244   }
245   
246   public void showColumn(String JavaDoc fieldName) {
247     Object JavaDoc[] selection = getSelection();
248     XObjectTableModel model = (XObjectTableModel)getModel();
249    
250     model.showColumn(fieldName);
251     setSelection(selection);
252   }
253   
254   public void hideColumn(String JavaDoc fieldName) {
255     Object JavaDoc[] selection = getSelection();
256     XObjectTableModel model = (XObjectTableModel)getModel();
257    
258     model.hideColumn(fieldName);
259     if(getSortColumn() >= getColumnCount()) {
260       setSortColumn(getColumnCount()-1);
261     }
262     setSelection(selection);
263   }
264
265   public TableColumn JavaDoc findColumn(String JavaDoc fieldName) {
266     XObjectTableModel model = (XObjectTableModel)getModel();
267     int index = model.getShowingFieldIndex(fieldName);
268     
269     return index != -1 ? getColumnModel().getColumn(index) : null;
270   }
271
272   public int getShowingFieldCount() {
273     return getColumnCount();
274   }
275   
276   public String JavaDoc[] getShowingFields() {
277     return ((XObjectTableModel)getModel()).getShowingFields();
278   }
279
280   public boolean isColumnShowing(String JavaDoc fieldName) {
281     return ((XObjectTableModel)getModel()).isColumnShowing(fieldName);
282   }
283
284   class MethodRenderer extends XTableCellRenderer {
285     protected TableCellEditor JavaDoc m_editor;
286
287     public MethodRenderer() {
288       super();
289       m_editor = createCellEditor();
290     }
291
292     protected TableCellEditor JavaDoc createCellEditor() {
293       return new MethodEditor();
294     }
295
296     public java.awt.Component JavaDoc getTableCellRendererComponent(
297       JTable JavaDoc table,
298       Object JavaDoc value,
299       boolean isSelected,
300       boolean hasFocus,
301       int row,
302       int col)
303     {
304       return m_editor.getTableCellEditorComponent(
305                table, value, false, row, col);
306     }
307   }
308
309   class MethodEditor extends XCellEditor {
310     InvokerButton button;
311
312     MethodEditor() {
313       super(new XCheckBox());
314
315       m_editorComponent = button = new InvokerButton();
316       button.addActionListener(new ActionListener JavaDoc() {
317         public void actionPerformed(ActionEvent JavaDoc ae) {
318           int row = button.getRow();
319           int col = button.getCol();
320           XObjectTableModel model = (XObjectTableModel)getModel();
321           Method JavaDoc method = (Method JavaDoc)model.getValueAt(row, col);
322           Object JavaDoc obj = model.getObjectAt(row);
323
324           try {
325             method.invoke(obj, new Object JavaDoc[]{});
326             XObjectTable.this.repaint();
327           }
328           catch(Exception JavaDoc e) {
329             e.printStackTrace();
330           }
331         }
332       });
333       m_clicksToStart = 1;
334     }
335
336     public java.awt.Component JavaDoc getTableCellEditorComponent(
337       JTable JavaDoc table,
338       Object JavaDoc value,
339       boolean isSelected,
340       int row,
341       int col)
342     {
343       super.getTableCellEditorComponent(table, value, isSelected, row, col);
344
345       XObjectTableModel model = (XObjectTableModel)table.getModel();
346       Method JavaDoc method = (Method JavaDoc)model.getValueAt(row, col);
347
348       button.setText(method.getName());
349       button.setCell(row, col);
350       button.setForeground(table.getForeground());
351       button.setBackground(table.getBackground());
352       button.setFont(table.getFont());
353       
354       return button;
355     }
356   }
357
358   protected void loadSortPrefs() {
359     PrefsHelper helper = PrefsHelper.getHelper();
360     Preferences JavaDoc prefs = helper.userNodeForClass(getClass());
361     String JavaDoc s;
362
363     // It's important that we set the sortDirection prior to the sortColumn
364
// because the latter does an actual sort.
365

366     if((s = prefs.get(SORT_DIRECTION_PREF_KEY, null)) != null) {
367       try {
368         internalSetSortDirection(Integer.parseInt(s));
369       } catch(NumberFormatException JavaDoc nfe) {/**/}
370     }
371
372     if((s = prefs.get(SORT_COLUMN_PREF_KEY, null)) != null) {
373       try {
374         internalSetSortColumn(Integer.parseInt(s));
375       } catch(NumberFormatException JavaDoc nfe) {/**/}
376     }
377   }
378   
379   protected void storeSortPrefs() {
380     PrefsHelper helper = PrefsHelper.getHelper();
381     Preferences JavaDoc prefs = helper.userNodeForClass(getClass());
382
383     prefs.put(SORT_COLUMN_PREF_KEY, Integer.toString(getSortColumn()));
384     prefs.put(SORT_DIRECTION_PREF_KEY, Integer.toString(getSortDirection()));
385     
386     helper.flush(prefs);
387   }
388
389   class InvokerButton extends XButton {
390     private int row;
391     private int col;
392     private boolean ignoreNextMouseEvent;
393
394     public InvokerButton() {
395       super();
396
397       setFocusable(false);
398       setOpaque(true);
399     }
400
401     void setCell(int row, int col) {
402       setRow(row);
403       setCol(col);
404     }
405
406     void setRow(int row) {
407       this.row = row;
408     }
409
410     int getRow() {
411       return row;
412     }
413
414     void setCol(int col) {
415       this.col = col;
416     }
417
418     int getCol() {
419       return col;
420     }
421
422     void setIgnoreNextEvent(boolean b) {
423       ignoreNextMouseEvent = b;
424     }
425
426     boolean getIgnoreNextEvent() {
427       return ignoreNextMouseEvent;
428     }
429
430     public Dimension JavaDoc _getPreferredSize() {
431       Dimension JavaDoc d = super.getPreferredSize();
432
433       if(true || System.getProperty("os.name").equals("Mac OS X")) {
434         d.height = 20;
435       }
436
437       return d;
438     }
439
440     protected void processMouseEvent(MouseEvent JavaDoc e) {
441       if(!ignoreNextMouseEvent) {
442         super.processMouseEvent(e);
443       }
444       else {
445         requestFocus();
446       }
447
448       ignoreNextMouseEvent = false;
449     }
450   }
451 }
452
Popular Tags