Revision 39071

View differences:

branches/v2_0_0_prep/extensions/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/resources/text.properties
12 12
_Start_editing=Comenzar edici?n
13 13
_Stop_editing=Terminar edici?n
14 14
_Create_link_between_tables=Enlazar tablas
15
_Unable_to_rename_attribute=No se pudo renombrar el campo
16
_Please_insert_new_field_name_Cannot_be_undone=Introduzca nuevo nombre de campo.\nAtenci?n: tras este cambio, se cerrar? la edici?n consolidando los cambios.
branches/v2_0_0_prep/extensions/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/resources/text_en.properties
12 12
_Start_editing=Start editing
13 13
_Stop_editing=Stop editing
14 14
_Create_link_between_tables=Create link between tables
15
_Unable_to_rename_attribute=Unable to rename attribute
16
_Please_insert_new_field_name_Cannot_be_undone=Please insert new field name.\nWarning: after this change, editing mode will be closed and changes will be consolidated.
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
29 29

  
30 30
import javax.swing.JOptionPane;
31 31

  
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

  
32 35
import org.gvsig.andami.PluginServices;
33 36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.app.ApplicationLocator;
34 38
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel;
35 39
import org.gvsig.fmap.dal.DataTypes;
36 40
import org.gvsig.fmap.dal.exception.DataException;
......
40 44
import org.gvsig.fmap.dal.feature.Feature;
41 45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42 46
import org.gvsig.fmap.dal.feature.FeatureSelection;
47
import org.gvsig.fmap.dal.feature.FeatureSet;
43 48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.dal.feature.FeatureType;
44 50
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable;
51
import org.gvsig.i18n.Messages;
45 52
import org.gvsig.tools.dispose.DisposableIterator;
46 53

  
47 54
/**
......
52 59
 */
53 60
public class TableOperations {
54 61

  
62
    private static Logger logger = LoggerFactory.getLogger(TableOperations.class);
63
    
55 64
    public static final int MAX_FIELD_LENGTH = 254;
56 65
    private static TableOperations fto = null;
57 66
    private FeatureStore featureStore;
......
197 206
    }
198 207

  
199 208
    public void renameAttributes(FeatureTable table) throws DataException {
200
        EditableFeatureType eft =
201
            featureStore.getDefaultFeatureType().getEditable();
209
        
210
        FeatureType _ft = featureStore.getDefaultFeatureType();
211

  
202 212
        FeatureAttributeDescriptor[] selecteds =
203 213
            table.getSelectedColumnsAttributeDescriptor();
204 214

  
205 215
        for (int i = selecteds.length - 1; i >= 0; i--) {
206 216
            String newName =
207 217
                JOptionPane.showInputDialog((Component) PluginServices
208
                    .getMDIManager().getActiveWindow(), PluginServices.getText(
209
                    this, "please_insert_new_field_name"), selecteds[i]
218
                    .getMDIManager().getActiveWindow(),
219
                    PluginServices.getText(
220
                    this, "_Please_insert_new_field_name_Cannot_be_undone"),
221
                    selecteds[i]
210 222
                    .getName());
211 223
            if (newName == null) {
212 224
                return;
213 225
            }
214
            if (eft.getIndex(newName) != -1) {
226
            if (_ft.getIndex(newName) != -1) {
215 227
                NotificationManager.showMessageInfo(
216 228
                    PluginServices.getText(this, "field_already_exists"), null);
217 229
                return;
218 230
            }
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());
231
            
232
            renameAttribute(featureStore, selecteds[i].getName(), newName);
225 233
        }
234
        
235
        featureStore.finishEditing();
236
        // featureStore.edit(FeatureStore.MODE_FULLEDIT);
237
    }
226 238

  
227
        featureStore.update(eft);
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

  
228 287
    }
229 288

  
230 289
}
branches/v2_0_0_prep/extensions/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/pom.xml
89 89
            <artifactId>org.gvsig.timesupport.lib.api</artifactId>
90 90
            <scope>compile</scope>
91 91
        </dependency>
92
        
93
        <dependency>
94
            <groupId>org.gvsig</groupId>
95
            <artifactId>org.gvsig.i18n</artifactId>
96
            <scope>compile</scope>
97
        </dependency>   
92 98
    </dependencies>
93 99
	<profiles>
94 100
		<profile>

Also available in: Unified diff