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

History | View | Annotate | Download (25.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.mapcontext.exceptions.LegendLayerException;
67
import org.gvsig.fmap.mapcontext.layers.FLayer;
68
import org.gvsig.fmap.mapcontext.layers.FLayers;
69
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable;
70
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
71
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
73
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
74
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
75
import org.gvsig.symbology.SymbologyLocator;
76
import org.gvsig.symbology.swing.SymbologySwingLocator;
77
import org.gvsig.symbology.swing.SymbologySwingManager;
78

    
79
/**
80
 * Implements the panel which allows the user to control all the information
81
 * about the
82
 * legends of a layer in order to improve the information that it offers to the
83
 * user.
84
 * There are options to create, save or load an existing legend.
85
 *
86
 * @author jaume dominguez faus - jaume.dominguez@iver.es
87
 */
88
public class LegendManager extends AbstractThemeManagerPage implements LegendsPanel {
89

    
90
    private static final Logger logger =
91
        LoggerFactory.getLogger(LegendManager.class);
92

    
93
    private static final long serialVersionUID = 7989057553773181019L;
94

    
95
    public static class Pages implements Iterable<ILegendPanel> {
96

    
97
        private Map<Class<? extends ILegendPanel>, ILegendPanel> pages;
98

    
99
        public Pages() {
100
            this.pages = new HashMap<>();
101
        }
102
        
103
        public void add(ILegendPanel page) {
104
            this.pages.put(page.getClass(), page);
105
        }
106

    
107
        public Iterator<ILegendPanel> iterator() {
108
            return this.pages.values().iterator();
109
        }
110

    
111
        public boolean contains(Class<? extends ILegendPanel> pageClass) {
112
            return this.pages.containsKey(pageClass);
113
        }
114

    
115
        public ILegendPanel get(Class<? extends ILegendPanel> pageClass) {
116
            return this.pages.get(pageClass);
117
        }
118

    
119
        public Collection<ILegendPanel> asCollection() {
120
            return this.pages.values();
121
        }
122
    }
123

    
124
    private FLayer layer;
125
    private ILegend legend; 
126
    private Pages pages = new Pages();
127
    
128
    private JPanel topPanel = null;
129
    private JTextArea titleArea = null;
130
    private JPanel preview = null;
131
    private JScrollPane jTitleScrollPane = null;
132
    private JTree jTreeLegends;
133
    private ILegendPanel activePanel;
134
    private JScrollPane legendTreeScrollPane;
135
//    private boolean dirtyTree_;
136
    private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
137
    private DefaultTreeModel treeModel;
138
    private JScrollPane jPanelContainer;
139
    private JPanel jCentralPanel;
140
    private JSplitPane jSplitPane;
141
    private boolean isTreeListenerDisabled;
142
    private JButton btnOptionalActions;
143
    private JPopupMenu menuOptionalActions;
144
    private Hashtable<FLayer, ILegend> table = null;
145
    private boolean empty = true;
146
    private JLabel iconLabel;
147

    
148
    public static String defaultLegendFolderPath;
149
    {
150
        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
151
        defaultLegendFolderPath = prefs.get("LegendsFolder", "");
152
    }
153

    
154

    
155
    public LegendManager() {
156
        initialize();
157
    }
158

    
159
    private void initialize() {
160
        setLayout(new BorderLayout());
161
        add(getTopPanel(), BorderLayout.NORTH);
162
        add(getSplitPane(), BorderLayout.CENTER);
163
        setSize(500, 360);
164
        treeModel = new DefaultTreeModel(root);
165
    }
166

    
167
    private JSplitPane getSplitPane() {
168
        if (jSplitPane == null) {
169
            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
170
            JPanel aux = new JPanel(new BorderLayout(0, 5));
171
            aux.add(getLegendTreeScrollPane(), BorderLayout.CENTER);
172
            aux.add(getPreviewPanel(), BorderLayout.SOUTH);
173
            jSplitPane.setLeftComponent(aux);
174
            jSplitPane.setRightComponent(getCentralPanel());
175
            jSplitPane.setDividerLocation(150);
176
        }
177
        return jSplitPane;
178
    }
179

    
180
    private JPanel getCentralPanel() {
181
        if (jCentralPanel == null) {
182
            jCentralPanel = new JPanel(new BorderLayout(0, 10));
183
            jCentralPanel.add(getTitleScroll(), BorderLayout.NORTH);
184
            jCentralPanel.add(getJPanelContainer(), BorderLayout.CENTER);
185
        }
186
        return jCentralPanel;
187
    }
188

    
189
    private JScrollPane getJPanelContainer() {
190
        if (jPanelContainer == null) {
191
            jPanelContainer = new JScrollPane();
192
        }
193
        return jPanelContainer;
194
    }
195

    
196
    /**
197
     * This method initializes jPanel
198
     *
199
     * @return javax.swing.JPanel
200
     */
201
    private JPanel getTopPanel() {
202
        if (topPanel == null) {
203
            topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
204
            topPanel.setPreferredSize(new Dimension(638, 31));
205
            topPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
206
                null, "",
207
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
208
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
209
            topPanel.add(getBtnOptionalActions(), null);
210
        }
211
        return topPanel;
212
    }
213

    
214
    private JButton getBtnOptionalActions() {
215
        if (btnOptionalActions == null) {
216
            btnOptionalActions =
217
                new JButton(PluginServices.getText(this, "_Mas_opciones")
218
                    + "...");
219
            btnOptionalActions.addActionListener(new ActionListener() {
220
                @Override
221
                public void actionPerformed(ActionEvent e) {
222
                    Point p = btnOptionalActions.getLocationOnScreen();
223
                    menuOptionalActions.show(asJComponent(),0,0);
224
                    menuOptionalActions.setLocation(p.x,p.y+btnOptionalActions.getHeight());
225
                }
226
            });
227
        }
228
        return btnOptionalActions;
229
    }
230

    
231
    /**
232
     * This method initializes jTextArea
233
     *
234
     * @return javax.swing.JTextArea
235
     */
236
    private JTextArea getTitleArea() {
237
        if (titleArea == null) {
238
            titleArea = new JTextArea();
239
            titleArea.setBackground(java.awt.SystemColor.control);
240
            titleArea.setLineWrap(true);
241
            titleArea.setRows(0);
242
            titleArea.setWrapStyleWord(false);
243
            titleArea.setEditable(false);
244
            titleArea.setPreferredSize(new java.awt.Dimension(495, 40));
245
        }
246
        return titleArea;
247
    }
248

    
249
    /**
250
     * This method initializes jPanel1
251
     *
252
     * @return javax.swing.JPanel
253
     */
254
    private JPanel getPreviewPanel() {
255
        if (preview == null) {
256
            preview = new JPanel();
257
            preview.setBorder(javax.swing.BorderFactory
258
                .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
259
            preview.setBackground(java.awt.SystemColor.text);
260
            preview.setLayout(new BorderLayout(5, 5));
261
            preview.add(getIconLabel());
262
            preview.setPreferredSize(new Dimension(getSplitPane()
263
                .getDividerLocation(), 130));
264
            preview.setBackground(Color.white);
265
        }
266
        return preview;
267
    }
268

    
269
    private JLabel getIconLabel() {
270
        if (iconLabel == null) {
271
            iconLabel = new JLabel();
272
            iconLabel.setVerticalAlignment(JLabel.CENTER);
273
            iconLabel.setHorizontalAlignment(JLabel.CENTER);
274
        }
275

    
276
        return iconLabel;
277
    }
278

    
279
    /**
280
     * This method initializes jScrollPane
281
     *
282
     * @return javax.swing.JScrollPane
283
     */
284
    private JScrollPane getLegendTreeScrollPane() {
285
        if (legendTreeScrollPane == null) {
286
            legendTreeScrollPane = new JScrollPane();
287
            legendTreeScrollPane.setViewportView(getJTreeLegends());
288
        }
289
        return legendTreeScrollPane;
290
    }
291

    
292
    /**
293
     * <p>
294
     * Adds a new fully-featured legend panel to the LegendManager.<br>
295
     * </p>
296
     *
297
     * <p>
298
     * <b>CAUTION:</b> Trying to add a child page whose parent hasn't been added
299
     * yet causes the application to fall in an infinite loop. This is a known
300
     * bug, sorry. Just avoid this case or try to fix it (lol).<br>
301
     * </p>
302
     *
303
     * @deprecated use SymbologySwingManager.registerLegendEditor
304
     */
305
    public static void addLegendPage(Class<? extends ILegendPanel> iLegendPanelClass) {
306
        SymbologySwingManager manager = SymbologySwingLocator.getSwingManager();
307
        manager.registerLegendEditor(iLegendPanelClass);
308
    }
309

    
310
    /**
311
     * Causes the component to be autofilled with the legend pages that
312
     * were added through the static method addLegendPage(ILegendPanel page)
313
     */
314
    private void fillDialog() {
315
        if (empty) {
316
            SymbologySwingManager manager = SymbologySwingLocator.getSwingManager();
317

    
318
            Iterator<ILegendPanel> it = manager.getLegendEditors(layer).iterator();
319
            while( it.hasNext() ) {
320
                ILegendPanel page = it.next();
321
                pages.add(page);
322
            }
323
            addOptionalActions();
324
            for(ILegendPanel page : this.pages ) {
325
                doInsertNode(treeModel, page);
326
            }
327
            getJTreeLegends().setModel(treeModel);
328
            getJTreeLegends().repaint();
329
            empty = false;
330
        }
331
    }
332

    
333
    private void addOptionalActions() {
334
        this.menuOptionalActions = new JPopupMenu();
335
        Iterable<Action> actions = SymbologySwingLocator.getSwingManager().getOptionalActionOfLegendsPanel();
336
        for( Action action : actions ) {
337
            JMenuItemForOptionalAction item = new JMenuItemForOptionalAction(action);
338
            this.menuOptionalActions.add(item);
339
        }
340
    }
341
    
342
    private class JMenuItemForOptionalAction extends JMenuItem implements ActionListener {
343

    
344
        private static final long serialVersionUID = 1656264978338543368L;
345
        Action action;
346
        
347
        JMenuItemForOptionalAction(Action action) {
348
            this.action = action;
349
            this.configurePropertiesFromAction(action);
350
            this.addActionListener(this);
351
        }
352
        
353
        @Override
354
        public void actionPerformed(ActionEvent e) {
355
            e.setSource(LegendManager.this);
356
            this.action.actionPerformed(e);
357
        }
358
    }
359
    
360
    @SuppressWarnings("unchecked")
361
    private DefaultMutableTreeNode findNode(Class searchID) {
362
        String title;
363
        try {
364
            title =
365
                ((ILegendPanel) Class.forName(searchID.getName()).newInstance())
366
                    .getTitle();
367
        } catch (Exception e) {
368
            // this should be impossible, but anyway this case will be treat as
369
            // the node does not
370
            // exist.
371
            return null;
372
        }
373

    
374
        Enumeration e = root.breadthFirstEnumeration();
375
        while (e.hasMoreElements()) {
376
            DefaultMutableTreeNode nodeAux =
377
                (DefaultMutableTreeNode) e.nextElement();
378
            if (nodeAux != null) {
379
                ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject();
380
                if (legend == null)
381
                    continue; // Root node
382
                if (legend.getTitle().equals(title)) {
383
                    return nodeAux;
384
                }
385
            }
386
        }
387
        return null;
388
    }
389

    
390
    /**
391
     * If parent_node == null, add to root.
392
     * Returns added node
393
     *
394
     * @param tm
395
     * @param parent_node
396
     * @param item
397
     */
398
    private MutableTreeNode insertNodeHere(
399
        DefaultTreeModel tm,
400
        MutableTreeNode parent_node,
401
        ILegendPanel item) {
402

    
403
        MutableTreeNode pn = null;
404

    
405
        if (parent_node == null) {
406
            pn = root;
407
        } else {
408
            pn = parent_node;
409
        }
410

    
411
        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(item);
412
        int children = pn.getChildCount();
413
        int pos = 0;
414
        String pTitle = null;
415

    
416
        for (int i = 0; i < children; i++) {
417
            DefaultMutableTreeNode node =
418
                (DefaultMutableTreeNode) tm.getChild(pn, i);
419
            if (node.getUserObject() instanceof ILegendPanel) {
420
                pTitle = ((ILegendPanel) node.getUserObject()).getTitle();
421
                if (pTitle.compareTo(item.getTitle()) < 0) {
422
                    pos++;
423
                }
424
            }
425
        }
426
        tm.insertNodeInto(nodeValue, pn, pos);
427
        return nodeValue;
428
    }
429

    
430
    /**
431
     * Returns inserted node
432
     *
433
     * @param tm
434
     * @param page
435
     * @return
436
     */
437
    private MutableTreeNode doInsertNode(DefaultTreeModel tm, ILegendPanel page) {
438

    
439
        if (tm == null || page == null) {
440
            return null;
441
        }
442

    
443
        MutableTreeNode aux = findNode(page.getClass());
444
        if (aux != null) {
445
            return aux;
446
        }
447

    
448
        Class parent_class = page.getParentClass();
449

    
450
        if (parent_class != null) {
451
//            if (pages.containsKey(parent_class)) {
452
            if (pages.contains(parent_class)) {
453
                ILegendPanel parent = (ILegendPanel) pages.get(parent_class);
454

    
455
                aux = doInsertNode(tm, parent);
456
                if (aux != null) {
457
                    return insertNodeHere(tm, aux, page);
458
                } else {
459
                    return null;
460
                }
461

    
462
            } else {
463
                return null;
464
            }
465
        } else {
466
            // add to root
467
            return insertNodeHere(tm, null, page);
468
        }
469

    
470

    
471
    }
472

    
473

    
474

    
475

    
476

    
477
    private JScrollPane getTitleScroll() {
478
        if (jTitleScrollPane == null) {
479
            jTitleScrollPane = new JScrollPane();
480
            jTitleScrollPane.setBounds(2, 2, 498, 42);
481
            jTitleScrollPane.setViewportView(getTitleArea());
482
        }
483
        return jTitleScrollPane;
484
    }
485

    
486
    private JTree getJTreeLegends() {
487
        if (jTreeLegends == null) {
488
            jTreeLegends = new JTree();
489
            jTreeLegends.setRootVisible(false);
490
            MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
491
            treeCellRenderer.setOpenIcon(null);
492
            treeCellRenderer.setClosedIcon(null);
493
            treeCellRenderer.setLeafIcon(null);
494

    
495
            jTreeLegends.setCellRenderer(treeCellRenderer);
496
            jTreeLegends.setShowsRootHandles(true);
497
            jTreeLegends
498
                .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
499

    
500
                    public void valueChanged(
501
                        javax.swing.event.TreeSelectionEvent e) {
502
                        if (isTreeListenerDisabled)
503
                            return;
504
                        DefaultMutableTreeNode node =
505
                            (DefaultMutableTreeNode) jTreeLegends
506
                                .getLastSelectedPathComponent();
507

    
508
                        if (node == null)
509
                            return;
510
                        setActivePage((ILegendPanel) node.getUserObject());
511
                    }
512
                });
513
            jTreeLegends.putClientProperty("JTree.linestyle", "Angled");
514
            jTreeLegends.getSelectionModel().setSelectionMode(
515
                TreeSelectionModel.SINGLE_TREE_SELECTION);
516
        }
517
        return jTreeLegends;
518
    }
519

    
520
    @Override
521
    public void setActivePage(ILegendPanel page) {
522
        if (page.getPanel() == null) {
523
            // this is what happens when the user clicked in a parent node
524
            // which only acts as a folder, and does not manage any legend
525
            // then it expands and selects the first child
526
            DefaultMutableTreeNode node = findNode(page.getClass());
527
            if (treeModel.getChildCount(node) > 0) {
528
                DefaultMutableTreeNode dmn =
529
                    (DefaultMutableTreeNode) treeModel.getChild(node, 0);
530
                page = (ILegendPanel) dmn.getUserObject();
531
                setActivePage(page);
532
                expandAndSelect(page);
533
            }
534
        } else {
535
            // show the page
536
            activePanel = page;
537
            setIcon(activePanel.getIcon());
538

    
539
            activePanel.setData(layer, legend);
540
            getTitleArea().setText(activePanel.getDescription());
541
            jPanelContainer.setViewportView(activePanel.getPanel());
542
        }
543
    }
544

    
545
    @Override
546
    public ILegendPanel getActivePage() {
547
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
548
    }
549

    
550
    private void setIcon(ImageIcon icon) {
551
        getIconLabel().setIcon(icon);
552
    }
553

    
554
    private class MyTreeCellRenderer extends DefaultTreeCellRenderer {
555

    
556
        private static final long serialVersionUID = -6013698992263578041L;
557

    
558
        public MyTreeCellRenderer() {
559
        }
560

    
561
        public Component getTreeCellRendererComponent(JTree tree, Object value,
562
            boolean sel, boolean expanded, boolean leaf, int row,
563
            boolean hasFocus) {
564

    
565
            super.getTreeCellRendererComponent(tree, value, sel, expanded,
566
                leaf, row, hasFocus);
567
            if (value instanceof DefaultMutableTreeNode) {
568
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
569
                if (node.getUserObject() instanceof ILegendPanel) {
570
                    ILegendPanel legend = (ILegendPanel) node.getUserObject();
571
                    this
572
                        .setText(legend.getPanel() == null ? "<html><b>"
573
                            + legend.getTitle() + "</b></html>" : legend
574
                            .getTitle());
575
                }
576
            }
577
            return this;
578
        }
579

    
580
    }
581

    
582
    private void expandAndSelect(Object node) {
583
        isTreeListenerDisabled = true;
584
        // will expand the tree and select the node
585
        int i = 0;
586
        boolean exit = false;
587

    
588
        TreePath tp = null;
589
        // find the page in the tree
590
        while (i < jTreeLegends.getRowCount() && !exit) {
591
            // see if this row is the node that we are looking for
592

    
593
            tp = jTreeLegends.getPathForRow(i);
594
            Object[] obj = tp.getPath();
595
            for (int j = 0; j < obj.length && !exit; j++) {
596
                Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject();
597

    
598
                if (o != null && o.getClass().equals(node.getClass())
599
                    && o.equals(node)) {
600
                    // found it! collapse the tree
601
                    while (i >= 0) {
602
                        jTreeLegends.collapseRow(i);
603
                        i--;
604
                    }
605
                    exit = true;
606
                }
607
            }
608
            jTreeLegends.expandRow(i);
609
            i++;
610
        }
611

    
612
        // expand the tree and set the selection
613
        if (tp != null) {
614
            jTreeLegends.expandPath(tp);
615
            jTreeLegends.setSelectionPath(tp);
616
        }
617
        isTreeListenerDisabled = false;
618
    }
619

    
620
    public String getName() {
621
        return PluginServices.getText(this, "Simbologia");
622
    }
623

    
624
    public void acceptAction() {
625
        applyAction();
626
        // automatically handled by the ThemeManagerWindow
627
    }
628

    
629
    public void cancelAction() {
630
        // does nothing
631
    }
632

    
633
    public void applyAction() {
634
        legend = activePanel.getLegend();
635

    
636
        if (table != null && table.size() > 1)
637
            applyRestOfLegends(table, layer.getMapContext().getLayers());
638

    
639
        /*
640
         * try to apply the legend to all the active layers that
641
         * can accept it
642
         */
643
        FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
644
        for (int i = 0; i < activeLyrs.length; i++) {
645
            FLayer laux = activeLyrs[i];
646

    
647
            if (activeLyrs[i] instanceof FLayers) {
648
                laux = getFirstActiveLayerVect((FLayers) activeLyrs[i]);
649
            }
650

    
651
            if (laux instanceof ClassifiableVectorial) {
652
                ClassifiableVectorial aux2 = (ClassifiableVectorial) laux;
653
                try {
654
                    if (legend instanceof IClassifiedVectorLegend) {
655
                        // Es una leyenda que necesita un recordset con un
656
                        // nombre de campo. Comprobamos que ese recordset
657
                        // tiene ese nombre de campo y es del tipo esperado
658
                        IClassifiedVectorLegend cl =
659
                            (IClassifiedVectorLegend) legend;
660

    
661
                        if (aux2 instanceof FLyrVect) {
662

    
663
                            if (cl.getValues().length == 0) {
664
                                JOptionPane.showMessageDialog(
665
                                    (Component) PluginServices.getMainFrame(),
666
                                    PluginServices.getText(this,
667
                                        "no_es_posible_aplicar_leyenda_vacia"));
668
                                return;
669
                            }
670

    
671
                            aux2.setLegend((IVectorLegend) legend);
672
                        }
673
                    } else
674
                        if (legend instanceof IVectorLegend) {
675
                            aux2.setLegend((IVectorLegend) legend);
676
                        }
677
                } catch (LegendLayerException e) {
678
                    NotificationManager.addError(PluginServices.getText(this,
679
                        "legend_exception"), e);
680
                }
681
            }
682
        }
683
    }
684

    
685
    private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2,
686
        FLayers layers) {
687

    
688
        for (int i = 0; i < layers.getLayersCount(); i++) {
689
            FLayer my_layer = layers.getLayer(i);
690

    
691
            if (!(my_layer instanceof FLayers)) {
692
                if (my_layer instanceof ClassifiableVectorial) {
693
                    try {
694
                        if (table.containsKey(my_layer)) {
695
                            ClassifiableVectorial lyr =
696
                                (ClassifiableVectorial) my_layer;
697
                            lyr.setLegend((IVectorLegend) table.get(my_layer));
698
                        }
699

    
700
                    } catch (LegendLayerException e) {
701
                        // TODO Auto-generated catch block
702
                        e.printStackTrace();
703
                    }
704
                }
705
            } else
706
                applyRestOfLegends(table, (FLayers) my_layer);
707
        }
708
    }
