Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / select / SelectRowPanel.java @ 29685

History | View | Annotate | Download (7.88 KB)

1
package org.gvsig.editing.gui.cad.tools.select;
2

    
3
/* gvSIG. Geographic Information System of the Valencian Government
4
*
5
* Copyright (C) 2007-2008 Infrastructures and Transports Department
6
* of the Valencian Government (CIT)
7
*
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License
10
* as published by the Free Software Foundation; either version 2
11
* of the License, or (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
* MA  02110-1301, USA.
22
*
23
*/
24

    
25
/*
26
* AUTHORS (In addition to CIT):
27
* 2009 IVER T.I. S.A.   {{Task}}
28
*/
29

    
30
import java.awt.BorderLayout;
31
import java.awt.Color;
32
import java.awt.Dimension;
33
import java.awt.FlowLayout;
34
import java.awt.event.ActionEvent;
35
import java.awt.event.ActionListener;
36
import java.util.ArrayList;
37
import java.util.Iterator;
38

    
39
import javax.swing.JButton;
40
import javax.swing.JOptionPane;
41
import javax.swing.JPanel;
42
import javax.swing.JScrollPane;
43
import javax.swing.ListSelectionModel;
44
import javax.swing.table.AbstractTableModel;
45

    
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.ui.mdiManager.IWindow;
48
import org.gvsig.andami.ui.mdiManager.WindowInfo;
49
import org.gvsig.editing.layers.VectorialLayerEdited;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.ReadException;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureSelection;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureType;
56
import org.gvsig.utils.swing.jtable.JTable;
57
/**
58
*
59
* @author Vicente Caballero Navarro
60
*
61
*/
62
public class SelectRowPanel extends JPanel implements IWindow {
63

    
64
        private static final long serialVersionUID = 1L;
65

    
66
        private JPanel jTablePanel;
67
        private JPanel buttonsPanel;
68
        private JScrollPane scrollPane;
69
        private JButton accept, cancel;
70
        private JTable jTable;
71
        private MyTableModel tableModel;
72
        private MyActionListener action;
73

    
74
        private ArrayList<Feature> features=new ArrayList<Feature>();
75

    
76
        private VectorialLayerEdited vle;
77
        private FeatureType featureType;
78

    
79
        public SelectRowPanel(FeatureSet featureSet, VectorialLayerEdited vle) throws ReadException, DataException {
80
                super();
81
                this.vle = vle;
82
                featureType = vle.getFeatureStore().getDefaultFeatureType();
83
                Iterator<Feature> iterator=featureSet.iterator();
84
                while (iterator.hasNext()) {
85
                        Feature feature = (Feature) iterator.next();
86
                        features.add(feature);
87
                }
88
                action = new MyActionListener();
89
                initialize();
90
        }
91

    
92
        private void initialize() {
93
                Dimension preferred = new Dimension(400, 150);
94
                this.setSize(preferred);
95
                this.setPreferredSize(preferred);
96
                this.setLayout(new BorderLayout());
97
                this.add(getScrollPane(), BorderLayout.CENTER);
98
                this.add(getButtonsPanel(), BorderLayout.SOUTH);
99
        }
100

    
101
        private JPanel getTablePanel() {
102
                if (jTablePanel == null) {
103
                        jTablePanel = new JPanel(new BorderLayout());
104
                        jTablePanel.setSize(new Dimension(400, 150));
105
                        jTablePanel.add(getTable().getTableHeader(), BorderLayout.NORTH);
106
                        jTablePanel.add(getTable(), BorderLayout.CENTER);
107
                }
108
                return jTablePanel;
109
        }
110

    
111
        private JScrollPane getScrollPane() {
112
                if (scrollPane == null) {
113
                        scrollPane = new JScrollPane();
114
                        scrollPane.setViewportView(getTablePanel());
115
                }
116
                return scrollPane;
117
        }
118

    
119
        private JPanel getButtonsPanel() {
120
                if (buttonsPanel == null) {
121
                        buttonsPanel = new JPanel(new FlowLayout());
122
                        buttonsPanel.add(getButtonAceptar());
123
                        buttonsPanel.add(getButtonCancelar());
124
                }
125
                return buttonsPanel;
126
        }
127

    
128
        private JButton getButtonAceptar() {
129
                if (accept == null) {
130
                        accept = new JButton();
131
                        accept.setText(PluginServices.getText(this, "accept"));
132
                        accept.addActionListener(action);
133
                }
134
                return accept;
135
        }
136

    
137
        private JButton getButtonCancelar() {
138
                if (cancel == null) {
139
                        cancel = new JButton();
140
                        cancel.setText(PluginServices.getText(this, "cancel"));
141
                        cancel.addActionListener(action);
142
                }
143
                return cancel;
144
        }
145

    
146
        @SuppressWarnings("unchecked")
147
        private JTable getTable() {
148
                if (jTable == null) {
149

    
150
                        jTable = new JTable();
151
                        tableModel = new MyTableModel(features);
152
                        jTable.setModel(tableModel);
153
                        jTable.setSelectionBackground(Color.yellow);
154
                        jTable.setSelectionForeground(Color.blue);
155
                        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
156

    
157
                        jTable.setCellSelectionEnabled(false);
158
                        jTable.setRowSelectionAllowed(true);
159
                        jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
160
                }
161
                return jTable;
162
        }
163

    
164
        /** ******************************************************* */
165
        /** ******************** IWindow ************************** */
166
        /** ******************************************************* */
167

    
168
        public WindowInfo getWindowInfo() {
169
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG);
170
                wi.setTitle(PluginServices.getText(this, "select_rows"));
171
                wi.setWidth(this.getWidth());
172
                wi.setHeight(this.getHeight());
173
                return wi;
174
        }
