-class ColorEditor
- extends AbstractCellEditor implements TableCellEditor,
- ActionListener
-{
- Color currentColor;
- JButton button;
- JColorChooser colorChooser;
- JDialog dialog;
- protected static final String EDIT = "edit";
-
- public ColorEditor()
- {
- //Set up the editor (from the table's point of view),
- //which is a button.
- //This button brings up the color chooser dialog,
- //which is the editor from the user's point of view.
- button = new JButton();
- button.setActionCommand(EDIT);
- button.addActionListener(this);
- button.setBorderPainted(false);
- //Set up the dialog that the button brings up.
- colorChooser = new JColorChooser();
- dialog = JColorChooser.createDialog(button,
- "Select new Colour",
- true, //modal
- colorChooser,
- this, //OK button handler
- null); //no CANCEL button handler
+ class ColorEditor extends AbstractCellEditor
+ implements TableCellEditor, ActionListener
+ {
+ FeatureSettings me;
+
+ FeatureColourI currentColor;
+
+ FeatureTypeSettings chooser;
+
+ String type;
+
+ JButton colourButton;
+
+ JColorChooser colorChooser;
+
+ JDialog dialog;
+
+ protected static final String EDIT = "edit";
+
+ int rowSelected = 0;
+
+ public ColorEditor(FeatureSettings me)
+ {
+ this.me = me;
+ // Set up the editor (from the table's point of view),
+ // which is a button.
+ // This button brings up the color chooser dialog,
+ // which is the editor from the user's point of view.
+ colourButton = new JButton();
+ colourButton.setActionCommand(EDIT);
+ colourButton.addActionListener(this);
+ colourButton.setBorderPainted(false);
+ // Set up the dialog that the button brings up.
+ colorChooser = new JColorChooser();
+ dialog = JColorChooser.createDialog(colourButton,
+ MessageManager.getString("label.select_colour"), true, // modal
+ colorChooser, this, // OK button handler
+ null); // no CANCEL button handler
+ }
+
+ /**
+ * Handles events from the editor button and from the dialog's OK button.
+ */
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ // todo test e.getSource() instead here
+ if (EDIT.equals(e.getActionCommand()))
+ {
+ // The user has clicked the cell, so
+ // bring up the dialog.
+ if (currentColor.isSimpleColour())
+ {
+ // bring up simple color chooser
+ colourButton.setBackground(currentColor.getColour());
+ colorChooser.setColor(currentColor.getColour());
+ dialog.setVisible(true);
+ }
+ else
+ {
+ // bring up graduated chooser.
+ chooser = new FeatureTypeSettings(me.fr, type);
+ chooser.setRequestFocusEnabled(true);
+ chooser.requestFocus();
+ chooser.addActionListener(this);
+ // Make the renderer reappear.
+ fireEditingStopped();
+ }
+ }
+ else
+ {
+ if (currentColor.isSimpleColour())
+ {
+ /*
+ * read off colour picked in colour chooser after OK pressed
+ */
+ currentColor = new FeatureColour(colorChooser.getColor());
+ me.table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
+ }
+ else
+ {
+ /*
+ * after OK in variable colour dialog, any changes to colour
+ * (or filters!) are already set in FeatureRenderer, so just
+ * update table data without triggering updateFeatureRenderer
+ */
+ refreshTable();
+ }
+ fireEditingStopped();
+ me.table.validate();
+ }
+ }
+
+ // Implement the one CellEditor method that AbstractCellEditor doesn't.
+ @Override
+ public Object getCellEditorValue()
+ {
+ return currentColor;
+ }
+
+ // Implement the one method defined by TableCellEditor.
+ @Override
+ public Component getTableCellEditorComponent(JTable theTable, Object value,
+ boolean isSelected, int row, int column)
+ {
+ currentColor = (FeatureColourI) value;
+ this.rowSelected = row;
+ type = me.table.getValueAt(row, TYPE_COLUMN).toString();
+ colourButton.setOpaque(true);
+ colourButton.setBackground(me.getBackground());
+ if (!currentColor.isSimpleColour())
+ {
+ JLabel btn = new JLabel();
+ btn.setSize(colourButton.getSize());
+ FeatureSettings.renderGraduatedColor(btn, currentColor);
+ colourButton.setBackground(btn.getBackground());
+ colourButton.setIcon(btn.getIcon());
+ colourButton.setText(btn.getText());
+ }
+ else
+ {
+ colourButton.setText("");
+ colourButton.setIcon(null);
+ colourButton.setBackground(currentColor.getColour());
+ }
+ return colourButton;
+ }