001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.common;
005:
006: import org.dijon.DefaultTableCellRenderer;
007: import org.dijon.Table;
008: import org.dijon.TableResource;
009:
010: import java.awt.Dimension;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.text.DateFormat;
014: import java.text.DecimalFormat;
015: import java.text.Format;
016: import java.text.NumberFormat;
017: import java.util.Date;
018: import java.util.prefs.Preferences;
019:
020: import javax.swing.JPopupMenu;
021: import javax.swing.SwingConstants;
022: import javax.swing.Timer;
023: import javax.swing.ToolTipManager;
024: import javax.swing.event.ChangeEvent;
025: import javax.swing.table.TableCellRenderer;
026: import javax.swing.table.TableColumnModel;
027: import javax.swing.table.TableModel;
028:
029: public class XTable extends Table {
030: protected XPopupListener m_popupHelper;
031: protected Timer m_columnPrefsTimer;
032:
033: private static final String COLUMNS_PREF_KEY = "Columns";
034:
035: public XTable() {
036: super ();
037: init();
038: }
039:
040: public XTable(TableModel model) {
041: super (model);
042: init();
043: }
044:
045: private void init() {
046: setDefaultRenderer(Integer.class, new IntegerRenderer());
047: setDefaultRenderer(Long.class, new LongRenderer());
048: setDefaultRenderer(Date.class, new DateRenderer());
049: setDefaultRenderer(Float.class, new FloatRenderer());
050: setDefaultRenderer(Double.class, new FloatRenderer());
051:
052: m_popupHelper = new XPopupListener(this );
053:
054: m_columnPrefsTimer = new Timer(1000, new ActionListener() {
055: public void actionPerformed(ActionEvent e) {
056: storeColumnPrefs();
057: }
058: });
059: m_columnPrefsTimer.setRepeats(false);
060:
061: getTableHeader().setReorderingAllowed(false);
062: }
063:
064: public void load(TableResource tableRes) {
065: super .load(tableRes);
066: ToolTipManager.sharedInstance().registerComponent(this );
067: }
068:
069: public void addNotify() {
070: super .addNotify();
071:
072: TableColumnModel colModel = getColumnModel();
073: if (colModel != null && colModel.getColumnCount() > 0) {
074: loadColumnPrefs();
075: }
076: }
077:
078: public static class BaseRenderer extends DefaultTableCellRenderer {
079: Format formatter;
080:
081: public BaseRenderer(String format) {
082: super ();
083: formatter = new DecimalFormat(format);
084: }
085:
086: public BaseRenderer(Format formatter) {
087: super ();
088: this .formatter = formatter;
089: }
090:
091: public void setValue(Object value) {
092: String text = "";
093:
094: try {
095: text = (value == null) ? "" : formatter.format(value);
096: } catch (Exception nfe) {
097: System.out.println(value.toString());
098: }
099:
100: setText(text);
101: }
102: }
103:
104: public static class IntegerRenderer extends BaseRenderer {
105: public IntegerRenderer() {
106: super ("#,##0;(#,##0)");
107: setHorizontalAlignment(SwingConstants.RIGHT);
108: }
109: }
110:
111: public static class PortNumberRenderer extends BaseRenderer {
112: public PortNumberRenderer() {
113: super ("###0;(###0)");
114: setHorizontalAlignment(SwingConstants.RIGHT);
115: }
116: }
117:
118: public static class LongRenderer extends BaseRenderer {
119: public LongRenderer() {
120: super ("#,##0;(#,##0)");
121: setHorizontalAlignment(SwingConstants.RIGHT);
122: }
123: }
124:
125: public static class DateRenderer extends BaseRenderer {
126: public DateRenderer() {
127: super (DateFormat.getDateTimeInstance());
128: }
129: }
130:
131: public static class FloatRenderer extends BaseRenderer {
132: public FloatRenderer() {
133: super ("#,##0.00####;(#,##0.00####)");
134: setHorizontalAlignment(SwingConstants.RIGHT);
135: }
136: }
137:
138: public static class PercentRenderer extends BaseRenderer {
139: public PercentRenderer() {
140: super (NumberFormat.getPercentInstance());
141: }
142: }
143:
144: public void setPopupMenu(JPopupMenu popupMenu) {
145: m_popupHelper.setPopupMenu(popupMenu);
146: }
147:
148: public JPopupMenu getPopupMenu() {
149: return m_popupHelper.getPopupMenu();
150: }
151:
152: public void setModel(TableModel model) {
153: super .setModel(model);
154:
155: TableColumnModel colModel = getColumnModel();
156: if (colModel != null && colModel.getColumnCount() > 0) {
157: loadColumnPrefs();
158: }
159: }
160:
161: protected void loadColumnPrefs() {
162: if (getClass().equals(XTable.class)) {
163: return;
164: }
165:
166: PrefsHelper helper = PrefsHelper.getHelper();
167: Preferences prefs = helper.userNodeForClass(getClass());
168: TableColumnModel colModel = getColumnModel();
169: String s = prefs.get(COLUMNS_PREF_KEY, null);
170: int width;
171:
172: if (s != null) {
173: String[] split = s.split(",");
174:
175: for (int i = 0; i < colModel.getColumnCount(); i++) {
176: if (i < split.length && split[i] != null) {
177: try {
178: width = Integer.parseInt(split[i]);
179: colModel.getColumn(i).setPreferredWidth(width);
180: } catch (Exception e) {/**/
181: }
182: }
183: }
184: }
185: }
186:
187: protected void storeColumnPrefs() {
188: if (getClass().equals(XTable.class)) {
189: return;
190: }
191:
192: PrefsHelper helper = PrefsHelper.getHelper();
193: Preferences prefs = helper.userNodeForClass(getClass());
194: StringBuffer sb = new StringBuffer();
195: TableColumnModel colModel = getColumnModel();
196: int width;
197:
198: for (int i = 0; i < colModel.getColumnCount(); i++) {
199: width = colModel.getColumn(i).getWidth();
200: sb.append(width);
201: sb.append(",");
202: }
203:
204: String s = sb.substring(0, sb.length() - 1);
205: prefs.put(COLUMNS_PREF_KEY, s);
206: helper.flush(prefs);
207: }
208:
209: public void columnMarginChanged(ChangeEvent e) {
210: boolean showing = isShowing();
211:
212: if (showing) {
213: m_columnPrefsTimer.stop();
214: }
215: super .columnMarginChanged(e);
216: if (showing) {
217: m_columnPrefsTimer.start();
218: }
219: }
220:
221: public Dimension getPreferredSize() {
222: int rowCount = getRowCount();
223:
224: if (rowCount > 0) {
225: int columnCount = getColumnCount();
226: TableCellRenderer renderer;
227: java.awt.Component comp;
228: Dimension prefSize;
229: int height = 0;
230:
231: for (int row = 0; row < rowCount; row++) {
232: for (int col = 0; col < columnCount; col++) {
233: if ((renderer = getCellRenderer(row, col)) != null) {
234: comp = renderer.getTableCellRendererComponent(
235: this , getValueAt(row, col), true, true,
236: row, col);
237:
238: prefSize = comp.getPreferredSize();
239: height = Math.max(height, prefSize.height);
240: }
241: }
242: }
243:
244: if (height > 10) {
245: setRowHeight(height);
246: }
247: }
248:
249: return super .getPreferredSize();
250: }
251:
252: public void setSelectedRows(int[] rows) {
253: int rowCount = getRowCount();
254:
255: for (int i = 0; i < rows.length; i++) {
256: if (rows[i] < rowCount) {
257: setRowSelectionInterval(rows[i], rows[i]);
258: }
259: }
260: }
261: }
|