175

    
176
        /** ******************************************************* */
177
        /** ***************** ActionListener ********************** */
178
        /** ******************************************************* */
179
        private class MyActionListener implements ActionListener {
180

    
181
                public void actionPerformed(ActionEvent e) {
182
                        FeatureSelection rowSelecteds;
183
                        try {
184
                                rowSelecteds = vle.getFeatureStore().getFeatureSelection();
185

    
186
                        if (e.getSource().equals(accept)) {
187
                                int[] index = jTable.getSelectedRows();
188
                                if (index.length > 0) {
189
                                        for (int i = 0; i < index.length; i++) {
190
                                                Feature sl = features.get(index[i]);
191
                                                rowSelecteds.select(sl);
192
                                        }
193
                                        vle.getFeatureStore().setSelection(rowSelecteds);
194
                                        PluginServices.getMDIManager().closeWindow(
195
                                                        SelectRowPanel.this);
196
                                } else {
197
                                        JOptionPane.showMessageDialog(null, PluginServices.getText(
198
                                                        this, "select_one_row"), PluginServices.getText(
199
                                                        this, "attention"), JOptionPane.WARNING_MESSAGE);
200
                                }
201
                        }
202
                        } catch (ReadException e1) {
203
                                e1.printStackTrace();
204
                        } catch (DataException e1) {
205
                                e1.printStackTrace();
206
                        }
207
                        if (e.getSource().equals(cancel)) {
208
                                PluginServices.getMDIManager().closeWindow(SelectRowPanel.this);
209
                        }
210
                }
211
        }
212

    
213
        /** ******************************************************* */
214
        /** ******************* TableModel ************************ */
215
        /** ******************************************************* */
216
        private class MyTableModel extends AbstractTableModel {
217

    
218
                private static final long serialVersionUID = 1L;
219

    
220
                private ArrayList<Feature> features;
221

    
222
                public void setSelectedSymbols(ArrayList<Feature> rs) {
223
                        features = rs;
224
                        fireTableDataChanged();
225
                }
226

    
227
                public MyTableModel(ArrayList<Feature> rs) {
228
                        features = rs;
229
                }
230

    
231
                public ArrayList<Feature> getSymbolsList() {
232
                        return features;
233
                }
234

    
235
                public int getRowCount() {
236
                        return features.size();
237
                }
238

    
239
                public int getColumnCount() {
240
                        return featureType.size();
241
                }
242

    
243
                public boolean isCellEditable(int rowIndex, int columnIndex) {
244
                        return false;
245
                }
246

    
247
                public Object getValueAt(int rowIndex, int columnIndex) {
248
                        Feature r = (Feature) features.get(rowIndex);
249
                        String s = r.get(columnIndex).toString();
250
                        return s;
251
                }
252

    
253
                @SuppressWarnings("unchecked")
254
                public Class getColumnClass(int columnIndex) {
255
                        return String.class;
256
                }
257

    
258
                public String getColumnName(int columnIndex) {
259
                        return featureType.getAttributeDescriptor(columnIndex).getName();
260
                }
261
        }
262

    
263
        public Object getWindowProfile() {
264
                return WindowInfo.DIALOG_PROFILE;
265
        }
266
}