svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / LegendManager.java @ 44237
History | View | Annotate | Download (26 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project.documents.view.legend.gui; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Color; |
28 |
import java.awt.Component; |
29 |
import java.awt.Dimension; |
30 |
import java.awt.FlowLayout; |
31 |
import java.awt.Point; |
32 |
import java.awt.event.ActionEvent; |
33 |
import java.awt.event.ActionListener; |
34 |
import java.util.Collection; |
35 |
import java.util.Enumeration; |
36 |
import java.util.HashMap; |
37 |
import java.util.Hashtable; |
38 |
import java.util.Iterator; |
39 |
import java.util.Map; |
40 |
import java.util.prefs.Preferences; |
41 |
import javax.swing.Action; |
42 |
|
43 |
import javax.swing.ImageIcon; |
44 |
import javax.swing.JButton; |
45 |
import javax.swing.JLabel; |
46 |
import javax.swing.JMenuItem; |
47 |
import javax.swing.JOptionPane; |
48 |
import javax.swing.JPanel; |
49 |
import javax.swing.JPopupMenu; |
50 |
import javax.swing.JScrollPane; |
51 |
import javax.swing.JSplitPane; |
52 |
import javax.swing.JTextArea; |
53 |
import javax.swing.JTree; |
54 |
import javax.swing.tree.DefaultMutableTreeNode; |
55 |
import javax.swing.tree.DefaultTreeCellRenderer; |
56 |
import javax.swing.tree.DefaultTreeModel; |
57 |
import javax.swing.tree.MutableTreeNode; |
58 |
import javax.swing.tree.TreePath; |
59 |
import javax.swing.tree.TreeSelectionModel; |
60 |
|
61 |
import org.slf4j.Logger; |
62 |
import org.slf4j.LoggerFactory; |
63 |
|
64 |
import org.gvsig.andami.PluginServices; |
65 |
import org.gvsig.andami.messages.NotificationManager; |
66 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
67 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
68 |
import static org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel.OPEN_LAYER_FILE_CHOOSER_ID; |
69 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
70 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
71 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
72 |
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable; |
73 |
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial; |
74 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
75 |
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend; |
76 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
77 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
78 |
import org.gvsig.symbology.SymbologyLocator; |
79 |
import org.gvsig.symbology.swing.SymbologySwingLocator; |
80 |
import org.gvsig.symbology.swing.SymbologySwingManager; |
81 |
import org.gvsig.tools.ToolsLocator; |
82 |
import org.gvsig.tools.folders.FoldersManager; |
83 |
|
84 |
/**
|
85 |
* Implements the panel which allows the user to control all the information
|
86 |
* about the
|
87 |
* legends of a layer in order to improve the information that it offers to the
|
88 |
* user.
|
89 |
* There are options to create, save or load an existing legend.
|
90 |
*
|
91 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
92 |
*/
|
93 |
public class LegendManager extends AbstractThemeManagerPage implements LegendsPanel { |
94 |
|
95 |
private static final Logger logger = |
96 |
LoggerFactory.getLogger(LegendManager.class); |
97 |
|
98 |
private static final long serialVersionUID = 7989057553773181019L; |
99 |
|
100 |
public static class Pages implements Iterable<ILegendPanel> { |
101 |
|
102 |
private Map<Class<? extends ILegendPanel>, ILegendPanel> pages; |
103 |
|
104 |
public Pages() {
|
105 |
this.pages = new HashMap<>(); |
106 |
} |
107 |
|
108 |
public void add(ILegendPanel page) { |
109 |
this.pages.put(page.getClass(), page);
|
110 |
} |
111 |
|
112 |
public Iterator<ILegendPanel> iterator() { |
113 |
return this.pages.values().iterator(); |
114 |
} |
115 |
|
116 |
public boolean contains(Class<? extends ILegendPanel> pageClass) { |
117 |
return this.pages.containsKey(pageClass); |
118 |
} |
119 |
|
120 |
public ILegendPanel get(Class<? extends ILegendPanel> pageClass) { |
121 |
return this.pages.get(pageClass); |
122 |
} |
123 |
|
124 |
public Collection<ILegendPanel> asCollection() { |
125 |
return this.pages.values(); |
126 |
} |
127 |
} |
128 |
|
129 |
private FLayer layer;
|
130 |
private ILegend legend;
|
131 |
private Pages pages = new Pages(); |
132 |
|
133 |
private JPanel topPanel = null; |
134 |
private JTextArea titleArea = null; |
135 |
private JPanel preview = null; |
136 |
private JScrollPane jTitleScrollPane = null; |
137 |
private JTree jTreeLegends; |
138 |
private ILegendPanel activePanel;
|
139 |
private JScrollPane legendTreeScrollPane; |
140 |
// private boolean dirtyTree_;
|
141 |
private final DefaultMutableTreeNode root = new DefaultMutableTreeNode(); |
142 |
private DefaultTreeModel treeModel; |
143 |
private JScrollPane jPanelContainer; |
144 |
private JPanel jCentralPanel; |
145 |
private JSplitPane jSplitPane; |
146 |
private boolean isTreeListenerDisabled; |
147 |
private JButton btnOptionalActions; |
148 |
private JPopupMenu menuOptionalActions; |
149 |
private Hashtable<FLayer, ILegend> table = null; |
150 |
private boolean empty = true; |
151 |
private JLabel iconLabel; |
152 |
|
153 |
public static String defaultLegendFolderPath; |
154 |
{ |
155 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
156 |
defaultLegendFolderPath = folderManager.getLastPath( |
157 |
"LegendsFolder",
|
158 |
folderManager.getHome() |
159 |
).getAbsolutePath(); |
160 |
} |
161 |
|
162 |
|
163 |
public LegendManager() {
|
164 |
initialize(); |
165 |
} |
166 |
|
167 |
private void initialize() { |
168 |
setLayout(new BorderLayout()); |
169 |
add(getTopPanel(), BorderLayout.NORTH);
|
170 |
add(getSplitPane(), BorderLayout.CENTER);
|
171 |
setSize(500, 360); |
172 |
treeModel = new DefaultTreeModel(root); |
173 |
} |
174 |
|
175 |
private JSplitPane getSplitPane() { |
176 |
if (jSplitPane == null) { |
177 |
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); |
178 |
JPanel aux = new JPanel(new BorderLayout(0, 5)); |
179 |
aux.add(getLegendTreeScrollPane(), BorderLayout.CENTER);
|
180 |
aux.add(getPreviewPanel(), BorderLayout.SOUTH);
|
181 |
jSplitPane.setLeftComponent(aux); |
182 |
jSplitPane.setRightComponent(getCentralPanel()); |
183 |
jSplitPane.setDividerLocation(150);
|
184 |
} |
185 |
return jSplitPane;
|
186 |
} |
187 |
|
188 |
private JPanel getCentralPanel() { |
189 |
if (jCentralPanel == null) { |
190 |
jCentralPanel = new JPanel(new BorderLayout(0, 10)); |
191 |
jCentralPanel.add(getTitleScroll(), BorderLayout.NORTH);
|
192 |
jCentralPanel.add(getJPanelContainer(), BorderLayout.CENTER);
|
193 |
} |
194 |
return jCentralPanel;
|
195 |
} |
196 |
|
197 |
private JScrollPane getJPanelContainer() { |
198 |
if (jPanelContainer == null) { |
199 |
jPanelContainer = new JScrollPane(); |
200 |
} |
201 |
return jPanelContainer;
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* This method initializes jPanel
|
206 |
*
|
207 |
* @return javax.swing.JPanel
|
208 |
*/
|
209 |
private JPanel getTopPanel() { |
210 |
if (topPanel == null) { |
211 |
topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); |
212 |
topPanel.setPreferredSize(new Dimension(638, 31)); |
213 |
topPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
214 |
null, "", |
215 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
216 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
217 |
topPanel.add(getBtnOptionalActions(), null);
|
218 |
} |
219 |
return topPanel;
|
220 |
} |
221 |
|
222 |
private JButton getBtnOptionalActions() { |
223 |
if (btnOptionalActions == null) { |
224 |
btnOptionalActions = |
225 |
new JButton(PluginServices.getText(this, "_Mas_opciones") |
226 |
+ "...");
|
227 |
btnOptionalActions.addActionListener(new ActionListener() { |
228 |
@Override
|
229 |
public void actionPerformed(ActionEvent e) { |
230 |
Point p = btnOptionalActions.getLocationOnScreen();
|
231 |
menuOptionalActions.show(asJComponent(),0,0); |
232 |
menuOptionalActions.setLocation(p.x,p.y+btnOptionalActions.getHeight()); |
233 |
} |
234 |
}); |
235 |
} |
236 |
return btnOptionalActions;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* This method initializes jTextArea
|
241 |
*
|
242 |
* @return javax.swing.JTextArea
|
243 |
*/
|
244 |
private JTextArea getTitleArea() { |
245 |
if (titleArea == null) { |
246 |
titleArea = new JTextArea(); |
247 |
titleArea.setBackground(java.awt.SystemColor.control); |
248 |
titleArea.setLineWrap(true);
|
249 |
titleArea.setRows(0);
|
250 |
titleArea.setWrapStyleWord(false);
|
251 |
titleArea.setEditable(false);
|
252 |
titleArea.setPreferredSize(new java.awt.Dimension(495, 40)); |
253 |
} |
254 |
return titleArea;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* This method initializes jPanel1
|
259 |
*
|
260 |
* @return javax.swing.JPanel
|
261 |
*/
|
262 |
private JPanel getPreviewPanel() { |
263 |
if (preview == null) { |
264 |
preview = new JPanel(); |
265 |
preview.setBorder(javax.swing.BorderFactory |
266 |
.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
267 |
preview.setBackground(java.awt.SystemColor.text); |
268 |
preview.setLayout(new BorderLayout(5, 5)); |
269 |
preview.add(getIconLabel()); |
270 |
preview.setPreferredSize(new Dimension(getSplitPane() |
271 |
.getDividerLocation(), 130));
|
272 |
preview.setBackground(Color.white);
|
273 |
} |
274 |
return preview;
|
275 |
} |
276 |
|
277 |
private JLabel getIconLabel() { |
278 |
if (iconLabel == null) { |
279 |
iconLabel = new JLabel(); |
280 |
iconLabel.setVerticalAlignment(JLabel.CENTER);
|
281 |
iconLabel.setHorizontalAlignment(JLabel.CENTER);
|
282 |
} |
283 |
|
284 |
return iconLabel;
|
285 |
} |
286 |
|
287 |
/**
|
288 |
* This method initializes jScrollPane
|
289 |
*
|
290 |
* @return javax.swing.JScrollPane
|
291 |
*/
|
292 |
private JScrollPane getLegendTreeScrollPane() { |
293 |
if (legendTreeScrollPane == null) { |
294 |
legendTreeScrollPane = new JScrollPane(); |
295 |
legendTreeScrollPane.setViewportView(getJTreeLegends()); |
296 |
} |
297 |
return legendTreeScrollPane;
|
298 |
} |
299 |
|
300 |
/**
|
301 |
* <p>
|
302 |
* Adds a new fully-featured legend panel to the LegendManager.<br>
|
303 |
* </p>
|
304 |
*
|
305 |
* <p>
|
306 |
* <b>CAUTION:</b> Trying to add a child page whose parent hasn't been added
|
307 |
* yet causes the application to fall in an infinite loop. This is a known
|
308 |
* bug, sorry. Just avoid this case or try to fix it (lol).<br>
|
309 |
* </p>
|
310 |
*
|
311 |
* @deprecated use SymbologySwingManager.registerLegendEditor
|
312 |
*/
|
313 |
public static void addLegendPage(Class<? extends ILegendPanel> iLegendPanelClass) { |
314 |
SymbologySwingManager manager = SymbologySwingLocator.getSwingManager(); |
315 |
manager.registerLegendEditor(iLegendPanelClass); |
316 |
} |
317 |
|
318 |
/**
|
319 |
* Causes the component to be autofilled with the legend pages that
|
320 |
* were added through the static method addLegendPage(ILegendPanel page)
|
321 |
*/
|
322 |
private void fillDialog() { |
323 |
if (empty) {
|
324 |
SymbologySwingManager manager = SymbologySwingLocator.getSwingManager(); |
325 |
|
326 |
Iterator<ILegendPanel> it = manager.getLegendEditors(layer).iterator();
|
327 |
while( it.hasNext() ) {
|
328 |
ILegendPanel page = it.next(); |
329 |
pages.add(page); |
330 |
} |
331 |
addOptionalActions(); |
332 |
for(ILegendPanel page : this.pages ) { |
333 |
doInsertNode(treeModel, page); |
334 |
} |
335 |
getJTreeLegends().setModel(treeModel); |
336 |
getJTreeLegends().repaint(); |
337 |
empty = false;
|
338 |
} |
339 |
} |
340 |
|
341 |
private void addOptionalActions() { |
342 |
this.menuOptionalActions = new JPopupMenu(); |
343 |
Iterable<Action> actions = SymbologySwingLocator.getSwingManager().getOptionalActionOfLegendsPanel(); |
344 |
for( Action action : actions ) { |
345 |
JMenuItemForOptionalAction item = new JMenuItemForOptionalAction(action);
|
346 |
this.menuOptionalActions.add(item);
|
347 |
} |
348 |
} |
349 |
|
350 |
private class JMenuItemForOptionalAction extends JMenuItem implements ActionListener { |
351 |
|
352 |
private static final long serialVersionUID = 1656264978338543368L; |
353 |
Action action;
|
354 |
|
355 |
JMenuItemForOptionalAction(Action action) {
|
356 |
this.action = action;
|
357 |
this.configurePropertiesFromAction(action);
|
358 |
this.addActionListener(this); |
359 |
} |
360 |
|
361 |
@Override
|
362 |
public void actionPerformed(ActionEvent e) { |
363 |
e.setSource(LegendManager.this); |
364 |
this.action.actionPerformed(e);
|
365 |
} |
366 |
} |
367 |
|
368 |
@SuppressWarnings("unchecked") |
369 |
private DefaultMutableTreeNode findNode(Class searchID) { |
370 |
String title;
|
371 |
try {
|
372 |
title = |
373 |
((ILegendPanel) Class.forName(searchID.getName()).newInstance())
|
374 |
.getTitle(); |
375 |
} catch (Exception e) { |
376 |
// this should be impossible, but anyway this case will be treat as
|
377 |
// the node does not
|
378 |
// exist.
|
379 |
return null; |
380 |
} |
381 |
|
382 |
Enumeration e = root.breadthFirstEnumeration();
|
383 |
while (e.hasMoreElements()) {
|
384 |
DefaultMutableTreeNode nodeAux =
|
385 |
(DefaultMutableTreeNode) e.nextElement();
|
386 |
if (nodeAux != null) { |
387 |
ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject(); |
388 |
if (legend == null) |
389 |
continue; // Root node |
390 |
if (legend.getTitle().equals(title)) {
|
391 |
return nodeAux;
|
392 |
} |
393 |
} |
394 |
} |
395 |
return null; |
396 |
} |
397 |
|
398 |
/**
|
399 |
* If parent_node == null, add to root.
|
400 |
* Returns added node
|
401 |
*
|
402 |
* @param tm
|
403 |
* @param parent_node
|
404 |
* @param item
|
405 |
*/
|
406 |
private MutableTreeNode insertNodeHere( |
407 |
DefaultTreeModel tm,
|
408 |
MutableTreeNode parent_node,
|
409 |
ILegendPanel item) { |
410 |
|
411 |
MutableTreeNode pn = null; |
412 |
|
413 |
if (parent_node == null) { |
414 |
pn = root; |
415 |
} else {
|
416 |
pn = parent_node; |
417 |
} |
418 |
|
419 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(item); |
420 |
int children = pn.getChildCount();
|
421 |
int pos = 0; |
422 |
String pTitle = null; |
423 |
|
424 |
for (int i = 0; i < children; i++) { |
425 |
DefaultMutableTreeNode node =
|
426 |
(DefaultMutableTreeNode) tm.getChild(pn, i);
|
427 |
if (node.getUserObject() instanceof ILegendPanel) { |
428 |
pTitle = ((ILegendPanel) node.getUserObject()).getTitle(); |
429 |
if (pTitle.compareTo(item.getTitle()) < 0) { |
430 |
pos++; |
431 |
} |
432 |
} |
433 |
} |
434 |
tm.insertNodeInto(nodeValue, pn, pos); |
435 |
return nodeValue;
|
436 |
} |
437 |
|
438 |
/**
|
439 |
* Returns inserted node
|
440 |
*
|
441 |
* @param tm
|
442 |
* @param page
|
443 |
* @return
|
444 |
*/
|
445 |
private MutableTreeNode doInsertNode(DefaultTreeModel tm, ILegendPanel page) { |
446 |
|
447 |
if (tm == null || page == null) { |
448 |
return null; |
449 |
} |
450 |
|
451 |
MutableTreeNode aux = findNode(page.getClass());
|
452 |
if (aux != null) { |
453 |
return aux;
|
454 |
} |
455 |
|
456 |
Class parent_class = page.getParentClass();
|
457 |
|
458 |
if (parent_class != null) { |
459 |
// if (pages.containsKey(parent_class)) {
|
460 |
if (pages.contains(parent_class)) {
|
461 |
ILegendPanel parent = (ILegendPanel) pages.get(parent_class); |
462 |
|
463 |
aux = doInsertNode(tm, parent); |
464 |
if (aux != null) { |
465 |
return insertNodeHere(tm, aux, page);
|
466 |
} else {
|
467 |
return null; |
468 |
} |
469 |
|
470 |
} else {
|
471 |
return null; |
472 |
} |
473 |
} else {
|
474 |
// add to root
|
475 |
return insertNodeHere(tm, null, page); |
476 |
} |
477 |
|
478 |
|
479 |
} |
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
private JScrollPane getTitleScroll() { |
486 |
if (jTitleScrollPane == null) { |
487 |
jTitleScrollPane = new JScrollPane(); |
488 |
jTitleScrollPane.setBounds(2, 2, 498, 42); |
489 |
jTitleScrollPane.setViewportView(getTitleArea()); |
490 |
} |
491 |
return jTitleScrollPane;
|
492 |
} |
493 |
|
494 |
private JTree getJTreeLegends() { |
495 |
if (jTreeLegends == null) { |
496 |
jTreeLegends = new JTree(); |
497 |
jTreeLegends.setRootVisible(false);
|
498 |
MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
|
499 |
treeCellRenderer.setOpenIcon(null);
|
500 |
treeCellRenderer.setClosedIcon(null);
|
501 |
treeCellRenderer.setLeafIcon(null);
|
502 |
|
503 |
jTreeLegends.setCellRenderer(treeCellRenderer); |
504 |
jTreeLegends.setShowsRootHandles(true);
|
505 |
jTreeLegends |
506 |
.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
507 |
|
508 |
public void valueChanged( |
509 |
javax.swing.event.TreeSelectionEvent e) { |
510 |
if (isTreeListenerDisabled)
|
511 |
return;
|
512 |
DefaultMutableTreeNode node =
|
513 |
(DefaultMutableTreeNode) jTreeLegends
|
514 |
.getLastSelectedPathComponent(); |
515 |
|
516 |
if (node == null) |
517 |
return;
|
518 |
setActivePage((ILegendPanel) node.getUserObject()); |
519 |
} |
520 |
}); |
521 |
jTreeLegends.putClientProperty("JTree.linestyle", "Angled"); |
522 |
jTreeLegends.getSelectionModel().setSelectionMode( |
523 |
TreeSelectionModel.SINGLE_TREE_SELECTION);
|
524 |
} |
525 |
return jTreeLegends;
|
526 |
} |
527 |
|
528 |
@Override
|
529 |
public void setActivePage(ILegendPanel page) { |
530 |
if (page.getPanel() == null) { |
531 |
// this is what happens when the user clicked in a parent node
|
532 |
// which only acts as a folder, and does not manage any legend
|
533 |
// then it expands and selects the first child
|
534 |
DefaultMutableTreeNode node = findNode(page.getClass());
|
535 |
if (treeModel.getChildCount(node) > 0) { |
536 |
DefaultMutableTreeNode dmn =
|
537 |
(DefaultMutableTreeNode) treeModel.getChild(node, 0); |
538 |
page = (ILegendPanel) dmn.getUserObject(); |
539 |
setActivePage(page); |
540 |
expandAndSelect(page); |
541 |
} |
542 |
} else {
|
543 |
// show the page
|
544 |
activePanel = page; |
545 |
setIcon(activePanel.getIcon()); |
546 |
|
547 |
activePanel.setData(layer, legend); |
548 |
getTitleArea().setText(activePanel.getDescription()); |
549 |
jPanelContainer.setViewportView(activePanel.getPanel()); |
550 |
} |
551 |
} |
552 |
|
553 |
@Override
|
554 |
public ILegendPanel getActivePage() {
|
555 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
556 |
} |
557 |
|
558 |
private void setIcon(ImageIcon icon) { |
559 |
getIconLabel().setIcon(icon); |
560 |
} |
561 |
|
562 |
private class MyTreeCellRenderer extends DefaultTreeCellRenderer { |
563 |
|
564 |
private static final long serialVersionUID = -6013698992263578041L; |
565 |
|
566 |
public MyTreeCellRenderer() {
|
567 |
} |
568 |
|
569 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
570 |
boolean sel, boolean expanded, boolean leaf, int row, |
571 |
boolean hasFocus) {
|
572 |
|
573 |
super.getTreeCellRendererComponent(tree, value, sel, expanded,
|
574 |
leaf, row, hasFocus); |
575 |
if (value instanceof DefaultMutableTreeNode) { |
576 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; |
577 |
if (node.getUserObject() instanceof ILegendPanel) { |
578 |
ILegendPanel legend = (ILegendPanel) node.getUserObject(); |
579 |
this
|
580 |
.setText(legend.getPanel() == null ? "<html><b>" |
581 |
+ legend.getTitle() + "</b></html>" : legend
|
582 |
.getTitle()); |
583 |
} |
584 |
} |
585 |
return this; |
586 |
} |
587 |
|
588 |
} |
589 |
|
590 |
private void expandAndSelect(Object node) { |
591 |
isTreeListenerDisabled = true;
|
592 |
// will expand the tree and select the node
|
593 |
int i = 0; |
594 |
boolean exit = false; |
595 |
|
596 |
TreePath tp = null; |
597 |
// find the page in the tree
|
598 |
while (i < jTreeLegends.getRowCount() && !exit) {
|
599 |
// see if this row is the node that we are looking for
|
600 |
|
601 |
tp = jTreeLegends.getPathForRow(i); |
602 |
Object[] obj = tp.getPath(); |
603 |
for (int j = 0; j < obj.length && !exit; j++) { |
604 |
Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject(); |
605 |
|
606 |
if (o != null && o.getClass().equals(node.getClass()) |
607 |
&& o.equals(node)) { |
608 |
// found it! collapse the tree
|
609 |
while (i >= 0) { |
610 |
jTreeLegends.collapseRow(i); |
611 |
i--; |
612 |
} |
613 |
exit = true;
|
614 |
} |
615 |
} |
616 |
jTreeLegends.expandRow(i); |
617 |
i++; |
618 |
} |
619 |
|
620 |
// expand the tree and set the selection
|
621 |
if (tp != null) { |
622 |
jTreeLegends.expandPath(tp); |
623 |
jTreeLegends.setSelectionPath(tp); |
624 |
} |
625 |
isTreeListenerDisabled = false;
|
626 |
} |
627 |
|
628 |
public String getName() { |
629 |
return PluginServices.getText(this, "Simbologia"); |
630 |
} |
631 |
|
632 |
public void acceptAction() { |
633 |
applyAction(); |
634 |
// automatically handled by the ThemeManagerWindow
|
635 |
} |
636 |
|
637 |
public void cancelAction() { |
638 |
// does nothing
|
639 |
} |
640 |
|
641 |
public void applyAction() { |
642 |
legend = activePanel.getLegend(); |
643 |
|
644 |
if (table != null && table.size() > 1) |
645 |
applyRestOfLegends(table, layer.getMapContext().getLayers()); |
646 |
|
647 |
/*
|
648 |
* try to apply the legend to all the active layers that
|
649 |
* can accept it
|
650 |
*/
|
651 |
FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
|
652 |
for (int i = 0; i < activeLyrs.length; i++) { |
653 |
FLayer laux = activeLyrs[i]; |
654 |
|
655 |
if (activeLyrs[i] instanceof FLayers) { |
656 |
laux = getFirstActiveLayerVect((FLayers) activeLyrs[i]); |
657 |
} |
658 |
|
659 |
if (laux instanceof ClassifiableVectorial) { |
660 |
ClassifiableVectorial aux2 = (ClassifiableVectorial) laux; |
661 |
try {
|
662 |
if (legend instanceof IClassifiedVectorLegend) { |
663 |
// Es una leyenda que necesita un recordset con un
|
664 |
// nombre de campo. Comprobamos que ese recordset
|
665 |
// tiene ese nombre de campo y es del tipo esperado
|
666 |
IClassifiedVectorLegend cl = |
667 |
(IClassifiedVectorLegend) legend; |
668 |
|
669 |
if (aux2 instanceof FLyrVect) { |
670 |
|
671 |
if (cl.getValues().length == 0) { |
672 |
JOptionPane.showMessageDialog(
|
673 |
(Component) PluginServices.getMainFrame(),
|
674 |
PluginServices.getText(this,
|
675 |
"no_es_posible_aplicar_leyenda_vacia"));
|
676 |
return;
|
677 |
} |
678 |
|
679 |
aux2.setLegend((IVectorLegend) legend); |
680 |
} |
681 |
} else
|
682 |
if (legend instanceof IVectorLegend) { |
683 |
aux2.setLegend((IVectorLegend) legend); |
684 |
} |
685 |
} catch (LegendLayerException e) {
|
686 |
NotificationManager.addError(PluginServices.getText(this,
|
687 |
"legend_exception"), e);
|
688 |
} |
689 |
} |
690 |
} |
691 |
} |
692 |
|
693 |
private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2, |
694 |
FLayers layers) { |
695 |
|
696 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
697 |
FLayer my_layer = layers.getLayer(i); |
698 |
|
699 |
if (!(my_layer instanceof FLayers)) { |
700 |
if (my_layer instanceof ClassifiableVectorial) { |
701 |
try {
|
702 |
if (table.containsKey(my_layer)) {
|
703 |
ClassifiableVectorial lyr = |
704 |
(ClassifiableVectorial) my_layer; |
705 |
lyr.setLegend((IVectorLegend) table.get(my_layer)); |
706 |
} |
707 |
|
708 |
} catch (LegendLayerException e) {
|
709 |
// TODO Auto-generated catch block
|
710 |
e.printStackTrace(); |
711 |
} |
712 |
} |
713 |
} else
|
714 |
applyRestOfLegends(table, (FLayers) my_layer); |
715 |
} |
716 |
} |
717 |
|
718 |
@Override
|
719 |
public void setModel(FLayer layer) { |
720 |
this.layer = layer;
|
721 |
applyLegend(((Classifiable) layer).getLegend()); |
722 |
} |
723 |
|
724 |
/**
|
725 |
* Applies the legend to the layer.
|
726 |
*
|
727 |
* @param aLegend
|
728 |
* , legend that the user wants to apply
|
729 |
*/
|
730 |
private void applyLegend(ILegend aLegend) { |
731 |
this.legend = aLegend;
|
732 |
fillDialog(); |
733 |
for(ILegendPanel page : this.pages ) { |
734 |
// Enumeration<Class<? extends ILegendPanel>> en = pages.keys();
|
735 |
// while (en.hasMoreElements()) {
|
736 |
// ILegendPanel page = (ILegendPanel) pages.get(en.nextElement());
|
737 |
if (legend.getClass().equals(page.getLegendClass())) {
|
738 |
setActivePage(page); |
739 |
expandAndSelect(page); |
740 |
return;
|
741 |
} |
742 |
} |
743 |
NotificationManager.addWarning(PluginServices.getText(this,
|
744 |
"caution_no_registered_panel_associated_to_"
|
745 |
+ "loaded_legend_the_legend_wont_be_applied"));
|
746 |
} |
747 |
|
748 |
@Override
|
749 |
public int getPriority() { |
750 |
return 800; |
751 |
} |
752 |
|
753 |
@Override
|
754 |
public ILegend getLegend() {
|
755 |
return this.legend; |
756 |
} |
757 |
|
758 |
@Override
|
759 |
public void setLegend(ILegend legend) { |
760 |
applyLegend(legend); |
761 |
} |
762 |
|
763 |
@Override
|
764 |
public FLayer getLayer() {
|
765 |
return this.layer; |
766 |
} |
767 |
|
768 |
@Override
|
769 |
public void setLayer(FLayer layer) { |
770 |
this.layer = layer;
|
771 |
applyLegend(((Classifiable) layer).getLegend()); |
772 |
} |
773 |
|
774 |
@Override
|
775 |
public ILegendPanel getPage(Class<? extends ILegendPanel> pageClass) { |
776 |
return this.pages.get(pageClass); |
777 |
} |
778 |
|
779 |
@Override
|
780 |
public Collection<ILegendPanel> getPages() { |
781 |
return this.pages.asCollection(); |
782 |
} |
783 |
|
784 |
} |