Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / swing / dynobject / impl / DefaultLayersDynObjectSetComponent.java @ 38564

History | View | Annotate | Download (4.51 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.fmap.mapcontrol.swing.dynobject.impl;
23

    
24
import java.awt.BorderLayout;
25
import java.util.Map;
26

    
27
import javax.swing.JComponent;
28
import javax.swing.JList;
29
import javax.swing.JPanel;
30
import javax.swing.ListSelectionModel;
31
import javax.swing.event.ListSelectionEvent;
32
import javax.swing.event.ListSelectionListener;
33

    
34
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
35
import org.gvsig.tools.dynobject.DynObjectSet;
36
import org.gvsig.tools.exception.BaseException;
37
import org.gvsig.tools.swing.api.ToolsSwingLocator;
38
import org.gvsig.tools.swing.api.dynobject.set.JDynObjectSetComponent;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
/**
43
 * @author gvSIG Team
44
 * @version $Id$
45
 * 
46
 */
47
public class DefaultLayersDynObjectSetComponent extends JPanel implements
48
    LayersDynObjectSetComponent, ListSelectionListener {
49

    
50
    private static final long serialVersionUID = 5864674721657215264L;
51

    
52
    private static final Logger LOG = LoggerFactory
53
        .getLogger(DefaultLayersDynObjectSetComponent.class);
54

    
55
    private final LayersDynObjectSetComponentModel model;
56

    
57
    private JDynObjectSetComponent component;
58

    
59
    private JList layersList;
60

    
61
    private final boolean writable;
62

    
63
    /**
64
     * Creates a new {@link DefaultLayersDynObjectSetComponent} with the given
65
     * information for a list of layers.
66
     */
67
    public DefaultLayersDynObjectSetComponent(
68
        Map<String, DynObjectSet> layerName2InfoByPoint) {
69
        this(layerName2InfoByPoint, true);
70
    }
71

    
72
    /**
73
     * @param isDoubleBuffered
74
     */
75
    public DefaultLayersDynObjectSetComponent(
76
        Map<String, DynObjectSet> layerName2InfoByPoint,
77
 boolean writable) {
78
        super(new BorderLayout());
79
        this.writable = writable;
80
        model = new LayersDynObjectSetComponentModel(layerName2InfoByPoint);
81
        initializeUI();
82
    }
83

    
84
    private void initializeUI() {
85
        addLayerList();
86
    }
87

    
88
    private void addLayerList() {
89
        layersList = new JList(model);
90
        layersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
91
        layersList.setLayoutOrientation(JList.VERTICAL);
92
        // list.setVisibleRowCount(20);
93
        layersList.addListSelectionListener(this);
94

    
95
        add(layersList, BorderLayout.WEST);
96

    
97
        layersList.setSelectedIndex(0);
98
    }
99

    
100
    public void valueChanged(ListSelectionEvent e) {
101
        if (!e.getValueIsAdjusting()) {
102
            String layerName = (String) layersList.getSelectedValue();
103
            setCurrentLayerInfoByPoint(layerName);
104
        }
105
    }
106

    
107
    private void setCurrentLayerInfoByPoint(String layerName) {
108
        JDynObjectSetComponent newComponent = null;
109

    
110
        DynObjectSet dynObjectSet = model.getLayerInfoByPoint(layerName);
111
        try {
112
            newComponent =
113
                ToolsSwingLocator.getDynObjectSwingManager()
114
                    .createJDynObjectSetComponent(dynObjectSet, writable);
115
        } catch (BaseException e) {
116
            LOG.error("Error creating the JDynObjectSetComponent for "
117
                + "the DynObjectSet: " + dynObjectSet, e);
118
        }
119

    
120
        if (newComponent != null) {
121
            removeCurrentDynObjectSetComponent();
122
            component = newComponent;
123
            add(component.asJComponent(), BorderLayout.CENTER);
124
            revalidate();
125
            repaint();
126
        }
127
    }
128

    
129
    public JComponent asJComponent() {
130
        return this;
131
    }
132

    
133
    public void dispose() {
134
        removeCurrentDynObjectSetComponent();
135
        model.dispose();
136
    }
137

    
138
    private void removeCurrentDynObjectSetComponent() {
139
        if (component != null) {
140
            remove(component.asJComponent());
141
            component.dispose();
142
        }
143
    }
144

    
145
}