Statistics
| Revision:

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

History | View | Annotate | Download (10.4 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 3537 2006-01-05 23:15:59Z jaume $
45
* $Log$
46
* Revision 1.1.2.3  2006-01-05 23:15:53  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.2  2006/01/04 18:09:02  jaume
50
* Time dimension
51
*
52
* Revision 1.1.2.1  2006/01/03 18:08:40  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.1  2006/01/02 18:08:01  jaume
56
* Tree de estilos
57
*
58
*
59
*/
60
/**
61
 * 
62
 */
63
package com.iver.cit.gvsig.gui.panels;
64

    
65
import java.awt.Color;
66
import java.awt.Component;
67
import java.util.ArrayList;
68
import java.util.Hashtable;
69
import java.util.Iterator;
70
import java.util.Vector;
71

    
72
import javax.swing.JLabel;
73
import javax.swing.JPanel;
74
import javax.swing.JRadioButton;
75
import javax.swing.JTree;
76
import javax.swing.event.TreeModelListener;
77
import javax.swing.tree.TreeCellRenderer;
78
import javax.swing.tree.TreeModel;
79
import javax.swing.tree.TreePath;
80

    
81
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
82
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode.FMapWMSStyle;
83

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

    
182

    
183
class StylesTreeModel implements TreeModel {
184
    WMSLayerNode root;
185
    Hashtable selections = new Hashtable();
186
    
187
    public StylesTreeModel(WMSLayerNode root){
188
        this.root = root;
189
        
190
    }
191
    
192
    /**
193
     * Marks this style as selected in the StylesTreeModel.
194
     * @param style
195
     */
196
    public void selectStyle(Object style) {
197
        selections.put(((FMapWMSStyle)style).parent, style);
198
    }
199

    
200
    /**
201
     * Gets the names of the selected styles into a Vector using the same order
202
     * as they was represented in the StylesTree.
203
     * @return
204
     */
205
    protected Vector getStylesSelection() {
206
        Vector values = new Vector();
207
        for (int i = 0; i < ((WMSLayerNode)getRoot()).getChildren().size(); i++) {
208
            values.add(i, "");
209
        }
210
        Iterator it = selections.keySet().iterator();
211
        int i = 0;
212
        while (it.hasNext()){
213
            Object key = it.next();
214
            values.set(i, ((FMapWMSStyle)selections.get(key)).name);
215
            i++;
216
        }
217
        return values;
218
    }
219

    
220
    /**
221
     * Sets a leaf (an style) selected.
222
     * @param selectionPath to this leaf.
223
     */
224
    protected void setSelectedLeaf(TreePath selectionPath) {
225
        if (selectionPath!=null){
226
            Object[] objects = selectionPath.getPath();
227
            Object item = objects[objects.length-1];
228
            if (isLeaf(item)){
229
                selections.put(((FMapWMSStyle) item).parent, item);
230
            }
231
        }
232
    }
233
    
234
    /**
235
     * Will return true if this style is selected.
236
     * @param style
237
     * @return
238
     */
239
    protected boolean isSelected(FMapWMSStyle style){
240
        return selections.get(style.parent) == style;
241
    }
242
    
243
    public int getChildCount(Object parent) {
244
        int count=0;
245
        
246
        if (parent == root)
247
            count = ((WMSLayerNode) parent).getChildren().size();
248
        else
249
            count = ((WMSLayerNode) parent).getStyles().size();
250
        return count;
251
    }
252

    
253
    public boolean isLeaf(Object node) {
254
        return (node instanceof FMapWMSStyle);
255
        
256
    }
257

    
258
    public void addTreeModelListener(TreeModelListener l) {
259
    }
260

    
261
    public void removeTreeModelListener(TreeModelListener l) {
262
    }
263

    
264
    public Object getChild(Object parent, int index) {
265
        if (parent instanceof FMapWMSStyle)
266
            return null;
267
        if (parent == root)
268
            return ((WMSLayerNode) parent).getChildren().get(index);
269
        
270
        return ((WMSLayerNode) parent).getStyles().get(index);
271
    }
272

    
273
    public int getIndexOfChild(Object parent, Object child) {
274
        if (parent instanceof FMapWMSStyle){ 
275
            return -1;   
276
        }
277
         
278
        WMSLayerNode l = (WMSLayerNode)parent;
279
        if (l.getParent()==null){
280
            for (int i = 0; i < l.getChildren().size(); i++) {
281
                if (l.getChildren().get(i).equals(child)){
282
                   return i;
283
                }
284
            }
285
        } else {
286
            for (int i = 0; i < l.getStyles().size(); i++) {
287
                if (l.getChildren().get(i).equals(child)){
288
                    return i;
289
                }
290
            }
291
        }
292
        return -1;
293
    }
294

    
295
    public void valueForPathChanged(TreePath path, Object newValue) {
296
    }
297

    
298
    /**
299
     * Adds a new branch to this tree. It must be a layer node.
300
     * @param node
301
     */
302
    public void addLayerBranch(WMSLayerNode node){
303
        WMSLayerNode myNode = (WMSLayerNode) node.clone();
304
        myNode.setParent((WMSLayerNode) getRoot());
305
        ((WMSLayerNode)getRoot()).getChildren().add(myNode);
306
    }
307

    
308

    
309
    public Object getRoot() {
310
        if (root == null) {
311
            root = new WMSLayerNode();
312
            root.setParent(null);
313
        }
314
        return root;
315
    }
316
    
317
    /**
318
     * @param node, the tree's desired node.
319
     * @return Returns the title.
320
     */
321
    public String getTitle(Object node) {
322
        if (node instanceof WMSLayerNode)
323
            return ((WMSLayerNode) node).getTitle();
324
        return ((FMapWMSStyle) node).toString();
325
    }
326
    
327
    public String toString(){
328
        return getTitle(this);
329
    }
330
}