gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.app / org.gvsig.raster.wmts.app.wmtsclient / src / main / java / org / gvsig / raster / wmts / app / wmtsclient / gui / panel / StyleTree.java @ 393
History | View | Annotate | Download (11.5 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.app.wmtsclient.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.raster.wmts.app.wmtsclient.gui.panel.WMTSParamsPanel.LayerUI; |
40 |
import org.gvsig.remoteclient.wmts.struct.WMTSStyle; |
41 |
import org.gvsig.remoteclient.wmts.struct.WMTSTheme; |
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 |
@SuppressWarnings("serial") |
65 |
public class StyleTree extends JTree { |
66 |
private static final long serialVersionUID = 1L; |
67 |
public boolean showLayerNames = false; |
68 |
|
69 |
public StyleTree() {
|
70 |
super();
|
71 |
initialize(); |
72 |
} |
73 |
|
74 |
|
75 |
private void initialize() { |
76 |
setToggleClickCount(1);
|
77 |
setRowHeight(22);
|
78 |
|
79 |
setCellRenderer(new DefaultTreeCellRenderer() { |
80 |
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
81 |
if (leaf) {
|
82 |
JPanel leafComponent = new JPanel(); |
83 |
leafComponent.setBackground(Color.WHITE);
|
84 |
JRadioButton leafRadioButton = new JRadioButton("", ((StyleTreeModel) getModel()).isSelected((WMTSStyle) value));//selected); |
85 |
leafRadioButton.setBackground(Color.WHITE);
|
86 |
leafComponent.add(leafRadioButton); |
87 |
leafComponent.add(new JLabel(((WMTSStyle) value).getIdentifier())); |
88 |
return leafComponent;
|
89 |
} else {
|
90 |
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
|
91 |
if (value instanceof WMTSTheme) { |
92 |
WMTSTheme layer = (WMTSTheme) value; |
93 |
if (!showLayerNames) {
|
94 |
if (layer.getIdentifier() != null || layer.getIdentifier() == "") { |
95 |
String text = layer.toString();
|
96 |
text = text.substring(text.indexOf(']') + 2, text.length()); |
97 |
setText(text); |
98 |
} |
99 |
} |
100 |
} |
101 |
return this; |
102 |
} |
103 |
}}); |
104 |
addMouseListener(new java.awt.event.MouseAdapter() {
|
105 |
public void mouseClicked(java.awt.event.MouseEvent e) { |
106 |
((StyleTreeModel)getModel()).setSelectedLeaf(getSelectionPath()); |
107 |
clearSelection(); |
108 |
repaint(); |
109 |
} |
110 |
}); |
111 |
} |
112 |
|
113 |
/**
|
114 |
* Expands this tree.
|
115 |
*
|
116 |
*/
|
117 |
public void expandAll() { |
118 |
int row = 0; |
119 |
while (row < getRowCount()) {
|
120 |
expandRow(row); |
121 |
row++; |
122 |
} |
123 |
} |
124 |
|
125 |
/**
|
126 |
* Returns a Vector of Strings containing the style titles.
|
127 |
*/
|
128 |
@SuppressWarnings("unchecked") |
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 |
@SuppressWarnings("unchecked") |
147 |
public void initSelections(Vector styleNames){ |
148 |
if (styleNames!=null) { |
149 |
StyleTreeModel model = (StyleTreeModel) getModel(); |
150 |
model.setStylesSelection(styleNames); |
151 |
} |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Gets the style selected
|
156 |
* @param layerName
|
157 |
* @return
|
158 |
*/
|
159 |
public WMTSStyle getSelectedStyle(String layerName) { |
160 |
StyleTreeModel model = (StyleTreeModel) getModel(); |
161 |
LayerUI layerUI = model.getLayer(layerName); |
162 |
if(layerUI != null) |
163 |
return layerUI.styleSelected;
|
164 |
return null; |
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
class StyleTreeModel implements TreeModel { |
170 |
private String root = null; |
171 |
private ArrayList<LayerUI> layers = new ArrayList<LayerUI>(); |
172 |
|
173 |
public StyleTreeModel(String root) { |
174 |
this.root = root;
|
175 |
} |
176 |
|
177 |
public LayerUI getLayer(String layerName) { |
178 |
for (int i = 0; i < layers.size(); i++) { |
179 |
if(layers.get(i).theme.getLayer().getTitle().compareTo(layerName) == 0) { |
180 |
return layers.get(i);
|
181 |
} |
182 |
} |
183 |
return null; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Will return true if this style is selected.
|
188 |
* @param style
|
189 |
* @return
|
190 |
*/
|
191 |
public boolean isSelected(WMTSStyle style) { |
192 |
for (int i = 0; i < layers.size(); i++) { |
193 |
LayerUI layerUI = layers.get(i); |
194 |
if(layerUI.styleSelected == style)
|
195 |
return true; |
196 |
} |
197 |
return false; |
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 |
public Vector<String> getStyleSelectionTitles() { |
206 |
Vector<String> v = new Vector<String>(); |
207 |
for (int i = 0; i < layers.size(); i++) { |
208 |
LayerUI layer = (LayerUI) layers.get(i); |
209 |
WMTSStyle sty = layer.styleSelected; |
210 |
if (sty == null) |
211 |
v.add("");
|
212 |
else
|
213 |
v.add(sty.getIdentifier()); |
214 |
} |
215 |
return v;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* Sets the selected styles in the StylesTree. The argument styleNames is
|
220 |
* a Vector with exactly the same amount of strings than the amount of
|
221 |
* themes (layers) contained by the tree. A blank or null string will
|
222 |
* leave the default style for that layer, but this element <b>must exist</b>
|
223 |
* in the array in any case.
|
224 |
*
|
225 |
* @param styleNames, Vector containing the style names.
|
226 |
* The styles order <b>must match</b> with the layer order.
|
227 |
*/
|
228 |
@SuppressWarnings("unchecked") |
229 |
public void setStylesSelection(Vector styleNames) { |
230 |
for (int i = 0; i < layers.size(); i++) { |
231 |
LayerUI layer = (LayerUI) layers.get(i); |
232 |
for (int j = 0; j < layer.theme.getLayer().getStyle().size(); j++) { |
233 |
WMTSStyle sty = (WMTSStyle) layer.theme.getLayer().getStyle().get(j); |
234 |
if ( sty.getIdentifier().compareTo((String) styleNames.get(i)) == 0) { |
235 |
layer.styleSelected = sty; |
236 |
} |
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Adds a new branch to this tree. It must be a layer node.
|
243 |
* @param node
|
244 |
* @return <b>True</b>, if the added branch actually defines any style.
|
245 |
* <b>False</b>, if the layer has no styles.
|
246 |
*/
|
247 |
@SuppressWarnings("unchecked") |
248 |
public boolean addLayerBranch(LayerUI node) { |
249 |
layers.add(node); |
250 |
|
251 |
//Si no hay ninguno seleccionado seleccionamos el primero
|
252 |
for (int i = 0; i < layers.size(); i++) { |
253 |
if(layers.get(i).styleSelected == null) { |
254 |
ArrayList<WMTSStyle> sty = layers.get(i).theme.getLayer().getStyle();
|
255 |
if(sty != null && sty.get(0) != null) |
256 |
layers.get(i).styleSelected = sty.get(0);
|
257 |
} |
258 |
} |
259 |
|
260 |
if (node.theme.getLayer().getStyle() == null || node.theme.getLayer().getStyle().size() == 0) { |
261 |
return false; |
262 |
} |
263 |
return true; |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Sets a leaf (an style) selected.
|
268 |
* @param selectionPath to this leaf.
|
269 |
*/
|
270 |
protected void setSelectedLeaf(TreePath selectionPath) { |
271 |
if (selectionPath != null) { |
272 |
Object[] objects = selectionPath.getPath(); |
273 |
Object item = objects[objects.length - 1]; |
274 |
if (isLeaf(item)) {
|
275 |
LayerUI layer = (LayerUI)objects[objects.length - 2];
|
276 |
WMTSStyle style = (WMTSStyle) item; |
277 |
for (int j = 0; j < layer.theme.getLayer().getStyle().size(); j++) { |
278 |
WMTSStyle sty = (WMTSStyle)layer.theme.getLayer().getStyle().get(j); |
279 |
if(sty.getIdentifier().equals(style.getIdentifier())) {
|
280 |
layer.styleSelected = sty; |
281 |
} |
282 |
} |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
/*
|
288 |
* (non-Javadoc)
|
289 |
* @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
|
290 |
*/
|
291 |
public int getChildCount(Object parent) { |
292 |
if (parent instanceof LayerUI) |
293 |
return ((LayerUI) parent).theme.getLayer().getStyle().size();
|
294 |
if (parent instanceof WMTSStyle) |
295 |
return 0; |
296 |
return layers.size();
|
297 |
} |
298 |
|
299 |
/*
|
300 |
* (non-Javadoc)
|
301 |
* @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
|
302 |
*/
|
303 |
public boolean isLeaf(Object node) { |
304 |
return (node instanceof WMTSStyle); |
305 |
} |
306 |
|
307 |
/*
|
308 |
* (non-Javadoc)
|
309 |
* @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
|
310 |
*/
|
311 |
public void addTreeModelListener(TreeModelListener l) { |
312 |
} |
313 |
|
314 |
/*
|
315 |
* (non-Javadoc)
|
316 |
* @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
|
317 |
*/
|
318 |
public void removeTreeModelListener(TreeModelListener l) { |
319 |
} |
320 |
|
321 |
/*
|
322 |
* (non-Javadoc)
|
323 |
* @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
|
324 |
*/
|
325 |
public Object getChild(Object parent, int index) { |
326 |
if (parent instanceof WMTSStyle) |
327 |
return null; |
328 |
|
329 |
if (parent instanceof LayerUI) |
330 |
return ((LayerUI) parent).theme.getLayer().getStyle().get(index);
|
331 |
|
332 |
return layers.get(index);
|
333 |
} |
334 |
|
335 |
/*
|
336 |
* (non-Javadoc)
|
337 |
* @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
|
338 |
*/
|
339 |
@SuppressWarnings("unchecked") |
340 |
public int getIndexOfChild(Object parent, Object child) { |
341 |
if (parent instanceof LayerUI && child instanceof WMTSStyle) { |
342 |
ArrayList<WMTSStyle> listStyle = ((LayerUI)parent).theme.getLayer().getStyle();
|
343 |
for (int i = 0; i < listStyle.size(); i++) { |
344 |
WMTSStyle sty = listStyle.get(i); |
345 |
if(sty.equals(child))
|
346 |
return i;
|
347 |
} |
348 |
} |
349 |
|
350 |
if(child instanceof LayerUI) { |
351 |
for (int i = 0; i < layers.size(); i++) { |
352 |
LayerUI layerUI = layers.get(i); |
353 |
if(layerUI.equals(child))
|
354 |
return i;
|
355 |
} |
356 |
} |
357 |
|
358 |
return -1; |
359 |
} |
360 |
|
361 |
/*
|
362 |
* (non-Javadoc)
|
363 |
* @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
|
364 |
*/
|
365 |
public void valueForPathChanged(TreePath path, Object newValue) { |
366 |
} |
367 |
|
368 |
public Object getRoot() { |
369 |
return root;
|
370 |
} |
371 |
} |