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