1 19 20 29 30 31 package org.netbeans.modules.web.monitor.client; 32 33 import java.awt.Font ; 34 import java.awt.FontMetrics ; 35 import java.awt.Graphics ; 36 import javax.swing.BorderFactory ; 37 import javax.swing.DefaultCellEditor ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JTable ; 40 import javax.swing.border.Border ; 41 import javax.swing.table.TableCellEditor ; 42 import javax.swing.table.TableColumn ; 43 import javax.swing.table.TableColumnModel ; 44 import javax.swing.table.TableModel ; 45 import javax.swing.event.TableModelEvent ; 46 import javax.swing.event.TableModelListener ; 47 import java.awt.Color ; 48 import java.awt.Dimension ; 49 import java.awt.SystemColor ; 50 51 import org.netbeans.modules.web.monitor.data.Param; 52 import org.openide.util.NbBundle; 53 54 public class DisplayTable extends JTable { 55 56 private static final boolean debug = false; 57 58 public static final int UNEDITABLE = 0; 60 public static final int REQUEST = 1; 61 public static final int SERVER = 2; 62 public static final int HEADERS = 3; 63 public static final int PARAMS = 4; 64 public static final int COOKIES = 5; 65 66 public static final int NEUTRAL = 0; 68 public static final int A2Z = 1; 69 public static final int Z2A = 2; 70 71 private int numRows = 0; 72 private int numCols = 3; 73 74 private Object [][] data = null; 75 76 private TableCellEditor [][] cellEditors = null; 77 78 private boolean editableNames = false; 80 private int editable = UNEDITABLE; 81 82 private int sort = NEUTRAL; 84 private boolean sortable = false; 85 86 boolean fontChanged = true; 88 89 public DisplayTable(String [] categories) { 90 this(categories, null, UNEDITABLE, false); 91 } 92 93 public DisplayTable(String [] categories, boolean sortable) { 94 this(categories, null, UNEDITABLE, sortable); 95 } 96 97 public DisplayTable(String [] categories, int editable) { 98 this(categories, null, editable, false); 99 } 100 101 public DisplayTable(String [] categories, int editable, boolean sortable) { 102 this(categories, null, editable, sortable); 103 } 104 105 public DisplayTable(String [] names, String [] values) { 106 this(names, values, UNEDITABLE, false); 107 } 108 109 public DisplayTable(String [] names, String [] values, boolean sortable) { 110 this(names, values, UNEDITABLE, sortable); 111 } 112 113 public DisplayTable(String [] names, String [] values, int editable) { 114 this(names, values, editable, false); 115 } 116 117 public DisplayTable(String [] names, String [] values, int editable, 118 boolean sortable) { 119 120 super(); 121 numRows = names.length; 122 editableNames = false; 123 this.editable = editable; 124 this.sortable = sortable; 125 126 data = new Object [numRows][numCols]; 127 cellEditors = new TableCellEditor [numRows][numCols]; 128 for(int i=0; i<numRows; ++i) { 129 data[i][0] = names[i]; 130 if (values == null) { 131 data[i][1] = new String (""); } else { 133 data[i][1] = values[i]; 134 } 135 data[i][2] = NbBundle.getBundle(DisplayTable.class).getString("MON_Edit_dots"); cellEditors[i][2] = 137 NameValueCellEditor.createCellEditor((JTable )this, data, 138 false, i, editable); 139 } 140 setMyModel(data, editable > UNEDITABLE); 141 setup(); 142 } 143 144 public DisplayTable(Param[] params) { 145 this(params, UNEDITABLE, false); 146 } 147 148 public DisplayTable(Param[] params, boolean sortable) { 149 this(params, UNEDITABLE, sortable); 150 } 151 152 153 public DisplayTable(Param[] params, int editable) { 154 this(params, editable, false); 155 } 156 157 158 public DisplayTable(Param[] params, int editable, boolean sortable) { 159 160 super(); 161 162 if(editable < 3) 163 editableNames = false; 164 else 165 editableNames = true; 166 167 this.editable = editable; 168 this.sortable = sortable; 169 170 numRows = params.length; 171 data = new Object [numRows][numCols]; 172 cellEditors = new TableCellEditor [numRows][numCols]; 173 for(int i=0; i<numRows; ++i) { 174 data[i][0] = params[i].getAttributeValue("name"); data[i][1] = params[i].getAttributeValue("value"); data[i][2] = NbBundle.getBundle(DisplayTable.class).getString("MON_Edit_dots"); cellEditors[i][2] = 178 NameValueCellEditor.createCellEditor((JTable )this, data, 179 true, i, editable); 180 } 181 setMyModel(data, editable > UNEDITABLE); 182 setup(); 183 } 184 185 private void setup() { 186 setBorderAndColorScheme(); 187 Dimension margins = new Dimension (6, 4); 188 setIntercellSpacing(margins); 189 sort(); 190 } 191 192 196 private void setBorderAndColorScheme() { 197 setBorderAndColorScheme(editable != UNEDITABLE); 198 } 199 200 private void setBorderAndColorScheme(boolean editable) { 201 Color bg; 202 this.setBorder(BorderFactory.createLoweredBevelBorder()); 203 if (!editable) { 204 bg = this.getBackground().darker(); 206 } else { 207 bg = Color.white; 208 } 209 this.setTableHeader(null); 210 this.setBackground(bg); 211 } 212 213 218 public JComboBox setChoices(int row, int col, String [] choices, 219 boolean editable) { 220 TableCellEditor ed = new ComboBoxTableCellEditor(choices); 221 cellEditors[row][col] = ed; 222 223 data[row][2]=NbBundle.getBundle(DisplayTable.class).getString("MON_Editing"); 226 cellEditors[row][2] = null; 227 228 return ((ComboBoxTableCellEditor)ed).getComboBox(); 229 } 230 231 235 public TableCellEditor getCellEditor(int row, int col) { 236 TableCellEditor ed = cellEditors[row][col]; 237 if (ed == null) { 238 return super.getCellEditor(row, col); 239 } 240 return ed; 241 } 242 243 public void setSorting(int state) { 244 sort = state; 245 if(getModel() instanceof DisplayTableSorter) 246 ((DisplayTableSorter)getModel()).sort(sort); 247 } 248 249 public void sort() { 250 if(getModel() instanceof DisplayTableSorter) 251 ((DisplayTableSorter)getModel()).sort(sort); 252 } 253 254 private void setMyModel(Object [][] data, boolean canEdit) { 255 256 DisplayTableModel model = new DisplayTableModel(data, 257 canEdit, 258 editable > 2); 259 if(sortable) { 260 DisplayTableSorter sorter = new DisplayTableSorter(model); 261 setModel(sorter); 262 } 263 else { 264 setModel(model); 265 } 266 267 TableColumnModel tcm = getColumnModel(); 269 if (tcm.getColumnCount() > 0) { 270 TableColumn column = tcm.getColumn(0); 271 column.setPreferredWidth(10); 272 tcm.getColumn(2).setMaxWidth(5); 273 } 274 } 275 276 public void addTableModelListener(TableModelListener tml) { 277 TableModel tableModel = getModel(); 278 if (tableModel != null) { 279 tableModel.addTableModelListener(tml); 280 } 281 } 282 283 public void removeTableModelListener(TableModelListener tml) { 284 TableModel tableModel = getModel(); 285 if (tableModel != null) { 286 tableModel.removeTableModelListener(tml); 287 } 288 } 289 290 public Object [][] getData() { 291 return data; 292 } 293 294 public void setFont(Font f) { 295 fontChanged = true; 296 super.setFont(f); 297 } 298 299 302 public void paint(Graphics g) { 303 if (fontChanged) { 304 Font f = getFont(); 305 FontMetrics fm = g.getFontMetrics(f); 306 int rowHeight = fm.getHeight(); 307 fontChanged = false; 308 this.setRowHeight(rowHeight); 310 return; 311 } 312 super.paint(g); 313 } 314 315 private void log(String s) { 316 System.out.println("DisplayTable::" + s); } 318 319 } | Popular Tags |