Statistics
| Revision:

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 @ 44888

History | View | Annotate | Download (26.6 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 void clear() {
113
          this.pages.clear();
114
        }
115
        
116
        @Override
117
        public Iterator<ILegendPanel> iterator() {
118
            return this.pages.values().iterator();
119
        }
120

    
121
        public boolean contains(Class<? extends ILegendPanel> pageClass) {
122
            return this.pages.containsKey(pageClass);
123
        }
124

    
125
        public ILegendPanel get(Class<? extends ILegendPanel> pageClass) {
126
            return this.pages.get(pageClass);
127
        }
128

    
129
        public Collection<ILegendPanel> asCollection() {
130
            return this.pages.values();
131
        }
132
    }
133

    
134
    private FLayer layer;
135
    private ILegend legend; 
136
    private final Pages pages = new Pages();
137
    
138
    private JPanel topPanel = null;
139
    private JTextArea titleArea = null;
140
    private JPanel preview = null;
141
    private JScrollPane jTitleScrollPane = null;
142
    private JTree jTreeLegends;
143
    private ILegendPanel activePanel;
144
    private JScrollPane legendTreeScrollPane;
145
//    private boolean dirtyTree_;
146
    private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
147
    private DefaultTreeModel treeModel;
148
    private JScrollPane jPanelContainer;
149
    private JPanel jCentralPanel;
150
    private JSplitPane jSplitPane;
151
    private boolean isTreeListenerDisabled;
152
    private JButton btnOptionalActions;
153
    private JPopupMenu menuOptionalActions;
154
    private Hashtable<FLayer, ILegend> table = null;
155
    private boolean empty = true;
156
    private JLabel iconLabel;
157

    
158
    public static String defaultLegendFolderPath;
159
    {
160
        FoldersManager folderManager = ToolsLocator.getFoldersManager();
161
        defaultLegendFolderPath = folderManager.getLastPath(
162
                "LegendsFolder", 
163
                folderManager.getHome()
164
        ).getAbsolutePath();
165
    }
166

    
167

    
168
    public LegendManager() {
169
        initialize();
170
    }
171

    
172
    private void initialize() {
173
        setLayout(new BorderLayout());
174
        add(getTopPanel(), BorderLayout.NORTH);
175
        add(getSplitPane(), BorderLayout.CENTER);
176
        setSize(500, 360);
177
        treeModel = new DefaultTreeModel(root);
178
    }
179

    
180
    private JSplitPane getSplitPane() {
181
        if (jSplitPane == null) {
182
            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
183
            JPanel aux = new JPanel(new BorderLayout(0, 5));
184
            aux.add(getLegendTreeScrollPane(), BorderLayout.CENTER);
185
            aux.add(getPreviewPanel(), BorderLayout.SOUTH);
186
            jSplitPane.setLeftComponent(aux);
187
            jSplitPane.setRightComponent(getCentralPanel());
188
            jSplitPane.setDividerLocation(150);
189
        }
190
        return jSplitPane;
191
    }
192

    
193
    private JPanel getCentralPanel() {
194
        if (jCentralPanel == null) {
195
            jCentralPanel = new JPanel(new BorderLayout(0, 10));
196
            jCentralPanel.add(getTitleScroll(), BorderLayout.NORTH);
197
            jCentralPanel.add(getJPanelContainer(), BorderLayout.CENTER);
198
        }
199
        return jCentralPanel;
200
    }
201

    
202
    private JScrollPane getJPanelContainer() {
203
        if (jPanelContainer == null) {
204
            jPanelContainer = new JScrollPane();
205
            jPanelContainer.setBorder(null);
206
        }
207
        return jPanelContainer;
208
    }
209

    
210
    /**
211
     * This method initializes jPanel
212
     *
213
     * @return javax.swing.JPanel
214
     */
215
    private JPanel getTopPanel() {
216
        if (topPanel == null) {
217
            topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
218
            topPanel.setPreferredSize(new Dimension(638, 31));
219
            topPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
220
                null, "",
221
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
222
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
223
            topPanel.add(getBtnOptionalActions(), null);
224
        }
225
        return topPanel;
226
    }
