Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_WMSv2 / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / StyleTree.java @ 3649

History | View | Annotate | Download (11.7 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 3649 2006-01-16 09:57:07Z jaume $
45
* $Log$
46
* Revision 1.1.2.8  2006-01-16 09:57:07  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.6  2006/01/11 12:20:30  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.5  2006/01/10 11:33:31  jaume
53
* Time dimension working against Jet Propulsion Laboratory's WMS server
54
*
55
* Revision 1.1.2.4  2006/01/09 18:10:38  jaume
56
* casi con el time dimension
57
*
58
* Revision 1.1.2.3  2006/01/05 23:15:53  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.1.2.2  2006/01/04 18:09:02  jaume
62
* Time dimension
63
*
64
* Revision 1.1.2.1  2006/01/03 18:08:40  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2006/01/02 18:08:01  jaume
68
* Tree de estilos
69
*
70
*
71
*/
72
/**
73
 * 
74
 */
75
package com.iver.cit.gvsig.gui.panels;
76

    
77
import java.awt.Color;
78
import java.awt.Component;
79
import java.util.Hashtable;
80
import java.util.Iterator;
81
import java.util.Vector;
82

    
83
import javax.swing.JLabel;
84
import javax.swing.JPanel;
85
import javax.swing.JRadioButton;
86
import javax.swing.JTree;
87
import javax.swing.event.TreeModelListener;
88
import javax.swing.event.TreeSelectionEvent;
89
import javax.swing.tree.TreeCellRenderer;
90
import javax.swing.tree.TreeModel;
91
import javax.swing.tree.TreePath;
92

    
93
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
94
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
95

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

    
194

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

    
216
    /**
217
     * Gets the names of the selected styles into a Vector using the same order
218
     * as they was represented in the StylesTree.
219
     * @return
220
     */
221
    protected Vector getStylesSelection() {
222
        Vector values = new Vector();
223
        for (int i = 0; i < ((WMSLayerNode)getRoot()).getChildren().size(); i++) {
224
            values.add(i, "");
225
        }
226
        
227
        // TODO em pareix que a?? no est? b? (que no reestablix l'ordre), comprovar.
228
        Iterator it = selections.keySet().iterator();
229
        int i = 0;
230
        while (it.hasNext()){
231
            Object key = it.next();
232
            values.set(i, ((FMapWMSStyle)selections.get(key)).name);
233
            i++;
234
        }
235
        return values;
236
    }
237
    
238
    /**
239
     * Gets the names of the selected styles into a Vector using the same order
240
     * as they was represented in the StylesTree.
241
     * @return
242
     */
243
    protected Vector getStyleSelectionTitles() {
244
        Vector values = new Vector();
245
        for (int i = 0; i < ((WMSLayerNode)getRoot()).getChildren().size(); i++) {
246
            values.add(i, "");
247
        }
248
        
249
        Iterator it = selections.keySet().iterator();
250
        int i = 0;
251
        while (it.hasNext()){
252
            Object key = it.next();
253
            values.set(i, ((FMapWMSStyle)selections.get(key)).title);
254
            i++;
255
        }
256
        return values;
257
    }
258

    
259
    /**
260
     * Sets a leaf (an style) selected.
261
     * @param selectionPath to this leaf.
262
     */
263
    protected void setSelectedLeaf(TreePath selectionPath) {
264
        if (selectionPath!=null){
265
            Object[] objects = selectionPath.getPath();
266
            Object item = objects[objects.length-1];
267
            if (isLeaf(item)){
268
                selections.put(((FMapWMSStyle) item).parent, item);
269
            }
270
        }
271
    }
272
    
273
    /**
274
     * Will return true if this style is selected.
275
     * @param style
276
     * @return
277
     */
278
    protected boolean isSelected(FMapWMSStyle style){
279
        return selections.get(style.parent) == style;
280
    }
281
    
282
    public int getChildCount(Object parent) {
283
        int count=0;
284
        
285
        if (parent == root)
286
            count = ((WMSLayerNode) parent).getChildren().size();
287
        else
288
            count = ((WMSLayerNode) parent).getStyles().size();
289
        return count;
290
    }
291

    
292
    public boolean isLeaf(Object node) {
293
        return (node instanceof FMapWMSStyle);
294
        
295
    }
296

    
297
    public void addTreeModelListener(TreeModelListener l) {
298
    }
299

    
300
    public void removeTreeModelListener(TreeModelListener l) {
301
    }
302

    
303
    public Object getChild(Object parent, int index) {
304
        if (parent instanceof FMapWMSStyle)
305
            return null;
306
        if (parent == root)
307
            return ((WMSLayerNode) parent).getChildren().get(index);
308
        
309
        return ((WMSLayerNode) parent).getStyles().get(index);
310
    }
311

    
312
    public int getIndexOfChild(Object parent, Object child) {
313
        if (parent instanceof FMapWMSStyle){ 
314
            return -1;   
315
        }
316
         
317
        WMSLayerNode l = (WMSLayerNode)parent;
318
        if (l.getParent()==null){
319
            for (int i = 0; i < l.getChildren().size(); i++) {
320
                if (l.getChildren().get(i).equals(child)){
321
                   return i;
322
                }
323
            }
324
        } else {
325
            for (int i = 0; i < l.getStyles().size(); i++) {
326
                if (l.getChildren().get(i).equals(child)){
327
                    return i;
328
                }
329
            }
330
        }
331
        return -1;
332
    }
333

    
334
    public void valueForPathChanged(TreePath path, Object newValue) {
335
    }
336

    
337
    /**
338
     * Adds a new branch to this tree. It must be a layer node.
339
     * @param node
340
     * @return <b>True</b>, if the added branch actually defines any style.
341
     *         <b>False</b>, if the layer has no styles.
342
     */
343
    public boolean addLayerBranch(WMSLayerNode node){
344
        if (node.getStyles()==null || node.getStyles().size()==0){
345
            return false;
346
        }
347
        WMSLayerNode myNode = (WMSLayerNode) node.clone();
348
        myNode.setParent((WMSLayerNode) getRoot());
349
        ((WMSLayerNode)getRoot()).getChildren().add(myNode);
350
        return true;
351
    }
352

    
353

    
354
    public Object getRoot() {
355
        if (root == null) {
356
            root = new WMSLayerNode();
357
            root.setParent(null);
358
        }
359
        return root;
360
    }
361
    
362
    /**
363
     * @param node, the tree's desired node.
364
     * @return Returns the title.
365
     */
366
    public String getTitle(Object node) {
367
        if (node instanceof WMSLayerNode)
368
            return ((WMSLayerNode) node).getTitle();
369
        return ((FMapWMSStyle) node).toString();
370
    }
371
    
372
    public String toString(){
373
        return getTitle(this);
374
    }
375
}