Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extWFS2 / src / org / gvsig / wfs / gui / panels / fieldstree / CheckBoxTreeCellEditor.java @ 34026

History | View | Annotate | Download (6.05 KB)

1
package org.gvsig.wfs.gui.panels.fieldstree;
2

    
3
import java.awt.Component;
4
import java.awt.event.MouseEvent;
5
import java.util.EventObject;
6

    
7
import javax.swing.AbstractCellEditor;
8
import javax.swing.JCheckBox;
9
import javax.swing.JTree;
10
import javax.swing.event.ChangeEvent;
11
import javax.swing.tree.TreeCellEditor;
12
import javax.swing.tree.TreePath;
13

    
14
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
15
 *
16
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
31
 *
32
 * For more information, contact:
33
 *
34
 *  Generalitat Valenciana
35
 *   Conselleria d'Infraestructures i Transport
36
 *   Av. Blasco Ib??ez, 50
37
 *   46010 VALENCIA
38
 *   SPAIN
39
 *
40
 *      +34 963862235
41
 *   gvsig@gva.es
42
 *      www.gvsig.gva.es
43
 *
44
 *    or
45
 *
46
 *   IVER T.I. S.A
47
 *   Salamanca 50
48
 *   46005 Valencia
49
 *   Spain
50
 *
51
 *   +34 963163400
52
 *   dac@iver.es
53
 */
54
/* CVS MESSAGES:
55
 *
56
 * $Id$
57
 * $Log$
58
 * Revision 1.3  2007-09-19 16:14:50  jaume
59
 * removed unnecessary imports
60
 *
61
 * Revision 1.2  2006/12/26 10:25:37  ppiqueras
62
 * Corregidas las dependencias con las nuevas ubicaciones de clases: IXMLType, XMLElement, IXMLComplexType, etc. (en libRemoteServices)
63
 *
64
 * Revision 1.1  2006/12/26 09:12:48  ppiqueras
65
 * Cambiado "atttibutes" en todas las aparaciones en atributos, m?todos, clases, paquetes o comentarios por "fields". (S?lo a aquellas que afectan a clases dentro del proyecto extWFS2). (En este caso se ha cambiado el nombre del paquete aparte de alguno otro nombre que puede haber cambiado).
66
 *
67
 * Revision 1.2  2006/10/31 12:24:04  jorpiell
68
 * Comprobado el caso en el que los atributos no tienen tipo
69
 *
70
 * Revision 1.1  2006/10/27 11:33:19  jorpiell
71
 * A?adida la treetable con los check box para seleccionar los atributos
72
 *
73
 *
74
 */
75
/**
76
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
77
 */
78
public class CheckBoxTreeCellEditor extends AbstractCellEditor implements TreeCellEditor {
79
        CheckBoxTreeCellRenderer renderer = null;
80
        ChangeEvent changeEvent = null;
81
        JTree tree;
82
        FieldsTreeTable treetable;
83
        
84
        public CheckBoxTreeCellEditor(FieldsTreeTable treetable) {
85
                this.treetable = treetable;
86
                this.tree = (JTree)treetable.getTree();
87
                this.renderer = new CheckBoxTreeCellRenderer(treetable);
88
        }        
89
        
90
        /*
91
         *  (non-Javadoc)
92
         * @see javax.swing.CellEditor#getCellEditorValue()
93
         */
94
        public Object getCellEditorValue() {
95
                return null;
96
        }
97
        
98
        /*
99
         *  (non-Javadoc)
100
         * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject)
101
         */
102
        public boolean isCellEditable(EventObject event) {
103
                if (event instanceof MouseEvent) {
104
                        MouseEvent mouseEvent = (MouseEvent) event;
105
                        TreePath path = tree.getPathForLocation(mouseEvent.getX(),
106
                                        mouseEvent.getY());
107
                        if (path != null) {
108
                                Object node = path.getLastPathComponent();
109
                                if ((node != null) && (node instanceof CheckBoxNode)) {
110
                                        CheckBoxNode selectedNode = (CheckBoxNode)node;
111
                                        if (mouseEvent.getClickCount() == 2){
112
                                                
113
                                        }else if(mouseEvent.getClickCount() == 1){
114
                                                changeAllChildren(selectedNode,!selectedNode.isSelected());
115
                                                changeParentState(selectedNode,!selectedNode.isSelected());
116
                                                selectedNode.setSelected(!selectedNode.isSelected());
117
                                                tree.repaint();
118
                                        }
119
                                }
120
                        }
121
                }
122
                return false;
123
        }
124
        
125
        /*
126
         *  (non-Javadoc)
127
         * @see javax.swing.tree.TreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int)
128
         */
129
        public Component getTreeCellEditorComponent(JTree tree, Object value,
130
                        boolean selected, boolean expanded, boolean leaf, int row) {
131
                Component editor = renderer.getTreeCellRendererComponent(tree, value,
132
                                true, expanded, leaf, row, true);
133
                
134
                if (editor instanceof JCheckBox) {
135
                        //((JCheckBox) editor).addItemListener(new CheckBoxListener());
136
                }                
137
                return editor;
138
        }        
139
        
140
        /**
141
         * It enable or disable the parent node state depending of its children values
142
         * @param selectedNode
143
         * Selected node
144
         * @param isSelected
145
         * Current node status
146
         */
147
        private void changeParentState(CheckBoxNode selectedNode,boolean isSelected){
148
                CheckBoxNode parent = selectedNode.getParentNode();
149
                while (parent != null){                        
150
                        if (isSelected != parent.isSelected()){
151
                                parent.setColor(TetraStateCheckBox.GREY);
152
                        }else{        
153
                                boolean isEnabled = true;
154
                                for (int i=0 ; i<parent.getChildren().size() ; i++){
155
                                        CheckBoxNode child = (CheckBoxNode)parent.getChildren().get(i);
156
                                        if (child != selectedNode){
157
                                                if (parent.isSelected() != child.isSelected()){
158
                                                        isEnabled = false;
159
                                                }
160
                                        }
161
                                }
162
                                if (!isEnabled){
163
                                        parent.setColor(TetraStateCheckBox.GREY);
164
                                }else{
165
                                        parent.setColor(TetraStateCheckBox.WHITE);
166
                                }                                        
167
                                
168
                        }                        
169
                        selectedNode = parent;
170
                        parent = selectedNode.getParentNode();                        
171
                }
172
        }
173
        
174
        
175
        /**
176
         * It changes all the children status
177
         * @param selectedNode
178
         * Root node
179
         * @param selected
180
         * New state
181
         */
182
        private void changeAllChildren(CheckBoxNode selectedNode,boolean selected){
183
                for (int i=0 ; i<selectedNode.getChildren().size() ; i++){
184
                        CheckBoxNode child = ((CheckBoxNode)selectedNode.getChildren().get(i));
185
                        if (child.getFeatureField().isGeometry()){
186
                                child.setSelected(true);        
187
                        }else{                            
188
                            child.setSelected(selected);
189
                            if (child.getChildren().size() > 0){
190
                                    changeAllChildren(child,selected);
191
                            }        
192
                        }
193
                }
194
        }
195
}