Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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 3592 2006-01-11 12:20:50Z jaume $
45
* $Log$
46
* Revision 1.1.2.7  2006-01-11 12:20:30  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.6  2006/01/10 13:11:38  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
*
68
*/
69
/**
70
 * 
71
 */
72
package com.iver.cit.gvsig.gui.panels;
73
import java.awt.Color;
74
import java.awt.Component;
75
import java.util.Hashtable;
76
import java.util.Iterator;
77
import java.util.Vector;
78

    
79
import javax.swing.JButton;
80
import javax.swing.JLabel;
81
import javax.swing.JPanel;
82
import javax.swing.JTree;
83
import javax.swing.event.TreeModelListener;
84
import javax.swing.tree.TreeCellRenderer;
85
import javax.swing.tree.TreeModel;
86
import javax.swing.tree.TreePath;
87

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

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

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

    
178
    /**
179
     * @return
180
     */
181
    public Vector getDimensions() {
182
        return ((DimensionTreeModel) getModel()).getDimension();
183
    }
184

    
185
    /**
186
     * @param vector
187
     */
188
    public void setSelections(Vector dimensions) {
189
        DimensionTreeModel model = ((DimensionTreeModel) getModel());
190
        for (int i = 0; i < dimensions.size(); i++) {
191
            String[] dim = ((String) dimensions.get(i)).split("=");
192
            for (int j = 0; j < dim.length; j++) {
193
                
194
            }
195
        }
196
    }
197
    
198
}
199

    
200
class DimensionTreeModel implements TreeModel {
201
    Hashtable lowLimits   = new Hashtable();
202
    Hashtable highLimits  = new Hashtable();
203
    Hashtable expressions = new Hashtable();
204

    
205
        WMSLayerNode root;
206

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

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

    
275
    /*
276
     *  (non-Javadoc)
277
     * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
278
     */
279
    public Object getChild(Object parent, int index) {
280
        if (parent instanceof IFMapWMSDimension)
281
            return null;
282
        if (parent == root)
283
            return ((WMSLayerNode) parent).getChildren().get(index);
284
        
285
        return ((WMSLayerNode) parent).getDimensions().get(index);
286
    }
287

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

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