Statistics
| Revision:

root / org.gvsig.wfs.app / trunk / org.gvsig.wfs.app / org.gvsig.wfs.app.mainplugin / src / main / java / org / gvsig / wfs / gui / panels / fieldstree / CheckBoxTreeCellEditor.java @ 10

History | View | Annotate | Download (6.94 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
                        if (!selectedNode.getFeatureField().isGeometry()){
115
                            changeAllChildren(selectedNode,!selectedNode.isSelected());
116
                            changeParentState(selectedNode,!selectedNode.isSelected());                                                
117
                            selectedNode.setSelected(!selectedNode.isSelected());
118
                        }
119
                        tree.repaint();
120
                    }
121
                }
122
            }
123
        }
124
        return false;
125
    }
126

    
127
    /*
128
     *  (non-Javadoc)
129
     * @see javax.swing.tree.TreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int)
130
     */
131
    public Component getTreeCellEditorComponent(JTree tree, Object value,
132
        boolean selected, boolean expanded, boolean leaf, int row) {
133
        Component editor = renderer.getTreeCellRendererComponent(tree, value,
134
            true, expanded, leaf, row, true);
135

    
136
        if (editor instanceof JCheckBox) {
137
            //((JCheckBox) editor).addItemListener(new CheckBoxListener());
138
        }                
139
        return editor;
140
    }        
141

    
142
    /**
143
     * It enable or disable the parent node state depending of its children values
144
     * @param selectedNode
145
     * Selected node
146
     * @param isSelected
147
     * Current node status
148
     */
149
    private void changeParentState(CheckBoxNode selectedNode,boolean isSelected){
150
        CheckBoxNode parent = selectedNode.getParentNode();
151
        while (parent != null){                        
152
            if (isSelected != parent.isSelected()){
153
                parent.setColor(TetraStateCheckBox.GREY);
154
            }else{        
155
                boolean isEnabled = true;
156
                for (int i=0 ; i<parent.getChildren().size() ; i++){
157
                    CheckBoxNode child = (CheckBoxNode)parent.getChildren().get(i);
158
                    if (child != selectedNode){
159
                        if (parent.isSelected() != child.isSelected()){
160
                            isEnabled = false;
161
                        }
162
                    }
163
                }
164
                if (!isEnabled){
165
                    parent.setColor(TetraStateCheckBox.GREY);
166
                }else{
167
                    parent.setColor(TetraStateCheckBox.WHITE);
168
                }                                        
169

    
170
            }                        
171
            selectedNode = parent;
172
            parent = selectedNode.getParentNode();                        
173
        }
174
    }
175

    
176

    
177
    /**
178
     * It changes all the children status
179
     * @param selectedNode
180
     * Root node
181
     * @param selected
182
     * New state
183
     */
184
    private void changeAllChildren(CheckBoxNode selectedNode,boolean selected){
185
        for (int i=0 ; i<selectedNode.getChildren().size() ; i++){
186
            CheckBoxNode child = ((CheckBoxNode)selectedNode.getChildren().get(i));
187
            if (child.getFeatureField().isGeometry()){
188
                child.setSelected(true);        
189
            }else{                            
190
                child.setSelected(selected);
191
                if (child.getChildren().size() > 0){
192
                    changeAllChildren(child,selected);
193
                }        
194
            }
195
        }
196
    }
197
}