Statistics
| Revision:

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

History | View | Annotate | Download (11.1 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: DimensionTree.java 3655 2006-01-17 12:59:53Z jaume $
45
* $Log$
46
* Revision 1.1.2.9  2006-01-17 12:55:40  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.7  2006/01/11 12:20:30  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.6  2006/01/10 13:11:38  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.5  2006/01/10 11:33:31  jaume
56
* Time dimension working against Jet Propulsion Laboratory's WMS server
57
*
58
* Revision 1.1.2.4  2006/01/09 18:10:38  jaume
59
* casi con el time dimension
60
*
61
* Revision 1.1.2.3  2006/01/05 23:15:53  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1.2.2  2006/01/04 18:09:02  jaume
65
* Time dimension
66
*
67
* Revision 1.1.2.1  2006/01/03 18:08:40  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
/**
73
 * 
74
 */
75
package com.iver.cit.gvsig.gui.panels;
76
import java.awt.Color;
77
import java.awt.Component;
78
import java.util.Hashtable;
79
import java.util.Iterator;
80
import java.util.Vector;
81

    
82
import javax.swing.JButton;
83
import javax.swing.JLabel;
84
import javax.swing.JPanel;
85
import javax.swing.JTree;
86
import javax.swing.event.TreeModelListener;
87
import javax.swing.tree.TreeCellRenderer;
88
import javax.swing.tree.TreeModel;
89
import javax.swing.tree.TreePath;
90

    
91
import com.iver.andami.PluginServices;
92
import com.iver.cit.gvsig.fmap.layers.IFMapWMSDimension;
93
import com.iver.cit.gvsig.fmap.layers.TimeDimension;
94
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
95
import com.iver.cit.gvsig.gui.beans.listeners.BeanListener;
96
/**
97
 * @author jaume
98
 *
99
 */
100
public class DimensionTree extends JTree {
101
        private int rowHeight = 28;
102
    private EditionPanel ep = null;
103
    private IFMapWMSDimension current = null;
104
    
105
    public DimensionTree(){
106
        super();
107
        initialize();
108
    }
109
    
110
    private void initialize(){
111
        setToggleClickCount(1);
112
        setRowHeight(rowHeight);
113
        setCellRenderer(new DimensionTreeCellRenderer());
114
        ep = new EditionPanel();
115
        ep.addListener(new BeanListener(){
116

    
117
            public void beanValueChanged(Object value) {
118
                Object[] obj = (Object[]) value;
119
                Integer min = (Integer) obj[0];
120
                Integer max = (Integer) obj[1];
121
                String expression = (String) obj[2];
122
                
123
                DimensionTreeModel model = (DimensionTreeModel) getModel();
124
                model.setMin(current, min);
125
                model.setMax(current, max);
126
                model.setExpr(current, expression);
127
            }
128
            
129
        });
130
        
131
        addMouseListener(new java.awt.event.MouseAdapter() { 
132
            public void mouseClicked(java.awt.event.MouseEvent e) {
133
                TreePath tp = getSelectionPath();
134
                if (tp == null)
135
                    return;
136
                Object[] objects = tp.getPath();
137
                if (!(objects[objects.length-1] instanceof IFMapWMSDimension))
138
                    return;
139
                current = (IFMapWMSDimension) objects[objects.length-1];
140
                
141
                // Build the edition panel.
142
                DimensionTreeModel model = (DimensionTreeModel) getModel();
143
                ep.setProperties(current, model.getMin(current), model.getMax(current));
144
                ep.addListener(new BeanListener(){
145

    
146
                    public void beanValueChanged(Object value) {
147
                        DimensionTreeModel model = (DimensionTreeModel) getModel();
148
                        Object[] obj = (Object[]) value;
149
                        model.setMin(current, (Integer) obj[0]);
150
                        model.setMax(current, (Integer) obj[1]);
151
                        model.setExpr(current, (String) obj[2]);
152
                    }
153
                    
154
                });
155
                PluginServices.getMDIManager().addView(ep);
156
            }
157
        });
158
    }
159
    
160
    private class DimensionTreeCellRenderer implements TreeCellRenderer {
161
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
162
                    if (!leaf){
163
                            JTree t = new JTree();
164
                            Component branchComponent = t.getCellRenderer().getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 
165
                            return branchComponent;
166
                    } else {
167
                            JPanel leafComponent = new JPanel();
168
                            leafComponent.setBackground(Color.WHITE);
169
                            JButton btn = new JButton();
170
                            btn.setBounds(0, 0, 40, rowHeight);
171
                            btn.setText(PluginServices.getText(this, "edit"));
172
                            leafComponent.add(btn);
173
                            JLabel lbl =new JLabel(((TimeDimension) value).getName());
174
                            leafComponent.add(lbl);
175
                            return leafComponent;
176
                    }
177
                    
178
            }
179
    }
180

    
181
    /**
182
     * @return
183
     */
184
    public Vector getDimensions() {
185
        if (getModel() instanceof DimensionTreeModel)
186
            return ((DimensionTreeModel) getModel()).getDimension();
187
        else
188
            return new Vector();
189
    }
190

    
191
    /**
192
     * @param vector
193
     */
194
    public void setSelections(Vector dimensions) {
195
        DimensionTreeModel model = ((DimensionTreeModel) getModel());
196
        for (int i = 0; i < dimensions.size(); i++) {
197
            String[] dim = ((String) dimensions.get(i)).split("=");
198
            for (int j = 0; j < dim.length; j++) {
199
                
200
            }
201
        }
202
    }
203
    
204
}
205

    
206
class DimensionTreeModel implements TreeModel {
207
    Hashtable lowLimits   = new Hashtable();
208
    Hashtable highLimits  = new Hashtable();
209
    Hashtable expressions = new Hashtable();
210

    
211
        WMSLayerNode root;
212

    
213
        public DimensionTreeModel(WMSLayerNode root){
214
        this.root = root;
215
    }
216
    /**
217
     * @param currentDimensionNode
218
     * @param min
219
     */
220
    protected void setMin(IFMapWMSDimension currentDimensionNode, Integer val) {
221
        lowLimits.put(currentDimensionNode, val);
222
    }
223
    /**
224
     * @param currentDimensionNode
225
     * @param min
226
     */
227
    protected void setMax(IFMapWMSDimension currentDimensionNode, Integer val) {
228
        highLimits.put(currentDimensionNode, val);
229
    }
230
    /**
231
     * @param currentDimensionNode
232
     * @param min
233
     */
234
    protected void setExpr(IFMapWMSDimension currentDimensionNode, String val) {
235
        expressions.put(currentDimensionNode, val);
236
    }
237
    
238
    /*
239
     *  (non-Javadoc)
240
     * @see javax.swing.tree.TreeModel#getRoot()
241
     */
242
    public Object getRoot() {
243
        return root;
244
    }
245

    
246
    /*
247
     *  (non-Javadoc)
248
     * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
249
     */
250
    public int getChildCount(Object parent) {
251
        int count=0;
252
        
253
        if (parent == root)
254
            count = ((WMSLayerNode) parent).getChildren().size();
255
        else
256
            count = ((WMSLayerNode) parent).getDimensions().size();
257
        return count;
258
    }
259
    /*
260
     *  (non-Javadoc)
261
     * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
262
     */
263
    public boolean isLeaf(Object node) {
264
        return (node instanceof IFMapWMSDimension);
265
    }
266
    
267
    /*
268
     *  (non-Javadoc)
269
     * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
270
     */
271
    public void addTreeModelListener(TreeModelListener l) {
272
    }
273
    
274
    /*
275
     *  (non-Javadoc)
276
     * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
277
     */
278
    public void removeTreeModelListener(TreeModelListener l) {
279
    }
280

    
281
    /*
282
     *  (non-Javadoc)
283
     * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
284
     */
285
    public Object getChild(Object parent, int index) {
286
        if (parent instanceof IFMapWMSDimension)
287
            return null;
288
        if (parent == root)
289
            return ((WMSLayerNode) parent).getChildren().get(index);
290
        
291
        return ((WMSLayerNode) parent).getDimensions().get(index);
292
    }
293

    
294
    /*
295
     *  (non-Javadoc)
296
     * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
297
     */
298
    public int getIndexOfChild(Object parent, Object child) {
299
        if (parent instanceof IFMapWMSDimension){ 
300
            return -1;   
301
        }
302
         
303
        WMSLayerNode l = (WMSLayerNode)parent;
304
        if (l.getParent()==null){
305
            for (int i = 0; i < l.getChildren().size(); i++) {
306
                if (l.getChildren().get(i).equals(child)){
307
                   return i;
308
                }
309
            }
310
        } else {
311
            for (int i = 0; i < l.getDimensions().size(); i++) {
312
                if (l.getChildren().get(i).equals(child)){
313
                    return i;
314
                }
315
            }
316
        }
317
        return -1;
318
    }
319

    
320
    /*
321
     *  (non-Javadoc)
322
     * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
323
     */
324
    public void valueForPathChanged(TreePath path, Object newValue) {
325
        
326
    }  
327
    
328
    /**
329
     * Adds a new branch to this tree. It must be a layer node.
330
     * @param node
331
     */
332
    public boolean addLayerBranch(WMSLayerNode node){
333
        if (node.getDimensions() == null || node.getDimensions().size()==0)
334
            return false;
335
        
336
        WMSLayerNode myNode = (WMSLayerNode) node.clone();
337
        myNode.setParent((WMSLayerNode) getRoot());
338
        ((WMSLayerNode)getRoot()).getChildren().add(myNode);
339
        return true;
340
    }
341
    
342
    /**
343
     * Gets the low edge of the Dimension 
344
     * @param dim
345
     * @return
346
     */
347
    protected Integer getMin(IFMapWMSDimension dim){
348
        Integer min = (Integer) lowLimits.get(dim);
349
        if (min == null)
350
            min = new Integer(1);
351
        return min;
352
    }
353
    
354
    protected Integer getMax(IFMapWMSDimension dim){
355
        Integer max = (Integer) highLimits.get(dim);
356
        if (max == null)
357
            max = new Integer(dim.valueCount());
358
        return max;
359
    }
360
    
361
    public Vector getDimension(){
362
        Vector v = new Vector();
363
        Iterator it = expressions.keySet().iterator();
364
        while (it.hasNext()){
365
            IFMapWMSDimension key = (IFMapWMSDimension) it.next();
366
            String myDimensionString = key.getName()+"="+expressions.get(key);
367
            if (!v.contains(myDimensionString))
368
                v.add(myDimensionString);
369
        }
370
        return v;
371
    }
372
}
373