1 19 20 25 package org.netbeans.modules.web.wizards; 26 27 import java.awt.Color ; 28 import java.awt.Dimension ; 29 import java.awt.Font ; 30 import java.awt.FontMetrics ; 31 import java.awt.Graphics ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 35 import javax.swing.BorderFactory ; 36 import javax.swing.JTable ; 37 import javax.swing.ListSelectionModel ; 38 import javax.swing.table.TableColumnModel ; 39 import javax.swing.table.TableModel ; 40 import javax.swing.table.AbstractTableModel ; 41 import javax.swing.event.TableModelListener ; 42 43 import org.openide.util.NbBundle; 44 45 class MappingTable extends JTable { 46 47 private static final boolean debug = false; 48 49 private boolean fontChanged = true; 51 private int margin = 6; 52 53 private static final long serialVersionUID = 3482048644419079279L; 54 55 MappingTable(String filterName, ArrayList filterMappings) { 56 57 super(); 58 if(debug) log("::Constructor"); if(debug) log("\tFilterName is " + filterName); this.setModel(new MappingTableModel(filterName, filterMappings)); 61 62 TableColumnModel tcm = this.getColumnModel(); 63 64 tcm.getColumn(0).setPreferredWidth(72); 66 67 tcm.getColumn(1).setPreferredWidth(72); 71 this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 72 setColors(false); 73 setIntercellSpacing(new Dimension (margin, margin)); 74 } 75 76 ArrayList getFilterMappings() { 77 return ((MappingTableModel)this.getModel()).getFilterMappings(); 78 } 79 80 void setFilterName(String name) { 81 ((MappingTableModel)this.getModel()).setFilterName(name); 82 this.invalidate(); 83 } 84 85 void addRow(FilterMappingData fmd) { 86 this.invalidate(); 87 ((MappingTableModel)getModel()).addRow(fmd); 88 } 89 90 void addRow(int row, FilterMappingData fmd) { 91 this.invalidate(); 92 ((MappingTableModel)getModel()).addRow(row, fmd); 93 } 94 95 void setRow(int row, FilterMappingData fmd) { 96 this.invalidate(); 97 ((MappingTableModel)getModel()).setRow(row, fmd); 98 } 99 100 FilterMappingData getRow(int row) { 101 return ((MappingTableModel)getModel()).getRow(row); 102 } 103 104 void moveRowUp(int row) { 105 ((MappingTableModel)getModel()).moveRowUp(row); 106 getSelectionModel().setSelectionInterval(row-1, row-1); 107 this.invalidate(); 108 } 109 void moveRowDown(int row) { 110 ((MappingTableModel)getModel()).moveRowUp(row+1); 111 getSelectionModel().setSelectionInterval(row+1, row+1); 112 this.invalidate(); 113 } 114 115 void removeRow(int row) { 116 ((MappingTableModel)getModel()).removeRow(row); 117 this.invalidate(); 118 return; 119 } 120 121 public void setValueAt(Object o, int row, int col) { 122 if(debug) log("::setValueAt()"); return; 124 } 125 126 private void setColors(boolean editable) { 127 Color bg; 128 this.setBorder(BorderFactory.createLoweredBevelBorder()); 129 if (!editable) { 130 bg = this.getBackground().darker(); 131 } else { 132 bg = Color.white; 133 } 134 this.setBackground(bg); 135 } 136 137 void addTableModelListener(TableModelListener tml) { 138 TableModel tableModel = getModel(); 139 if (tableModel != null) { 140 tableModel.addTableModelListener(tml); 141 } 142 } 143 144 void removeTableModelListener(TableModelListener tml) { 145 TableModel tableModel = getModel(); 146 if (tableModel != null) { 147 tableModel.removeTableModelListener(tml); 148 } 149 } 150 151 public void setFont(Font f) { 152 if(debug) log("::setFont()"); fontChanged = true; 154 super.setFont(f); 155 } 156 157 160 public void paint(Graphics g) { 161 if(debug) log("::paint()"); 163 if (fontChanged) { 164 165 fontChanged = false; 166 167 int height = 0; 168 if(debug) log("\tGetting font height"); FontMetrics fm = g.getFontMetrics(getFont()); 170 height = fm.getHeight() + margin; 171 if(height > rowHeight) rowHeight = height; 172 if(debug) log("\trow height is " + rowHeight); this.setRowHeight(rowHeight); 175 return; 176 } 177 super.paint(g); 178 } 179 180 private void log(String s) { 181 System.out.println("MappingTable" + s); } 183 184 class MappingTableModel extends AbstractTableModel { 185 186 private final String [] colheaders = { 187 NbBundle.getMessage(MappingTable.class, "LBL_filter_name"), 188 NbBundle.getMessage(MappingTable.class, "LBL_applies_to"), 189 }; 190 191 private ArrayList filterMappings = null; 192 private String filterName; 193 194 private static final long serialVersionUID = 2845252365404044474L; 195 196 MappingTableModel(String filterName, ArrayList filterMappings) { 197 this.filterName = filterName; 198 this.filterMappings = filterMappings; 199 } 200 201 ArrayList getFilterMappings() { 202 return filterMappings; 203 } 204 205 void setFilterName(String name) { 206 Iterator i = filterMappings.iterator(); 207 FilterMappingData fmd; 208 while(i.hasNext()) { 209 fmd = (FilterMappingData)(i.next()); 210 if(fmd.getName().equals(filterName)) 211 fmd.setName(name); 212 } 213 this.filterName = name; 214 } 215 216 public int getColumnCount() { 217 return colheaders.length; 218 } 219 220 public int getRowCount() { 221 return filterMappings.size(); 222 } 223 224 public String getColumnName(int col) { 225 return colheaders[col]; 226 } 227 228 public Object getValueAt(int row, int col) { 229 FilterMappingData fmd = (FilterMappingData)(filterMappings.get(row)); 230 if(col == 0) return fmd.getName(); 231 else return fmd.getPattern(); 232 } 233 234 public Class getColumnClass(int c) { 235 return String .class; 236 } 237 238 public boolean isCellEditable(int row, int col) { 239 return false; 240 } 241 242 public void setValueAt(Object value, int row, int col) { 243 if(debug) log("::setValueAt()"); return; 245 } 246 247 void addRow(int row, FilterMappingData fmd) { 248 filterMappings.add(row, fmd); 249 } 250 251 void addRow(FilterMappingData fmd) { 252 filterMappings.add(fmd); 253 } 254 255 FilterMappingData getRow(int row) { 256 return (FilterMappingData)(filterMappings.get(row)); 257 } 258 259 void setRow(int row, FilterMappingData fmd) { 260 filterMappings.set(row, fmd); 261 } 262 263 void moveRowUp(int row) { 264 Object o = filterMappings.remove(row); 265 filterMappings.add(row-1, o); 266 } 267 268 void removeRow(int row) { 269 filterMappings.remove(row); 270 } 271 272 private void log(String s) { 273 System.out.println("MappingTableModel" + s); } 275 } 277 } | Popular Tags |