227

    
228
    private JButton getBtnOptionalActions() {
229
        if (btnOptionalActions == null) {
230
            btnOptionalActions =
231
                new JButton(PluginServices.getText(this, "_Mas_opciones")
232
                    + "...");
233
            btnOptionalActions.addActionListener(new ActionListener() {
234
                @Override
235
                public void actionPerformed(ActionEvent e) {
236
                    Point p = btnOptionalActions.getLocationOnScreen();
237
                    menuOptionalActions.show(asJComponent(),0,0);
238
                    menuOptionalActions.setLocation(p.x,p.y+btnOptionalActions.getHeight());
239
                }
240
            });
241
        }
242
        return btnOptionalActions;
243
    }
244

    
245
    /**
246
     * This method initializes jTextArea
247
     *
248
     * @return javax.swing.JTextArea
249
     */
250
    private JTextArea getTitleArea() {
251
        if (titleArea == null) {
252
            titleArea = new JTextArea();
253
            titleArea.setBackground(java.awt.SystemColor.control);
254
            titleArea.setLineWrap(true);
255
            titleArea.setRows(0);
256
            titleArea.setWrapStyleWord(false);
257
            titleArea.setEditable(false);
258
            titleArea.setPreferredSize(new java.awt.Dimension(495, 40));
259
        }
260
        return titleArea;
261
    }
262

    
263
    /**
264
     * This method initializes jPanel1
265
     *
266
     * @return javax.swing.JPanel
267
     */
