KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.dijon.DefaultTableCellRenderer;
7 import org.dijon.Table;
8 import org.dijon.TableResource;
9
10 import java.awt.Dimension JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.text.DateFormat JavaDoc;
14 import java.text.DecimalFormat JavaDoc;
15 import java.text.Format JavaDoc;
16 import java.text.NumberFormat JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.prefs.Preferences JavaDoc;
19
20 import javax.swing.JPopupMenu JavaDoc;
21 import javax.swing.SwingConstants JavaDoc;
22 import javax.swing.Timer JavaDoc;
23 import javax.swing.ToolTipManager JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.table.TableCellRenderer JavaDoc;
26 import javax.swing.table.TableColumnModel JavaDoc;
27 import javax.swing.table.TableModel JavaDoc;
28
29 public class XTable extends Table {
30   protected XPopupListener m_popupHelper;
31   protected Timer JavaDoc m_columnPrefsTimer;
32
33   private static final String JavaDoc COLUMNS_PREF_KEY = "Columns";
34
35   public XTable() {
36     super();
37     init();
38   }
39
40   public XTable(TableModel JavaDoc model) {
41     super(model);
42     init();
43   }
44
45   private void init() {
46     setDefaultRenderer(Integer JavaDoc.class, new IntegerRenderer());
47     setDefaultRenderer(Long JavaDoc.class, new LongRenderer());
48     setDefaultRenderer(Date JavaDoc.class, new DateRenderer());
49     setDefaultRenderer(Float JavaDoc.class, new FloatRenderer());
50     setDefaultRenderer(Double JavaDoc.class, new FloatRenderer());
51
52     m_popupHelper = new XPopupListener(this);
53
54     m_columnPrefsTimer = new Timer JavaDoc(1000, new ActionListener JavaDoc() {
55       public void actionPerformed(ActionEvent JavaDoc e) {
56         storeColumnPrefs();
57       }
58     });
59     m_columnPrefsTimer.setRepeats(false);
60
61     getTableHeader().setReorderingAllowed(false);
62   }
63
64   public void load(TableResource tableRes) {
65     super.load(tableRes);
66     ToolTipManager.sharedInstance().registerComponent(this);
67   }
68   
69   public void addNotify() {
70     super.addNotify();
71     
72     TableColumnModel JavaDoc colModel = getColumnModel();
73     if(colModel != null && colModel.getColumnCount() > 0) {
74       loadColumnPrefs();
75     }
76   }
77
78   public static class BaseRenderer extends DefaultTableCellRenderer {
79     Format JavaDoc formatter;
80
81     public BaseRenderer(String JavaDoc format) {
82       super();
83       formatter = new DecimalFormat JavaDoc(format);
84     }
85
86     public BaseRenderer(Format JavaDoc formatter) {
87       super();
88       this.formatter = formatter;
89     }
90     
91     public void setValue(Object JavaDoc value) {
92       String JavaDoc text = "";
93
94       try {
95         text = (value == null) ? "" : formatter.format(value);
96       } catch(NumberFormatException JavaDoc nfe) {/**/}
97
98       setText(text);
99     }
100   }
101
102   public static class IntegerRenderer extends BaseRenderer {
103     public IntegerRenderer() {
104       super("#,##0;(#,##0)");
105       setHorizontalAlignment(SwingConstants.RIGHT);
106     }
107   }
108
109   public static class PortNumberRenderer extends BaseRenderer {
110     public PortNumberRenderer() {
111       super("###0;(###0)");
112       setHorizontalAlignment(SwingConstants.RIGHT);
113     }
114   }
115
116   public static class LongRenderer extends BaseRenderer {
117     public LongRenderer() {
118       super("#,##0;(#,##0)");
119       setHorizontalAlignment(SwingConstants.RIGHT);
120     }
121   }
122
123   public static class DateRenderer extends BaseRenderer {
124     public DateRenderer() {
125       super(DateFormat.getDateTimeInstance());
126     }
127   }
128
129   public static class FloatRenderer extends BaseRenderer {
130     public FloatRenderer() {
131       super("#,##0.00####;(#,##0.00####)");
132       setHorizontalAlignment(SwingConstants.RIGHT);
133     }
134   }
135
136   public static class PercentRenderer extends BaseRenderer {
137     public PercentRenderer() {
138       super(NumberFormat.getPercentInstance());
139     }
140   }
141
142   public void setPopupMenu(JPopupMenu JavaDoc popupMenu) {
143     m_popupHelper.setPopupMenu(popupMenu);
144   }
145
146   public JPopupMenu JavaDoc getPopupMenu() {
147     return m_popupHelper.getPopupMenu();
148   }
149
150   public void setModel(TableModel JavaDoc model) {
151     super.setModel(model);
152
153     TableColumnModel JavaDoc colModel = getColumnModel();
154     if(colModel != null && colModel.getColumnCount() > 0) {
155       loadColumnPrefs();
156     }
157   }
158   
159   protected void loadColumnPrefs() {
160     if(getClass().equals(XTable.class)) {
161       return;
162     }
163     
164     PrefsHelper helper = PrefsHelper.getHelper();
165     Preferences JavaDoc prefs = helper.userNodeForClass(getClass());
166     TableColumnModel JavaDoc colModel = getColumnModel();
167     String JavaDoc s = prefs.get(COLUMNS_PREF_KEY, null);
168     int width;
169
170     if(s != null) {
171       String JavaDoc [] split = s.split(",");
172
173       for(int i = 0; i < colModel.getColumnCount(); i++) {
174         if(i < split.length && split[i] != null) {
175           try {
176             width = Integer.parseInt(split[i]);
177             colModel.getColumn(i).setPreferredWidth(width);
178           } catch(Exception JavaDoc e) {/**/}
179         }
180       }
181     }
182   }
183
184   protected void storeColumnPrefs() {
185     if(getClass().equals(XTable.class)) {
186       return;
187     }
188     
189     PrefsHelper helper = PrefsHelper.getHelper();
190     Preferences JavaDoc prefs = helper.userNodeForClass(getClass());
191     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
192     TableColumnModel JavaDoc colModel = getColumnModel();
193     int width;
194
195     for(int i = 0; i < colModel.getColumnCount(); i++) {
196       width = colModel.getColumn(i).getWidth();
197       sb.append(width);
198       sb.append(",");
199     }
200
201     String JavaDoc s = sb.substring(0, sb.length()-1);
202     prefs.put(COLUMNS_PREF_KEY, s);
203     helper.flush(prefs);
204   }
205
206   public void columnMarginChanged(ChangeEvent JavaDoc e) {
207     boolean showing = isShowing();
208     
209     if(showing) {
210       m_columnPrefsTimer.stop();
211     }
212     super.columnMarginChanged(e);
213     if(showing) {
214       m_columnPrefsTimer.start();
215     }
216   }
217
218   public Dimension JavaDoc getPreferredSize() {
219     int rowCount = getRowCount();
220     
221     if(rowCount > 0) {
222       int columnCount = getColumnCount();
223       TableCellRenderer renderer;
224       java.awt.Component JavaDoc comp;
225       Dimension JavaDoc prefSize;
226       int height = 0;
227     
228       for(int row = 0; row < rowCount; row++) {
229         for(int col = 0; col < columnCount; col++) {
230           if((renderer = getCellRenderer(row, col)) != null) {
231             comp = renderer.getTableCellRendererComponent(
232               this, getValueAt(row, col), true, true, row, col
233             );
234   
235             prefSize = comp.getPreferredSize();
236             height = Math.max(height, prefSize.height);
237           }
238         }
239       }
240
241       if(height > 10) {
242         setRowHeight(height);
243       }
244     }
245
246     return super.getPreferredSize();
247   }
248
249   public void setSelectedRows(int[] rows) {
250     int rowCount = getRowCount();
251     
252     for(int i = 0; i < rows.length; i++) {
253       if(rows[i] < rowCount) {
254         setRowSelectionInterval(rows[i], rows[i]);
255       }
256     }
257   }
258 }
259
Popular Tags