709

    
710
    @Override
711
    public void setModel(FLayer layer) {
712
        this.layer = layer;
713
        applyLegend(((Classifiable) layer).getLegend());
714
    }
715

    
716
    /**
717
     * Applies the legend to the layer.
718
     *
719
     * @param aLegend
720
     *            , legend that the user wants to apply
721
     */
722
    private void applyLegend(ILegend aLegend) {
723
        this.legend = aLegend;
724
        fillDialog();
725
        for(ILegendPanel page : this.pages ) {
726
//        Enumeration<Class<? extends ILegendPanel>> en = pages.keys();
727
//        while (en.hasMoreElements()) {
728
//            ILegendPanel page = (ILegendPanel) pages.get(en.nextElement());
729
            if (legend.getClass().equals(page.getLegendClass())) {
730
                setActivePage(page);
731
                expandAndSelect(page);
732
                return;
733
            }
734
        }
735
        NotificationManager.addWarning(PluginServices.getText(this,
736
            "caution_no_registered_panel_associated_to_"
737
                + "loaded_legend_the_legend_wont_be_applied"));
738
    }
739

    
740
    @Override
741
    public int getPriority() {
742
            return 800;
743
    }
744

    
745
    @Override
746
    public ILegend getLegend() {
747
        return this.legend;
748
    }
749

    
750
    @Override
751
    public void setLegend(ILegend legend) {
752
        applyLegend(legend);
753
    }
754

    
755
    @Override
756
    public FLayer getLayer() {
757
        return this.layer;
758
    }
759

    
760
    @Override
761
    public void setLayer(FLayer layer) {
762
        this.layer = layer;
763
        applyLegend(((Classifiable) layer).getLegend());
764
    }
765

    
766
    @Override
767
    public ILegendPanel getPage(Class<? extends ILegendPanel> pageClass) {
768
        return this.pages.get(pageClass);
769
    }
770

    
771
    @Override
772
    public Collection<ILegendPanel> getPages() {
773
        return this.pages.asCollection();
774
    }
775
    
776
}