268
    private JPanel getPreviewPanel() {
269
        if (preview == null) {
270
            preview = new JPanel();
271
            preview.setBorder(javax.swing.BorderFactory
272
                .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
273
            preview.setBackground(java.awt.SystemColor.text);
274
            preview.setLayout(new BorderLayout(5, 5));
275
            preview.add(getIconLabel());
276
            preview.setPreferredSize(new Dimension(getSplitPane()
277
                .getDividerLocation(), 130));
278
            preview.setBackground(Color.white);
279
        }
280
        return preview;
281
    }
282

    
283
    private JLabel getIconLabel() {
284
        if (iconLabel == null) {
285
            iconLabel = new JLabel();
286
            iconLabel.setVerticalAlignment(JLabel.CENTER);
287
            iconLabel.setHorizontalAlignment(JLabel.CENTER);
288
        }
289

    
290
        return iconLabel;
291
    }
292

    
293
    /**
294
     * This method initializes jScrollPane
295
     *
296
     * @return javax.swing.JScrollPane
297
     */
298
    private JScrollPane getLegendTreeScrollPane() {
299
        if (legendTreeScrollPane == null) {
300
            legendTreeScrollPane = new JScrollPane();
301
            legendTreeScrollPane.setViewportView(getJTreeLegends());
302
        }
303
        return legendTreeScrollPane;
304
    }
305

    
306
    /**
307
     * <p>
308
     * Adds a new fully-featured legend panel to the LegendManager.<br>
309
     * </p>
310
     *
311
     * <p>
312
     * <b>CAUTION:</b> Trying to add a child page whose parent hasn't been added
313
     * yet causes the application to fall in an infinite loop. This is a known
314
     * bug, sorry. Just avoid this case or try to fix it (lol).<br>
315
     * </p>
316
     *
317
   * @param iLegendPanelClass
318
     * @deprecated use SymbologySwingManager.registerLegendEditor
319
     */
320
    public static void addLegendPage(Class<? extends ILegendPanel> iLegendPanelClass) {
321
        SymbologySwingManager manager = SymbologySwingLocator.getSwingManager();
322
        manager.registerLegendEditor(iLegendPanelClass);
323
    }
324

    
325
    /**
326
     * Causes the component to be autofilled with the legend pages that
327
     * were added through the static method addLegendPage(ILegendPanel page)
328
     */
329
    private void fillDialog() {
330
        if (empty) {
331
            SymbologySwingManager manager = SymbologySwingLocator.getSwingManager();
332
            pages.clear();
333
            treeModel = new DefaultTreeModel(root);
334
            try {
335
              for (ILegendPanel page : manager.getLegendEditors(layer)) {
336
                  pages.add(page);
337
              }
338
            } catch(Throwable th) {
339
              logger.warn("Problems loading legend editors.",th);
340
            }
341
            for(ILegendPanel page : this.pages ) {
342
                try {
343
                  doInsertNode(treeModel, page);
344
                } catch(Throwable th) {
345
                  logger.warn("Problems adding legend editor ("+(page==null?"NULL":page.getTitle())+") to the GUI.",th);
346
                }
347
            }
348
            addOptionalActions();
349
            getJTreeLegends().setModel(treeModel);
350
            getJTreeLegends().repaint();
351
            empty = false;
352
        }
353
    }
354

    
355
    private void addOptionalActions() {
356
        this.menuOptionalActions = new JPopupMenu();
357
        Iterable<Action> actions = SymbologySwingLocator.getSwingManager().getOptionalActionOfLegendsPanel();
358
        for( Action action : actions ) {
359
            JMenuItemForOptionalAction item = new JMenuItemForOptionalAction(action);
360
            this.menuOptionalActions.add(item);
361
        }
362
    }
363
    
364
    private class JMenuItemForOptionalAction extends JMenuItem implements ActionListener {
365

    
366
        private static final long serialVersionUID = 1656264978338543368L;
367
        Action action;
368
        
369
        JMenuItemForOptionalAction(Action action) {
370
            this.action = action;
371
            this.configurePropertiesFromAction(action);
372
            this.addActionListener(this);
373
        }
374
        
375
        @Override
376
        public void actionPerformed(ActionEvent e) {
377
            e.setSource(LegendManager.this);
378
            this.action.actionPerformed(e);
379
        }
380
    }
381
    
382
    @SuppressWarnings("unchecked")
383
    private DefaultMutableTreeNode findNode(Class searchID) {
384
        String title;
385
        try {
386
            title =
387
                ((ILegendPanel) Class.forName(searchID.getName()).newInstance())
388
                    .getTitle();
389
        } catch (Exception e) {
390
            // this should be impossible, but anyway this case will be treat as
391
            // the node does not
392
            // exist.
393
            return null;
394
        }
395

    
396
        Enumeration e = root.breadthFirstEnumeration();
397
        while (e.hasMoreElements()) {
398
            DefaultMutableTreeNode nodeAux =
399
                (DefaultMutableTreeNode) e.nextElement();
400
            if (nodeAux != null) {
401
                ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject();
402
                if (legend == null)
403
                    continue; // Root node
404
                if (legend.getTitle().equals(title)) {
405
                    return nodeAux;
406
                }
407
            }
408
        }
409
        return null;
410
    }
411

    
412
    /**
413
     * If parent_node == null, add to root.
414
     * Returns added node
415
     *
416
     * @param tm
417
     * @param parent_node
418
     * @param item
419
     */
420
    private MutableTreeNode insertNodeHere(
421
        DefaultTreeModel tm,
422
        MutableTreeNode parent_node,
423
        ILegendPanel item) {
424

    
425
        MutableTreeNode pn = null;
426

    
427
        if (parent_node == null) {
428
            pn = root;
429
        } else {
430
            pn = parent_node;
431
        }
432

    
433
        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(item);
434
        int children = pn.getChildCount();
435
        int pos = 0;
436
        String pTitle = null;
437

    
438
        for (int i = 0; i < children; i++) {
439
            DefaultMutableTreeNode node =
440
                (DefaultMutableTreeNode) tm.getChild(pn, i);
441
            if (node.getUserObject() instanceof ILegendPanel) {
442
                pTitle = ((ILegendPanel) node.getUserObject()).getTitle();
443
                if (pTitle.compareTo(item.getTitle()) < 0) {
444
                    pos++;
445
                }
446
            }
447
        }
448
        tm.insertNodeInto(nodeValue, pn, pos);
449
        return nodeValue;
450
    }
451

    
452
    /**
453
     * Returns inserted node
454
     *
455
     * @param tm
456
     * @param page
457
     * @return
458
     */
459
    private MutableTreeNode doInsertNode(DefaultTreeModel tm, ILegendPanel page) {
460

    
461
        if (tm == null || page == null) {
462
            return null;
463
        }
464

    
465
        MutableTreeNode aux = findNode(page.getClass());
466
        if (aux != null) {
467
            return aux;
468
        }
469

    
470
        Class parent_class = page.getParentClass();
471

    
472
        if (parent_class != null) {
473
//            if (pages.containsKey(parent_class)) {
474
            if (pages.contains(parent_class)) {
475
                ILegendPanel parent = (ILegendPanel) pages.get(parent_class);
476

    
477
                aux = doInsertNode(tm, parent);
478
                if (aux != null) {
479
                    return insertNodeHere(tm, aux, page);
480
                } else {
481
                    return null;
482
                }
483

    
484
            } else {
485
                return null;
486
            }
487
        } else {
488
            // add to root
489
            return insertNodeHere(tm, null, page);
490
        }
491

    
492

    
493
    }
494

    
495

    
496

    
497

    
498

    
499
    private JScrollPane getTitleScroll() {
500
        if (jTitleScrollPane == null) {
501
            jTitleScrollPane = new JScrollPane();
502
            jTitleScrollPane.setBounds(2, 2, 498, 42);
503
            jTitleScrollPane.setViewportView(getTitleArea());
504
            jTitleScrollPane.setBorder(null);
505
        }
506
        return jTitleScrollPane;
507
    }
508

    
509
    private JTree getJTreeLegends() {
510
        if (jTreeLegends == null) {
511
            jTreeLegends = new JTree();
512
            jTreeLegends.setRootVisible(false);
513
            MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
514
            treeCellRenderer.setOpenIcon(null);
515
            treeCellRenderer.setClosedIcon(null);
516
            treeCellRenderer.setLeafIcon(null);
517

    
518
            jTreeLegends.setCellRenderer(treeCellRenderer);
519
            jTreeLegends.setShowsRootHandles(true);
520
            jTreeLegends
521
                .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
522

    
523
                    public void valueChanged(
524
                        javax.swing.event.TreeSelectionEvent e) {
525
                        if (isTreeListenerDisabled)
526
                            return;
527
                        DefaultMutableTreeNode node =
528
                            (DefaultMutableTreeNode) jTreeLegends
529
                                .getLastSelectedPathComponent();
530

    
531
                        if (node == null)
532
                            return;
533
                        setActivePage((ILegendPanel) node.getUserObject());
534
                    }
535
                });
536
            jTreeLegends.putClientProperty("JTree.linestyle", "Angled");
537
            jTreeLegends.getSelectionModel().setSelectionMode(
538
                TreeSelectionModel.SINGLE_TREE_SELECTION);
539
        }
