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

History | View | Annotate | Download (26.5 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
        }
206
        return jPanelContainer;
207
    }
208

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

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

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

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

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

    
289
        return iconLabel;
290
    }
291

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

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

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

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

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

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

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

    
424
        MutableTreeNode pn = null;
425

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

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

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

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

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

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

    
469
        Class parent_class = page.getParentClass();
470

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

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

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

    
491

    
492
    }
493

    
494

    
495

    
496

    
497

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

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

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

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

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

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

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

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

    
571
    private void setIcon(ImageIcon icon) {
572
        getIconLabel().setIcon(icon);
573
    }
574

    
575
    private class MyTreeCellRenderer extends DefaultTreeCellRenderer {
576

    
577
        private static final long serialVersionUID = -6013698992263578041L;
578

    
579
        public MyTreeCellRenderer() {
580
        }
581

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

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

    
601
    }
602

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

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

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

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

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

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

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

    
650
    public void cancelAction() {
651
        // does nothing
652
    }
653

    
654
    public void applyAction() {
655
        legend = activePanel.getLegend();
656

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

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

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

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

    
682
                        if (aux2 instanceof FLyrVect) {
683

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

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

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

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

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

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

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

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

    
761
    @Override
762
    public int getPriority() {
763
            return 800;
764
    }
765

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

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

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

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

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

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