Revision 39208

View differences:

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
299 299
     * @param fs
300 300
     * @param name
301 301
     * @param newName
302
     * @return true if the change took place
302 303
     */
303
    private static void renameAttribute(FeatureStore fs, String name, String newName) {
304
    private static boolean renameAttribute(FeatureStore fs, String name, String newName) {
304 305

  
306
        Component root_comp =
307
            ApplicationLocator.getManager().getRootComponent();
308

  
309
        EditableFeatureType eft = null;
310
        
305 311
        try {
306
            
307 312
            // ========== add new field
308
            EditableFeatureType eft = fs.getDefaultFeatureType().getEditable();
313
            eft = fs.getDefaultFeatureType().getEditable();
309 314
            FeatureAttributeDescriptor fad = eft.getAttributeDescriptor(name);
310 315
            eft.add(newName, fad.getType(), fad.getSize());
311 316
            fs.update(eft);
312
            
317
        } catch (DataException ex) {
318
            logger.info("Unable to rename attribute (" + name + " --> " + newName + ")", ex);
319
            ApplicationLocator.getManager().message(
320
                Messages.getText("_Unable_to_rename_attribute"),
321
                JOptionPane.ERROR_MESSAGE);
322
            /*
323
             * did not even add new field
324
             */
325
            return false;
326
        }
327
        
328
        boolean error_when_inserting = false;
329
        try {
313 330
            // ========== copy value old field -> new field
314 331
            FeatureSet fset = fs.getFeatureSet();
315 332
            DisposableIterator diter = fset.fastIterator();
......
324 341
                fset.update(efeat);
325 342
            }
326 343
            diter.dispose();
344
            
345
            // Closing editing to check that store admits new field
346
            fs.finishEditing();
347
        } catch (DataException ex) {
348
            
349
            logger.info("Error while renaming att to: " + newName, ex);
350
            String final_msg = getLastMessage(ex);
351
            JOptionPane.showMessageDialog(
352
                root_comp,
353
                Messages.getText("_Unable_to_rename_attribute")
354
                + ": " + final_msg,
355
                Messages.getText("_Rename_column"),
356
                JOptionPane.ERROR_MESSAGE);
357
            error_when_inserting = true;
358
        }
359
        
360
        if (error_when_inserting) {
361
            try {
362
                /*
363
                 * Trying to remove new field and leave
364
                 * table as it was
365
                 */
366
                eft.remove(newName);
367
                fs.update(eft);
368
            } catch (DataException ex) {
369
                /*
370
                 * Unable to remove added field but user was
371
                 * already notified that something went wrong
372
                 */
373
            }
374
            /*
375
             * not changed
376
             */
377
            return false;
378
        }
379
            
327 380

  
328
            // ========== delete old field
381
        try {
382
            // Finally reopen editing and delete old field
383
            fs.edit(FeatureStore.MODE_FULLEDIT);
329 384
            eft = fs.getDefaultFeatureType().getEditable();
330 385
            eft.remove(name);
331 386
            fs.update(eft);
332 387
            
333
        } catch (Exception ex) {
388
        } catch (DataException ex) {
334 389
            logger.info("Unable to rename attribute (" + name + " --> " + newName + ")", ex);
335 390
            ApplicationLocator.getManager().message(
336 391
                Messages.getText("_Unable_to_rename_attribute"),
337 392
                JOptionPane.ERROR_MESSAGE);
393
            return false;
338 394
        }
395
        return true;
339 396

  
340 397
    }
341 398
    
342 399
    /**
400
     * @param ex
401
     * @return
402
     */
403
    private static String getLastMessage(Throwable ex) {
404
        
405
        Throwable p = ex;
406
        while (p.getCause() != null && p.getCause() != p) {
407
            p = p.getCause();
408
        }
409
        return p.getMessage();
410
    }
411

  
412
    /**
343 413
     * Renames field in feature store
344 414
     * 
345 415
     * @param fs

Also available in: Unified diff