540
        return jTreeLegends;
541
    }
542

    
543
    @Override
544
    public void setActivePage(ILegendPanel page) {
545
        if (page.getPanel() == null) {
546
            // this is what happens when the user clicked in a parent node
547
            // which only acts as a folder, and does not manage any legend
548
            // then it expands and selects the first child
549
            DefaultMutableTreeNode node = findNode(page.getClass());
550
            if (treeModel.getChildCount(node) > 0) {
551
                DefaultMutableTreeNode dmn =
552
                    (DefaultMutableTreeNode) treeModel.getChild(node, 0);
553
                page = (ILegendPanel) dmn.getUserObject();
554
                setActivePage(page);
555
                expandAndSelect(page);
556
            }
557
        } else {
558
            // show the page
559
            activePanel = page;
560
            setIcon(activePanel.getIcon());
561

    
562
            activePanel.setData(layer, legend);
563
            getTitleArea().setText(activePanel.getDescription());
564
            jPanelContainer.setViewportView(activePanel.getPanel());
565
        }
566
    }
567

    
568
    @Override
569
    public ILegendPanel getActivePage() {
570
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
571
    }
572

    
573
    private void setIcon(ImageIcon icon) {
574
        getIconLabel().setIcon(icon);
575
    }
576

    
577
    private class MyTreeCellRenderer extends DefaultTreeCellRenderer {
578

    
579
        private static final long serialVersionUID = -6013698992263578041L;
580

    
581
        public MyTreeCellRenderer() {
582
        }
583

    
584
        public Component getTreeCellRendererComponent(JTree tree, Object value,
585
            boolean sel, boolean expanded, boolean leaf, int row,
586
            boolean hasFocus) {
587

    
588
            super.getTreeCellRendererComponent(tree, value, sel, expanded,
589
                leaf, row, hasFocus);
590
            if (value instanceof DefaultMutableTreeNode) {
591
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
592
                if (node.getUserObject() instanceof ILegendPanel) {
593
                    ILegendPanel legend = (ILegendPanel) node.getUserObject();
594
                    this
595
                        .setText(legend.getPanel() == null ? "<html><b>"
596
                            + legend.getTitle() + "</b></html>" : legend
597
                            .getTitle());
598
                }
599
            }
600
            return this;
601
        }
602

    
603
    }
