root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / LegendManager.java @ 20779
History | View | Annotate | Download (26.5 KB)
1 |
/*
|
---|---|
2 |
* Created on 08-feb-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.project.documents.view.legend.gui; |
45 |
|
46 |
import java.awt.BorderLayout; |
47 |
import java.awt.Color; |
48 |
import java.awt.Component; |
49 |
import java.awt.Dimension; |
50 |
import java.awt.FlowLayout; |
51 |
import java.awt.event.ActionListener; |
52 |
import java.io.File; |
53 |
import java.util.ArrayList; |
54 |
import java.util.Enumeration; |
55 |
import java.util.Hashtable; |
56 |
import java.util.prefs.Preferences; |
57 |
|
58 |
import javax.swing.ImageIcon; |
59 |
import javax.swing.JComponent; |
60 |
import javax.swing.JFileChooser; |
61 |
import javax.swing.JLabel; |
62 |
import javax.swing.JOptionPane; |
63 |
import javax.swing.JPanel; |
64 |
import javax.swing.JScrollPane; |
65 |
import javax.swing.JSplitPane; |
66 |
import javax.swing.JTextArea; |
67 |
import javax.swing.JTree; |
68 |
import javax.swing.tree.DefaultMutableTreeNode; |
69 |
import javax.swing.tree.DefaultTreeCellRenderer; |
70 |
import javax.swing.tree.DefaultTreeModel; |
71 |
import javax.swing.tree.TreePath; |
72 |
import javax.swing.tree.TreeSelectionModel; |
73 |
|
74 |
import org.gvsig.gui.beans.swing.JButton; |
75 |
|
76 |
import com.iver.andami.PluginServices; |
77 |
import com.iver.andami.messages.NotificationManager; |
78 |
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException; |
79 |
import com.iver.cit.gvsig.fmap.drivers.legend.IFMapLegendDriver; |
80 |
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException; |
81 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
82 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
83 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
84 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable; |
85 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial; |
86 |
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend; |
87 |
import com.iver.cit.gvsig.fmap.rendering.ILegend; |
88 |
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend; |
89 |
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend; |
90 |
/**
|
91 |
* Implements the panel which allows the user to control all the information about the
|
92 |
* legends of a layer in order to improve the information that it offers to the user.
|
93 |
* There are options to create, save or load an existing legend.
|
94 |
*
|
95 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
96 |
*/
|
97 |
public class LegendManager extends AbstractThemeManagerPage { |
98 |
private static final long serialVersionUID = 7989057553773181019L; |
99 |
private static ArrayList<Class<? extends ILegendPanel>> legendPool = new ArrayList<Class<? extends ILegendPanel>>(); |
100 |
private static ArrayList<Class<? extends IFMapLegendDriver>> legendDriverPool = new ArrayList<Class<? extends IFMapLegendDriver>>(); |
101 |
|
102 |
private FLayer layer;
|
103 |
private ILegend legend; // Le asignaremos la leyenda del primer tema activo. |
104 |
private Hashtable<Class<? extends ILegendPanel>, ILegendPanel> pages = new Hashtable<Class<? extends ILegendPanel>, ILegendPanel>(); |
105 |
private JPanel topPanel = null; |
106 |
private JTextArea titleArea = null; |
107 |
private JPanel preview = null; |
108 |
private JScrollPane jTitleScrollPane = null; |
109 |
private JTree jTreeLegends; |
110 |
private ILegendPanel activePanel;
|
111 |
private JScrollPane legendTreeScrollPane; |
112 |
private boolean dirtyTree; |
113 |
private DefaultMutableTreeNode root = new DefaultMutableTreeNode(); |
114 |
private DefaultTreeModel treeModel; |
115 |
private JScrollPane jPanelContainer; |
116 |
private JPanel jCentralPanel; |
117 |
private JSplitPane jSplitPane; |
118 |
private boolean isTreeListenerDisabled; |
119 |
private JButton btnSaveLegend; |
120 |
private JButton btnLoadLegend; |
121 |
private Hashtable<FLayer, ILegend> table = null; |
122 |
private boolean empty = true; |
123 |
private JLabel iconLabel; |
124 |
|
125 |
public static String defaultLegendFolderPath; |
126 |
{ |
127 |
|
128 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
129 |
defaultLegendFolderPath = prefs.get("LegendsFolder", ""); |
130 |
} |
131 |
private ActionListener loadSaveLegendAction = new ActionListener() { |
132 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
133 |
JComponent c = (JComponent) e.getSource(); |
134 |
if (c.equals(getBtnSaveLegend())) {
|
135 |
|
136 |
JLegendFileChooser jfc = new JLegendFileChooser(getLegendDrivers(),true); |
137 |
jfc.setAcceptAllFileFilterUsed(false);
|
138 |
|
139 |
|
140 |
File basedir = null; |
141 |
Object[] options = {PluginServices.getText(this, "yes"), |
142 |
PluginServices.getText(this, "no"), |
143 |
PluginServices.getText(this, "Cancel")}; |
144 |
|
145 |
jfc.setCurrentDirectory(basedir); |
146 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
147 |
|
148 |
File file=jfc.getSelectedFile();
|
149 |
IFMapLegendDriver driver = jfc.getSuitableDriver(); |
150 |
|
151 |
if (file.exists()) {
|
152 |
int answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(), |
153 |
PluginServices.getText(this, "error_file_exists")+"\n"+ |
154 |
PluginServices.getText(this, "do_you_want_to_overwrite_it")+"?", |
155 |
PluginServices.getText(this, "confirmation_dialog"), |
156 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
157 |
JOptionPane.QUESTION_MESSAGE,
|
158 |
null,
|
159 |
options, options[1]);
|
160 |
if (answer!=JOptionPane.OK_OPTION) { |
161 |
// 'Cancel' pressed or window closed: don't save anythig, exit save dialog
|
162 |
return;
|
163 |
} |
164 |
} |
165 |
|
166 |
|
167 |
try {
|
168 |
driver.write(layer.getMapContext().getLayers(),layer, activePanel.getLegend(), file); |
169 |
|
170 |
} catch (LegendDriverException e1) {
|
171 |
int type = e1.getType();
|
172 |
String message=PluginServices.getText(this, "could_not_save_legend")+":\n"; |
173 |
|
174 |
if ((type & LegendDriverException.SAVE_LEGEND_ERROR) != 0){ |
175 |
type = type & ~LegendDriverException.SAVE_LEGEND_ERROR; |
176 |
message += PluginServices.getText(this, "error_writing_file")+".\n"; |
177 |
} |
178 |
if ((type & LegendDriverException.UNSUPPORTED_LEGEND_CREATION) != 0){ |
179 |
type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_CREATION; |
180 |
message += "-"+ PluginServices.getText(this, "legend_format_not_yet_supported")+"\n"; |
181 |
} |
182 |
if (type != 0) { |
183 |
message = "-"+PluginServices.getText(this, "unknown_error")+"\n"; |
184 |
} |
185 |
JOptionPane.showMessageDialog(null, message); |
186 |
} |
187 |
} |
188 |
} else if (c.equals(getBtnLoadLegend())) { |
189 |
|
190 |
JLegendFileChooser jfc = new JLegendFileChooser(getLegendDrivers());
|
191 |
jfc.setAcceptAllFileFilterUsed(false);
|
192 |
|
193 |
if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
194 |
File file=jfc.getSelectedFile();
|
195 |
IFMapLegendDriver driver = jfc.getSuitableDriver(); |
196 |
String path = file.getAbsolutePath();
|
197 |
defaultLegendFolderPath = path.substring(0, path.lastIndexOf(File.separator)); |
198 |
|
199 |
try {
|
200 |
table = driver.read(layer.getMapContext().getLayers(),layer, file); |
201 |
|
202 |
if(table.containsKey(layer))
|
203 |
applyLegend((ILegend) table.get(layer)); |
204 |
|
205 |
|
206 |
} catch (LegendDriverException e1) {
|
207 |
int type = e1.getType();
|
208 |
String message="La leyenda no se aplicar? a la capa:\n"; |
209 |
boolean hasReason = false; |
210 |
|
211 |
if ((type & LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND) != 0){ |
212 |
type = type & ~LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND; |
213 |
message +="-"+ PluginServices.getText(this, "classification_field_does_not_exists")+"\n"; |
214 |
hasReason = true;
|
215 |
} |
216 |
if ((type & LegendDriverException.LEGEND_TYPE_NOT_YET_SUPPORTED) != 0){ |
217 |
type = type & ~LegendDriverException.LEGEND_TYPE_NOT_YET_SUPPORTED; |
218 |
message += "-"+ PluginServices.getText(this, "legend_type_not_yet_supported")+"\n"; |
219 |
hasReason = true;
|
220 |
} |
221 |
if ((type & LegendDriverException.SYMBOL_TYPE_NOT_YET_SUPPORTED) != 0){ |
222 |
type = type & ~LegendDriverException.SYMBOL_TYPE_NOT_YET_SUPPORTED; |
223 |
message += "-"+ PluginServices.getText(this, "unsupported_symbol_type")+"\n"; |
224 |
hasReason = true;
|
225 |
} |
226 |
if ((type & LegendDriverException.LAYER_SHAPETYPE_MISMATCH) != 0){ |
227 |
type = type & ~LegendDriverException.LAYER_SHAPETYPE_MISMATCH; |
228 |
message += "-"+ PluginServices.getText(this, "layer_geometry_and_legend_types_are_incompatible")+"\n"; |
229 |
hasReason = true;
|
230 |
} |
231 |
if ((type & LegendDriverException.PARSE_LEGEND_FILE_ERROR) != 0){ |
232 |
type = type & ~LegendDriverException.PARSE_LEGEND_FILE_ERROR; |
233 |
message += "-"+ PluginServices.getText(this, "failed_reading_file")+"\n"; |
234 |
hasReason = true;
|
235 |
} |
236 |
if ((type & LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION) != 0){ |
237 |
type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION; |
238 |
message += "-"+ PluginServices.getText(this, "unsupported_legend_file_version")+"\n"; |
239 |
hasReason = true;
|
240 |
} |
241 |
if ((type & LegendDriverException.UNSUPPORTED_LEGEND_READING) != 0){ |
242 |
type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_READING; |
243 |
message +="-"+ PluginServices.getText(this, "unsupported_legend_file_format")+"\n"; |
244 |
hasReason = true;
|
245 |
} |
246 |
if ((type & LegendDriverException.LAYER_NAME_NOT_FOUND) != 0){ |
247 |
type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_READING; |
248 |
message +="-"+ PluginServices.getText(this, "could_not_find_layer")+"\n"; |
249 |
hasReason = true;
|
250 |
} |
251 |
if (!hasReason) {
|
252 |
message = "-"+ PluginServices.getText(this, "unknown_error")+"\n"; |
253 |
} |
254 |
JOptionPane.showMessageDialog(null, message); |
255 |
} |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
|
261 |
private IFMapLegendDriver[] getLegendDrivers() { |
262 |
Class<?>[] legendDriverClasses = legendDriverPool.toArray(new Class[0]); |
263 |
ArrayList<IFMapLegendDriver> drivers = new ArrayList<IFMapLegendDriver>(); |
264 |
for (int i = 0; i < legendDriverClasses.length; i++) { |
265 |
String message = PluginServices.getText(this, "adding_legend_file_format_support")+": "; |
266 |
try {
|
267 |
Class<?> c = legendDriverClasses[i];
|
268 |
drivers.add((IFMapLegendDriver) c.newInstance()); |
269 |
} catch (Exception e) { |
270 |
NotificationManager.addError(message+PluginServices.getText(this, "failed"), e); |
271 |
} |
272 |
NotificationManager.addInfo(message+PluginServices.getText(this, "OK")); |
273 |
|
274 |
} |
275 |
return (IFMapLegendDriver[]) drivers.toArray(new IFMapLegendDriver[0]); |
276 |
}; |
277 |
}; |
278 |
|
279 |
|
280 |
public LegendManager() {
|
281 |
initialize(); |
282 |
} |
283 |
|
284 |
private void initialize() { |
285 |
setLayout(new BorderLayout()); |
286 |
add(getTopPanel(), BorderLayout.NORTH);
|
287 |
add(getSplitPane(), BorderLayout.CENTER);
|
288 |
setSize(500, 360); |
289 |
treeModel = new DefaultTreeModel(root); |
290 |
} |
291 |
|
292 |
private JSplitPane getSplitPane() { |
293 |
if (jSplitPane == null) { |
294 |
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); |
295 |
JPanel aux = new JPanel(new BorderLayout(0, 5)); |
296 |
aux.add(getLegendTreeScrollPane(), BorderLayout.CENTER);
|
297 |
aux.add(getPreviewPanel(), BorderLayout.SOUTH);
|
298 |
jSplitPane.setLeftComponent(aux); |
299 |
jSplitPane.setRightComponent(getCentralPanel()); |
300 |
jSplitPane.setDividerLocation(150);
|
301 |
} |
302 |
return jSplitPane;
|
303 |
} |
304 |
|
305 |
private JPanel getCentralPanel() { |
306 |
if (jCentralPanel == null) { |
307 |
jCentralPanel = new JPanel(new BorderLayout(0,10)); |
308 |
jCentralPanel.add(getTitleScroll(), BorderLayout.NORTH);
|
309 |
jCentralPanel.add(getJPanelContainer(), BorderLayout.CENTER);
|
310 |
} |
311 |
return jCentralPanel;
|
312 |
} |
313 |
|
314 |
private JScrollPane getJPanelContainer() { |
315 |
if (jPanelContainer == null) { |
316 |
jPanelContainer = new JScrollPane(); |
317 |
} |
318 |
return jPanelContainer;
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* This method initializes jPanel
|
323 |
*
|
324 |
* @return javax.swing.JPanel
|
325 |
*/
|
326 |
private JPanel getTopPanel() { |
327 |
if (topPanel == null) { |
328 |
topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); |
329 |
topPanel.setPreferredSize(new Dimension(638, 31)); |
330 |
topPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
331 |
topPanel.add(getBtnSaveLegend(), null);
|
332 |
topPanel.add(getBtnLoadLegend(), null);
|
333 |
} |
334 |
return topPanel;
|
335 |
} |
336 |
|
337 |
private JButton getBtnSaveLegend() { |
338 |
if (btnSaveLegend == null) { |
339 |
btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"..."); |
340 |
btnSaveLegend.addActionListener(loadSaveLegendAction); |
341 |
} |
342 |
return btnSaveLegend;
|
343 |
} |
344 |
|
345 |
private JButton getBtnLoadLegend() { |
346 |
if (btnLoadLegend == null) { |
347 |
btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"..."); |
348 |
btnLoadLegend.addActionListener(loadSaveLegendAction); |
349 |
} |
350 |
return btnLoadLegend;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* This method initializes jTextArea
|
355 |
*
|
356 |
* @return javax.swing.JTextArea
|
357 |
*/
|
358 |
private JTextArea getTitleArea() { |
359 |
if (titleArea == null) { |
360 |
titleArea = new JTextArea(); |
361 |
titleArea.setBackground(java.awt.SystemColor.control); |
362 |
titleArea.setLineWrap(true);
|
363 |
titleArea.setRows(0);
|
364 |
titleArea.setWrapStyleWord(false);
|
365 |
titleArea.setEditable(false);
|
366 |
titleArea.setPreferredSize(new java.awt.Dimension(495,40)); |
367 |
} |
368 |
return titleArea;
|
369 |
} |
370 |
/**
|
371 |
* This method initializes jPanel1
|
372 |
*
|
373 |
* @return javax.swing.JPanel
|
374 |
*/
|
375 |
private JPanel getPreviewPanel() { |
376 |
if (preview == null) { |
377 |
preview = new JPanel(); |
378 |
preview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
379 |
preview.setBackground(java.awt.SystemColor.text); |
380 |
preview.setLayout(new BorderLayout(5,5)); |
381 |
preview.add(getIconLabel()); |
382 |
preview.setPreferredSize(new Dimension( |
383 |
getSplitPane().getDividerLocation(), |
384 |
130));
|
385 |
preview.setBackground(Color.white);
|
386 |
} |
387 |
return preview;
|
388 |
} |
389 |
|
390 |
private JLabel getIconLabel() { |
391 |
if (iconLabel == null) { |
392 |
iconLabel = new JLabel(); |
393 |
iconLabel.setVerticalAlignment(JLabel.CENTER);
|
394 |
iconLabel.setHorizontalAlignment(JLabel.CENTER);
|
395 |
} |
396 |
|
397 |
return iconLabel;
|
398 |
} |
399 |
|
400 |
/**
|
401 |
* This method initializes jScrollPane
|
402 |
*
|
403 |
* @return javax.swing.JScrollPane
|
404 |
*/
|
405 |
private JScrollPane getLegendTreeScrollPane() { |
406 |
if (legendTreeScrollPane == null) { |
407 |
legendTreeScrollPane = new JScrollPane(); |
408 |
legendTreeScrollPane.setViewportView(getJTreeLegends()); |
409 |
} |
410 |
return legendTreeScrollPane;
|
411 |
} |
412 |
|
413 |
/**
|
414 |
* <p>Adds a new fully-featured legend panel to the LegendManager.<br></p>
|
415 |
*
|
416 |
* <p><b>CAUTION:</b> Trying to add a child page whose parent hasn't been added yet
|
417 |
* causes the application to fall in an infinite loop. This is a known
|
418 |
* bug, sorry. Just avoid this case or try to fix it (lol).<br></p>
|
419 |
*
|
420 |
* <p><b>Notice</b> that there is no type check so if you add a non-ILegendPanel class,
|
421 |
* you'll have a runtime error later.</p>
|
422 |
* @param page, Class of type ILegendPanel
|
423 |
*/
|
424 |
public static void addLegendPage(Class<? extends ILegendPanel> iLegendPanelClass) { |
425 |
if (!legendPool.contains(iLegendPanelClass)) {
|
426 |
legendPool.add(iLegendPanelClass); |
427 |
} |
428 |
} |
429 |
|
430 |
/**
|
431 |
* Causes the component to be autofilled with the legend pages that
|
432 |
* were added through the static method addLegendPage(ILegendPanel page)
|
433 |
*/
|
434 |
private void fillDialog() { |
435 |
if (empty) {
|
436 |
for (int i = 0; i < legendPool.size(); i++) { |
437 |
Class<?> pageClass = (Class<?>) legendPool.get(i); |
438 |
ILegendPanel page; |
439 |
try {
|
440 |
page = (ILegendPanel) pageClass.newInstance(); |
441 |
if (page.isSuitableFor(layer)) {
|
442 |
// this legend can be applied to this layer
|
443 |
pages.put(page.getClass(), page); |
444 |
|
445 |
if (dirtyTree) {
|
446 |
// rebuild page tree
|
447 |
dirtyTree = false;
|
448 |
|
449 |
ArrayList<ILegendPanel> legList = new ArrayList<ILegendPanel>(pages.values()); |
450 |
ArrayList<ILegendPanel> alreadyAdded = new ArrayList<ILegendPanel>(); |
451 |
DefaultTreeModel model = new DefaultTreeModel(root); |
452 |
while (legList.size()>0) { |
453 |
ILegendPanel legend = (ILegendPanel) legList.get(0);
|
454 |
Class<? extends ILegendPanel> parent = legend.getParentClass(); |
455 |
while (parent != null && |
456 |
!alreadyAdded.contains(pages.get(parent))) { |
457 |
legend = (ILegendPanel) pages.get(parent); |
458 |
} |
459 |
doInsertNode(model, legend); |
460 |
legList.remove(legend); |
461 |
alreadyAdded.add(legend); |
462 |
} |
463 |
treeModel = model; |
464 |
jTreeLegends.setModel(model); |
465 |
} |
466 |
doInsertNode(treeModel, page); |
467 |
|
468 |
} |
469 |
getJTreeLegends().setModel(treeModel); |
470 |
|
471 |
} catch (InstantiationException e) { |
472 |
NotificationManager.addError("Trying to instantiate an interface" +
|
473 |
" or abstract class + "+pageClass.getName(), e);
|
474 |
} catch (IllegalAccessException e) { |
475 |
NotificationManager.addError("IllegalAccessException: does " +
|
476 |
pageClass.getName() + " class have an anonymous" +
|
477 |
" constructor?", e);
|
478 |
} |
479 |
|
480 |
} |
481 |
getJTreeLegends().repaint(); |
482 |
empty = false;
|
483 |
} |
484 |
} |
485 |
|
486 |
@SuppressWarnings("unchecked") |
487 |
private DefaultMutableTreeNode findNode(Class searchID) { |
488 |
String title;
|
489 |
try {
|
490 |
title = ((ILegendPanel) Class.forName(searchID.getName()).newInstance()).getTitle();
|
491 |
} catch (Exception e) { |
492 |
// this should be impossible, but anyway this case will be treat as the node does not
|
493 |
// exist.
|
494 |
return null; |
495 |
} |
496 |
|
497 |
Enumeration e = root.breadthFirstEnumeration();
|
498 |
while (e.hasMoreElements()) {
|
499 |
DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement(); |
500 |
if (nodeAux != null) { |
501 |
ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject(); |
502 |
if (legend == null) continue; // Root node |
503 |
if (legend.getTitle().equals(title)) {
|
504 |
return nodeAux;
|
505 |
} |
506 |
} |
507 |
} |
508 |
return null; |
509 |
} |
510 |
|
511 |
private void doInsertNode(DefaultTreeModel treeModel, ILegendPanel page) { |
512 |
dirtyTree = ((page.getParentClass() != null) && (findNode(page.getParentClass())==null)); |
513 |
if (findNode(page.getClass()) != null) // It is already added |
514 |
return;
|
515 |
if (page.getParentClass() != null) { |
516 |
if (pages.containsKey(page.getParentClass())) {
|
517 |
ILegendPanel parent = (ILegendPanel) pages.get(page.getParentClass()); |
518 |
DefaultMutableTreeNode nodeParent = findNode(parent.getClass());
|
519 |
if (nodeParent == null) { |
520 |
// the parent is empty
|
521 |
// Recursively add it
|
522 |
doInsertNode(treeModel, parent); |
523 |
} else {
|
524 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page); |
525 |
int children = nodeParent.getChildCount();
|
526 |
int pos=0; |
527 |
for (int i = 0; i < children; i++) { |
528 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(nodeParent, i); |
529 |
if (node.getUserObject() instanceof ILegendPanel) { |
530 |
String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
|
531 |
if (pageTitle.compareTo(page.getTitle()) < 0) ++pos; |
532 |
} |
533 |
} |
534 |
treeModel.insertNodeInto(nodeValue, nodeParent, pos); |
535 |
} |
536 |
} |
537 |
} else {
|
538 |
// First level node
|
539 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page); |
540 |
int children = root.getChildCount();
|
541 |
int pos=0; |
542 |
for (int i = 0; i < children; i++) { |
543 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(root, i); |
544 |
if (node.getUserObject() instanceof ILegendPanel) { |
545 |
String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
|
546 |
if (pageTitle.compareTo(page.getTitle()) < 0) ++pos; |
547 |
} |
548 |
} |
549 |
treeModel.insertNodeInto(nodeValue, root, pos); |
550 |
} |
551 |
} |
552 |
|
553 |
private JScrollPane getTitleScroll() { |
554 |
if (jTitleScrollPane == null) { |
555 |
jTitleScrollPane = new JScrollPane(); |
556 |
jTitleScrollPane.setBounds(2, 2, 498, 42); |
557 |
jTitleScrollPane.setViewportView(getTitleArea()); |
558 |
} |
559 |
return jTitleScrollPane;
|
560 |
} |
561 |
|
562 |
private JTree getJTreeLegends() { |
563 |
if (jTreeLegends == null) { |
564 |
jTreeLegends = new JTree(); |
565 |
jTreeLegends.setRootVisible(false);
|
566 |
MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
|
567 |
treeCellRenderer.setOpenIcon(null);
|
568 |
treeCellRenderer.setClosedIcon(null);
|
569 |
treeCellRenderer.setLeafIcon(null);
|
570 |
|
571 |
jTreeLegends.setCellRenderer(treeCellRenderer); |
572 |
jTreeLegends.setShowsRootHandles(true);
|
573 |
jTreeLegends.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
574 |
public void valueChanged(javax.swing.event.TreeSelectionEvent e) { |
575 |
if (isTreeListenerDisabled) return; |
576 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) |
577 |
jTreeLegends.getLastSelectedPathComponent(); |
578 |
|
579 |
if (node == null) return; |
580 |
setActivePage((ILegendPanel) node.getUserObject()); |
581 |
} |
582 |
}); |
583 |
jTreeLegends.putClientProperty("JTree.linestyle", "Angled"); |
584 |
jTreeLegends.getSelectionModel(). |
585 |
setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
586 |
} |
587 |
return jTreeLegends;
|
588 |
} |
589 |
|
590 |
private void setActivePage(ILegendPanel page) { |
591 |
if (page.getPanel() == null) { |
592 |
// this is what happens when the user clicked in a parent node
|
593 |
// which only acts as a folder, and does not manage any legend
|
594 |
// then it expands and selects the first child
|
595 |
DefaultMutableTreeNode node = findNode(page.getClass());
|
596 |
if (treeModel.getChildCount(node)>0) { |
597 |
DefaultMutableTreeNode dmn = (DefaultMutableTreeNode) treeModel.getChild(node, 0); |
598 |
page = (ILegendPanel) dmn.getUserObject(); |
599 |
setActivePage(page); |
600 |
expandAndSelect(page); |
601 |
} |
602 |
} else {
|
603 |
// show the page
|
604 |
activePanel = page; |
605 |
setIcon(activePanel.getIcon()); |
606 |
|
607 |
activePanel.setData(layer, legend); |
608 |
getTitleArea().setText(activePanel.getDescription()); |
609 |
jPanelContainer.setViewportView(activePanel.getPanel()); |
610 |
} |
611 |
} |
612 |
|
613 |
|
614 |
private void setIcon(ImageIcon icon) { |
615 |
getIconLabel().setIcon(icon); |
616 |
} |
617 |
|
618 |
|
619 |
private class MyTreeCellRenderer extends DefaultTreeCellRenderer { |
620 |
private static final long serialVersionUID = -6013698992263578041L; |
621 |
|
622 |
public MyTreeCellRenderer() {}
|
623 |
|
624 |
public Component getTreeCellRendererComponent( |
625 |
JTree tree,
|
626 |
Object value,
|
627 |
boolean sel,
|
628 |
boolean expanded,
|
629 |
boolean leaf,
|
630 |
int row,
|
631 |
boolean hasFocus) {
|
632 |
|
633 |
super.getTreeCellRendererComponent(
|
634 |
tree, value, sel, |
635 |
expanded, leaf, row, |
636 |
hasFocus); |
637 |
if (value instanceof DefaultMutableTreeNode) |
638 |
{ |
639 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; |
640 |
if (node.getUserObject() instanceof ILegendPanel) |
641 |
{ |
642 |
ILegendPanel legend = (ILegendPanel) node.getUserObject(); |
643 |
this.setText(
|
644 |
legend.getPanel() == null ? "<html><b>"+legend.getTitle()+"</b></html>":legend.getTitle()); |
645 |
} |
646 |
} |
647 |
return this; |
648 |
} |
649 |
|
650 |
} |
651 |
|
652 |
private void expandAndSelect(Object node) { |
653 |
isTreeListenerDisabled = true;
|
654 |
// will expand the tree and select the node
|
655 |
int i = 0; |
656 |
boolean exit = false; |
657 |
|
658 |
TreePath tp = null; |
659 |
// find the page in the tree
|
660 |
while (i<jTreeLegends.getRowCount() && !exit) {
|
661 |
//see if this row is the node that we are looking for
|
662 |
|
663 |
tp = jTreeLegends.getPathForRow(i); |
664 |
Object[] obj = tp.getPath(); |
665 |
for (int j = 0; j < obj.length && !exit ; j++) { |
666 |
Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject(); |
667 |
|
668 |
if (o!=null && o.getClass().equals(node.getClass()) && o.equals(node)) { |
669 |
// found it! collapse the tree
|
670 |
while (i>=0) { |
671 |
jTreeLegends.collapseRow(i); |
672 |
i--; |
673 |
} |
674 |
exit = true;
|
675 |
} |
676 |
} |
677 |
jTreeLegends.expandRow(i); |
678 |
i++; |
679 |
} |
680 |
|
681 |
// expand the tree and set the selection
|
682 |
if (tp != null) { |
683 |
jTreeLegends.expandPath(tp); |
684 |
jTreeLegends.setSelectionPath(tp); |
685 |
} |
686 |
isTreeListenerDisabled = false;
|
687 |
} |
688 |
|
689 |
public String getName() { |
690 |
return PluginServices.getText(this,"Simbologia"); |
691 |
} |
692 |
|
693 |
public void acceptAction() { |
694 |
// automatically handled by the ThemeManagerWindow
|
695 |
} |
696 |
|
697 |
public void cancelAction() { |
698 |
// does nothing
|
699 |
} |
700 |
|
701 |
public void applyAction() { |
702 |
legend = activePanel.getLegend(); |
703 |
|
704 |
|
705 |
if (table != null && table.size() > 1) |
706 |
applyRestOfLegends(table,layer.getMapContext().getLayers()); |
707 |
|
708 |
/* try to apply the legend to all the active layers that
|
709 |
can accept it */
|
710 |
FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
|
711 |
for (int i=0; i < activeLyrs.length; i++) { |
712 |
FLayer laux=activeLyrs[i]; |
713 |
|
714 |
if (activeLyrs[i] instanceof FLayers){ |
715 |
laux=getFirstActiveLayerVect((FLayers)activeLyrs[i]); |
716 |
} |
717 |
|
718 |
if (laux instanceof ClassifiableVectorial) { |
719 |
ClassifiableVectorial aux2 = (ClassifiableVectorial) laux; |
720 |
try {
|
721 |
if (legend instanceof IClassifiedVectorLegend) { |
722 |
// Es una leyenda que necesita un recordset con un
|
723 |
// nombre de campo. Comprobamos que ese recordset
|
724 |
// tiene ese nombre de campo y es del tipo esperado
|
725 |
IClassifiedVectorLegend cl = (IClassifiedVectorLegend) legend; |
726 |
|
727 |
if (aux2 instanceof AlphanumericData) { |
728 |
|
729 |
if (cl.getValues().length==0) { |
730 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_es_posible_aplicar_leyenda_vacia")); |
731 |
return;
|
732 |
} |
733 |
|
734 |
aux2.setLegend((IVectorLegend) legend); |
735 |
} |
736 |
} |
737 |
else if (legend instanceof SingleSymbolLegend) |
738 |
aux2.setLegend((IVectorLegend) legend); |
739 |
} catch (LegendLayerException e) {
|
740 |
NotificationManager.addError(PluginServices.getText(this, "legend_exception"), e); |
741 |
} |
742 |
} |
743 |
} |
744 |
} |
745 |
|
746 |
private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2,FLayers layers) { |
747 |
|
748 |
for(int i = 0; i < layers.getLayersCount(); i++) { |
749 |
FLayer my_layer= layers.getLayer(i); |
750 |
|
751 |
if(!(my_layer instanceof FLayers)){ |
752 |
if(my_layer instanceof ClassifiableVectorial){ |
753 |
try {
|
754 |
if(table.containsKey(my_layer)){
|
755 |
ClassifiableVectorial lyr = (ClassifiableVectorial)my_layer; |
756 |
lyr.setLegend((IVectorLegend) table.get(my_layer)); |
757 |
} |
758 |
|
759 |
} catch (LegendLayerException e) {
|
760 |
// TODO Auto-generated catch block
|
761 |
e.printStackTrace(); |
762 |
} |
763 |
} |
764 |
} |
765 |
else
|
766 |
applyRestOfLegends(table,(FLayers) my_layer); |
767 |
} |
768 |
} |
769 |
|
770 |
@Override
|
771 |
public void setModel(FLayer layer) { |
772 |
this.layer = layer;
|
773 |
applyLegend(((Classifiable) layer).getLegend()); |
774 |
} |
775 |
|
776 |
/**
|
777 |
* Applies the legend to the layer.
|
778 |
*
|
779 |
* @param aLegend , legend that the user wants to apply
|
780 |
*/
|
781 |
private void applyLegend(ILegend aLegend) { |
782 |
this.legend = aLegend;
|
783 |
fillDialog(); |
784 |
Enumeration<Class<? extends ILegendPanel>> en = pages.keys(); |
785 |
while (en.hasMoreElements()) {
|
786 |
ILegendPanel page = (ILegendPanel) pages.get(en.nextElement()); |
787 |
if (legend.getClass().equals(page.getLegendClass())) {
|
788 |
setActivePage(page); |
789 |
expandAndSelect(page); |
790 |
return;
|
791 |
} |
792 |
} |
793 |
NotificationManager.addWarning( |
794 |
PluginServices.getText(this,
|
795 |
"caution_no_registered_panel_associated_to_" +
|
796 |
"loaded_legend_the_legend_wont_be_applied"));
|
797 |
} |
798 |
|
799 |
public static void addLegendDriver(Class<? extends IFMapLegendDriver> legendDriverClass) { |
800 |
if (!legendDriverPool.contains(legendDriverClass)) {
|
801 |
legendDriverPool.add(legendDriverClass); |
802 |
} |
803 |
} |
804 |
} |