gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src/gdxstudio/panel/BaseTable.java
| a9b2a64 | 1 | package gdxstudio.panel; |
| a9b2a64 | 2 | import gdxstudio.Style; |
| a9b2a64 | 3 | |
| a62d533 | 4 | import java.awt.Color; |
| a62d533 | 5 | import java.awt.Component; |
| a62d533 | 6 | import java.awt.Dimension; |
| a62d533 | 7 | import java.awt.Graphics; |
| a62d533 | 8 | import java.awt.Insets; |
| a62d533 | 9 | import java.awt.event.ActionEvent; |
| a62d533 | 10 | import java.awt.event.ActionListener; |
| a62d533 | 11 | import java.util.ArrayList; |
| a62d533 | 12 | import java.util.EventObject; |
| a62d533 | 13 | import java.util.List; |
| a62d533 | 14 | |
| a62d533 | 15 | import javax.swing.AbstractCellEditor; |
| a62d533 | 16 | import javax.swing.BorderFactory; |
| a62d533 | 17 | import javax.swing.DefaultCellEditor; |
| a62d533 | 18 | import javax.swing.JButton; |
| a62d533 | 19 | import javax.swing.JCheckBox; |
| a62d533 | 20 | import javax.swing.JColorChooser; |
| a62d533 | 21 | import javax.swing.JComboBox; |
| a62d533 | 22 | import javax.swing.JDialog; |
| a62d533 | 23 | import javax.swing.JLabel; |
| a62d533 | 24 | import javax.swing.JOptionPane; |
| a62d533 | 25 | import javax.swing.JPanel; |
| a62d533 | 26 | import javax.swing.JScrollPane; |
| a62d533 | 27 | import javax.swing.JSpinner; |
| a62d533 | 28 | import javax.swing.JSpinner.DefaultEditor; |
| a62d533 | 29 | import javax.swing.JTable; |
| a62d533 | 30 | import javax.swing.JTextField; |
| a62d533 | 31 | import javax.swing.ListSelectionModel; |
| a62d533 | 32 | import javax.swing.SpinnerNumberModel; |
| a62d533 | 33 | import javax.swing.event.CellEditorListener; |
| a9b2a64 | 34 | import javax.swing.event.ChangeEvent; |
| a9b2a64 | 35 | import javax.swing.event.ChangeListener; |
| a62d533 | 36 | import javax.swing.event.TableModelEvent; |
| a62d533 | 37 | import javax.swing.event.TableModelListener; |
| a62d533 | 38 | import javax.swing.table.DefaultTableCellRenderer; |
| a62d533 | 39 | import javax.swing.table.DefaultTableModel; |
| a62d533 | 40 | import javax.swing.table.TableCellEditor; |
| a62d533 | 41 | |
| 7c84877 | 42 | import com.badlogic.gdx.scenes.scene2d.Touchable; |
| 7c84877 | 43 | |
| a62d533 | 44 | import web.laf.lite.layout.ToolbarLayout; |
| a62d533 | 45 | import web.laf.lite.layout.VerticalFlowLayout; |
| a62d533 | 46 | import web.laf.lite.utils.UIUtils; |
| a62d533 | 47 | import web.laf.lite.widget.NumberTextField; |
| a62d533 | 48 | |
| a62d533 | 49 | public abstract class BaseTable extends JPanel implements ActionListener, TableModelListener {
|
| a62d533 | 50 | private static final long serialVersionUID = 1L; |
| a62d533 | 51 | |
| a62d533 | 52 | protected DefaultTableModel tableModel = new DefaultTableModel(){
|
| a62d533 | 53 | private static final long serialVersionUID = 1L; |
| a62d533 | 54 | |
| a62d533 | 55 | @Override |
| a62d533 | 56 | public Class<?> getColumnClass(int columnIndex) {
|
| a62d533 | 57 | return String.class; |
| a62d533 | 58 | } |
| a62d533 | 59 | |
| a62d533 | 60 | public boolean isCellEditable(int row, int column) {
|
| a62d533 | 61 | if (column == 1) |
| a62d533 | 62 | return true; |
| a62d533 | 63 | return false; |
| a62d533 | 64 | } |
| a62d533 | 65 | }; |
| a9b2a64 | 66 | public final JTable table = new JTable(tableModel){
|
| a62d533 | 67 | private static final long serialVersionUID = 1L; |
| a62d533 | 68 | |
| a62d533 | 69 | public TableCellEditor getCellEditor(int row, int column) |
| a62d533 | 70 | {
|
| a62d533 | 71 | int modelColumn = convertColumnIndexToModel( column ); |
| a62d533 | 72 | if (modelColumn == 1){
|
| a62d533 | 73 | if(row<editors.size()) |
| a62d533 | 74 | return editors.get(row); |
| a62d533 | 75 | } |
| a62d533 | 76 | return super.getCellEditor(row, column); |
| a62d533 | 77 | } |
| a62d533 | 78 | }; |
| a62d533 | 79 | protected final JScrollPane scrollPane; |
| a62d533 | 80 | protected final List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3); |
| 7c84877 | 81 | protected final JButton headerButton; |
| a62d533 | 82 | |
| a62d533 | 83 | public BaseTable(String title, DefaultTableModel model, DefaultTableCellRenderer renderer){
|
| a62d533 | 84 | super(new VerticalFlowLayout()); |
| a62d533 | 85 | if(model != null) |
| a62d533 | 86 | tableModel = model; |
| a62d533 | 87 | UIUtils.setShadeWidth(this, 0); |
| a62d533 | 88 | UIUtils.setRound(this, 0); |
| a62d533 | 89 | //UIUtils.setDrawSides(this, false, false, false, false); |
| a62d533 | 90 | table.setTableHeader(null); |
| a62d533 | 91 | if(renderer != null) |
| a62d533 | 92 | table.setDefaultRenderer(String.class, renderer); |
| a62d533 | 93 | else |
| a62d533 | 94 | table.setDefaultRenderer(String.class, new BaseRenderer()); |
| a62d533 | 95 | table.setShowGrid(true); |
| a62d533 | 96 | table.setColumnSelectionAllowed(false); |
| a62d533 | 97 | table.setRowSelectionAllowed(false); |
| a62d533 | 98 | table.setCellSelectionEnabled(true); |
| a62d533 | 99 | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| a62d533 | 100 | tableModel.setColumnCount(2); |
| a62d533 | 101 | tableModel.addTableModelListener(this); |
| a62d533 | 102 | clear(); |
| 7c84877 | 103 | headerButton = new Style.TitleButton(title, this); |
| a62d533 | 104 | if(!title.isEmpty()){
|
| a62d533 | 105 | add(headerButton); |
| a62d533 | 106 | } |
| a62d533 | 107 | scrollPane = new JScrollPane(table); |
| a62d533 | 108 | scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); |
| a62d533 | 109 | scrollPane.setPreferredSize(new Dimension(200, table.getRowCount()*table.getRowHeight())); |
| a62d533 | 110 | UIUtils.setDrawBorder(scrollPane, false); |
| a62d533 | 111 | add(scrollPane); |
| a62d533 | 112 | } |
| a62d533 | 113 | |
| a62d533 | 114 | public BaseTable(String title){
|
| a62d533 | 115 | this(title, null, null); |
| a62d533 | 116 | } |
| a62d533 | 117 | |
| a62d533 | 118 | public void lock(){
|
| a62d533 | 119 | tableModel.removeTableModelListener(this); |
| a62d533 | 120 | } |
| a62d533 | 121 | |
| a62d533 | 122 | public void unlock(){
|
| a62d533 | 123 | tableModel.addTableModelListener(this); |
| a62d533 | 124 | } |
| a62d533 | 125 | |
| a62d533 | 126 | public void enable(){
|
| a62d533 | 127 | table.setEnabled(true); |
| a62d533 | 128 | } |
| a62d533 | 129 | |
| a62d533 | 130 | public void disable(){
|
| a62d533 | 131 | table.setEnabled(false); |
| a62d533 | 132 | } |
| a62d533 | 133 | |
| a62d533 | 134 | public void addRow(String key, String value){
|
| a62d533 | 135 | tableModel.addRow(new String[]{key, value});
|
| a62d533 | 136 | } |
| a62d533 | 137 | |
| a62d533 | 138 | public void addRow(String key, boolean value){
|
| a62d533 | 139 | tableModel.addRow(new Object[]{key, new Boolean(value)});
|
| a62d533 | 140 | } |
| a62d533 | 141 | |
| a62d533 | 142 | public void update(String... propertyAndValueNames){
|
| a62d533 | 143 | if(table.isEditing()) |
| a62d533 | 144 | table.getCellEditor().stopCellEditing(); |
| a62d533 | 145 | tableModel.getDataVector().clear(); |
| a62d533 | 146 | for(int i=0;i<propertyAndValueNames.length;i+=2) |
| a62d533 | 147 | addRow(propertyAndValueNames[i], propertyAndValueNames[i+1]); |
| a62d533 | 148 | table.setEnabled(true); |
| a62d533 | 149 | table.repaint(); |
| a62d533 | 150 | } |
| a62d533 | 151 | |
| a62d533 | 152 | public void clear(String... propertyNames){
|
| a62d533 | 153 | if(table.isEditing()) |
| a62d533 | 154 | table.getCellEditor().stopCellEditing(); |
| a62d533 | 155 | tableModel.getDataVector().clear(); |
| a62d533 | 156 | for(int i=0;i<propertyNames.length;i++) |
| a62d533 | 157 | addRow(propertyNames[i], ""); |
| a62d533 | 158 | table.repaint(); |
| a62d533 | 159 | table.setEnabled(false); |
| a62d533 | 160 | } |
| a62d533 | 161 | |
| a62d533 | 162 | @Override |
| a62d533 | 163 | public void tableChanged(TableModelEvent e) {
|
| a62d533 | 164 | if(e.getType() == TableModelEvent.UPDATE && e.getLastRow() < tableModel.getRowCount()){
|
| a62d533 | 165 | String key = (String)tableModel.getValueAt(e.getLastRow(), e.getColumn()-1); |
| a62d533 | 166 | String value = tableModel.getValueAt(e.getLastRow(), e.getColumn()).toString(); |
| a62d533 | 167 | setProperty(key, value); |
| a62d533 | 168 | } |
| a62d533 | 169 | } |
| a62d533 | 170 | |
| 7c84877 | 171 | |
| 7c84877 | 172 | public void setProperty(String name, String value){
|
| 7c84877 | 173 | } |
| 7c84877 | 174 | |
| a62d533 | 175 | public void updateProperty(String key, String value, int row){
|
| a62d533 | 176 | lock(); |
| a62d533 | 177 | tableModel.setValueAt(value, row, 1); |
| a62d533 | 178 | unlock(); |
| a62d533 | 179 | } |
| a62d533 | 180 | |
| a62d533 | 181 | public int getRowCount(){
|
| a62d533 | 182 | return table.getRowCount(); |
| a62d533 | 183 | } |
| a62d533 | 184 | |
| a62d533 | 185 | @Override |
| a62d533 | 186 | public void actionPerformed(ActionEvent arg0) {
|
| a62d533 | 187 | scrollPane.setVisible(!scrollPane.isVisible()); |
| a62d533 | 188 | validate(); |
| a62d533 | 189 | repaint(); |
| a62d533 | 190 | } |
| a62d533 | 191 | |
| a9b2a64 | 192 | |
| a9b2a64 | 193 | public SpinnerIntegerEditor createIntegerSpinner(final String key){
|
| a9b2a64 | 194 | final SpinnerIntegerEditor editor = new SpinnerIntegerEditor(); |
| a9b2a64 | 195 | editor.spinner.addChangeListener(new ChangeListener(){
|
| a9b2a64 | 196 | @Override |
| a9b2a64 | 197 | public void stateChanged(ChangeEvent arg0) {
|
| a9b2a64 | 198 | setProperty(key, editor.spinner.getValue().toString()); |
| a9b2a64 | 199 | } |
| a9b2a64 | 200 | }); |
| a9b2a64 | 201 | return editor; |
| a9b2a64 | 202 | } |
| a9b2a64 | 203 | |
| a9b2a64 | 204 | public SpinnerFloatEditor createFloatSpinner(final String key){
|
| a9b2a64 | 205 | final SpinnerFloatEditor editor = new SpinnerFloatEditor(); |
| a9b2a64 | 206 | editor.spinner.addChangeListener(new ChangeListener(){
|
| a9b2a64 | 207 | @Override |
| a9b2a64 | 208 | public void stateChanged(ChangeEvent arg0) {
|
| a9b2a64 | 209 | setProperty(key, editor.spinner.getValue().toString()); |
| a9b2a64 | 210 | } |
| a9b2a64 | 211 | }); |
| a9b2a64 | 212 | return editor; |
| a9b2a64 | 213 | } |
| a9b2a64 | 214 | |
| a62d533 | 215 | public static JSpinner createSpinnerInteger(){
|
| a62d533 | 216 | JSpinner spinner = new JSpinner(new SpinnerNumberModel(new Integer(0), new Integer(-99999), |
| a62d533 | 217 | new Integer(99999), new Integer(1))); |
| a62d533 | 218 | UIUtils.setDrawBorder(spinner, false); |
| a62d533 | 219 | UIUtils.setShadeWidth(spinner, 0); |
| a62d533 | 220 | UIUtils.setRound(spinner, 0); |
| a62d533 | 221 | UIUtils.setDrawFocus(spinner, false); |
| 7c84877 | 222 | spinner.setFocusable(false); |
| a62d533 | 223 | return spinner; |
| a62d533 | 224 | } |
| a62d533 | 225 | |
| a62d533 | 226 | public static JSpinner createSpinnerFloat(){
|
| a62d533 | 227 | JSpinner spinner = new JSpinner(new SpinnerNumberModel(new Float(0), new Float(-99999), |
| a62d533 | 228 | new Float(99999), new Float(0.1f))); |
| a62d533 | 229 | UIUtils.setDrawBorder(spinner, false); |
| a62d533 | 230 | UIUtils.setShadeWidth(spinner, 0); |
| a62d533 | 231 | UIUtils.setRound(spinner, 0); |
| a62d533 | 232 | UIUtils.setDrawFocus(spinner, false); |
| 7c84877 | 233 | spinner.setFocusable(false); |
| a62d533 | 234 | return spinner; |
| a62d533 | 235 | } |
| a62d533 | 236 | |
| a62d533 | 237 | public static DefaultCellEditor createTextFieldEditor(){
|
| a62d533 | 238 | JTextField tf = new JTextField(); |
| a62d533 | 239 | UIUtils.setShadeWidth(tf, 0); |
| a62d533 | 240 | UIUtils.setRound(tf, 0); |
| a62d533 | 241 | UIUtils.setDrawFocus(tf, false); |
| 7c84877 | 242 | tf.setFocusable(false); |
| a62d533 | 243 | return new DefaultCellEditor(tf); |
| a62d533 | 244 | } |
| a62d533 | 245 | |
| a62d533 | 246 | public static JTextField createTextField(){
|
| a62d533 | 247 | JTextField tf = new JTextField(); |
| a62d533 | 248 | UIUtils.setShadeWidth(tf, 0); |
| a62d533 | 249 | UIUtils.setRound(tf, 0); |
| a62d533 | 250 | UIUtils.setDrawFocus(tf, false); |
| 7c84877 | 251 | tf.setFocusable(false); |
| a62d533 | 252 | return tf; |
| a62d533 | 253 | } |
| a62d533 | 254 | |
| a62d533 | 255 | public static DefaultCellEditor createNumberField(){
|
| a62d533 | 256 | NumberTextField tf = new NumberTextField(); |
| a62d533 | 257 | UIUtils.setShadeWidth(tf, 0); |
| a62d533 | 258 | UIUtils.setRound(tf, 0); |
| a62d533 | 259 | UIUtils.setDrawFocus(tf, false); |
| 7c84877 | 260 | tf.setFocusable(false); |
| a62d533 | 261 | return new DefaultCellEditor(tf); |
| a62d533 | 262 | } |
| a62d533 | 263 | |
| a62d533 | 264 | public static JComboBox<String> createComboBox(){
|
| a62d533 | 265 | JComboBox<String> combo = new JComboBox<String>(); |
| a62d533 | 266 | UIUtils.setShadeWidth(combo, 0); |
| a62d533 | 267 | UIUtils.setRound(combo, 0); |
| a62d533 | 268 | UIUtils.setDrawFocus(combo, false); |
| 7c84877 | 269 | combo.setFocusable(false); |
| a62d533 | 270 | return combo; |
| a62d533 | 271 | } |
| a62d533 | 272 | |
| a62d533 | 273 | public static JComboBox<String> createComboBox(String... items){
|
| a62d533 | 274 | JComboBox<String> combo = new JComboBox<String>(items); |
| a62d533 | 275 | UIUtils.setShadeWidth(combo, 0); |
| a62d533 | 276 | UIUtils.setRound(combo, 0); |
| a62d533 | 277 | UIUtils.setDrawFocus(combo, false); |
| a62d533 | 278 | return combo; |
| a62d533 | 279 | } |
| a62d533 | 280 | |
| a62d533 | 281 | public static DefaultCellEditor createBooleanEditor(){
|
| a9b2a64 | 282 | return new DefaultCellEditor(createComboBox("false", "true"));
|
| a62d533 | 283 | } |
| a62d533 | 284 | |
| 7c84877 | 285 | public static DefaultCellEditor createTouchableEditor(){
|
| a9b2a64 | 286 | return new DefaultCellEditor(createComboBox(Touchable.enabled.toString() |
| a9b2a64 | 287 | , Touchable.disabled.toString(), Touchable.childrenOnly.toString())); |
| a62d533 | 288 | } |
| a62d533 | 289 | |
| 7c84877 | 290 | public static TableCellEditor createCheckBoxEditor(){
|
| 7c84877 | 291 | return new CheckBoxEditor(); |
| a62d533 | 292 | } |
| a62d533 | 293 | } |
| a62d533 | 294 | |
| a62d533 | 295 | |
| a62d533 | 296 | class BaseRenderer extends DefaultTableCellRenderer {
|
| a62d533 | 297 | private static final long serialVersionUID = 1L; |
| a62d533 | 298 | JLabel label = new JLabel(" ");
|
| a62d533 | 299 | JCheckBox checkBox = new JCheckBox(); |
| a62d533 | 300 | JSpinner spinnerFloat = BaseTable.createSpinnerFloat(); |
| a62d533 | 301 | JSpinner spinnerInteger = BaseTable.createSpinnerInteger(); |
| a62d533 | 302 | |
| a62d533 | 303 | public BaseRenderer(){
|
| a62d533 | 304 | spinnerInteger.setBorder(BorderFactory.createEmptyBorder()); |
| a62d533 | 305 | spinnerFloat.setBorder(BorderFactory.createEmptyBorder()); |
| a62d533 | 306 | } |
| a62d533 | 307 | |
| a62d533 | 308 | @Override |
| a62d533 | 309 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
| a62d533 | 310 | setBorder(noFocusBorder); |
| a62d533 | 311 | if(column == 0) |
| a62d533 | 312 | return new HeaderLabel(value.toString()); |
| a62d533 | 313 | else {
|
| a62d533 | 314 | if(value instanceof Boolean) { // Boolean
|
| a62d533 | 315 | checkBox.setSelected(((Boolean) value).booleanValue()); |
| a62d533 | 316 | checkBox.repaint(); |
| a62d533 | 317 | checkBox.setHorizontalAlignment(JLabel.CENTER); |
| a62d533 | 318 | return checkBox; |
| a62d533 | 319 | } |
| a62d533 | 320 | } |
| a62d533 | 321 | return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
| a62d533 | 322 | } |
| a62d533 | 323 | } |
| a62d533 | 324 | |
| a62d533 | 325 | class HeaderLabel extends JLabel{
|
| a62d533 | 326 | private static final long serialVersionUID = 1L; |
| a62d533 | 327 | |
| a62d533 | 328 | HeaderLabel(String text){
|
| a62d533 | 329 | super(text); |
| a62d533 | 330 | setHorizontalAlignment ( JLabel.CENTER ); |
| a62d533 | 331 | UIUtils.setMargin(this, new Insets(0,0,0,0)); |
| a62d533 | 332 | setForeground(Style.font); |
| a62d533 | 333 | } |
| a62d533 | 334 | |
| a62d533 | 335 | @Override |
| a62d533 | 336 | public void paint ( Graphics g){
|
| a62d533 | 337 | Style.drawTableHeader(g, getWidth(), getHeight()); |
| a62d533 | 338 | super.paint(g); |
| a62d533 | 339 | } |
| a62d533 | 340 | } |
| a62d533 | 341 | |
| a62d533 | 342 | class ColorEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
|
| a62d533 | 343 | private static final long serialVersionUID = 1L; |
| a62d533 | 344 | Color currentColor = Color.red; |
| a62d533 | 345 | JButton button; |
| a62d533 | 346 | JColorChooser colorChooser; |
| a62d533 | 347 | JDialog dialog; |
| a62d533 | 348 | JPanel pan; |
| a62d533 | 349 | protected static final String EDIT = "edit"; |
| a62d533 | 350 | |
| a62d533 | 351 | public ColorEditor() {
|
| a62d533 | 352 | //Set up the editor (from the table's point of view), |
| a62d533 | 353 | //which is a button. |
| a62d533 | 354 | //This button brings up the color chooser dialog, |
| a62d533 | 355 | //which is the editor from the user's point of view. |
| a62d533 | 356 | button = new JButton(); |
| a62d533 | 357 | button.setActionCommand(EDIT); |
| a62d533 | 358 | button.addActionListener(this); |
| a62d533 | 359 | button.setBorderPainted(false); |
| a62d533 | 360 | button.setRolloverEnabled(false); |
| a62d533 | 361 | button.setOpaque(true); |
| a62d533 | 362 | pan = new JPanel(new ToolbarLayout()); |
| a62d533 | 363 | pan.setOpaque(false); |
| a62d533 | 364 | UIUtils.setMargin(pan, new Insets(2,3,2,3)); |
| a62d533 | 365 | pan.add(button); |
| a62d533 | 366 | //Set up the dialog that the button brings up. |
| a62d533 | 367 | colorChooser = new JColorChooser(); |
| a62d533 | 368 | dialog = JColorChooser.createDialog(button, |
| a62d533 | 369 | "Pick a Color",true, //modal |
| a62d533 | 370 | colorChooser, |
| a62d533 | 371 | this, //OK button handler |
| a62d533 | 372 | null); //no CANCEL button handler |
| a62d533 | 373 | } |
| a62d533 | 374 | |
| a62d533 | 375 | /** |
| a62d533 | 376 | * Handles events from the editor button and from |
| a62d533 | 377 | * the dialog's OK button. |
| a62d533 | 378 | */ |
| a62d533 | 379 | public void actionPerformed(ActionEvent e) {
|
| a62d533 | 380 | if (EDIT.equals(e.getActionCommand())) {
|
| a62d533 | 381 | //The user has clicked the cell, so |
| a62d533 | 382 | //bring up the dialog. |
| a62d533 | 383 | button.setBackground(currentColor); |
| a62d533 | 384 | colorChooser.setColor(currentColor); |
| a62d533 | 385 | dialog.setVisible(true); |
| a62d533 | 386 | |
| a62d533 | 387 | //Make the renderer reappear. |
| a62d533 | 388 | fireEditingStopped(); |
| a62d533 | 389 | |
| a62d533 | 390 | } else { //User pressed dialog's "OK" button.
|
| a62d533 | 391 | currentColor = colorChooser.getColor(); |
| a62d533 | 392 | dialog.setVisible(false); |
| a62d533 | 393 | } |
| a62d533 | 394 | } |
| a62d533 | 395 | |
| a62d533 | 396 | //Implement the one CellEditor method that AbstractCellEditor doesn't. |
| a62d533 | 397 | @Override |
| a62d533 | 398 | public String getCellEditorValue() {
|
| a62d533 | 399 | return String.format("%02x%02x%02x%02x",currentColor.getRed(),
|
| a62d533 | 400 | currentColor.getGreen(), currentColor.getBlue(), currentColor.getAlpha()); |
| a62d533 | 401 | } |
| a62d533 | 402 | |
| a62d533 | 403 | //Implement the one method defined by TableCellEditor. |
| a62d533 | 404 | @Override |
| a62d533 | 405 | public Component getTableCellEditorComponent(JTable table, |
| a62d533 | 406 | Object value, |
| a62d533 | 407 | boolean isSelected, |
| a62d533 | 408 | int row, |
| a62d533 | 409 | int column) {
|
| a62d533 | 410 | String colorString = (String) value; |
| a62d533 | 411 | if(colorString.length() == 8 && colorString.matches("[0-9A-Fa-f]+")){
|
| a62d533 | 412 | com.badlogic.gdx.graphics.Color cl = com.badlogic.gdx.graphics.Color.valueOf(colorString); |
| a62d533 | 413 | currentColor = new Color(cl.r, cl.g, cl.b, cl.a); |
| a62d533 | 414 | } |
| a62d533 | 415 | return pan; |
| a62d533 | 416 | } |
| a62d533 | 417 | |
| a62d533 | 418 | @Override |
| a62d533 | 419 | public void addCellEditorListener(CellEditorListener arg0) {
|
| a62d533 | 420 | super.addCellEditorListener(arg0); |
| a62d533 | 421 | } |
| a62d533 | 422 | |
| a62d533 | 423 | @Override |
| a62d533 | 424 | public void cancelCellEditing() {
|
| a62d533 | 425 | dialog.setVisible(false); |
| a62d533 | 426 | super.cancelCellEditing(); |
| a62d533 | 427 | } |
| a62d533 | 428 | |
| a62d533 | 429 | @Override |
| a62d533 | 430 | public boolean isCellEditable(EventObject arg0) {
|
| a62d533 | 431 | return true; |
| a62d533 | 432 | } |
| a62d533 | 433 | |
| a62d533 | 434 | @Override |
| a62d533 | 435 | public void removeCellEditorListener(CellEditorListener arg0) {
|
| a62d533 | 436 | super.removeCellEditorListener(arg0); |
| a62d533 | 437 | } |
| a62d533 | 438 | |
| a62d533 | 439 | @Override |
| a62d533 | 440 | public boolean shouldSelectCell(EventObject arg0) {
|
| a62d533 | 441 | return super.shouldSelectCell(arg0); |
| a62d533 | 442 | } |
| a62d533 | 443 | |
| a62d533 | 444 | @Override |
| a62d533 | 445 | public boolean stopCellEditing() {
|
| a62d533 | 446 | dialog.setVisible(false); |
| a62d533 | 447 | return super.stopCellEditing(); |
| a62d533 | 448 | } |
| a62d533 | 449 | } |
| a62d533 | 450 | |
| a62d533 | 451 | class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
|
| a62d533 | 452 | private static final long serialVersionUID = 1L; |
| a62d533 | 453 | private JCheckBox checkBox; |
| a62d533 | 454 | |
| a62d533 | 455 | public CheckBoxEditor() {
|
| a62d533 | 456 | checkBox = new JCheckBox(); |
| a62d533 | 457 | checkBox.setHorizontalAlignment(JLabel.LEFT); |
| a62d533 | 458 | //checkBox.addActionListener(this); |
| a62d533 | 459 | } |
| a62d533 | 460 | |
| a62d533 | 461 | public CheckBoxEditor(boolean value) {
|
| a62d533 | 462 | checkBox = new JCheckBox(); |
| a62d533 | 463 | checkBox.setSelected(value); |
| a62d533 | 464 | checkBox.setHorizontalAlignment(JLabel.LEFT); |
| a62d533 | 465 | //checkBox.addActionListener(this); |
| a62d533 | 466 | } |
| a62d533 | 467 | |
| a62d533 | 468 | /** |
| a62d533 | 469 | * Handles events from the editor button and from |
| a62d533 | 470 | * the dialog's OK button. |
| a62d533 | 471 | */ |
| a62d533 | 472 | public void actionPerformed(ActionEvent e) {
|
| a62d533 | 473 | } |
| a62d533 | 474 | |
| a62d533 | 475 | //Implement the one CellEditor method that AbstractCellEditor doesn't. |
| a62d533 | 476 | @Override |
| a62d533 | 477 | public Boolean getCellEditorValue() {
|
| a62d533 | 478 | return checkBox.isSelected(); |
| a62d533 | 479 | } |
| a62d533 | 480 | |
| a62d533 | 481 | //Implement the one method defined by TableCellEditor. |
| a62d533 | 482 | @Override |
| a62d533 | 483 | public Component getTableCellEditorComponent(JTable table, |
| a62d533 | 484 | Object value, |
| a62d533 | 485 | boolean isSelected, |
| a62d533 | 486 | int row, |
| a62d533 | 487 | int column) {
|
| a62d533 | 488 | checkBox.setSelected(((Boolean) value).booleanValue()); |
| a62d533 | 489 | checkBox.repaint(); |
| a62d533 | 490 | return checkBox; |
| a62d533 | 491 | } |
| a62d533 | 492 | |
| a62d533 | 493 | @Override |
| a62d533 | 494 | public void addCellEditorListener(CellEditorListener arg0) {
|
| a62d533 | 495 | super.addCellEditorListener(arg0); |
| a62d533 | 496 | } |
| a62d533 | 497 | |
| a62d533 | 498 | @Override |
| a62d533 | 499 | public void cancelCellEditing() {
|
| a62d533 | 500 | super.cancelCellEditing(); |
| a62d533 | 501 | } |
| a62d533 | 502 | |
| a62d533 | 503 | @Override |
| a62d533 | 504 | public boolean isCellEditable(EventObject arg0) {
|
| a62d533 | 505 | return true; |
| a62d533 | 506 | } |
| a62d533 | 507 | |
| a62d533 | 508 | @Override |
| a62d533 | 509 | public void removeCellEditorListener(CellEditorListener arg0) {
|
| a62d533 | 510 | super.removeCellEditorListener(arg0); |
| a62d533 | 511 | } |
| a62d533 | 512 | |
| a62d533 | 513 | @Override |
| a62d533 | 514 | public boolean shouldSelectCell(EventObject arg0) {
|
| a62d533 | 515 | return super.shouldSelectCell(arg0); |
| a62d533 | 516 | } |
| a62d533 | 517 | |
| a62d533 | 518 | @Override |
| a62d533 | 519 | public boolean stopCellEditing() {
|
| a62d533 | 520 | return super.stopCellEditing(); |
| a62d533 | 521 | } |
| a62d533 | 522 | } |
| a62d533 | 523 | |
| a62d533 | 524 | class SpinnerIntegerEditor extends AbstractCellEditor implements TableCellEditor {
|
| a62d533 | 525 | private static final long serialVersionUID = 1L; |
| a62d533 | 526 | JSpinner spinner; |
| a62d533 | 527 | |
| a62d533 | 528 | // Initializes the spinner. |
| a62d533 | 529 | public SpinnerIntegerEditor() {
|
| a62d533 | 530 | spinner = BaseTable.createSpinnerInteger(); |
| a62d533 | 531 | spinner.setBorder(BorderFactory.createEmptyBorder()); |
| a62d533 | 532 | ((DefaultEditor) spinner.getEditor()).getTextField().setEditable(false); |
| a62d533 | 533 | } |
| a62d533 | 534 | |
| a62d533 | 535 | // Prepares the spinner component and returns it. |
| a62d533 | 536 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, |
| a62d533 | 537 | int row, int column) {
|
| a9b2a64 | 538 | float f = (Float) Float.parseFloat(value.toString()); |
| a9b2a64 | 539 | spinner.setValue((int) f); |
| a62d533 | 540 | return spinner; |
| a62d533 | 541 | } |
| a62d533 | 542 | |
| a62d533 | 543 | public boolean isCellEditable( EventObject eo ) {
|
| a62d533 | 544 | return true; |
| a62d533 | 545 | } |
| a62d533 | 546 | |
| a62d533 | 547 | // Returns the spinners current value. |
| a62d533 | 548 | public Object getCellEditorValue() {
|
| a62d533 | 549 | return spinner.getValue(); |
| a62d533 | 550 | } |
| a62d533 | 551 | |
| a62d533 | 552 | public boolean stopCellEditing() {
|
| a62d533 | 553 | try {
|
| a62d533 | 554 | spinner.commitEdit(); |
| a62d533 | 555 | } catch ( java.text.ParseException e ) {
|
| a62d533 | 556 | JOptionPane.showMessageDialog(null,"Invalid value, discarding."); |
| a62d533 | 557 | } |
| a62d533 | 558 | return super.stopCellEditing(); |
| a62d533 | 559 | } |
| a62d533 | 560 | } |
| a62d533 | 561 | |
| a62d533 | 562 | class SpinnerFloatEditor extends AbstractCellEditor implements TableCellEditor {
|
| a62d533 | 563 | private static final long serialVersionUID = 1L; |
| a62d533 | 564 | JSpinner spinner; |
| a62d533 | 565 | |
| a62d533 | 566 | // Initializes the spinner. |
| a62d533 | 567 | public SpinnerFloatEditor() {
|
| a62d533 | 568 | spinner = BaseTable.createSpinnerFloat(); |
| a62d533 | 569 | spinner.setBorder(BorderFactory.createEmptyBorder()); |
| a62d533 | 570 | ((DefaultEditor) spinner.getEditor()).getTextField().setEditable(false); |
| a62d533 | 571 | } |
| a62d533 | 572 | |
| a62d533 | 573 | // Prepares the spinner component and returns it. |
| a62d533 | 574 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, |
| a62d533 | 575 | int row, int column) {
|
| a62d533 | 576 | spinner.setValue((Float)Float.parseFloat(value.toString())); |
| a62d533 | 577 | return spinner; |
| a62d533 | 578 | } |
| a62d533 | 579 | |
| a62d533 | 580 | public boolean isCellEditable( EventObject eo ) {
|
| a62d533 | 581 | return true; |
| a62d533 | 582 | } |
| a62d533 | 583 | |
| a62d533 | 584 | // Returns the spinners current value. |
| a62d533 | 585 | public Object getCellEditorValue() {
|
| a62d533 | 586 | return spinner.getValue(); |
| a62d533 | 587 | } |
| a62d533 | 588 | |
| a62d533 | 589 | public boolean stopCellEditing() {
|
| a62d533 | 590 | try {
|
| a62d533 | 591 | spinner.commitEdit(); |
| a62d533 | 592 | } catch ( java.text.ParseException e ) {
|
| a62d533 | 593 | JOptionPane.showMessageDialog(null, "Invalid value, discarding."); |
| a62d533 | 594 | } |
| a62d533 | 595 | return super.stopCellEditing(); |
| a62d533 | 596 | } |
| a62d533 | 597 | } |