Statistics
| Revision:

root / trunk / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / StyleTree.java @ 37961

History | View | Annotate | Download (13.5 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 37961 2012-02-15 16:56:30Z fpenarrubia $
45
* $Log$
46
* Revision 1.5  2006-02-28 15:25:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2.2.15  2006/02/20 15:23:08  jaume
50
* Se muestran algunas de las capas de aes
51
*
52
* Revision 1.2.2.14  2006/02/20 08:48:36  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.2.2.13  2006/02/17 13:06:12  jaume
56
* y las imagenes que faltan
57
*
58
* Revision 1.2.2.12  2006/02/17 12:57:34  jaume
59
* oculta/eXconde los nombres de las capas y adem�s corrige el error de selecci�n de varios styles si hay alguna capa seleccionada repetida
60
*
61
* Revision 1.2.2.11  2006/02/14 16:34:40  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.2.2.10  2006/02/14 11:08:24  jaume
65
* ahora refresca correctamente styles si arrastras y sueltas incluso desde un proyecto guardado
66
*
67
* Revision 1.2.2.9  2006/02/14 09:33:38  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.2.2.8  2006/02/07 07:53:22  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.2.2.7  2006/02/06 18:03:47  jaume
74
* fixed style edition, now supports drag'n'drop on the layer list refreshing
75
*
76
* Revision 1.2.2.6  2006/02/06 15:19:50  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.2.2.5  2006/02/02 10:44:18  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2.2.4  2006/02/01 16:25:23  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.2.2.3  2006/01/31 16:25:24  jaume
86
* correcciones de bugs
87
*
88
* Revision 1.4  2006/01/31 10:40:31  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.3  2006/01/26 16:07:14  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.2.2.1  2006/01/26 12:59:32  jaume
95
* 0.5
96
*
97
* Revision 1.2  2006/01/24 14:36:33  jaume
98
* This is the new version
99
*
100
* Revision 1.1.2.11  2006/01/23 12:54:45  jaume
101
* *** empty log message ***
102
*
103
* Revision 1.1.2.10  2006/01/20 08:50:52  jaume
104
* handles time dimension for the NASA Jet Propulsion Laboratory WMS
105
*
106
* Revision 1.1.2.9  2006/01/17 12:55:40  jaume
107
* *** empty log message ***
108
*
109
* Revision 1.1.2.6  2006/01/11 12:20:30  jaume
110
* *** empty log message ***
111
*
112
* Revision 1.1.2.5  2006/01/10 11:33:31  jaume
113
* Time dimension working against Jet Propulsion Laboratory's WMS server
114
*
115
* Revision 1.1.2.4  2006/01/09 18:10:38  jaume
116
* casi con el time dimension
117
*
118
* Revision 1.1.2.3  2006/01/05 23:15:53  jaume
119
* *** empty log message ***
120
*
121
* Revision 1.1.2.2  2006/01/04 18:09:02  jaume
122
* Time dimension
123
*
124
* Revision 1.1.2.1  2006/01/03 18:08:40  jaume
125
* *** empty log message ***
126
*
127
* Revision 1.1.2.1  2006/01/02 18:08:01  jaume
128
* Tree de estilos
129
*
130
*
131
*/
132
/**
133
 * 
134
 */
135
package com.iver.cit.gvsig.gui.panels;
136

    
137
import java.awt.Color;
138
import java.awt.Component;
139
import java.util.ArrayList;
140
import java.util.Vector;
141

    
142
import javax.swing.JLabel;
143
import javax.swing.JPanel;
144
import javax.swing.JRadioButton;
145
import javax.swing.JTree;
146
import javax.swing.event.TreeModelListener;
147
import javax.swing.tree.DefaultTreeCellRenderer;
148
import javax.swing.tree.TreeModel;
149
import javax.swing.tree.TreePath;
150

    
151
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
152
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
153

    
154
/**
155
 * This class holds a visual tree that handles the selection of the styles in the
156
 * selected layers. It encapsulates any gui interface handling the mouse selection.
157
 * <p>
158
 * It has a method getStylesSelection that returns the current selection in a 
159
 * bi-dimensional string array containing the layer name and the selected style name.
160
 * </p>
161
 * <p>
162
 * Keep in mind that although it's similar to LayersTree it is not the same.
163
 * It does not use the same instances of the WMSLayerNode that LayerTreeModel
164
 * because it allows to repeat layers. So, each layer is a completely new 
165
 * WMSLayerNode containing same properties but not the same parent (which is always
166
 * the tree root) and each style is a completely new FMapStyle containing same
167
 * properties but its parent is one of the layers of the StylesTree and not one
168
 * of those of the Layers tree.
169
 * </p>
170
 * 
171
 * @author jaume dominguez faus
172
 *
173
 */
174
public class StyleTree extends JTree {
175
        public boolean showLayerNames = false;
176
    public StyleTree(){
177
        super();
178
        initialize();
179
    }
180
    
181
    private void initialize(){
182
        setToggleClickCount(1);
183
        setRowHeight(22);
184
        
185
        setCellRenderer(new DefaultTreeCellRenderer() {
186
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
187
                if (leaf) {
188
                    JPanel leafComponent = new JPanel();
189
                    leafComponent.setBackground(Color.WHITE);
190
                    JRadioButton leafRadioButton = new JRadioButton("", ((StyleTreeModel) getModel()).isSelected((FMapWMSStyle) value));//selected);
191
                    leafRadioButton.setBackground(Color.WHITE);
192
                    leafComponent.add(leafRadioButton);
193
                    leafComponent.add(new JLabel(((FMapWMSStyle) value).title));
194
                    return leafComponent;
195
                } else {
196
                        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
197
                        if (value instanceof WMSLayerNode) {
198
                                WMSLayerNode layer = (WMSLayerNode) value;
199
                                if (!showLayerNames) {
200
                                        if (layer.getName() != null || layer.getName() == "") {
201
                                                String text = layer.toString();
202
                                                text = text.substring(text.indexOf(']')+2, text.length());
203
                                                setText(text);
204
                                        }
205
                                }
206
                        }
207
                            return this;
208
                }
209
            }});
210
        addMouseListener(new java.awt.event.MouseAdapter() { 
211
            public void mouseClicked(java.awt.event.MouseEvent e) {
212
                ((StyleTreeModel)getModel()).setSelectedLeaf(getSelectionPath());
213
                clearSelection();
214
                repaint();
215
            }
216
        });
217
    }
218
    
219
    /**
220
     * Expands this tree.
221
     *
222
     */
223
    public void expandAll(){
224
        int row = 0; 
225
        while (row < getRowCount()) {
226
          expandRow(row);
227
          row++;
228
          }
229
    }
230
    /**
231
     * Returns a Vector of Strings containing the style titles.
232
     */
233
    public Vector getStyleSelectionTitles(){
234
        if (getModel() instanceof StyleTreeModel)
235
            return ((StyleTreeModel)getModel()).getStyleSelectionTitles();
236
        else
237
            return null;
238
    }
239
    
240
    /**
241
     * Sets the selected styles in the StylesTree. The argument styleNames is
242
     * a Vector with exactly the same amount of strings than the amount of
243
     * themes (layers) contained by the tree. A blank or null string will
244
     * leave the default style for that layer, but this element <b>must exist</b>
245
     * in the array in any case.
246
     * 
247
     * @param styleNames, Vector containing the style names. 
248
     * The styles order <b>must match</b> with the layer order. 
249
     */
250
    public void initSelections(Vector styleNames){
251
            if (styleNames!=null) {
252
                    StyleTreeModel model = (StyleTreeModel) getModel();
253
                    model.setStylesSelection(styleNames);
254
            }
255
    }
256
        
257
}
258

    
259
class StyleTreeModel implements TreeModel {
260
        private WMSLayerNode root;
261
        private ArrayList layers = new ArrayList();
262

    
263
        public StyleTreeModel(WMSLayerNode root){
264
        this.root = root;
265
        
266
    }
267
        
268
        /**
269
         * Will return true if this style is selected.
270
         * @param style
271
         * @return
272
         */
273
        public boolean isSelected(FMapWMSStyle style) {
274
                if (style.parent.getSelectedStyle() != null)
275
                        return style.parent.getSelectedStyle().equals(style);
276
                return false;
277
        }
278

    
279
        /**
280
         * Gets the names of the selected styles into a Vector using the same order
281
         * as they was represented in the StylesTree.
282
         * @return
283
         */
284
        public Vector getStyleSelectionTitles() {
285
                Vector v = new Vector();
286
                for (int i = 0; i < layers.size(); i++) {
287
                        WMSLayerNode layer = (WMSLayerNode) layers.get(i);
288
                        FMapWMSStyle sty = layer.getSelectedStyle();
289
                        if (sty==null) v.add("");
290
                        else v.add(sty.title);
291
                }
292
                return v;
293
        }
294

    
295
        /**
296
         * Sets the selected styles in the StylesTree. The argument styleNames is
297
         * a Vector with exactly the same amount of strings than the amount of
298
         * themes (layers) contained by the tree. A blank or null string will
299
         * leave the default style for that layer, but this element <b>must exist</b>
300
         * in the array in any case.
301
         * 
302
         * @param styleNames, Vector containing the style names. 
303
         * The styles order <b>must match</b> with the layer order. 
304
         */
305
        public void setStylesSelection(Vector styleNames) {
306
                for (int i = 0; i < layers.size(); i++) {
307
                        WMSLayerNode layer = (WMSLayerNode) layers.get(i);
308
                        for (int j = 0; j < layer.getStyles().size(); j++) {
309
                                FMapWMSStyle sty = (FMapWMSStyle) layer.getStyles().get(j);
310
                                if ( sty.name.equals((String) styleNames.get(i))) {
311
                                        layer.setSelectedStyleByIndex(j);
312
        }}}}
313
        
314
        /**
315
         * Adds a new branch to this tree. It must be a layer node.
316
         * @param node
317
         * @return <b>True</b>, if the added branch actually defines any style.
318
         *         <b>False</b>, if the layer has no styles.
319
         */
320
        public boolean addLayerBranch(WMSLayerNode node) {
321
                layers.add(node);
322
                if (node.getStyles()==null || node.getStyles().size()==0){
323
                        return false;
324
                }
325
                for (int i = 0; i < node.getStyles().size(); i++) {
326
                        ((FMapWMSStyle) node.getStyles().get(i)).parent = node;
327
                }
328
                ((WMSLayerNode)getRoot()).getChildren().add(node);
329
                return true;
330
        }
331
        
332
        /**
333
     * Sets a leaf (an style) selected.
334
     * @param selectionPath to this leaf.
335
     */
336
    protected void setSelectedLeaf(TreePath selectionPath) {
337
        if (selectionPath!=null){
338
            Object[] objects = selectionPath.getPath();
339
            Object item = objects[objects.length-1];
340
            if (isLeaf(item)){
341
                    FMapWMSStyle style = (FMapWMSStyle) item;
342
                    WMSLayerNode layer = style.parent ;
343
                    for (int i = 0; i < layer.getStyles().size(); i++) {
344
                            FMapWMSStyle sty = (FMapWMSStyle) layer.getStyles().get(i);
345
                                        if (sty.name.equals(style.name)) {
346
                                                layer.setSelectedStyleByIndex(i);
347
                                                return;
348
        }}}}}
349

    
350

    
351
        /*
352
         *  (non-Javadoc)
353
         * @see javax.swing.tree.TreeModel#getRoot()
354
         */
355
        public Object getRoot() {
356
                if (root == null) {
357
            root = new WMSLayerNode();
358
            root.setParent(null);
359
        }
360
        return root;
361
        }
362

    
363
        /*
364
         *  (non-Javadoc)
365
         * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
366
         */
367
        public int getChildCount(Object parent) {
368
        int count=0;
369
        
370
        if (parent == root)
371
            count = ((WMSLayerNode) parent).getChildren().size();
372
        else
373
            count = ((WMSLayerNode) parent).getStyles().size();
374
        return count;
375
    }
376

    
377
        /*
378
         *  (non-Javadoc)
379
         * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
380
         */
381
        public boolean isLeaf(Object node) {
382
                return (node instanceof FMapWMSStyle);
383
        }
384

    
385
        /*
386
         *  (non-Javadoc)
387
         * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
388
         */
389
        public void addTreeModelListener(TreeModelListener l) {
390
        }
391

    
392
        /*
393
         *  (non-Javadoc)
394
         * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
395
         */
396
        public void removeTreeModelListener(TreeModelListener l) {
397
        }
398

    
399
        /*
400
         *  (non-Javadoc)
401
         * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
402
         */
403
        public Object getChild(Object parent, int index) {
404
                if (parent instanceof FMapWMSStyle)
405
            return null;
406
        if (parent == root)
407
            return ((WMSLayerNode) parent).getChildren().get(index);
408
        
409
        return ((WMSLayerNode) parent).getStyles().get(index);
410
        }
411

    
412
        /*
413
         *  (non-Javadoc)
414
         * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
415
         */
416
        public int getIndexOfChild(Object parent, Object child) {
417
                if (parent instanceof FMapWMSStyle){ 
418
            return -1;   
419
        }
420
         
421
        WMSLayerNode l = (WMSLayerNode)parent;
422
        if (l.getParent()==null){
423
            for (int i = 0; i < l.getChildren().size(); i++) {
424
                if (l.getChildren().get(i).equals(child)){
425
                   return i;
426
                }
427
            }
428
        } else {
429
            for (int i = 0; i < l.getStyles().size(); i++) {
430
                if (l.getChildren().get(i).equals(child)){
431
                    return i;
432
                }
433
            }
434
        }
435
        return -1;
436
        }
437

    
438
        /*
439
         *  (non-Javadoc)
440
         * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
441
         */
442
        public void valueForPathChanged(TreePath path, Object newValue) {
443
        }
444
}