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 @ 39068
History | View | Annotate | Download (8.02 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.gvsig.andami.PluginServices; |
33 |
import org.gvsig.andami.messages.NotificationManager; |
34 |
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable; |
45 |
import org.gvsig.tools.dispose.DisposableIterator; |
46 |
|
47 |
/**
|
48 |
* Feature Table Operations.
|
49 |
*
|
50 |
* @author Vicente Caballero Navarro
|
51 |
*
|
52 |
*/
|
53 |
public class TableOperations { |
54 |
|
55 |
public static final int MAX_FIELD_LENGTH = 254; |
56 |
private static TableOperations fto = null; |
57 |
private FeatureStore featureStore;
|
58 |
private ArrayList<Feature> selectedFeatures = new ArrayList<Feature>(); |
59 |
private boolean cutting = false; |
60 |
|
61 |
public static TableOperations getInstance() { |
62 |
if (fto == null) { |
63 |
fto = new TableOperations();
|
64 |
} |
65 |
return fto;
|
66 |
} |
67 |
|
68 |
public void setStore(FeatureStore store) { |
69 |
featureStore = store; |
70 |
} |
71 |
|
72 |
public void copyFeatures() throws DataException { |
73 |
cutting = false;
|
74 |
copy(); |
75 |
} |
76 |
|
77 |
public boolean hasSelection() { |
78 |
return !selectedFeatures.isEmpty();
|
79 |
} |
80 |
|
81 |
public void pasteFeatures() throws DataException { |
82 |
if (cutting) {
|
83 |
delete(); |
84 |
cutting = false;
|
85 |
} |
86 |
Iterator<Feature> features = selectedFeatures.iterator();
|
87 |
while (features.hasNext()) {
|
88 |
Feature feature = features.next(); |
89 |
featureStore.insert(feature.getEditable()); |
90 |
} |
91 |
} |
92 |
|
93 |
public void cutFeatures() throws DataException { |
94 |
cutting = true;
|
95 |
copy(); |
96 |
} |
97 |
|
98 |
private void copy() throws DataException { |
99 |
DisposableIterator features = null;
|
100 |
try {
|
101 |
features = |
102 |
((FeatureSelection) featureStore.getSelection()).fastIterator(); |
103 |
selectedFeatures.clear(); |
104 |
while (features.hasNext()) {
|
105 |
Feature feature = (Feature) features.next(); |
106 |
selectedFeatures.add(feature); |
107 |
} |
108 |
} finally {
|
109 |
if (features != null) { |
110 |
features.dispose(); |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
private void delete() throws DataException { |
116 |
Iterator<Feature> features = selectedFeatures.iterator();
|
117 |
while (features.hasNext()) {
|
118 |
Feature feature = features.next(); |
119 |
featureStore.delete(feature); |
120 |
} |
121 |
} |
122 |
|
123 |
public void deleteFeatures() throws DataException { |
124 |
DisposableIterator features = null;
|
125 |
try {
|
126 |
features = |
127 |
((FeatureSelection) featureStore.getSelection()).fastIterator(); |
128 |
|
129 |
// featureStore.beginEditingGroup("Deleting one or more selected rows in table.");
|
130 |
|
131 |
List<Feature> sel = new ArrayList<Feature>(); |
132 |
while (features.hasNext()) {
|
133 |
Feature feature = (Feature) features.next(); |
134 |
sel.add(feature); |
135 |
} |
136 |
|
137 |
int n = sel.size();
|
138 |
for (int i=0; i<n; i++) { |
139 |
featureStore.delete(sel.get(i)); |
140 |
} |
141 |
sel.clear(); |
142 |
// featureStore.endEditingGroup();
|
143 |
|
144 |
} finally {
|
145 |
if (features != null) { |
146 |
features.dispose(); |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
public void insertNewFeature() throws DataException { |
152 |
// if (getModel().getAssociatedTable()!=null){
|
153 |
// JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
|
154 |
// return;
|
155 |
// }
|
156 |
EditableFeature feature = featureStore.createNewFeature(); |
157 |
featureStore.insert(feature); |
158 |
} |
159 |
|
160 |
public void deleteAttributes(FeatureTable table) throws DataException { |
161 |
EditableFeatureType eft = |
162 |
featureStore.getDefaultFeatureType().getEditable(); |
163 |
FeatureAttributeDescriptor[] selecteds =
|
164 |
table.getSelectedColumnsAttributeDescriptor(); |
165 |
for (int i = 0; i < selecteds.length; i++) { |
166 |
eft.remove(selecteds[i].getName()); |
167 |
} |
168 |
featureStore.update(eft); |
169 |
} |
170 |
|
171 |
public void insertAttributes(FeatureTable table) throws DataException { |
172 |
EditableFeatureType eft = |
173 |
featureStore.getDefaultFeatureType().getEditable(); |
174 |
|
175 |
try {
|
176 |
CreateNewAttributePanel panelNewField = |
177 |
new CreateNewAttributePanel();
|
178 |
|
179 |
EditableFeatureAttributeDescriptor ead = |
180 |
panelNewField.loadFieldDescription(eft); |
181 |
if (ead == null) { |
182 |
return;
|
183 |
} |
184 |
if (ead.getType() == DataTypes.STRING
|
185 |
&& ead.getSize() > MAX_FIELD_LENGTH) { |
186 |
NotificationManager.showMessageInfo( |
187 |
PluginServices.getText(this, "max_length_is") + ":" |
188 |
+ MAX_FIELD_LENGTH, null);
|
189 |
ead.setSize(MAX_FIELD_LENGTH); |
190 |
} |
191 |
PluginServices.getMDIManager().closeWindow(panelNewField); |
192 |
} catch (ParseException e2) { |
193 |
NotificationManager.addError(e2); |
194 |
} |
195 |
featureStore.update(eft); |
196 |
|
197 |
} |
198 |
|
199 |
public void renameAttributes(FeatureTable table) throws DataException { |
200 |
EditableFeatureType eft = |
201 |
featureStore.getDefaultFeatureType().getEditable(); |
202 |
FeatureAttributeDescriptor[] selecteds =
|
203 |
table.getSelectedColumnsAttributeDescriptor(); |
204 |
|
205 |
for (int i = selecteds.length - 1; i >= 0; i--) { |
206 |
String newName =
|
207 |
JOptionPane.showInputDialog((Component) PluginServices |
208 |
.getMDIManager().getActiveWindow(), PluginServices.getText( |
209 |
this, "please_insert_new_field_name"), selecteds[i] |
210 |
.getName()); |
211 |
if (newName == null) { |
212 |
return;
|
213 |
} |
214 |
if (eft.getIndex(newName) != -1) { |
215 |
NotificationManager.showMessageInfo( |
216 |
PluginServices.getText(this, "field_already_exists"), null); |
217 |
return;
|
218 |
} |
219 |
FeatureAttributeDescriptor ad = |
220 |
(FeatureAttributeDescriptor) eft.get(selecteds[i].getName()); |
221 |
eft.remove(ad.getName()); |
222 |
EditableFeatureAttributeDescriptor ead = |
223 |
eft.add(newName, ad.getType(), ad.getSize()); |
224 |
ead.setPrecision(ad.getPrecision()); |
225 |
} |
226 |
|
227 |
featureStore.update(eft); |
228 |
} |
229 |
|
230 |
} |