1 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 ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.text.DateFormat ; 14 import java.text.DecimalFormat ; 15 import java.text.Format ; 16 import java.text.NumberFormat ; 17 import java.util.Date ; 18 import java.util.prefs.Preferences ; 19 20 import javax.swing.JPopupMenu ; 21 import javax.swing.SwingConstants ; 22 import javax.swing.Timer ; 23 import javax.swing.ToolTipManager ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.table.TableCellRenderer ; 26 import javax.swing.table.TableColumnModel ; 27 import javax.swing.table.TableModel ; 28 29 public class XTable extends Table { 30 protected XPopupListener m_popupHelper; 31 protected Timer m_columnPrefsTimer; 32 33 private static final String COLUMNS_PREF_KEY = "Columns"; 34 35 public XTable() { 36 super(); 37 init(); 38 } 39 40 public XTable(TableModel model) { 41 super(model); 42 init(); 43 } 44 45 private void init() { 46 setDefaultRenderer(Integer .class, new IntegerRenderer()); 47 setDefaultRenderer(Long .class, new LongRenderer()); 48 setDefaultRenderer(Date .class, new DateRenderer()); 49 setDefaultRenderer(Float .class, new FloatRenderer()); 50 setDefaultRenderer(Double .class, new FloatRenderer()); 51 52 m_popupHelper = new XPopupListener(this); 53 54 m_columnPrefsTimer = new Timer (1000, new ActionListener () { 55 public void actionPerformed(ActionEvent 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 colModel = getColumnModel(); 73 if(colModel != null && colModel.getColumnCount() > 0) { 74 loadColumnPrefs(); 75 } 76 } 77 78 public static class BaseRenderer extends DefaultTableCellRenderer { 79 Format formatter; 80 81 public BaseRenderer(String format) { 82 super(); 83 formatter = new DecimalFormat (format); 84 } 85 86 public BaseRenderer(Format formatter) { 87 super(); 88 this.formatter = formatter; 89 } 90 91 public void setValue(Object value) { 92 String text = ""; 93 94 try { 95 text = (value == null) ? "" : formatter.format(value); 96 } catch(NumberFormatException 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 popupMenu) { 143 m_popupHelper.setPopupMenu(popupMenu); 144 } 145 146 public JPopupMenu getPopupMenu() { 147 return m_popupHelper.getPopupMenu(); 148 } 149 150 public void setModel(TableModel model) { 151 super.setModel(model); 152 153 TableColumnModel 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 prefs = helper.userNodeForClass(getClass()); 166 TableColumnModel colModel = getColumnModel(); 167 String s = prefs.get(COLUMNS_PREF_KEY, null); 168 int width; 169 170 if(s != null) { 171 String [] 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 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 prefs = helper.userNodeForClass(getClass()); 191 StringBuffer sb = new StringBuffer (); 192 TableColumnModel 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 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 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 getPreferredSize() { 219 int rowCount = getRowCount(); 220 221 if(rowCount > 0) { 222 int columnCount = getColumnCount(); 223 TableCellRenderer renderer; 224 java.awt.Component comp; 225 Dimension 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 |