1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| public class TypeConfig extends JTable implements ActionListener, MouseListener, Table
{
/**
* @serial Generated value {@value VarConfig#serialVersionUID} <br />
*/
private static final long serialVersionUID = -357661060467107577L;
/**
* Menu which appear when right clicking the panel
*/
private ClickMenu clickMenu;
/**
* TableModel which contain all data
*/
private TableModel tableModel;
/**
* Editable JTable containing tableModel
*/
private JTable table;
/**
* Parent frame
*/
private Window window;
/**
* TypeDescription
*/
private TypeDescription typeDescription;
/**
* Used for combo box for LsbMsb
*/
private JComboBox comboBoxInteger = new JComboBox(new String[]{"Lsb", "Msb"});
/**
* Type of data
*/
private String type;
@Getter
public boolean isCellEditable(int row, int col) {
if (col == 2)
return true;
else return false;
}
/**
* Main constructor<br />
* Create a new table from tb and set its ClickMenu
*
* @param tb_type TableModel corresponding to TypeDescription
* @param typeDescription TypeDescription
* @param window Main frame
* @param type return the type of data
*/
public TypeConfig(TableModel tb_type, TypeDescription typeDescription, Window window, String type)
{
super(tb_type);
tableModel = tb_type;
this.typeDescription = typeDescription;
this.window = window;
this.type = type;
//JTable table = new JTable(tb_type);
setFillsViewportHeight(true);
addMouseListener(this);
TableColumn col = getColumnModel().getColumn(0);
col.setMaxWidth(25);
getColumnModel().getColumn(2).setCellRenderer(new MyCellRenderer());
getColumnModel().getColumn(2).setCellEditor(new MyCellEditor());
setAutoCreateRowSorter(true);
clickMenu = new ClickMenu(this);
}
/**
* Modify display to have JCheckbox, JCombobox, JTextField
*/
private class MyCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col)
{
if(obj.toString().equalsIgnoreCase("true") || obj.toString().equalsIgnoreCase("false")) {
JCheckBox cb = new JCheckBox();
cb.setHorizontalAlignment(SwingConstants.CENTER);
cb.setBackground(Color.WHITE);
if(obj instanceof String)
cb.setSelected((Boolean.valueOf((String)obj).booleanValue()));
else if(obj instanceof Boolean)
cb.setSelected((Boolean)obj);
return cb;
}
if(obj.toString().equalsIgnoreCase("Lsb") || obj.toString().equalsIgnoreCase("Msb")) {
comboBoxInteger.setSelectedItem(obj);
return comboBoxInteger;
}
if(obj == NumberFormat.getNumberInstance()) {
JTextField tf = new JTextField();
tf.setText((String)obj);
return tf;
}
return new JTextField(obj.toString());
}
}
/**
* Modify edition of JCheckbox, JCombobox, JTextField
*/
public class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = -8869990909839956264L;
private JComponent editor;
public MyCellEditor () {}
@Override
public Object getCellEditorValue() {
if (editor != null) {
if (editor instanceof JCheckBox) {
return ((JCheckBox) editor).isSelected();
} else if (editor instanceof JComboBox) {
return ((JComboBox) editor).getSelectedItem();
} else if (editor instanceof JTextField)
return ((JTextField) editor).getText();
}
return null;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
int type = 100;
if(value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false"))
type = 0;
if(value.toString().equalsIgnoreCase("Lsb") || value.toString().equalsIgnoreCase("Msb"))
type = 1;
if(value == NumberFormat.getNumberInstance())
type = 2;
switch( type ) {
case 0:
editor = new JCheckBox("",Boolean.parseBoolean(value.toString()));
((AbstractButton) editor).setHorizontalAlignment(SwingConstants.CENTER);
editor.setBackground(Color.WHITE);
break;
case 1:
editor = comboBoxInteger;
break;
default:
editor = new JTextField(value.toString());
break;
}
return editor;
}
}
/**
* Action event<br />
* For popup menu events :<br />
* If the user click on Add, a new row is created and added to the table <br />
* Else if he click on Save, the configuration is saved <br />
* Else if he click on Delete, the selected row is removed from the table <br />
* <br />
* @param ae The ActionEvent
*/
@Override
public void actionPerformed(ActionEvent ae) {
// Auto-generated method stub
if (ae.getSource() instanceof JMenuItem)
{
JMenuItem item = (JMenuItem)ae.getSource();
if (item.getText() == "Add")
{
if(type.equalsIgnoreCase("integer"))
{
Object data1[] = {"Signed", "false"};
tableModel.addRow(data1);
Object data2[] = {"Significant bit", "Lsb"};
tableModel.addRow(data2);
Object data3[] = {"Value significant bit", "1"};
tableModel.addRow(data3);
}
}
else if (item.getText() == "Save")
{
typeDescription.resetDatas(tableModel);
window.queueEvent(ae, this);
}
else if (item.getText() == "Delete" && getSelectedRow() != -1)
{
int rows[] = getSelectedRows();
Arrays.sort(rows);
int i = rows.length;
while (--i >= 0)
tableModel.deleteRow(rows[i]);
}
else
window.queueEvent(ae, this);
}
}
/**
* MouseEvent<br />
* Show the popup menu if right click <br />
*
* @param me the MouseEvent
*
* @see MouseEvent
*/
@Override
public void mouseClicked(MouseEvent me) {
// Auto-generated method stub
if (me.getClickCount() == 1 && me.getButton() == 3 && me.getSource() instanceof JTable)
clickMenu.show(me.getComponent(), me.getX(), me.getY());
}
/**
* Not use
*/
@Override
public void mouseEntered(MouseEvent arg0) {
// Auto-generated method stub
}
/**
* Not use
*/
@Override
public void mouseExited(MouseEvent arg0) {
// Auto-generated method stub
}
/**
* Not use
*/
@Override
public void mousePressed(MouseEvent arg0) {
// Auto-generated method stub
}
/**
* Not use
*/
@Override
public void mouseReleased(MouseEvent arg0) {
// Auto-generated method stub
}
@Getter
@Override
public TableModel getTableModel()
{
// Auto-generated method stub
return tableModel;
}
@Getter
@Override
public JTable getJTable()
{
// Auto-generated method stub
return this;
}
} |
Partager