svn-gvsig-desktop / branches / v2_0_0_prep / 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 @ 39071
History | View | Annotate | Download (9.95 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 |
|
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.messages.NotificationManager; |
37 |
import org.gvsig.app.ApplicationLocator; |
38 |
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel; |
39 |
import org.gvsig.fmap.dal.DataTypes; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
42 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
47 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
48 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
50 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable; |
51 |
import org.gvsig.i18n.Messages; |
52 |
import org.gvsig.tools.dispose.DisposableIterator; |
53 |
|
54 |
/**
|
55 |
* Feature Table Operations.
|
56 |
*
|
57 |
* @author Vicente Caballero Navarro
|
58 |
*
|
59 |
*/
|
60 |
public class TableOperations { |
61 |
|
62 |
private static Logger logger = LoggerFactory.getLogger(TableOperations.class); |
63 |
|
64 |
public static final int MAX_FIELD_LENGTH = 254; |
65 |
private static TableOperations fto = null; |
66 |
private FeatureStore featureStore;
|
67 |
private ArrayList<Feature> selectedFeatures = new ArrayList<Feature>(); |
68 |
private boolean cutting = false; |
69 |
|
70 |
public static TableOperations getInstance() { |
71 |
if (fto == null) { |
72 |
fto = new TableOperations();
|
73 |
} |
74 |
return fto;
|
75 |
} |
76 |
|
77 |
public void setStore(FeatureStore store) { |
78 |
featureStore = store; |
79 |
} |
80 |
|
81 |
public void copyFeatures() throws DataException { |
82 |
cutting = false;
|
83 |
copy(); |
84 |
} |
85 |
|
86 |
public boolean hasSelection() { |
87 |
return !selectedFeatures.isEmpty();
|
88 |
} |
89 |
|
90 |
public void pasteFeatures() throws DataException { |
91 |
if (cutting) {
|
92 |
delete(); |
93 |
cutting = false;
|
94 |
} |
95 |
Iterator<Feature> features = selectedFeatures.iterator();
|
96 |
while (features.hasNext()) {
|
97 |
Feature feature = features.next(); |
98 |
featureStore.insert(feature.getEditable()); |
99 |
} |
100 |
} |
101 |
|
102 |
public void cutFeatures() throws DataException { |
103 |
cutting = true;
|
104 |
copy(); |
105 |
} |
106 |
|
107 |
private void copy() throws DataException { |
108 |
DisposableIterator features = null;
|
109 |
try {
|
110 |
features = |
111 |
((FeatureSelection) featureStore.getSelection()).fastIterator(); |
112 |
selectedFeatures.clear(); |
113 |
while (features.hasNext()) {
|
114 |
Feature feature = (Feature) features.next(); |
115 |
selectedFeatures.add(feature); |
116 |
} |
117 |
} finally {
|
118 |
if (features != null) { |
119 |
features.dispose(); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
private void delete() throws DataException { |
125 |
Iterator<Feature> features = selectedFeatures.iterator();
|
126 |
while (features.hasNext()) {
|
127 |
Feature feature = features.next(); |
128 |
featureStore.delete(feature); |
129 |
} |
130 |
} |
131 |
|
132 |
public void deleteFeatures() throws DataException { |
133 |
DisposableIterator features = null;
|
134 |
try {
|
135 |
features = |
136 |
((FeatureSelection) featureStore.getSelection()).fastIterator(); |
137 |
|
138 |
// featureStore.beginEditingGroup("Deleting one or more selected rows in table.");
|
139 |
|
140 |
List<Feature> sel = new ArrayList<Feature>(); |
141 |
while (features.hasNext()) {
|
142 |
Feature feature = (Feature) features.next(); |
143 |
sel.add(feature); |
144 |
} |
145 |
|
146 |
int n = sel.size();
|
147 |
for (int i=0; i<n; i++) { |
148 |
featureStore.delete(sel.get(i)); |
149 |
} |
150 |
sel.clear(); |
151 |
// featureStore.endEditingGroup();
|
152 |
|
153 |
} finally {
|
154 |
if (features != null) { |
155 |
features.dispose(); |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
public void insertNewFeature() throws DataException { |
161 |
// if (getModel().getAssociatedTable()!=null){
|
162 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
|
163 |
// return;
|
164 |
// }
|
165 |
EditableFeature feature = featureStore.createNewFeature(); |
166 |
featureStore.insert(feature); |
167 |
} |
168 |
|
169 |
public void deleteAttributes(FeatureTable table) throws DataException { |
170 |
EditableFeatureType eft = |
171 |
featureStore.getDefaultFeatureType().getEditable(); |
172 |
FeatureAttributeDescriptor[] selecteds =
|
173 |
table.getSelectedColumnsAttributeDescriptor(); |
174 |
for (int i = 0; i < selecteds.length; i++) { |
175 |
eft.remove(selecteds[i].getName()); |
176 |
} |
177 |
featureStore.update(eft); |
178 |
} |
179 |
|
180 |
public void insertAttributes(FeatureTable table) throws DataException { |
181 |
EditableFeatureType eft = |
182 |
featureStore.getDefaultFeatureType().getEditable(); |
183 |
|
184 |
try {
|
185 |
CreateNewAttributePanel panelNewField = |
186 |
new CreateNewAttributePanel();
|
187 |
|
188 |
EditableFeatureAttributeDescriptor ead = |
189 |
panelNewField.loadFieldDescription(eft); |
190 |
if (ead == null) { |
191 |
return;
|
192 |
} |
193 |
if (ead.getType() == DataTypes.STRING
|
194 |
&& ead.getSize() > MAX_FIELD_LENGTH) { |
195 |
NotificationManager.showMessageInfo( |
196 |
PluginServices.getText(this, "max_length_is") + ":" |
197 |
+ MAX_FIELD_LENGTH, null);
|
198 |
ead.setSize(MAX_FIELD_LENGTH); |
199 |
} |
200 |
PluginServices.getMDIManager().closeWindow(panelNewField); |
201 |
} catch (ParseException e2) { |
202 |
NotificationManager.addError(e2); |
203 |
} |
204 |
featureStore.update(eft); |
205 |
|
206 |
} |
207 |
|
208 |
public void renameAttributes(FeatureTable table) throws DataException { |
209 |
|
210 |
FeatureType _ft = featureStore.getDefaultFeatureType(); |
211 |
|
212 |
FeatureAttributeDescriptor[] selecteds =
|
213 |
table.getSelectedColumnsAttributeDescriptor(); |
214 |
|
215 |
for (int i = selecteds.length - 1; i >= 0; i--) { |
216 |
String newName =
|
217 |
JOptionPane.showInputDialog((Component) PluginServices |
218 |
.getMDIManager().getActiveWindow(), |
219 |
PluginServices.getText( |
220 |
this, "_Please_insert_new_field_name_Cannot_be_undone"), |
221 |
selecteds[i] |
222 |
.getName()); |
223 |
if (newName == null) { |
224 |
return;
|
225 |
} |
226 |
if (_ft.getIndex(newName) != -1) { |
227 |
NotificationManager.showMessageInfo( |
228 |
PluginServices.getText(this, "field_already_exists"), null); |
229 |
return;
|
230 |
} |
231 |
|
232 |
renameAttribute(featureStore, selecteds[i].getName(), newName); |
233 |
} |
234 |
|
235 |
featureStore.finishEditing(); |
236 |
// featureStore.edit(FeatureStore.MODE_FULLEDIT);
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* This method renames a field in three steps:
|
241 |
*
|
242 |
* (1) add new field using type and size of old field.
|
243 |
* (2) copy value from old field to new field.
|
244 |
* (3) remove old field.
|
245 |
*
|
246 |
* @param fs
|
247 |
* @param name
|
248 |
* @param newName
|
249 |
*/
|
250 |
private void renameAttribute(FeatureStore fs, String name, String newName) { |
251 |
|
252 |
try {
|
253 |
|
254 |
// ========== add new field
|
255 |
EditableFeatureType eft = fs.getDefaultFeatureType().getEditable(); |
256 |
FeatureAttributeDescriptor fad = eft.getAttributeDescriptor(name); |
257 |
eft.add(newName, fad.getType(), fad.getSize()); |
258 |
fs.update(eft); |
259 |
|
260 |
// ========== copy value old field -> new field
|
261 |
FeatureSet fset = fs.getFeatureSet(); |
262 |
DisposableIterator diter = fset.fastIterator(); |
263 |
Feature feat = null;
|
264 |
Object val = null; |
265 |
EditableFeature efeat = null;
|
266 |
while (diter.hasNext()) {
|
267 |
feat = (Feature) diter.next(); |
268 |
val = feat.get(name); |
269 |
efeat = feat.getEditable(); |
270 |
efeat.set(newName, val); |
271 |
fset.update(efeat); |
272 |
} |
273 |
diter.dispose(); |
274 |
|
275 |
// ========== delete old field
|
276 |
eft = fs.getDefaultFeatureType().getEditable(); |
277 |
eft.remove(name); |
278 |
fs.update(eft); |
279 |
|
280 |
} catch (Exception ex) { |
281 |
logger.info("Unable to rename attribute (" + name + " --> " + newName + ")", ex); |
282 |
ApplicationLocator.getManager().message( |
283 |
Messages.getText("_Unable_to_rename_attribute"),
|
284 |
JOptionPane.ERROR_MESSAGE);
|
285 |
} |
286 |
|
287 |
} |
288 |
|
289 |
} |