Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.swing / org.gvsig.raster.wmts.swing.impl / src / main / java / org / gvsig / raster / wmts / swing / impl / wizard / StyleTree.java @ 2609

History | View | Annotate | Download (10.6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
 
23
package org.gvsig.raster.wmts.swing.impl.wizard;
24

    
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.util.ArrayList;
28
import java.util.List;
29
import java.util.Vector;
30

    
31
import javax.swing.JLabel;
32
import javax.swing.JPanel;
33
import javax.swing.JRadioButton;
34
import javax.swing.JTree;
35
import javax.swing.event.TreeModelListener;
36
import javax.swing.tree.DefaultTreeCellRenderer;
37
import javax.swing.tree.TreeModel;
38
import javax.swing.tree.TreePath;
39

    
40
import org.gvsig.raster.wmts.ogc.struct.WMTSStyle;
41
import org.gvsig.raster.wmts.ogc.struct.WMTSTheme;
42
import org.gvsig.raster.wmts.swing.impl.wizard.WMTSParamsPanel.LayerUI;
43

    
44

    
45
/**
46
 * This class holds a visual tree that handles the selection of the styles in the
47
 * selected layers. It encapsulates any gui interface handling the mouse selection.
48
 * <p>
49
 * It has a method getStylesSelection that returns the current selection in a 
50
 * bi-dimensional string array containing the layer name and the selected style name.
51
 * </p>
52
 * <p>
53
 * Keep in mind that although it's similar to LayersTree it is not the same.
54
 * It does not use the same instances of the WMTSLayerNode that LayerTreeModel
55
 * because it allows to repeat layers. So, each layer is a completely new 
56
 * WMTSLayerNode containing same properties but not the same parent (which is always
57
 * the tree root) and each style is a completely new FMapStyle containing same
58
 * properties but its parent is one of the layers of the StylesTree and not one
59
 * of those of the Layers tree.
60
 * </p>
61
 * 
62
 * @author jaume dominguez faus
63
 *
64
 */
65
@SuppressWarnings("serial")
66
public class StyleTree extends JTree {
67
        private static final long serialVersionUID  = 1L;
68
        public boolean            showLayerNames    = false;
69
        
70
    public StyleTree() {
71
        super();
72
        initialize();
73
    }
74
    
75
  
76
        private void initialize() {
77
        setToggleClickCount(1);
78
        setRowHeight(22);
79
        
80
        setCellRenderer(new DefaultTreeCellRenderer() {
81
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
82
                if (leaf) {
83
                    JPanel leafComponent = new JPanel();
84
                    leafComponent.setBackground(Color.WHITE);
85
                    JRadioButton leafRadioButton = new JRadioButton("", ((StyleTreeModel) getModel()).isSelected((WMTSStyle) value));//selected);
86
                    leafRadioButton.setBackground(Color.WHITE);
87
                    leafComponent.add(leafRadioButton);
88
                    leafComponent.add(new JLabel(((WMTSStyle) value).getIdentifier()));
89
                    return leafComponent;
90
                } else {
91
                        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
92
                        if (value instanceof WMTSTheme) {
93
                                WMTSTheme layer = (WMTSTheme) value;
94
                                if (!showLayerNames) {
95
                                        if (layer.getIdentifier() != null || layer.getIdentifier() == "") {
96
                                                String text = layer.toString();
97
                                                text = text.substring(text.indexOf(']') + 2, text.length());
98
                                                setText(text);
99
                                        }
100
                                }
101
                        }
102
                            return this;
103
                }
104
            }});
105
        addMouseListener(new java.awt.event.MouseAdapter() { 
106
            public void mouseClicked(java.awt.event.MouseEvent e) {
107
                ((StyleTreeModel)getModel()).setSelectedLeaf(getSelectionPath());
108
                clearSelection();
109
                repaint();
110
            }
111
        });
112
    }
113
    
114
        /**
115
         * Expands this tree.
116
         *
117
         */
118
        public void expandAll() {
119
                int row = 0; 
120
                while (row < getRowCount()) {
121
                        expandRow(row);
122
                        row++;
123
                }
124
        }
125
        
126
        /**
127
         * Returns a Vector of Strings containing the style titles.
128
         */
129
        public Vector getStyleSelectionTitles() {
130
                if (getModel() instanceof StyleTreeModel)
131
                        return ((StyleTreeModel)getModel()).getStyleSelectionTitles();
132
                else
133
                        return null;
134
        }
135
    
136
    /**
137
     * Sets the selected styles in the StylesTree. The argument styleNames is
138
     * a Vector with exactly the same amount of strings than the amount of
139
     * themes (layers) contained by the tree. A blank or null string will
140
     * leave the default style for that layer, but this element <b>must exist</b>
141
     * in the array in any case.
142
     * 
143
     * @param styleNames, Vector containing the style names. 
144
     * The styles order <b>must match</b> with the layer order. 
145
     */
146
        public void initSelections(Vector styleNames){
147
            if (styleNames!=null) {
148
                    StyleTreeModel model = (StyleTreeModel) getModel();
149
                    model.setStylesSelection(styleNames);
150
            }
151
    }
152
    
153
    /**
154
     * Gets the style selected
155
     * @param layerName
156
     * @return
157
     */
158
    public WMTSStyle getSelectedStyle(String layerName) {
159
            StyleTreeModel model = (StyleTreeModel) getModel();
160
            LayerUI layerUI = model.getLayer(layerName);
161
            if(layerUI != null)
162
                    return layerUI.styleSelected;
163
            return null;
164
    }
165
        
166
}
167

    
168
class StyleTreeModel implements TreeModel {
169
        private String             root   = null;
170
        private ArrayList<LayerUI> layers = new ArrayList<LayerUI>();
171

    
172
        public StyleTreeModel(String root) {
173
                this.root = root;
174
    }
175
        
176
        public LayerUI getLayer(String layerName) {
177
                for (int i = 0; i < layers.size(); i++) {
178
                        if(layers.get(i).theme.getLayer().getTitle().compareTo(layerName) == 0) {
179
                                return layers.get(i);
180
                        }
181
                }
182
                return null;
183
        }
184
        
185
        /**
186
         * Will return true if this style is selected.
187
         * @param style
188
         * @return
189
         */
190
        public boolean isSelected(WMTSStyle style) {
191
                for (int i = 0; i < layers.size(); i++) {
192
                        LayerUI layerUI = layers.get(i);
193
                        if(layerUI.styleSelected == style)
194
                                return true;
195
                }
196
                return false;
197
        }
198

    
199
        /**
200
         * Gets the names of the selected styles into a Vector using the same order
201
         * as they was represented in the StylesTree.
202
         * @return
203
         */
204
        public Vector<String> getStyleSelectionTitles() {
205
                Vector<String> v = new Vector<String>();
206
                for (int i = 0; i < layers.size(); i++) {
207
                        LayerUI layer = (LayerUI) layers.get(i);
208
                        WMTSStyle sty = layer.styleSelected;
209
                        if (sty == null) 
210
                                v.add("");
211
                        else 
212
                                v.add(sty.getIdentifier());
213
                }
214
                return v;
215
        }
216

    
217
        /**
218
         * Sets the selected styles in the StylesTree. The argument styleNames is
219
         * a Vector with exactly the same amount of strings than the amount of
220
         * themes (layers) contained by the tree. A blank or null string will
221
         * leave the default style for that layer, but this element <b>must exist</b>
222
         * in the array in any case.
223
         * 
224
         * @param styleNames, Vector containing the style names. 
225
         * The styles order <b>must match</b> with the layer order. 
226
         */
227
        public void setStylesSelection(Vector styleNames) {
228
                for (int i = 0; i < layers.size(); i++) {
229
                        LayerUI layer = (LayerUI) layers.get(i);
230
                        for (int j = 0; j < layer.theme.getLayer().getStyle().size(); j++) {
231
                                WMTSStyle sty = (WMTSStyle) layer.theme.getLayer().getStyle().get(j);
232
                                if ( sty.getIdentifier().compareTo((String) styleNames.get(i)) == 0) {
233
                                        layer.styleSelected = sty;
234
                                }
235
                        }
236
                }
237
        }
238
        
239
        /**
240
         * Adds a new branch to this tree. It must be a layer node.
241
         * @param node
242
         * @return <b>True</b>, if the added branch actually defines any style.
243
         *         <b>False</b>, if the layer has no styles.
244
         */
245
        public boolean addLayerBranch(LayerUI node) {
246
                layers.add(node);
247
                
248
                //Si no hay ninguno seleccionado seleccionamos el primero
249
                for (int i = 0; i < layers.size(); i++) {
250
                        if(layers.get(i).styleSelected == null) {
251
                                List<WMTSStyle> sty = layers.get(i).theme.getLayer().getStyle();
252
                                if(sty != null && sty.size() > 0 && sty.get(0) != null)
253
                                        layers.get(i).styleSelected = sty.get(0);
254
                        }
255
                }
256

    
257
                if (node.theme.getLayer().getStyle() == null || node.theme.getLayer().getStyle().size() == 0) {
258
                        return false;
259
                }
260
                return true;
261
        }
262
        
263
        /**
264
     * Sets a leaf (an style) selected.
265
     * @param selectionPath to this leaf.
266
     */
267
        protected void setSelectedLeaf(TreePath selectionPath) {
268
                if (selectionPath != null) {
269
                        Object[] objects = selectionPath.getPath();
270
                        Object item = objects[objects.length - 1];
271
                        if (isLeaf(item)) {
272
                                LayerUI layer = (LayerUI)objects[objects.length - 2]; 
273
                                WMTSStyle style = (WMTSStyle) item;
274
                                for (int j = 0; j < layer.theme.getLayer().getStyle().size(); j++) {
275
                                        WMTSStyle sty = (WMTSStyle)layer.theme.getLayer().getStyle().get(j);
276
                                        if(sty.getIdentifier().equals(style.getIdentifier())) {
277
                                                layer.styleSelected = sty;
278
                                        }
279
                                }
280
                        }
281
                }
282
        }
283

    
284
        public int getChildCount(Object parent) {
285
        if (parent instanceof LayerUI)
286
            return ((LayerUI) parent).theme.getLayer().getStyle().size();
287
        if (parent instanceof WMTSStyle)
288
                return 0;
289
        return layers.size();
290
    }
291

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

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

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

    
302
        public Object getChild(Object parent, int index) {
303
                if (parent instanceof WMTSStyle)
304
            return null;
305
        
306
        if (parent instanceof LayerUI)
307
                return ((LayerUI) parent).theme.getLayer().getStyle().get(index);
308
        
309
        return layers.get(index);
310
        }
311

    
312
        public int getIndexOfChild(Object parent, Object child) {
313
                if (parent instanceof LayerUI && child instanceof WMTSStyle) {
314
                        List<WMTSStyle> listStyle = ((LayerUI)parent).theme.getLayer().getStyle();
315
                        for (int i = 0; i < listStyle.size(); i++) {
316
                                WMTSStyle sty = listStyle.get(i);
317
                                if(sty.equals(child))
318
                                        return i;
319
                        }
320
                }
321
                
322
                if(child instanceof LayerUI) {
323
                        for (int i = 0; i < layers.size(); i++) {
324
                                LayerUI layerUI = layers.get(i);
325
                                if(layerUI.equals(child))
326
                                        return i;
327
                        }
328
                }
329
                
330
                return -1;
331
        }
332

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

    
336
        public Object getRoot() {
337
                return root;
338
        }
339
}