1 14 package wingset; 15 16 import org.wings.*; 17 import org.wings.plaf.css.TableCG; 18 import org.wings.table.SDefaultTableCellRenderer; 19 20 import javax.swing.table.AbstractTableModel ; 21 import javax.swing.table.TableModel ; 22 import java.awt.*; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.ItemEvent ; 26 import java.awt.event.ItemListener ; 27 28 32 public class TableExample 33 extends WingSetPane { 34 static final SIcon image = new SResourceIcon("org/wings/icons/JavaCup.gif"); 35 36 public final MyCellRenderer cellRenderer = new MyCellRenderer(); 37 38 final static Color[] colors = { 39 Color.black, 40 Color.cyan, 41 Color.yellow, 42 Color.magenta, 43 Color.orange, 44 Color.pink, 45 Color.red, 46 Color.darkGray, 47 Color.gray, 48 Color.green, 49 Color.lightGray, 50 Color.white, 51 Color.blue 52 }; 53 54 private STable table; 55 private TableControls controls; 56 57 public SComponent createExample() { 58 controls = new TableControls(); 59 60 table = new STable(new MyTableModel(7, 5)); 61 table.setName("table"); 62 table.setShowGrid(true); 63 table.setSelectionMode(STable.NO_SELECTION); 64 table.setDefaultRenderer(cellRenderer); 65 table.setShowAsFormComponent(false); 66 table.setEditable(false); 67 controls.addSizable(table); 68 69 SForm panel = new SForm(new SBorderLayout()); 70 panel.add(controls, SBorderLayout.NORTH); 71 panel.add(table, SBorderLayout.CENTER); 72 return panel; 73 } 74 75 76 static class MyCellRenderer extends SDefaultTableCellRenderer { 77 78 public MyCellRenderer() { 79 setEditIcon(getSession().getCGManager().getIcon("TableCG.editIcon")); 80 } 81 82 public SComponent getTableCellRendererComponent(STable table, 83 Object value, 84 boolean selected, 85 int row, 86 int col) { 87 if (value instanceof Color) { 88 Color c = (Color)value; 89 setText("<html><span style=\"color:" + colorToHex(c) + "\">" + colorToHex(c) + "</span>"); 90 setIcon(null); 91 return this; 92 } 93 else 94 return super.getTableCellRendererComponent(table, value, selected, row, col); 95 } 96 } 97 98 static class MyTableModel extends AbstractTableModel { 99 int cols, rows; 100 101 Object [][] data; 102 boolean asc[]; 103 104 MyTableModel(int cols, int rows) { 105 this.cols = cols; 106 this.rows = rows; 107 108 data = new Object [rows][cols]; 109 asc = new boolean[cols]; 110 111 for (int c = 0; c < cols; c++) { 112 for (int r = 0; r < rows; r++) 113 data[r][c] = "cell " + r + ":" + c; 114 } 115 for (int r = 0; r < rows; r++) 116 data[r][2] = createColor(r); 117 for (int r = 0; r < rows; r++) 118 data[r][3] = createImage(r); 119 for (int r = 0; r < rows; r++) 120 data[r][4] = createBoolean(r); 121 } 122 123 public int getColumnCount() { 124 return cols; 125 } 126 127 public String getColumnName(int col) { 128 return "col " + col; 129 } 130 131 public int getRowCount() { 132 return rows; 133 } 134 135 public Object getValueAt(int row, int col) { 136 return data[row][col]; 137 } 138 139 public void setValueAt(Object value, int row, int col) { 140 if (value == null) 141 data[row][col] = null; 142 else if (getColumnClass(col).isAssignableFrom(String .class)) 143 data[row][col] = value.toString(); 144 else if (getColumnClass(col).isAssignableFrom(Boolean .class)) 145 data[row][col] = new Boolean (((Boolean ) value).booleanValue()); 146 } 147 148 public Class getColumnClass(int columnIndex) { 149 switch (columnIndex) { 150 case 2: 151 return Color.class; 152 case 3: 153 return SIcon.class; 154 case 4: 155 return Boolean .class; 156 } 157 return String .class; 158 } 159 160 public Color createColor(int row) { 161 return colors[row % colors.length]; 162 } 163 164 public SIcon createImage(int row) { 165 return image; 166 } 167 168 public Boolean createBoolean(int row) { 169 if (row % 2 == 1) 170 return new Boolean (false); 171 else 172 return new Boolean (true); 173 } 174 175 public void sort(int col, boolean ascending) { 176 log.debug("sort"); 177 if (col < asc.length) 178 asc[col] = !ascending; 179 } 180 181 public boolean isCellEditable(int row, int col) { 182 if (getColumnClass(col).isAssignableFrom(String .class) || 183 getColumnClass(col).isAssignableFrom(Boolean .class)) 184 return true; 185 else 186 return false; 187 } 188 } 189 190 static class ROTableModel extends MyTableModel { 191 public ROTableModel(int cols, int rows) { 192 super(cols, rows); 193 } 194 195 public boolean isCellEditable(int row, int col) { return false; } 196 } 197 198 static String colorToHex(Color color) { 199 String colorstr = new String ("#"); 200 201 String str = Integer.toHexString(color.getRed()); 203 if (str.length() > 2) 204 str = str.substring(0, 2); 205 else if (str.length() < 2) 206 colorstr += "0" + str; 207 else 208 colorstr += str; 209 210 str = Integer.toHexString(color.getGreen()); 212 if (str.length() > 2) 213 str = str.substring(0, 2); 214 else if (str.length() < 2) 215 colorstr += "0" + str; 216 else 217 colorstr += str; 218 219 str = Integer.toHexString(color.getBlue()); 221 if (str.length() > 2) 222 str = str.substring(0, 2); 223 else if (str.length() < 2) 224 colorstr += "0" + str; 225 else 226 colorstr += str; 227 228 return colorstr; 229 } 230 231 232 235 public class MyTable extends STable { 236 private final TableCG myTableCG = new TableCG(); 237 238 public MyTable(TableModel tm) { 239 super(tm); 240 myTableCG.setFixedTableBorderWidth("0"); 241 setCG(myTableCG); 242 } 243 244 247 public String getRowStyle(int row) { 248 return isRowSelected(row) ? "table_selected_row" : (row % 2 == 0 ? "table_row1" : "table_row2"); 249 } 250 } 251 252 class TableControls extends ComponentControls { 253 private final String [] SELECTION_MODES = new String []{"no", "single", "multiple"}; 254 255 public TableControls() { 256 final SCheckBox showAsFormComponent = new SCheckBox("<html>Show as Form Component "); 257 showAsFormComponent.addActionListener(new ActionListener () { 258 public void actionPerformed(ActionEvent e) { 259 table.setShowAsFormComponent(showAsFormComponent.isSelected()); 260 } 261 }); 262 263 final SCheckBox editable = new SCheckBox("<html>Editable "); 264 editable.addActionListener(new ActionListener () { 265 public void actionPerformed(ActionEvent e) { 266 table.setEditable(editable.isSelected()); 267 } 268 }); 269 270 final SComboBox selectionMode = new SComboBox(SELECTION_MODES); 271 selectionMode.addItemListener(new ItemListener () { 272 public void itemStateChanged(ItemEvent e) { 273 if ("no".equals(selectionMode.getSelectedItem())) 274 table.setSelectionMode(STable.NO_SELECTION); 275 else if ("single".equals(selectionMode.getSelectedItem())) 276 table.setSelectionMode(STable.SINGLE_SELECTION); 277 else if ("multiple".equals(selectionMode.getSelectedItem())) 278 table.setSelectionMode(STable.MULTIPLE_SELECTION); 279 } 280 }); 281 282 add(showAsFormComponent); 283 add(editable); 284 add(new SLabel(" selection mode ")); 285 add(selectionMode); 286 } 287 } 288 } 289 | Popular Tags |