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 / FeatureTablePanel.java @ 41611

History | View | Annotate | Download (9.98 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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {Create a Table component for Features}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing;
29

    
30
import java.awt.BorderLayout;
31
import static java.awt.BorderLayout.CENTER;
32
import java.awt.Component;
33
import javax.swing.AbstractListModel;
34
import javax.swing.JButton;
35

    
36
import javax.swing.JLabel;
37
import javax.swing.JList;
38
import javax.swing.JPanel;
39
import javax.swing.JScrollPane;
40
import javax.swing.JTable;
41
import javax.swing.ListCellRenderer;
42
import javax.swing.ListModel;
43
import javax.swing.UIManager;
44
import javax.swing.table.JTableHeader;
45

    
46
import org.gvsig.fmap.dal.DataStoreNotification;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureQuery;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.ConfigurableFeatureTableModel;
52
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableConfigurationPanel;
53
import org.gvsig.i18n.Messages;
54
import org.gvsig.tools.exception.BaseException;
55
import org.gvsig.tools.observer.Observable;
56
import org.gvsig.tools.observer.Observer;
57

    
58
/**
59
 * Panel to show a table of Feature data.
60
 * 
61
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
62
 */
63
public class FeatureTablePanel extends JPanel implements Observer {
64

    
65
    private static final long serialVersionUID = -9199073063283531216L;
66

    
67
    private ConfigurableFeatureTableModel tableModel;
68

    
69
    private JScrollPane jScrollPane = null;
70

    
71
    private FeatureTable table = null;
72
    private JPanel selectionPanel;
73
    private JLabel selectionLabel;
74

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

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

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

    
112
    /**
113
     * @param isDoubleBuffered
114
     * @throws BaseException
115
     */
116
    public FeatureTablePanel(FeatureStore featureStore,
117
        FeatureQuery featureQuery, boolean isDoubleBuffered)
118
        throws BaseException {
119
        this(createModel(featureStore, featureQuery));
120
    }
121

    
122
    public FeatureTablePanel(ConfigurableFeatureTableModel tableModel)
123
        throws DataException {
124
        this(tableModel, true);
125
    }
126

    
127
    public FeatureTablePanel(ConfigurableFeatureTableModel tableModel,
128
        boolean isDoubleBuffered) throws DataException {
129
        super(isDoubleBuffered);
130
        this.tableModel = tableModel;
131
        this.setLayout(new BorderLayout());
132
        this.add(getJScrollPane(), BorderLayout.CENTER);
133
        this.add(getSelectionPanel(), BorderLayout.SOUTH);
134
        tableModel.getFeatureStore().addObserver(this);
135
    }
136

    
137
    private JPanel getSelectionPanel() {
138
        if (selectionPanel == null) {
139
            selectionLabel = new JLabel();
140
            selectionLabel.setText(getFeatureSelectionSize() + " / "
141
                + getTableModel().getHelper().getTotalSize() + " "
142
                + Messages.getText("registros_seleccionados_total") + ".");
143
            selectionPanel = new JPanel();
144

    
145
            selectionPanel.add(selectionLabel, BorderLayout.EAST);
146
        }
147
        return selectionPanel;
148
    }
149

    
150
    private void updateSelection() {
151
        selectionLabel.setText(getFeatureSelectionSize() + " / "
152
            + getTableModel().getRowCount() + " "
153
            + Messages.getText("registros_seleccionados_total") + ".");
154
    }
155

    
156
    private long getFeatureSelectionSize() {
157
        try {
158
            return getFeatureStore().getFeatureSelection().getSize();
159
        } catch (DataException e) {
160
            throw new RuntimeException("Error updating selection information",
161
                e);
162
        }
163
    }
164

    
165
    public JPanel createConfigurationPanel() {
166
        return new FeatureTableConfigurationPanel(tableModel);
167
    }
168

    
169
    /**
170
     * Returns the internal Table Model for the Features of the FeatureStore.
171
     * 
172
     * @return the internal Table Model
173
     */
174
    public ConfigurableFeatureTableModel getTableModel() {
175
        return tableModel;
176
    }
177

    
178
    /**
179
     * Returns the {@link FeatureStore} of the {@link Feature}s being shown in
180
     * the table.
181
     * 
182
     * @return the store of the features
183
     */
184
    public FeatureStore getFeatureStore() {
185
        return getTableModel().getFeatureStore();
186
    }
187

    
188
    /**
189
     * Returns the FeatureQuery used to get the Features.
190
     * 
191
     * @return the FeatureQuery
192
     */
193
    public FeatureQuery getFeatureQuery() {
194
        return getTableModel().getFeatureQuery();
195
    }
196

    
197
    /**
198
     * Sets that the selected Features to be viewed first.
199
     */
200
    public void setSelectionUp(boolean selectionUp) {
201
        try {
202
            getTable().setSelectionUp(selectionUp);
203
        } catch (DataException e) {
204
            throw new RuntimeException("Error setting the selection up", e);
205
        }
206
    }
207

    
208
    private static ConfigurableFeatureTableModel createModel(
209
        FeatureStore featureStore, FeatureQuery featureQuery)
210
        throws BaseException {
211
        FeatureQuery query =
212
            featureQuery == null ? featureStore.createFeatureQuery()
213
                : featureQuery;
214

    
215
        return new ConfigurableFeatureTableModel(featureStore, query);
216
    }
217

    
218
    public FeatureTable getTable() throws DataException {
219
        if (table == null) {
220
            table = new FeatureTable(tableModel);
221
            // Change the selection model to link with the FeatureStore
222
            // selection
223
            // through the FeatureTableModel
224
            table.setRowSelectionAllowed(true);
225
            table.setColumnSelectionAllowed(false);
226
            // table.setSelectionModel(new FeatureSelectionModel(tableModel));
227
        }
228
        return table;
229
    }
230

    
231
    /**
232
     * This method initializes jScrollPane
233
     * 
234
     * @return javax.swing.JScrollPane
235
     * @throws DataException
236
     */
237
    private JScrollPane getJScrollPane() throws DataException {
238
        if (jScrollPane == null) {
239
            FeatureTable theTable = getTable();
240
            jScrollPane = new JScrollPane();
241
            jScrollPane.setViewportView(theTable);
242
            jScrollPane.setRowHeaderView(getRowHeader(theTable));
243
        }
244
        return jScrollPane;
245
    }
246

    
247
    private static class RowHeaderRenderer extends JButton implements ListCellRenderer {
248

    
249
        RowHeaderRenderer(JTable table) {
250
          JTableHeader header = table.getTableHeader();
251
          setOpaque(true);
252
          setBorder(UIManager.getBorder("TableHeader.cellBorder"));
253
          setHorizontalAlignment(CENTER);
254
          setForeground(header.getForeground());
255
          setBackground(header.getBackground());
256
          setFont(header.getFont());
257
        }
258

    
259
        public Component getListCellRendererComponent(JList list, Object value,
260
            int index, boolean isSelected, boolean cellHasFocus) {
261
          setText((value == null) ? "" : value.toString());
262
          return this;
263
        }
264
    }
265
    
266
    private JList getRowHeader(final FeatureTable theTable) {
267
      
268
      ListModel lm = new AbstractListModel() {
269
        public int getSize() {
270
          return theTable.getRowCount();
271
        }
272

    
273
        public Object getElementAt(int index) {
274
          return String.valueOf(index+1);
275
        }
276
      };
277
      JList rowHeader = new JList(lm);
278
      rowHeader.setCellRenderer(new RowHeaderRenderer(theTable));
279
      rowHeader.setFixedCellWidth(50);
280
      rowHeader.setFixedCellHeight(table.getRowHeight()); //+ table.getRowMargin());
281
      return rowHeader;
282
    }
283
    
284
    public void update(Observable observable, Object notification) {
285
        // If selection changes from nothing selected to anything selected
286
        // or the reverse, update selection info
287
        if (notification instanceof DataStoreNotification) {
288
            String type = ((DataStoreNotification) notification).getType();
289
            if (DataStoreNotification.SELECTION_CHANGE.equals(type)) {
290
                updateSelection();
291
            }
292
        }
293
    }
294
    // public void valueChanged(ListSelectionEvent e) {
295
    // updateSelection();
296
    // updateUI();
297
    // }
298
    //
299
    // public void tableChanged(TableModelEvent e) {
300
    // updateSelection();
301
    // updateUI();
302
    // }
303
}