Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / select / SelectRowPanel.java @ 40596

History | View | Annotate | Download (8.06 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.editing.gui.cad.tools.select;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.FlowLayout;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.ArrayList;
33

    
34
import javax.swing.JButton;
35
import javax.swing.JOptionPane;
36
import javax.swing.JPanel;
37
import javax.swing.JScrollPane;
38
import javax.swing.ListSelectionModel;
39
import javax.swing.table.AbstractTableModel;
40

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

    
61
        private static final long serialVersionUID = 1L;
62

    
63
        private JPanel jTablePanel;
64
        private JPanel buttonsPanel;
65
        private JScrollPane scrollPane;
66
        private JButton accept, cancel;
67
        private JTable jTable;
68
        private MyTableModel tableModel;
69
        private MyActionListener action;
70

    
71
        private ArrayList<Feature> features=new ArrayList<Feature>();
72

    
73
        private VectorialLayerEdited vle;
74
        private FeatureType featureType;
75

    
76
        public SelectRowPanel(FeatureSet featureSet, VectorialLayerEdited vle) throws ReadException, DataException {
77
                super();
78
                this.vle = vle;
79
                featureType = featureSet.getDefaultFeatureType();
80
                DisposableIterator iterator = null;
81
                try {
82
                        iterator = featureSet.iterator();
83
                        while (iterator.hasNext()) {
84
                                Feature feature = (Feature) iterator.next();
85
                                features.add(feature);
86
                        }
87
                } finally {
88
                        DisposeUtils.dispose(iterator);
89
                }
90
                action = new MyActionListener();
91
                initialize();
92
        }
93

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

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

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

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

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

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

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

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

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

    
166
        /** ******************************************************* */
167
        /** ******************** IWindow ************************** */
168
        /** ******************************************************* */
169

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

    
178
        /** ******************************************************* */
179
        /** ***************** ActionListener ********************** */
180
        /** ******************************************************* */
181
        private class MyActionListener implements ActionListener {
182

    
183
                public void actionPerformed(ActionEvent e) {
184
                        FeatureSelection rowSelecteds;
185
                        try {
186

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

    
216
        /** ******************************************************* */
217
        /** ******************* TableModel ************************ */
218
        /** ******************************************************* */
219
        private class MyTableModel extends AbstractTableModel {
220

    
221
                private static final long serialVersionUID = 1L;
222

    
223
                private ArrayList<Feature> features;
224

    
225
                public void setSelectedSymbols(ArrayList<Feature> rs) {
226
                        features = rs;
227
                        fireTableDataChanged();
228
                }
229

    
230
                public MyTableModel(ArrayList<Feature> rs) {
231
                        features = rs;
232
                }
233

    
234
                public ArrayList<Feature> getSymbolsList() {
235
                        return features;
236
                }
237

    
238
                public int getRowCount() {
239
                        return features.size();
240
                }
241

    
242
                public int getColumnCount() {
243
                        return featureType.size();
244
                }
245

    
246
                public boolean isCellEditable(int rowIndex, int columnIndex) {
247
                        return false;
248
                }
249

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

    
256
                @SuppressWarnings("unchecked")
257
                public Class getColumnClass(int columnIndex) {
258
                        return String.class;
259
                }
260

    
261
                public String getColumnName(int columnIndex) {
262
                        return featureType.getAttributeDescriptor(columnIndex).getName();
263
                }
264
        }
265

    
266
        public Object getWindowProfile() {
267
                return WindowInfo.DIALOG_PROFILE;
268
        }
269
}