604

    
605
    private void expandAndSelect(Object node) {
606
        isTreeListenerDisabled = true;
607
        // will expand the tree and select the node
608
        int i = 0;
609
        boolean exit = false;
610

    
611
        TreePath tp = null;
612
        // find the page in the tree
613
        while (i < jTreeLegends.getRowCount() && !exit) {
614
            // see if this row is the node that we are looking for
615

    
616
            tp = jTreeLegends.getPathForRow(i);
617
            Object[] obj = tp.getPath();
618
            for (int j = 0; j < obj.length && !exit; j++) {
619
                Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject();
620

    
621
                if (o != null && o.getClass().equals(node.getClass())
622
                    && o.equals(node)) {
623
                    // found it! collapse the tree
624
                    while (i >= 0) {
625
                        jTreeLegends.collapseRow(i);
626
                        i--;
627
                    }
628
                    exit = true;
629
                }
630
            }
631
            jTreeLegends.expandRow(i);
632
            i++;
633
        }
634

    
635
        // expand the tree and set the selection
636
        if (tp != null) {
637
            jTreeLegends.expandPath(tp);
638
            jTreeLegends.setSelectionPath(tp);
639
        }
640
        isTreeListenerDisabled = false;
641
    }
642

    
643
    public String getName() {
644
        return PluginServices.getText(this, "Simbologia");
645
    }
646

    
647
    public void acceptAction() {
648
        applyAction();
649
        // automatically handled by the ThemeManagerWindow
650
    }
651

    
652
    public void cancelAction() {
653
        // does nothing
654
    }
