gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / gui / panel / StyleTree.java @ 2484
History | View | Annotate | Download (10.8 KB)
1 | 2484 | nbrodin | /* 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.wms.app.wmsclient.gui.panel; |
||
24 | |||
25 | import java.awt.Color; |
||
26 | import java.awt.Component; |
||
27 | import java.util.ArrayList; |
||
28 | import java.util.Vector; |
||
29 | |||
30 | import javax.swing.JLabel; |
||
31 | import javax.swing.JPanel; |
||
32 | import javax.swing.JRadioButton; |
||
33 | import javax.swing.JTree; |
||
34 | import javax.swing.event.TreeModelListener; |
||
35 | import javax.swing.tree.DefaultTreeCellRenderer; |
||
36 | import javax.swing.tree.TreeModel; |
||
37 | import javax.swing.tree.TreePath; |
||
38 | |||
39 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
40 | import org.gvsig.raster.wms.io.RemoteWMSStyle; |
||
41 | import org.gvsig.raster.wms.io.WMSLayerNode; |
||
42 | |||
43 | |||
44 | /**
|
||
45 | * This class holds a visual tree that handles the selection of the styles in the
|
||
46 | * selected layers. It encapsulates any gui interface handling the mouse selection.
|
||
47 | * <p>
|
||
48 | * It has a method getStylesSelection that returns the current selection in a
|
||
49 | * bi-dimensional string array containing the layer name and the selected style name.
|
||
50 | * </p>
|
||
51 | * <p>
|
||
52 | * Keep in mind that although it's similar to LayersTree it is not the same.
|
||
53 | * It does not use the same instances of the WMSLayerNode that LayerTreeModel
|
||
54 | * because it allows to repeat layers. So, each layer is a completely new
|
||
55 | * WMSLayerNode containing same properties but not the same parent (which is always
|
||
56 | * the tree root) and each style is a completely new FMapStyle containing same
|
||
57 | * properties but its parent is one of the layers of the StylesTree and not one
|
||
58 | * of those of the Layers tree.
|
||
59 | * </p>
|
||
60 | *
|
||
61 | * @author jaume dominguez faus
|
||
62 | *
|
||
63 | */
|
||
64 | public class StyleTree extends JTree { |
||
65 | public boolean showLayerNames = false; |
||
66 | public StyleTree(){
|
||
67 | super();
|
||
68 | initialize(); |
||
69 | } |
||
70 | |||
71 | private void initialize(){ |
||
72 | setToggleClickCount(1);
|
||
73 | setRowHeight(22);
|
||
74 | |||
75 | setCellRenderer(new DefaultTreeCellRenderer() { |
||
76 | public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
||
77 | if (leaf) {
|
||
78 | JPanel leafComponent = new JPanel(); |
||
79 | leafComponent.setBackground(Color.WHITE);
|
||
80 | JRadioButton leafRadioButton = new JRadioButton("", ((StyleTreeModel) getModel()).isSelected((RemoteWMSStyle) value));//selected); |
||
81 | leafRadioButton.setBackground(Color.WHITE);
|
||
82 | leafComponent.add(leafRadioButton); |
||
83 | leafComponent.add(new JLabel(((RemoteWMSStyle) value).getTitle())); |
||
84 | return leafComponent;
|
||
85 | } else {
|
||
86 | super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
|
||
87 | if (value instanceof WMSLayerNode) { |
||
88 | WMSLayerNode layer = (WMSLayerNode) value; |
||
89 | if (!showLayerNames) {
|
||
90 | if (layer.getName() != null || layer.getName() == "") { |
||
91 | String text = layer.toString();
|
||
92 | text = text.substring(text.indexOf(']')+2, text.length()); |
||
93 | setText(text); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | return this; |
||
98 | } |
||
99 | }}); |
||
100 | addMouseListener(new java.awt.event.MouseAdapter() {
|
||
101 | public void mouseClicked(java.awt.event.MouseEvent e) { |
||
102 | ((StyleTreeModel)getModel()).setSelectedLeaf(getSelectionPath()); |
||
103 | clearSelection(); |
||
104 | repaint(); |
||
105 | } |
||
106 | }); |
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * Expands this tree.
|
||
111 | *
|
||
112 | */
|
||
113 | public void expandAll(){ |
||
114 | int row = 0; |
||
115 | while (row < getRowCount()) {
|
||
116 | expandRow(row); |
||
117 | row++; |
||
118 | } |
||
119 | } |
||
120 | /**
|
||
121 | * Returns a Vector of Strings containing the style titles.
|
||
122 | */
|
||
123 | public Vector getStyleSelectionTitles(){ |
||
124 | if (getModel() instanceof StyleTreeModel) |
||
125 | return ((StyleTreeModel)getModel()).getStyleSelectionTitles();
|
||
126 | else
|
||
127 | return null; |
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | * Sets the selected styles in the StylesTree. The argument styleNames is
|
||
132 | * a Vector with exactly the same amount of strings than the amount of
|
||
133 | * themes (layers) contained by the tree. A blank or null string will
|
||
134 | * leave the default style for that layer, but this element <b>must exist</b>
|
||
135 | * in the array in any case.
|
||
136 | *
|
||
137 | * @param styleNames, Vector containing the style names.
|
||
138 | * The styles order <b>must match</b> with the layer order.
|
||
139 | */
|
||
140 | public void initSelections(Vector styleNames){ |
||
141 | if (styleNames!=null) { |
||
142 | StyleTreeModel model = (StyleTreeModel) getModel(); |
||
143 | model.setStylesSelection(styleNames); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | } |
||
148 | |||
149 | class StyleTreeModel implements TreeModel { |
||
150 | private WMSLayerNode root;
|
||
151 | private ArrayList<WMSLayerNode> layers = new ArrayList<WMSLayerNode>(); |
||
152 | |||
153 | public StyleTreeModel(WMSLayerNode root){
|
||
154 | this.root = root;
|
||
155 | |||
156 | } |
||
157 | |||
158 | /**
|
||
159 | * Will return true if this style is selected.
|
||
160 | * @param style
|
||
161 | * @return
|
||
162 | */
|
||
163 | public boolean isSelected(RemoteWMSStyle style) { |
||
164 | return style.getParent().getSelectedStyle().equals(style);
|
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * Gets the names of the selected styles into a Vector using the same order
|
||
169 | * as they was represented in the StylesTree.
|
||
170 | * @return
|
||
171 | */
|
||
172 | public Vector<String> getStyleSelectionTitles() { |
||
173 | Vector<String> v = new Vector<String>(); |
||
174 | for (int i = 0; i < layers.size(); i++) { |
||
175 | WMSLayerNode layer = (WMSLayerNode) layers.get(i); |
||
176 | RemoteWMSStyle sty = layer.getSelectedStyle(); |
||
177 | if (sty == null) |
||
178 | v.add("");
|
||
179 | else
|
||
180 | v.add(sty.getTitle()); |
||
181 | } |
||
182 | return v;
|
||
183 | } |
||
184 | |||
185 | /**
|
||
186 | * Sets the selected styles in the StylesTree. The argument styleNames is
|
||
187 | * a Vector with exactly the same amount of strings than the amount of
|
||
188 | * themes (layers) contained by the tree. A blank or null string will
|
||
189 | * leave the default style for that layer, but this element <b>must exist</b>
|
||
190 | * in the array in any case.
|
||
191 | *
|
||
192 | * @param styleNames, Vector containing the style names.
|
||
193 | * The styles order <b>must match</b> with the layer order.
|
||
194 | */
|
||
195 | public void setStylesSelection(Vector styleNames) { |
||
196 | for (int i = 0; i < layers.size(); i++) { |
||
197 | WMSLayerNode layer = (WMSLayerNode) layers.get(i); |
||
198 | for (int j = 0; j < layer.getStyles().size(); j++) { |
||
199 | RemoteWMSStyle sty = (RemoteWMSStyle) layer.getStyles().get(j); |
||
200 | if ( sty.getName().equals((String) styleNames.get(i))) { |
||
201 | layer.setSelectedStyleByIndex(j); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /**
|
||
208 | * Adds a new branch to this tree. It must be a layer node.
|
||
209 | * @param node
|
||
210 | * @return <b>True</b>, if the added branch actually defines any style.
|
||
211 | * <b>False</b>, if the layer has no styles.
|
||
212 | */
|
||
213 | @SuppressWarnings("unchecked") |
||
214 | public boolean addLayerBranch(WMSLayerNode node) { |
||
215 | layers.add(node); |
||
216 | if (node.getStyles()==null || node.getStyles().size()==0){ |
||
217 | return false; |
||
218 | } |
||
219 | for (int i = 0; i < node.getStyles().size(); i++) { |
||
220 | ((RemoteWMSStyle) node.getStyles().get(i)).setParent(node); |
||
221 | } |
||
222 | ((WMSLayerNode)getRoot()).getChildren().add(node); |
||
223 | return true; |
||
224 | } |
||
225 | |||
226 | /**
|
||
227 | * Sets a leaf (an style) selected.
|
||
228 | * @param selectionPath to this leaf.
|
||
229 | */
|
||
230 | protected void setSelectedLeaf(TreePath selectionPath) { |
||
231 | if (selectionPath!=null){ |
||
232 | Object[] objects = selectionPath.getPath(); |
||
233 | Object item = objects[objects.length-1]; |
||
234 | if (isLeaf(item)){
|
||
235 | RemoteWMSStyle style = (RemoteWMSStyle) item; |
||
236 | WMSLayerNode layer = style.getParent() ; |
||
237 | for (int i = 0; i < layer.getStyles().size(); i++) { |
||
238 | RemoteWMSStyle sty = (RemoteWMSStyle) layer.getStyles().get(i); |
||
239 | if (sty.getName().equals(style.getName())) {
|
||
240 | layer.setSelectedStyleByIndex(i); |
||
241 | return;
|
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | |||
249 | /*
|
||
250 | * (non-Javadoc)
|
||
251 | * @see javax.swing.tree.TreeModel#getRoot()
|
||
252 | */
|
||
253 | public Object getRoot() { |
||
254 | if (root == null) { |
||
255 | root = new WMSLayerNode();
|
||
256 | root.setParent(null);
|
||
257 | } |
||
258 | return root;
|
||
259 | } |
||
260 | |||
261 | /*
|
||
262 | * (non-Javadoc)
|
||
263 | * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
|
||
264 | */
|
||
265 | public int getChildCount(Object parent) { |
||
266 | int count=0; |
||
267 | |||
268 | if (parent == root)
|
||
269 | count = ((WMSLayerNode) parent).getChildren().size(); |
||
270 | else
|
||
271 | count = ((WMSLayerNode) parent).getStyles().size(); |
||
272 | return count;
|
||
273 | } |
||
274 | |||
275 | /*
|
||
276 | * (non-Javadoc)
|
||
277 | * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
|
||
278 | */
|
||
279 | public boolean isLeaf(Object node) { |
||
280 | return (node instanceof RemoteWMSStyle); |
||
281 | } |
||
282 | |||
283 | /*
|
||
284 | * (non-Javadoc)
|
||
285 | * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
|
||
286 | */
|
||
287 | public void addTreeModelListener(TreeModelListener l) { |
||
288 | } |
||
289 | |||
290 | /*
|
||
291 | * (non-Javadoc)
|
||
292 | * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
|
||
293 | */
|
||
294 | public void removeTreeModelListener(TreeModelListener l) { |
||
295 | } |
||
296 | |||
297 | /*
|
||
298 | * (non-Javadoc)
|
||
299 | * @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
|
||
300 | */
|
||
301 | public Object getChild(Object parent, int index) { |
||
302 | if (parent instanceof RemoteWMSStyle) |
||
303 | return null; |
||
304 | if (parent == root)
|
||
305 | return ((WMSLayerNode) parent).getChildren().get(index);
|
||
306 | |||
307 | return ((WMSLayerNode) parent).getStyles().get(index);
|
||
308 | } |
||
309 | |||
310 | /*
|
||
311 | * (non-Javadoc)
|
||
312 | * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
|
||
313 | */
|
||
314 | public int getIndexOfChild(Object parent, Object child) { |
||
315 | if (parent instanceof RemoteWMSStyle) { |
||
316 | return -1; |
||
317 | } |
||
318 | |||
319 | WMSLayerNode l = (WMSLayerNode)parent; |
||
320 | if (l.getParent() == null){ |
||
321 | for (int i = 0; i < l.getChildren().size(); i++) { |
||
322 | if (l.getChildren().get(i).equals(child)){
|
||
323 | return i;
|
||
324 | } |
||
325 | } |
||
326 | } else {
|
||
327 | for (int i = 0; i < l.getStyles().size(); i++) { |
||
328 | if (l.getChildren().get(i).equals(child)){
|
||
329 | return i;
|
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | return -1; |
||
334 | } |
||
335 | |||
336 | /*
|
||
337 | * (non-Javadoc)
|
||
338 | * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
|
||
339 | */
|
||
340 | public void valueForPathChanged(TreePath path, Object newValue) { |
||
341 | } |
||
342 | } |