Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / FeatureTypesTablePanel.java @ 35437

History | View | Annotate | Download (8.66 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.dal.feature.swing;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Dimension;
26

    
27
import javax.swing.JLabel;
28
import javax.swing.JPanel;
29
import javax.swing.JSplitPane;
30
import javax.swing.event.ListSelectionEvent;
31
import javax.swing.event.TableModelEvent;
32

    
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.feature.Feature;
38
import org.gvsig.fmap.dal.feature.FeatureQuery;
39
import org.gvsig.fmap.dal.feature.FeatureSelection;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.ConfigurableFeatureTableModel;
43
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTypeChangeListener;
44
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTypesControl;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.tools.exception.BaseException;
47

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

    
59
        private static final long serialVersionUID = 5857146295213412304L;
60
        private static final Logger LOG =
61
                        LoggerFactory.getLogger(FeatureTypesTablePanel.class);
62
        private FeatureTablePanel tablePanel;
63
        private FeatureTypesControl typesControl;
64
        private JSplitPane jSplitPane = null;
65
        private JLabel jLabel;
66
        private JPanel jPanel;
67

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

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

    
97
    /**
98
     * @throws BaseException
99
     * 
100
     */
101
        public FeatureTypesTablePanel(FeatureStore featureStore,
102
        FeatureQuery featureQuery) throws BaseException {
103
                this(featureStore, featureQuery, true);
104
        }
105

    
106
    /**
107
     * @param isDoubleBuffered
108
     * @throws BaseException
109
     */
110
        public FeatureTypesTablePanel(FeatureStore featureStore,
111
                        FeatureQuery featureQuery, boolean isDoubleBuffered)
112
        throws BaseException {
113
                this(
114
                                new FeatureTablePanel(featureStore, featureQuery,
115
                                                isDoubleBuffered), isDoubleBuffered);
116
        }
117

    
118
        public FeatureTypesTablePanel(ConfigurableFeatureTableModel tableModel)
119
                        throws DataException {
120
                this(tableModel, true);
121
        }
122

    
123
        public FeatureTypesTablePanel(ConfigurableFeatureTableModel tableModel,
124
                        boolean isDoubleBuffered) throws DataException {
125
                this(new FeatureTablePanel(tableModel, isDoubleBuffered),
126
                                isDoubleBuffered);
127
        }
128

    
129
        private FeatureTypesTablePanel(FeatureTablePanel tablePanel,
130
                        boolean isDoubleBuffered) throws DataException {
131
                super(isDoubleBuffered);
132
                this.tablePanel = tablePanel;
133
                typesControl = new FeatureTypesControl(tablePanel.getFeatureStore());
134
                //Add a listener to change the selection
135
                typesControl.addFeatureTypeChangeListener(this);
136
                //Select first feature type
137
                typesControl.select(0);
138
                this.initializeUI();
139
                
140
        }
141

    
142
        private boolean hasManyFeatureTypes() throws DataException {
143
                return tablePanel.getFeatureStore().getFeatureTypes().size() > 1;
144
        }
145

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

    
154
        public void change(FeatureStore featureStore, FeatureType featureType,
155
        boolean isDoubleBuffered) throws BaseException {
156
                FeatureQuery featureQuery = featureStore.createFeatureQuery();
157
                featureQuery.setFeatureType(featureType);
158
                tablePanel =
159
                                new FeatureTablePanel(featureStore, featureQuery,
160
                                                isDoubleBuffered);
161
                getJSplitPane().setRightComponent(tablePanel);
162
        }
163

    
164
        public FeatureTablePanel getTablePanel() {
165
                return tablePanel;
166
        }
167

    
168
        public FeatureTypesControl getTypesControl() {
169
                return typesControl;
170
        }
171

    
172
        /**
173
         * Sets that the selected Features to be viewed first.
174
         */
175
        public void setSelectionUp(boolean selectionUp) {
176
                getTablePanel().setSelectionUp(selectionUp);
177
        }
178

    
179
        /**
180
         * Returns the internal Table Model for the Features of the FeatureStore.
181
         * 
182
         * @return the internal Table Model
183
         */
184
        protected ConfigurableFeatureTableModel getTableModel() {
185
                return getTablePanel().getTableModel();
186
        }
187

    
188
        /**
189
         * This method initializes jPanel
190
         * 
191
         * @return javax.swing.JPanel
192
         */
193
        private void initializeUI() {
194
                this.setLayout(new BorderLayout());
195
                this.setSize(new Dimension(331, 251));
196
                this.add(getJSplitPane(), BorderLayout.CENTER);
197
                try {
198
                        add(getJPanel(), BorderLayout.SOUTH);
199
                } catch (BaseException ex) {
200
                        LOG.error("Error creating the panel for the selection count", ex);
201
                }
202
        }
203

    
204
        /**
205
         * This method initializes jSplitPane
206
         * 
207
         * @return javax.swing.JSplitPane
208
         */
209
        private JSplitPane getJSplitPane() {
210
                if (jSplitPane == null) {
211
                        jSplitPane =
212
                                        new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
213
                                                        getTypesControl(), getTablePanel());
214
                        jSplitPane.setOneTouchExpandable(true);
215
                        jSplitPane.setDividerLocation(0.0d);
216
                        try {
217
                                if (hasManyFeatureTypes()) {
218
                                        jSplitPane.setDividerLocation(0.2d);
219
                                }
220
                        } catch (DataException ex) {
221
                                LOG.error("Error getting the number of feature types", ex);
222
                        }
223
                }
224
                return jSplitPane;
225
        }
226

    
227
        /**
228
         * This method initializes jPanel
229
         * 
230
         * @return javax.swing.JPanel
231
         * @throws DataException
232
         */
233
        private JPanel getJPanel() throws BaseException {
234
                if (jPanel == null) {
235

    
236
                        jLabel = new JLabel();
237
                        // jLabel.setText(Messages.getText("seleccionados") + ":   ");
238
                        FeatureSelection selection = getFeatureSelection();
239
                        jLabel.setText(selection.getSize() + " / "
240
                                        + getTableModel().getHelper().getTotalSize() + " "
241
                                        + Messages.getText("registros_seleccionados_total") + ".");
242
                        jPanel = new JPanel();
243

    
244
                        jPanel.add(jLabel, BorderLayout.EAST);
245
                }
246
                return jPanel;
247
        }
248

    
249
        public void updateSelection() {
250
                try {
251
                        FeatureSelection selection =
252
                                        (FeatureSelection) getFeatureSelection();
253
                        jLabel.setText(selection.getSize() + " / "
254
                                        + getTableModel().getRowCount() + " "
255
                                        + Messages.getText("registros_seleccionados_total") + ".");
256
                } catch (DataException e) {
257
                        LOG.error("Unable to update the table selection count", e);
258
                }
259
        }
260

    
261
        private FeatureSelection getFeatureSelection() throws DataException {
262
                return getFeatureStore().getFeatureSelection();
263
        }
264

    
265
        private FeatureStore getFeatureStore() {
266
                return getTableModel().getFeatureStore();
267
        }
268

    
269
        public void valueChanged(ListSelectionEvent e) {
270
                updateSelection();
271
                updateUI();
272

    
273
        }
274

    
275
        public void tableChanged(TableModelEvent e) {
276
                updateSelection();
277
                updateUI();
278
        }
279

    
280
}