Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / table / models / TableColorButtonColumnEditor.java @ 40561

History | View | Annotate | Download (3.18 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.table.models;
25

    
26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30

    
31
import javax.swing.AbstractCellEditor;
32
import javax.swing.JButton;
33
import javax.swing.JColorChooser;
34
import javax.swing.JDialog;
35
import javax.swing.JTable;
36
import javax.swing.table.DefaultTableModel;
37
import javax.swing.table.TableCellEditor;
38

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

    
55
        public TableColorButtonColumnEditor(DefaultTableModel tableModel, JTable table) {
56
                button = new JButton();
57
                button.setActionCommand(EDIT);
58
                button.addActionListener(this);
59
                button.setBorderPainted(false);
60
                this.tableModel = tableModel;
61
                this.table = table;
62

    
63
                colorChooser = new JColorChooser();
64
                dialog = JColorChooser.createDialog(button, Messages.getText("select_color"), true, colorChooser, this, null);
65
        }
66

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

    
85
        public Object getCellEditorValue() {
86
                return currentColor;
87
        }
88

    
89
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
90
                currentColor = (Color) value;
91
                return button;
92
        }
93
}