svn-gvsig-desktop / tags / v2_0_0_Build_2057 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / TableOperations.java @ 39180
History | View | Annotate | Download (12.2 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.app.project.documents.table; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.text.ParseException; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
import javax.swing.event.TableModelListener; |
32 |
|
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.messages.NotificationManager; |
38 |
import org.gvsig.app.ApplicationLocator; |
39 |
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel; |
40 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
41 |
import org.gvsig.fmap.dal.DataTypes; |
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 |
import org.gvsig.fmap.dal.feature.Feature; |
47 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
48 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
49 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
50 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
51 |
import org.gvsig.fmap.dal.feature.FeatureType; |
52 |
import org.gvsig.fmap.dal.feature.exception.StoreUpdateFeatureTypeException; |
53 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable; |
54 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel; |
55 |
import org.gvsig.i18n.Messages; |
56 |
import org.gvsig.tools.dispose.DisposableIterator; |
57 |
|
58 |
/**
|
59 |
* Feature Table Operations.
|
60 |
*
|
61 |
* @author Vicente Caballero Navarro
|
62 |
*
|
63 |
*/
|
64 |
public class TableOperations { |
65 |
|
66 |
private static Logger logger = LoggerFactory.getLogger(TableOperations.class); |
67 |
|
68 |
public static final int MAX_FIELD_LENGTH = 254; |
69 |
private static TableOperations fto = null; |
70 |
private ArrayList<Feature> selectedFeatures = new ArrayList<Feature>(); |
71 |
private boolean cutting = false; |
72 |
|
73 |
private FeatureTableDocumentPanel tablePanel = null; |
74 |
private FeatureStore featureStore;
|
75 |
|
76 |
public static TableOperations getInstance() { |
77 |
if (fto == null) { |
78 |
fto = new TableOperations();
|
79 |
} |
80 |
return fto;
|
81 |
} |
82 |
|
83 |
public void setTablePanel(FeatureTableDocumentPanel tp) { |
84 |
tablePanel = tp; |
85 |
featureStore = tp.getModel().getStore(); |
86 |
} |
87 |
|
88 |
public void copyFeatures() throws DataException { |
89 |
cutting = false;
|
90 |
copy(); |
91 |
} |
92 |
|
93 |
public boolean hasSelection() { |
94 |
return !selectedFeatures.isEmpty();
|
95 |
} |
96 |
|
97 |
public void pasteFeatures() throws DataException { |
98 |
if (cutting) {
|
99 |
delete(); |
100 |
cutting = false;
|
101 |
} |
102 |
Iterator<Feature> features = selectedFeatures.iterator();
|
103 |
while (features.hasNext()) {
|
104 |
Feature feature = features.next(); |
105 |
featureStore.insert(feature.getEditable()); |
106 |
} |
107 |
} |
108 |
|
109 |
public void cutFeatures() throws DataException { |
110 |
cutting = true;
|
111 |
copy(); |
112 |
} |
113 |
|
114 |
private void copy() throws DataException { |
115 |
DisposableIterator features = null;
|
116 |
try {
|
117 |
features = |
118 |
((FeatureSelection) featureStore.getSelection()).fastIterator(); |
119 |
selectedFeatures.clear(); |
120 |
while (features.hasNext()) {
|
121 |
Feature feature = (Feature) features.next(); |
122 |
selectedFeatures.add(feature); |
123 |
} |
124 |
} finally {
|
125 |
if (features != null) { |
126 |
features.dispose(); |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
private void delete() throws DataException { |
132 |
Iterator<Feature> features = selectedFeatures.iterator();
|
133 |
while (features.hasNext()) {
|
134 |
Feature feature = features.next(); |
135 |
featureStore.delete(feature); |
136 |
} |
137 |
} |
138 |
|
139 |
public void deleteFeatures() throws DataException { |
140 |
|
141 |
FeatureTableModel _ftm = this.tablePanel.getTablePanel().getTableModel();
|
142 |
List<TableModelListener> tmll = removeTableModelListeners(_ftm); |
143 |
|
144 |
|
145 |
DisposableIterator features = null;
|
146 |
try {
|
147 |
/*
|
148 |
features =
|
149 |
((FeatureSelection) featureStore.getSelection()).fastIterator();
|
150 |
*/
|
151 |
|
152 |
FeatureSet all_fset = featureStore.getFeatureSet(); |
153 |
FeatureSelection sele = (FeatureSelection) featureStore.getSelection(); |
154 |
features = all_fset.fastIterator(); |
155 |
Feature item = null;
|
156 |
|
157 |
while (features.hasNext()) {
|
158 |
item = (Feature) features.next(); |
159 |
if (sele.isSelected(item)) {
|
160 |
all_fset.delete(item); |
161 |
} |
162 |
} |
163 |
|
164 |
/*
|
165 |
while (features.hasNext()) {
|
166 |
features.remove();
|
167 |
}
|
168 |
*/
|
169 |
|
170 |
} finally {
|
171 |
if (features != null) { |
172 |
features.dispose(); |
173 |
} |
174 |
|
175 |
addTableModelListeners(_ftm, tmll); |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* @param _ftm
|
181 |
* @param tmll
|
182 |
*/
|
183 |
private void addTableModelListeners( |
184 |
FeatureTableModel _model, |
185 |
List<TableModelListener> _list) { |
186 |
|
187 |
Iterator<TableModelListener> iter = _list.iterator(); |
188 |
while (iter.hasNext()) {
|
189 |
_model.addTableModelListener(iter.next()); |
190 |
} |
191 |
_model.fireTableDataChanged(); |
192 |
} |
193 |
|
194 |
/**
|
195 |
* @param ftm
|
196 |
* @param class1
|
197 |
* @return
|
198 |
*/
|
199 |
private List<TableModelListener> removeTableModelListeners(FeatureTableModel ftm) { |
200 |
|
201 |
TableModelListener[] ll = ftm.getListeners(TableModelListener.class); |
202 |
List<TableModelListener> resp = new ArrayList<TableModelListener>(); |
203 |
|
204 |
int n = ll.length;
|
205 |
for (int i=0; i<n; i++) { |
206 |
resp.add(ll[i]); |
207 |
ftm.removeTableModelListener(ll[i]); |
208 |
} |
209 |
|
210 |
return resp;
|
211 |
} |
212 |
|
213 |
public void insertNewFeature() throws DataException { |
214 |
// if (getModel().getAssociatedTable()!=null){
|
215 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
|
216 |
// return;
|
217 |
// }
|
218 |
EditableFeature feature = featureStore.createNewFeature(); |
219 |
featureStore.insert(feature); |
220 |
} |
221 |
|
222 |
public void deleteAttributes(FeatureTable table) throws DataException { |
223 |
EditableFeatureType eft = |
224 |
featureStore.getDefaultFeatureType().getEditable(); |
225 |
FeatureAttributeDescriptor[] selecteds =
|
226 |
table.getSelectedColumnsAttributeDescriptor(); |
227 |
for (int i = 0; i < selecteds.length; i++) { |
228 |
eft.remove(selecteds[i].getName()); |
229 |
} |
230 |
featureStore.update(eft); |
231 |
} |
232 |
|
233 |
public void insertAttributes(FeatureTable table) throws DataException { |
234 |
EditableFeatureType eft = |
235 |
featureStore.getDefaultFeatureType().getEditable(); |
236 |
|
237 |
try {
|
238 |
CreateNewAttributePanel panelNewField = |
239 |
new CreateNewAttributePanel();
|
240 |
|
241 |
EditableFeatureAttributeDescriptor ead = |
242 |
panelNewField.loadFieldDescription(eft); |
243 |
if (ead == null) { |
244 |
return;
|
245 |
} |
246 |
if (ead.getType() == DataTypes.STRING
|
247 |
&& ead.getSize() > MAX_FIELD_LENGTH) { |
248 |
NotificationManager.showMessageInfo( |
249 |
PluginServices.getText(this, "max_length_is") + ":" |
250 |
+ MAX_FIELD_LENGTH, null);
|
251 |
ead.setSize(MAX_FIELD_LENGTH); |
252 |
} |
253 |
PluginServices.getMDIManager().closeWindow(panelNewField); |
254 |
} catch (ParseException e2) { |
255 |
NotificationManager.addError(e2); |
256 |
} |
257 |
featureStore.update(eft); |
258 |
|
259 |
} |
260 |
|
261 |
public void renameAttributes(FeatureTable table) throws DataException { |
262 |
|
263 |
FeatureType _ft = featureStore.getDefaultFeatureType(); |
264 |
|
265 |
FeatureAttributeDescriptor[] selecteds =
|
266 |
table.getSelectedColumnsAttributeDescriptor(); |
267 |
|
268 |
for (int i = selecteds.length - 1; i >= 0; i--) { |
269 |
String newName =
|
270 |
JOptionPane.showInputDialog((Component) PluginServices |
271 |
.getMDIManager().getActiveWindow(), |
272 |
PluginServices.getText( |
273 |
this, "_Please_insert_new_field_name_Cannot_be_undone"), |
274 |
selecteds[i] |
275 |
.getName()); |
276 |
if (newName == null) { |
277 |
return;
|
278 |
} |
279 |
if (_ft.getIndex(newName) != -1) { |
280 |
NotificationManager.showMessageInfo( |
281 |
PluginServices.getText(this, "field_already_exists"), null); |
282 |
return;
|
283 |
} |
284 |
|
285 |
renameAttribute(featureStore, selecteds[i].getName(), newName); |
286 |
} |
287 |
|
288 |
featureStore.finishEditing(); |
289 |
// featureStore.edit(FeatureStore.MODE_FULLEDIT);
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* This method renames a field in three steps:
|
294 |
*
|
295 |
* (1) add new field using type and size of old field.
|
296 |
* (2) copy value from old field to new field.
|
297 |
* (3) remove old field.
|
298 |
*
|
299 |
* @param fs
|
300 |
* @param name
|
301 |
* @param newName
|
302 |
*/
|
303 |
private static void renameAttribute(FeatureStore fs, String name, String newName) { |
304 |
|
305 |
try {
|
306 |
|
307 |
// ========== add new field
|
308 |
EditableFeatureType eft = fs.getDefaultFeatureType().getEditable(); |
309 |
FeatureAttributeDescriptor fad = eft.getAttributeDescriptor(name); |
310 |
eft.add(newName, fad.getType(), fad.getSize()); |
311 |
fs.update(eft); |
312 |
|
313 |
// ========== copy value old field -> new field
|
314 |
FeatureSet fset = fs.getFeatureSet(); |
315 |
DisposableIterator diter = fset.fastIterator(); |
316 |
Feature feat = null;
|
317 |
Object val = null; |
318 |
EditableFeature efeat = null;
|
319 |
while (diter.hasNext()) {
|
320 |
feat = (Feature) diter.next(); |
321 |
val = feat.get(name); |
322 |
efeat = feat.getEditable(); |
323 |
efeat.set(newName, val); |
324 |
fset.update(efeat); |
325 |
} |
326 |
diter.dispose(); |
327 |
|
328 |
// ========== delete old field
|
329 |
eft = fs.getDefaultFeatureType().getEditable(); |
330 |
eft.remove(name); |
331 |
fs.update(eft); |
332 |
|
333 |
} catch (Exception ex) { |
334 |
logger.info("Unable to rename attribute (" + name + " --> " + newName + ")", ex); |
335 |
ApplicationLocator.getManager().message( |
336 |
Messages.getText("_Unable_to_rename_attribute"),
|
337 |
JOptionPane.ERROR_MESSAGE);
|
338 |
} |
339 |
|
340 |
} |
341 |
|
342 |
/**
|
343 |
* Renames field in feature store
|
344 |
*
|
345 |
* @param fs
|
346 |
* @param oldname
|
347 |
* @param newname
|
348 |
* @return
|
349 |
* @throws DataException
|
350 |
*/
|
351 |
public static void renameColumn(FeatureStore fs, |
352 |
String oldname, String newname) throws DataException { |
353 |
|
354 |
FeatureType _ft = fs.getDefaultFeatureType(); |
355 |
if (_ft.getIndex(newname) != -1) { |
356 |
throw new StoreUpdateFeatureTypeException( |
357 |
new Exception("Attribute name already existed."), |
358 |
fs.getName()); |
359 |
} |
360 |
renameAttribute(fs, oldname, newname); |
361 |
fs.finishEditing(); |
362 |
} |
363 |
|
364 |
} |