655

    
656
    public void applyAction() {
657
        legend = activePanel.getLegend();
658

    
659
        if (table != null && table.size() > 1)
660
            applyRestOfLegends(table, layer.getMapContext().getLayers());
661

    
662
        /*
663
         * try to apply the legend to all the active layers that
664
         * can accept it
665
         */
666
        FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
667
        for (int i = 0; i < activeLyrs.length; i++) {
668
            FLayer laux = activeLyrs[i];
669

    
670
            if (activeLyrs[i] instanceof FLayers) {
671
                laux = getFirstActiveLayerVect((FLayers) activeLyrs[i]);
672
            }
673

    
674
            if (laux instanceof ClassifiableVectorial) {
675
                ClassifiableVectorial aux2 = (ClassifiableVectorial) laux;
676
                try {
677
                    if (legend instanceof IClassifiedVectorLegend) {
678
                        // Es una leyenda que necesita un recordset con un
679
                        // nombre de campo. Comprobamos que ese recordset
680
                        // tiene ese nombre de campo y es del tipo esperado
681
                        IClassifiedVectorLegend cl =
682
                            (IClassifiedVectorLegend) legend;
683

    
684
                        if (aux2 instanceof FLyrVect) {
685

    
686
                            if (cl.getValues().length == 0) {
687
                                JOptionPane.showMessageDialog(
688
                                    (Component) PluginServices.getMainFrame(),
689
                                    PluginServices.getText(this,
690
                                        "no_es_posible_aplicar_leyenda_vacia"));
691
                                return;
692
                            }
693

    
694
                            aux2.setLegend((IVectorLegend) legend);
695
                        }
696
                    } else
697
                        if (legend instanceof IVectorLegend) {
698
                            aux2.setLegend((IVectorLegend) legend);
699
                        }
700
                } catch (LegendLayerException e) {
701
                    NotificationManager.addError(PluginServices.getText(this,
702
                        "legend_exception"), e);
703
                }
704
            }
705
        }
706
    }
707

    
708
    private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2,
709
        FLayers layers) {
710

    
711
        for (int i = 0; i < layers.getLayersCount(); i++) {
712
            FLayer my_layer = layers.getLayer(i);
713

    
714
            if (!(my_layer instanceof FLayers)) {
715
                if (my_layer instanceof ClassifiableVectorial) {
716
                    try {
717
                        if (table.containsKey(my_layer)) {
718
                            ClassifiableVectorial lyr =
719
                                (ClassifiableVectorial) my_layer;
720
                            lyr.setLegend((IVectorLegend) table.get(my_layer));
721
                        }
722

    
723
                    } catch (LegendLayerException e) {
724
                        // TODO Auto-generated catch block
725
                        e.printStackTrace();
726
                    }
727
                }
728
            } else
729
                applyRestOfLegends(table, (FLayers) my_layer);
730
        }
731
    }
732

    
733
    @Override
734
    public void setModel(FLayer layer) {
735
        this.layer = layer;
736
        applyLegend(((Classifiable) layer).getLegend());
737
    }
738

    
739
    /**
740
     * Applies the legend to the layer.
741
     *
742
     * @param aLegend
743
     *            , legend that the user wants to apply
744
     */
745
    private void applyLegend(ILegend aLegend) {
746
        this.legend = aLegend;
747
        fillDialog();
748
        for(ILegendPanel page : this.pages ) {
749
//        Enumeration<Class<? extends ILegendPanel>> en = pages.keys();
750
//        while (en.hasMoreElements()) {
751
//            ILegendPanel page = (ILegendPanel) pages.get(en.nextElement());
752
            if (legend.getClass().equals(page.getLegendClass())) {
753
                setActivePage(page);
754
                expandAndSelect(page);
755
                return;
756
            }
757
        }
758
        NotificationManager.addWarning(PluginServices.getText(this,
759
            "caution_no_registered_panel_associated_to_"
760
                + "loaded_legend_the_legend_wont_be_applied"));
761
    }
762

    
763
    @Override
764
    public int getPriority() {
765
            return 800;
766
    }
767

    
768
    @Override
769
    public ILegend getLegend() {
770
        return this.legend;
771
    }
772

    
773
    @Override
774
    public void setLegend(ILegend legend) {
775
        applyLegend(legend);
776
    }
777

    
778
    @Override
779
    public FLayer getLayer() {
780
        return this.layer;
781
    }
782

    
783
    @Override
784
    public void setLayer(FLayer layer) {
785
        this.layer = layer;
786
        applyLegend(((Classifiable) layer).getLegend());
787
    }
788

    
789
    @Override
790
    public ILegendPanel getPage(Class<? extends ILegendPanel> pageClass) {
791
        return this.pages.get(pageClass);
792
    }
793

    
794
    @Override
795
    public Collection<ILegendPanel> getPages() {
796
        return this.pages.asCollection();
797
    }
798
    
799
}