Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / dal / feature / swing / FeatureTypesTablePanel.java @ 44259

History | View | Annotate | Download (9.94 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.fmap.mapcontrol.dal.feature.swing;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Dimension;
28
import javax.swing.DefaultListModel;
29
import javax.swing.JComponent;
30

    
31
import javax.swing.JPanel;
32
import javax.swing.JSplitPane;
33
import javax.swing.JList;
34
import javax.swing.event.ListSelectionEvent;
35
import javax.swing.event.ListSelectionListener;
36
import org.gvsig.fmap.dal.DataStore;
37

    
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureQuery;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.ConfigurableFeatureTableModel;
44
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureStoreModel;
45
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTypeList;
46
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.SelectedFeatureTypeChangeListener;
47
import org.gvsig.tools.exception.BaseException;
48
import org.gvsig.tools.swing.api.ListElement;
49
import org.gvsig.tools.util.UnmodifiableBasicMap;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
/**
54
 * Panel to show a table of Features. It is able to take into account the
55
 * availability of many FeatureTypes, and if it is the case, allows the user to
56
 * select the {@link FeatureType} of the {@link Feature}s to show.
57
 * 
58
 * @author 2005- Vicente Caballero
59
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
60
 */
61
public class FeatureTypesTablePanel extends JPanel implements
62
    SelectedFeatureTypeChangeListener {
63

    
64
    private static final long serialVersionUID = 5857146295213412304L;
65
    private static final Logger LOG = LoggerFactory
66
        .getLogger(FeatureTypesTablePanel.class);
67
    private FeatureTablePanel tablePanel;
68
    private FeatureTypeList typesControl;
69
    private JSplitPane jSplitPane = null;
70
    private FeatureStoreModel model;
71

    
72
    /**
73
     * Constructs a Panel to show a table with the features of a FeatureStore.
74
     * 
75
     * @param featureStore
76
     *            to extract the features from
77
     * @throws BaseException
78
     *             if there is an error reading data from the FeatureStore
79
     */
80
    public FeatureTypesTablePanel(FeatureStore featureStore)
81
        throws BaseException {
82
        this(featureStore, true);
83
    }
84

    
85
    /**
86
     * Constructs a Panel to show a table with the features of a FeatureStore.
87
     * 
88
     * @param featureStore
89
     *            to extract the features from
90
     * @param isDoubleBuffered
91
     *            a boolean, true for double-buffering, which uses additional
92
     *            memory space to achieve fast, flicker-free updates
93
     * @throws BaseException
94
     *             if there is an error reading data from the FeatureStore
95
     */
96
    public FeatureTypesTablePanel(FeatureStore featureStore,
97
        boolean isDoubleBuffered) throws BaseException {
98
        this(featureStore, null, isDoubleBuffered);
99
    }
100

    
101
    /**
102
     * @throws BaseException
103
     * 
104
     */
105
    public FeatureTypesTablePanel(FeatureStore featureStore,
106
        FeatureQuery featureQuery) throws BaseException {
107
        this(featureStore, featureQuery, true);
108
    }
109

    
110
    /**
111
     * @param isDoubleBuffered
112
     * @throws BaseException
113
     */
114
    public FeatureTypesTablePanel(FeatureStore featureStore,
115
        FeatureQuery featureQuery, boolean isDoubleBuffered)
116
        throws BaseException {
117
        this(new FeatureStoreModel(featureStore, featureQuery),
118
            isDoubleBuffered);
119
    }
120

    
121
    public FeatureTypesTablePanel(FeatureStoreModel model) throws DataException {
122
        this(model, true);
123
    }
124

    
125
    public FeatureTypesTablePanel(FeatureStoreModel model,
126
        boolean isDoubleBuffered) throws DataException {
127
        super(isDoubleBuffered);
128
        this.model = model;
129
        typesControl =
130
            new FeatureTypeList(model.getFeatureStore(),
131
                model.getFeatureQuery());
132
        this.initializeUI();
133
        // Add a listener to change the selection
134
        typesControl.addSelectedFeatureTypeChangeListener(this);
135
    }
136

    
137
    private boolean hasManyFeatureTypes() throws DataException {
138
        UnmodifiableBasicMap<String, DataStore> children = this.model.getFeatureStore().getChildren();
139
        if( !children.isEmpty() ) {
140
            return true;
141
        }
142
        return typesControl.getFeatureTypesSize() > 1;
143
    }
144

    
145
    /**
146
     * @return
147
     * @see org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTablePanel#createConfigurationPanel()
148
     */
149
    public JPanel createConfigurationPanel() {
150
        return tablePanel.createConfigurationPanel();
151
    }
152

    
153
    @Override
154
    public void change(FeatureStore featureStore, FeatureType featureType) {
155
        if (featureType != null) {
156
            showFeatureTypeData(featureType);
157
        }
158
    }
159

    
160
    private void showFeatureTypeData(FeatureType featureType) {
161
        showFeatureTypeData(featureType.getId());
162
    }
163

    
164
    private void showFeatureTypeData(String typeId) {
165
        this.model.setCurrentFeatureTypeId(typeId);
166
        try {
167
            tablePanel =
168
                new FeatureTablePanel(this.model.getCurrentFeatureTableModel());
169
        } catch (BaseException e) {
170
            throw new RuntimeException(
171
                "Error creating a new FeatureTablePanel to show "
172
                    + "the selected FeatureType with id: " + typeId, e);
173
        }
174
        getJSplitPane().setRightComponent(tablePanel);
175
    }
176

    
177
    public FeatureTablePanel getTablePanel() {
178
        return tablePanel;
179
    }
180

    
181
    public FeatureTypeList getTypesControl() {
182
        return typesControl;
183
    }
184

    
185
    /**
186
     * Sets that the selected Features to be viewed first.
187
     */
188
    public void setSelectionUp(boolean selectionUp) {
189
        getTablePanel().setSelectionUp(selectionUp);
190
    }
191

    
192
    /**
193
     * Returns the internal Table Model for the Features of the FeatureStore.
194
     * 
195
     * @return the internal Table Model
196
     */
197
    protected ConfigurableFeatureTableModel getTableModel() {
198
        return getTablePanel().getTableModel();
199
    }
200

    
201
    /**
202
     * This method initializes jPanel
203
     * 
204
     * @return javax.swing.JPanel
205
     */
206
    private void initializeUI() {
207
        this.setLayout(new BorderLayout());
208
        this.setSize(new Dimension(331, 251));
209
        this.add(getJSplitPane(), BorderLayout.CENTER);
210
        showFeatureTypeData(model.getCurrentFeatureTypeId());
211
    }
212

    
213
    /**
214
     * This method initializes jSplitPane
215
     * 
216
     * @return javax.swing.JSplitPane
217
     */
218
    private JSplitPane getJSplitPane() {
219
        if (jSplitPane == null) {
220
            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
221
            JSplitPane split2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
222
            split2.setBottomComponent(getTypesControl());
223
            split2.setTopComponent(getChildControl());
224
            split2.setDividerLocation(0.2d);
225
            jSplitPane.setLeftComponent(split2);
226
            jSplitPane.setOneTouchExpandable(true);
227
            try {
228
                if (hasManyFeatureTypes()) {
229
                    jSplitPane.setDividerLocation(0.2d);
230
                } else {
231
                    jSplitPane.setDividerLocation(0.0d);
232
                }
233
            } catch (DataException ex) {
234
                LOG.error("Error getting the number of feature types", ex);
235
            }
236
        }
237
        return jSplitPane;
238
    }
239

    
240
    private JComponent getChildControl() {
241
        final JList l = new JList();
242
        DefaultListModel model = new DefaultListModel();
243
        FeatureStore store = this.model.getFeatureStore();
244
        model.addElement(new ListElement<>(store.getName(),store));
245
        for (String childName : store.getChildren().keySet() ) {
246
            model.addElement(new ListElement<>(childName,store.getChildren().get(childName)));
247
        }
248
        l.setModel(model);
249
        l.addListSelectionListener(new ListSelectionListener() {
250
            @Override
251
            public void valueChanged(ListSelectionEvent e) {
252
                if( e.getValueIsAdjusting() ) {
253
                    return;
254
                }
255
                FeatureStore s = (FeatureStore) ListElement.getSelected(l);
256
                if( s!=null ) {
257
                    doSelectStore(s);
258
                }
259
            }
260

    
261
        });
262
        return l;
263
    }
264

    
265
    private void doSelectStore(FeatureStore featureStore) {
266
        try {
267
            this.model = new FeatureStoreModel(
268
                    featureStore, 
269
                    featureStore.createFeatureQuery()
270
            );
271
            tablePanel =
272
                new FeatureTablePanel(this.model.getCurrentFeatureTableModel());
273
            getJSplitPane().setRightComponent(tablePanel);
274
        } catch (BaseException e) {
275
            throw new RuntimeException(
276
                "Error creating a new FeatureTablePanel to show store '"+featureStore.getName()+"'.",e);
277
        }
278
    }
279
}