Statistics
| Revision:

root / branches / gvSIG_WMSv2 / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / StyleTree.java @ 3592

History | View | Annotate | Download (11 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: StyleTree.java 3592 2006-01-11 12:20:50Z jaume $
45
* $Log$
46
* Revision 1.1.2.6  2006-01-11 12:20:30  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.5  2006/01/10 11:33:31  jaume
50
* Time dimension working against Jet Propulsion Laboratory's WMS server
51
*
52
* Revision 1.1.2.4  2006/01/09 18:10:38  jaume
53
* casi con el time dimension
54
*
55
* Revision 1.1.2.3  2006/01/05 23:15:53  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1.2.2  2006/01/04 18:09:02  jaume
59
* Time dimension
60
*
61
* Revision 1.1.2.1  2006/01/03 18:08:40  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1.2.1  2006/01/02 18:08:01  jaume
65
* Tree de estilos
66
*
67
*
68
*/
69
/**
70
 * 
71
 */
72
package com.iver.cit.gvsig.gui.panels;
73

    
74
import java.awt.Color;
75
import java.awt.Component;
76
import java.util.Hashtable;
77
import java.util.Iterator;
78
import java.util.Vector;
79

    
80
import javax.swing.JLabel;
81
import javax.swing.JPanel;
82
import javax.swing.JRadioButton;
83
import javax.swing.JTree;
84
import javax.swing.event.TreeModelListener;
85
import javax.swing.tree.TreeCellRenderer;
86
import javax.swing.tree.TreeModel;
87
import javax.swing.tree.TreePath;
88

    
89
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
90
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
91

    
92
/**
93
 * This class holds a visual tree that handles the selection of the styles in the
94
 * selected layers. It encapsulates any gui interface handling the mouse selection.
95
 * <p>
96
 * It has a method getStylesSelection that returns the current selection in a 
97
 * bi-dimensional string array containing the layer name and the selected style name.
98
 * </p>
99
 * <p>
100
 * Keep in mind that although it's similar to LayersTree it is not the same.
101
 * It does not use the same instances of the WMSLayerNode that LayerTreeModel
102
 * because it allows to repeat layers. So, each layer is a completely new 
103
 * WMSLayerNode containing same properties but not the same parent (which is always
104
 * the tree root) and each style is a completely new FMapStyle containing same
105
 * properties but its parent is one of the layers of the StylesTree and not one
106
 * of those of the Layers tree.
107
 * </p>
108
 * 
109
 * @author jaume dominguez faus
110
 *
111
 */
112
public class StyleTree extends JTree {
113
    public StyleTree(){
114
        super();
115
        initialize();
116
    }
117
    
118
    private void initialize(){
119
        setToggleClickCount(1);
120
        setRowHeight(22);
121
        
122
        setCellRenderer(new TreeCellRenderer() {
123
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
124
                if (leaf) {
125
                    JPanel leafComponent = new JPanel();
126
                    leafComponent.setBackground(Color.WHITE);
127
                    JRadioButton leafRadioButton = new JRadioButton("", ((StylesTreeModel) getModel()).isSelected((FMapWMSStyle) value));//selected);
128
                    leafRadioButton.setBackground(Color.WHITE);
129
                    leafComponent.add(leafRadioButton);
130
                    leafComponent.add(new JLabel(((FMapWMSStyle) value).title));
131
                    return leafComponent;
132
                } else {
133
                        JTree t = new JTree();
134
                            Component branchComponent = t.getCellRenderer().getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 
135
                            return branchComponent;
136
                            
137
                        /*
138
                    JPanel branchComponent = new JPanel();
139
                    branchComponent.setBackground(Color.WHITE);
140
                    if (value instanceof WMSLayerNode)
141
                        branchComponent.add(new JLabel(((WMSLayerNode) value).getTitle()));
142
                    return branchComponent;
143
                    */
144
                }
145
            }});
146
        addMouseListener(new java.awt.event.MouseAdapter() { 
147
            public void mouseClicked(java.awt.event.MouseEvent e) {
148
                ((StylesTreeModel)getModel()).setSelectedLeaf(getSelectionPath());
149
                clearSelection();
150
                repaint();
151
            }
152
        });
153
    }
154
    
155
    public Vector getStylesSelection(){
156
        return ((StylesTreeModel)getModel()).getStylesSelection();
157
    }
158
    
159
    /**
160
     * Sets the selected styles in the StylesTree. The argument styleNames is
161
     * a Vector with exactly the same amount of strings than the amount of
162
     * themes (layers) contained by the tree. A blank or null string will
163
     * leave the default style for that layer, but this element <b>must exist</b>
164
     * in the array in any case.
165
     * 
166
     * @param styleNames, Vector containing the style names. 
167
     * The styles order <b>must match</b> with the layer order. 
168
     */
169
    public void setSelections(Vector styleNames){
170
        
171
        StylesTreeModel model = (StylesTreeModel) getModel();
172
        if (model.getChildCount(model.getRoot())!=styleNames.size()){
173
            throw new RuntimeException("Bad arguments: styleNames length is "
174
                    +styleNames.size()+", should be "
175
                    +model.getChildCount(model.getRoot())+".");
176
        }
177
             
178
        for (int i = 0; i < styleNames.size(); i++) {
179
            if (styleNames.get(i)==null || ((String)styleNames.get(i)).equals(""))
180
                continue;
181
            Object node = model.getChild(model.getRoot(), i);
182
            for (int j = 0; j < model.getChildCount(node); j++) {
183
                if (((String)styleNames.get(i)).equals(((FMapWMSStyle)model.getChild(node, j)).name))
184
                    model.selectStyle(model.getChild(node, j));
185
                    
186
            }
187
        }
188
    }
189
}
190

    
191

    
192
class StylesTreeModel implements TreeModel {
193
    WMSLayerNode root;
194
    /**
195
     * key: LayerNode.
196
     * value: FMapWMSStyle
197
     */
198
    Hashtable selections = new Hashtable();
199
    
200
    public StylesTreeModel(WMSLayerNode root){
201
        this.root = root;
202
        
203
    }
204
    
205
    /**
206
     * Marks this style as selected in the StylesTreeModel.
207
     * @param style
208
     */
209
    public void selectStyle(Object style) {
210
        selections.put(((FMapWMSStyle)style).parent, style);
211
    }
212

    
213
    /**
214
     * Gets the names of the selected styles into a Vector using the same order
215
     * as they was represented in the StylesTree.
216
     * @return
217
     */
218
    protected Vector getStylesSelection() {
219
        Vector values = new Vector();
220
        for (int i = 0; i < ((WMSLayerNode)getRoot()).getChildren().size(); i++) {
221
            values.add(i, "");
222
        }
223
        
224
        // TODO em pareix que a?? no est? b? (que no reestablix l'ordre), comprovar.
225
        Iterator it = selections.keySet().iterator();
226
        int i = 0;
227
        while (it.hasNext()){
228
            Object key = it.next();
229
            values.set(i, ((FMapWMSStyle)selections.get(key)).name);
230
            i++;
231
        }
232
        return values;
233
    }
234

    
235
    /**
236
     * Sets a leaf (an style) selected.
237
     * @param selectionPath to this leaf.
238
     */
239
    protected void setSelectedLeaf(TreePath selectionPath) {
240
        if (selectionPath!=null){
241
            Object[] objects = selectionPath.getPath();
242
            Object item = objects[objects.length-1];
243
            if (isLeaf(item)){
244
                selections.put(((FMapWMSStyle) item).parent, item);
245
            }
246
        }
247
    }
248
    
249
    /**
250
     * Will return true if this style is selected.
251
     * @param style
252
     * @return
253
     */
254
    protected boolean isSelected(FMapWMSStyle style){
255
        return selections.get(style.parent) == style;
256
    }
257
    
258
    public int getChildCount(Object parent) {
259
        int count=0;
260
        
261
        if (parent == root)
262
            count = ((WMSLayerNode) parent).getChildren().size();
263
        else
264
            count = ((WMSLayerNode) parent).getStyles().size();
265
        return count;
266
    }
267

    
268
    public boolean isLeaf(Object node) {
269
        return (node instanceof FMapWMSStyle);
270
        
271
    }
272

    
273
    public void addTreeModelListener(TreeModelListener l) {
274
    }
275

    
276
    public void removeTreeModelListener(TreeModelListener l) {
277
    }
278

    
279
    public Object getChild(Object parent, int index) {
280
        if (parent instanceof FMapWMSStyle)
281
            return null;
282
        if (parent == root)
283
            return ((WMSLayerNode) parent).getChildren().get(index);
284
        
285
        return ((WMSLayerNode) parent).getStyles().get(index);
286
    }
287

    
288
    public int getIndexOfChild(Object parent, Object child) {
289
        if (parent instanceof FMapWMSStyle){ 
290
            return -1;   
291
        }
292
         
293
        WMSLayerNode l = (WMSLayerNode)parent;
294
        if (l.getParent()==null){
295
            for (int i = 0; i < l.getChildren().size(); i++) {
296
                if (l.getChildren().get(i).equals(child)){
297
                   return i;
298
                }
299
            }
300
        } else {
301
            for (int i = 0; i < l.getStyles().size(); i++) {
302
                if (l.getChildren().get(i).equals(child)){
303
                    return i;
304
                }
305
            }
306
        }
307
        return -1;
308
    }
309

    
310
    public void valueForPathChanged(TreePath path, Object newValue) {
311
    }
312

    
313
    /**
314
     * Adds a new branch to this tree. It must be a layer node.
315
     * @param node
316
     * @return <b>True</b>, if the added branch actually defines any style.
317
     *         <b>False</b>, if the layer has no styles.
318
     */
319
    public boolean addLayerBranch(WMSLayerNode node){
320
        if (node.getStyles()==null || node.getStyles().size()==0){
321
            return false;
322
        }
323
        WMSLayerNode myNode = (WMSLayerNode) node.clone();
324
        myNode.setParent((WMSLayerNode) getRoot());
325
        ((WMSLayerNode)getRoot()).getChildren().add(myNode);
326
        return true;
327
    }
328

    
329

    
330
    public Object getRoot() {
331
        if (root == null) {
332
            root = new WMSLayerNode();
333
            root.setParent(null);
334
        }
335
        return root;
336
    }
337
    
338
    /**
339
     * @param node, the tree's desired node.
340
     * @return Returns the title.
341
     */
342
    public String getTitle(Object node) {
343
        if (node instanceof WMSLayerNode)
344
            return ((WMSLayerNode) node).getTitle();
345
        return ((FMapWMSStyle) node).toString();
346
    }
347
    
348
    public String toString(){
349
        return getTitle(this);
350
    }
351
}