Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / table / models / TableColorButtonColumnEditor.java @ 28193

History | View | Annotate | Download (3.02 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.gui.beans.table.models;
20

    
21
import java.awt.Color;
22
import java.awt.Component;
23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
25

    
26
import javax.swing.AbstractCellEditor;
27
import javax.swing.JButton;
28
import javax.swing.JColorChooser;
29
import javax.swing.JDialog;
30
import javax.swing.JTable;
31
import javax.swing.table.DefaultTableModel;
32
import javax.swing.table.TableCellEditor;
33

    
34
import org.gvsig.gui.beans.Messages;
35
/**
36
 * Editor de celda con un bot?n selector de color-
37
 *
38
 * @author Nacho Brodin (brodin_ign@gva.es)
39
 */
40
public class TableColorButtonColumnEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
41
        private static final long serialVersionUID = -6627842834708616873L;
42
        Color currentColor;
43
        JButton button;
44
        JColorChooser colorChooser;
45
        JDialog dialog;
46
        private DefaultTableModel tableModel = null;
47
        private JTable table = null;
48
        protected static final String EDIT = "edit";
49

    
50
        public TableColorButtonColumnEditor(DefaultTableModel tableModel, JTable table) {
51
                button = new JButton();
52
                button.setActionCommand(EDIT);
53
                button.addActionListener(this);
54
                button.setBorderPainted(false);
55
                this.tableModel = tableModel;
56
                this.table = table;
57

    
58
                colorChooser = new JColorChooser();
59
                dialog = JColorChooser.createDialog(button, Messages.getText("select_color"), true, colorChooser, this, null);
60
        }
61

    
62
        public void actionPerformed(ActionEvent e) {
63
                if (EDIT.equals(e.getActionCommand())) {
64
                        button.setBackground(currentColor);
65
                        colorChooser.setColor(currentColor);
66
                        dialog.setVisible(true);
67
                        if ((tableModel != null) && (table != null)) {
68
                                String newColor = currentColor.getRed() + "," + currentColor.getGreen() + "," + currentColor.getBlue();
69
                                if (!newColor.equals(tableModel.getValueAt(table.getSelectedRow(), 2))) {
70
                                        if (! (tableModel instanceof ROIsTableModel))
71
                                                tableModel.setValueAt(newColor, table.getSelectedRow(), 2);
72
                                }
73
                        }
74
                        fireEditingStopped();
75
                } else
76
                        currentColor = colorChooser.getColor();
77
        }
78

    
79
        public Object getCellEditorValue() {
80
                return currentColor;
81
        }
82

    
83
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
84
                currentColor = (Color) value;
85
                return button;
86
        }
87
}