Revision 590

View differences:

org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingScript.java
178 178

  
179 179
    @Override
180 180
    public void save() {
181
        this.saveInfo();
182
        // Guardo el codigo en el fichero
183
        File fcode = this.getFileResource(this.getExtension());
184
        try {
185
            FileUtils.write(fcode, this.getCode());
186
        } catch (Exception e) {
187
            logger.warn("Can't write code to file '" + fcode.getAbsolutePath() + "'.", e);
188
        }
189
        this.setSaved(true);
190
    }
191

  
192
    private void saveInfo() {
181 193
        File f = getFileResource(".inf");
182 194
        if (!f.isFile()) {
183 195
            try {
......
193 205
            logger.warn("Can't load 'inf' file '" + f.getAbsolutePath() + "'.", e);
194 206
        }
195 207
        save(prefs);
196
        // Guardo el codigo en el fichero
197
        File fcode = this.getFileResource(this.getExtension());
198
        try {
199
            FileUtils.write(fcode, this.getCode());
200
        } catch (Exception e) {
201
            logger.warn("Can't write code to file '" + fcode.getAbsolutePath() + "'.", e);
202
        }
203
        this.setSaved(true);
204 208
    }
205

  
209
    
206 210
    @Override
207 211
    protected void save(Ini prefs) {
208 212
        super.save(prefs);
......
523 527
    public ScriptingUnit get(File file) {
524 528
        return this.manager.getScript(file);
525 529
    }
530

  
531
    @Override
532
    public boolean move(ScriptingFolder target) {
533
        if (! manager.validateUnitId(target, this.getId()) ) {
534
            logger.info("Can't move script '"+this.getId()+"' to '"+target.getFile().getAbsolutePath()+"', is not valid.");
535
            return false;
536
        }
537
        if( !this.isSaved() ) {
538
            logger.info("Can't move script '"+this.getId()+"', is not saved.");
539
            return false;
540
        }
541
        try {
542
            File codefile = this.getFileResource(this.extension);
543
            FileUtils.moveFileToDirectory(this.getFile(), target.getFile(),true);
544
            FileUtils.moveFileToDirectory(codefile, target.getFile(), true);
545
            this.parent = target;
546
            this.load(target, id);
547
        } catch (IOException ex) {
548
            logger.info("Can't move script '"+this.getId()+"' to '"+target.getFile().getAbsolutePath()+"', "+ex.getMessage(),ex);
549
            return false;
550
        }
551
        return true;
552
    }
553

  
554
    @Override
555
    public boolean rename(String newId) {
556
        if (! manager.validateUnitId(this.getParent(), newId) ) {
557
            logger.info("Can't rename script '"+this.getId()+"', target id '"+newId+"' is not valid.");
558
            return false;
559
        }
560
        if( !this.isSaved() ) {
561
            logger.info("Can't rename script '"+this.getId()+"', is not saved.");
562
            return false;
563
        }
564
        try {
565
            ScriptingFolder target = this.getParent();
566
            File codefile = this.getFileResource(this.extension);
567
            FileUtils.moveFile(this.getFile(),  new File(target.getFile(),newId+".inf") );
568
            FileUtils.moveFile(codefile, new File(target.getFile(),newId+this.extension));
569
            this.setId(newId);
570
            this.saveInfo();
571
            this.load(target, id);
572
        } catch (IOException ex) {
573
            logger.info("Can't rename script '"+this.getId()+"' to '"+newId+"', "+ex.getMessage(),ex);
574
            return false;
575
        }        
576
        return true;
577
    }
578

  
526 579
}
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/AbstractUnit.java
138 138
    public ScriptingFolder getParent() {
139 139
        return this.parent;
140 140
    }
141
//
142
//    /* (non-Javadoc)
143
//     * @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
144
//     */
145
//    private boolean moveFiles(ScriptingFolder folder, String id) {
146
//
147
//        if (manager.validateUnitId(folder, id)) {
148
//            String oldId = this.getId();
149
//            String fileName = null;
150
//            String s[] = null;
151
//            String extension = null;
152
//            List<ScriptingUnit> units = this.getParent().getUnits();
153
//            for (int i = 0; i < units.size(); i++) {
154
//                fileName = (units.get(i)).getId();
155
//                s = fileName.split("\\.");
156
//                extension = "";
157
//                if (s.length > 1) {
158
//                    extension = "." + s[s.length - 1];
159
//                    fileName = fileName.substring(0, fileName.length() - extension.length());
160
//                }
161
//                if (extension.equals("") && this instanceof ScriptingScript) {
162
//                    extension = manager.getExtensionOfLanguage(((ScriptingScript) this).getLangName());
163
//                }
164
//                if (fileName.equals(oldId)) {
165
//                    //renombramos fichero
166
//                    File f = new File(this.getParent().getFile(), fileName + extension);
167
//                    File fDest = new File(folder.getFile(), id + extension);
168
//                    if (this instanceof ScriptingScript) {
169
//                        String code;
170
//                        try {
171
//                            code = FileUtils.readFileToString(f);
172
//                            ((ScriptingScript) this).setCode(code);
173
//                        } catch (IOException ex) {
174
//                            
175
//                        }
176
//                    }
177
//                    f.renameTo(fDest);
178
//
179
//                    File fInf = new File(this.getParent().getFile(), fileName + ".inf");
180
//                    if (fInf.exists()) {
181
//                        File fInfDest = new File(folder.getFile(), id + ".inf");
182
//                        fInf.renameTo(fInfDest);
183
//                    }
184
//                    File fDialog = new File(this.getParent().getFile(), fileName + ".dlg");
185
//                    if (fDialog.exists()) {
186
//                        File fDialogDest = new File(folder.getFile(), id + ".dlg");
187
//                        fDialog.renameTo(fDialogDest);
188
//                    }
189
//                }
190
//            }
191
//            this.setParent(folder);
192
//            this.setId(id);
193
//            return true;
194
//        }
195
//        return false;
196
//    }
141 197

  
142
    /* (non-Javadoc)
143
     * @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
144
     */
145
    private boolean moveFiles(ScriptingFolder folder, String id) {
146

  
147
        if (manager.validateUnitId(folder, id)) {
148
            String oldId = this.getId();
149
            String fileName = null;
150
            String s[] = null;
151
            String extension = null;
152
            List<ScriptingUnit> units = this.getParent().getUnits();
153
            for (int i = 0; i < units.size(); i++) {
154
                fileName = (units.get(i)).getId();
155
                s = fileName.split("\\.");
156
                extension = "";
157
                if (s.length > 1) {
158
                    extension = "." + s[s.length - 1];
159
                    fileName = fileName.substring(0, fileName.length() - extension.length());
160
                }
161
                if (extension.equals("") && this instanceof ScriptingScript) {
162
                    extension = manager.getExtensionOfLanguage(((ScriptingScript) this).getLangName());
163
                }
164
                if (fileName.equals(oldId)) {
165
                    //renombramos fichero
166
                    File f = new File(this.getParent().getFile(), fileName + extension);
167
                    File fDest = new File(folder.getFile(), id + extension);
168
                    if (this instanceof ScriptingScript) {
169
                        String code;
170
                        try {
171
                            code = FileUtils.readFileToString(f);
172
                            ((ScriptingScript) this).setCode(code);
173
                        } catch (IOException ex) {
174
                            
175
                        }
176
                    }
177
                    f.renameTo(fDest);
178

  
179
                    File fInf = new File(this.getParent().getFile(), fileName + ".inf");
180
                    if (fInf.exists()) {
181
                        File fInfDest = new File(folder.getFile(), id + ".inf");
182
                        fInf.renameTo(fInfDest);
183
                    }
184
                    File fDialog = new File(this.getParent().getFile(), fileName + ".dlg");
185
                    if (fDialog.exists()) {
186
                        File fDialogDest = new File(folder.getFile(), id + ".dlg");
187
                        fDialog.renameTo(fDialogDest);
188
                    }
189
                }
190
            }
191
            this.setParent(folder);
192
            this.setId(id);
193
            return true;
194
        }
195
        return false;
196
    }
197

  
198
    public boolean move(ScriptingFolder target) {
199
        return moveFiles(target, this.getId());
200
    }
201

  
202
    /* (non-Javadoc)
203
     * @see org.gvsig.scripting.impl.Unit#rename(java.lang.String)
204
     */
205
    public boolean rename(String newId) {
206
        return moveFiles(this.getParent(), newId);
207
    }
208

  
209 198
    private String toStringNotNull(String s) {
210 199
        if (s == null) {
211 200
            return "";
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingFolder.java
11 11

  
12 12
import org.apache.commons.io.FileUtils;
13 13
import org.apache.commons.io.FilenameUtils;
14
import org.apache.commons.lang3.StringUtils;
14 15
import org.gvsig.scripting.ScriptingFolder;
15 16
import org.gvsig.scripting.ScriptingManager;
16 17
import org.gvsig.scripting.ScriptingUnit;
......
22 23

  
23 24
    private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingFolder.class);
24 25
    protected File folder;
26
    private boolean islink;
25 27

  
26 28
    public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager) {
27 29
        this(parent, manager, (File) null);
......
33 35
        if (folder != null) {
34 36
            this.setId(folder.getName());
35 37
        }
36
    }
38
        this.islink = false;
39
   }
37 40

  
38 41
    public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, URL folder) {
39 42
        super(parent, ScriptingManager.UNIT_FOLDER, manager, null);
......
43 46
        File f = new File(folder.getPath());
44 47
        this.setId(f.getName());
45 48
        this.folder = f;
49
        this.islink = false;
46 50
    }
47 51

  
48 52
    @Override
......
154 158
                    ScriptingUnit unit = this.getUnit(f);
155 159
                    ol.add(unit);
156 160
                } catch (Exception ex) {
157
                    logger.warn("Can't create unit from file '" + f.getAbsolutePath() + "'.");
161
                    logger.warn("Can't create unit from file '" + f.getAbsolutePath() + "'.",ex);
158 162
                }
159 163
            }
160 164
        }
161 165
        return ol;
162 166
    }
163 167

  
168
    @Override
164 169
    public List<ScriptingFolder> getUnitFolders() {
165 170
        List<ScriptingFolder> ol = new ArrayList<>();
166 171
        File[] files = this.folder.listFiles(new FilenameFilter() {
......
226 231
            try {
227 232
                prefs = new Ini(f);
228 233
                loadInf(prefs);
229
                String path = getInfString(
234
                String spath = getInfString(
230 235
                        prefs, 
231 236
                        ScriptingManager.UNIT_FOLDER, 
232 237
                        "path",
233
                        this.folder.getAbsolutePath()
238
                        null
234 239
                );
235
                File ff = new File(path);
236
                if( ff.isAbsolute() ) {
237
                    this.folder = ff;
240
                if( StringUtils.isEmpty(spath) ) {
241
                    this.folder = this.folder.getAbsoluteFile();
242
                    this.islink = false;
238 243
                } else {
239
                    this.folder = new File(parent.getFile(), path);
244
                    File path = new File(spath);
245
                    if( path.isAbsolute() ) {
246
                        this.folder = path;
247
                    } else {
248
                        this.folder = new File(parent.getFile(), spath);
249
                    }
250
                    this.islink = true;
240 251
                }
241 252
            } catch (Exception e) {
242 253
                logger.warn("Can't load 'inf' file '" + f.getAbsolutePath() + "'.", e);
......
251 262

  
252 263
    @Override
253 264
    public boolean remove() {
254
        File folder = this.getParent().getFile();
255
        File f = new File(folder, this.getId());
265
        File f = this.getFile();
256 266
        try {
257
            FileUtils.forceDelete(f);
267
            File infofile = getFileResource(".inf");
268
            if( infofile.exists() ) {
269
                FileUtils.forceDelete(infofile);
270
            }            
271
            if( !this.islink ) {
272
                if( f!=null ) {
273
                    FileUtils.forceDelete(this.getFile());
274
                }
275
            }
258 276
            return true;
259
        } catch (IOException e) {
260
            logger.warn("Can't remove folder '" + f.getAbsolutePath() + "'.", e);
277
        } catch (Throwable e) {
278
            logger.warn("Can't remove folder '" + ((f==null)?"unknown":f.getAbsolutePath()) + "'.", e);
261 279
            return false;
262 280
        }
263 281
    }
......
268 286
        f.mkdir();
269 287
        this.load(folder, id);
270 288
    }
289

  
290
    @Override
291
    public boolean move(ScriptingFolder target) {
292
        if (! manager.validateUnitId(target, this.getId()) ) {
293
            logger.info("Can't move folder '"+this.getId()+"' to '"+target.getFile().getAbsolutePath()+"', is not valid.");
294
            return false;
295
        }
296
        try {
297
            File infofile = getFileResource(".inf");
298
            if( infofile.exists() ) {
299
                FileUtils.moveFileToDirectory(infofile, target.getFile(), true);
300
            }
301
            if( !this.islink ) {
302
                FileUtils.moveDirectoryToDirectory(this.getFile(), target.getFile(),true);
303
            }
304
            this.parent = target;
305
            this.load(target, id);
306
        } catch (IOException ex) {
307
            logger.info("Can't move folder '"+this.getId()+"' to '"+target.getFile().getAbsolutePath()+"', "+ex.getMessage(),ex);
308
            return false;
309
        }
310
        return true;
311
    }
312

  
313
    @Override
314
    public boolean rename(String newId) {
315
        if (! manager.validateUnitId(this.getParent(), newId) ) {
316
            logger.info("Can't rename folder '"+this.getId()+"', target id '"+newId+"' is not valid.");
317
            return false;
318
        }
319
        try {
320
            File target = this.getParent().getFile();
321
            File infofile = getFileResource(".inf");
322
            if( this.islink ) {
323
                FileUtils.moveFile(infofile, new File(target,newId));
324
            } else {
325
                FileUtils.moveDirectory(this.getFile(), new File(target,newId));
326
                if( infofile.exists() ) {
327
                    FileUtils.moveFile(infofile, new File(target,newId));
328
                }
329
            }
330
            this.setId(newId);
331
            this.load(this.getParent(), id);
332
        } catch (IOException ex) {
333
            logger.info("Can't rename folder '"+this.getId()+"' to '"+newId+"', "+ex.getMessage(),ex);
334
            return false;
335
        }        
336
        return true;
337
    }
338
    
339
    
271 340
}
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingDialog.java
166 166
    public String getMimeType() {
167 167
        return "text";
168 168
    }
169

  
170
    @Override
171
    public boolean move(ScriptingFolder target) {
172
        File dlgfile = this.getFileResource(".dlg");
173
        if( !super.move(target) ) {
174
            return false;
175
        }
176
        try {
177
            FileUtils.moveFileToDirectory(dlgfile, target.getFile(),true);
178
        } catch (IOException ex) {
179
            logger.info("Can't move script '"+this.getId()+"' to '"+target.getFile().getAbsolutePath()+"', "+ex.getMessage(),ex);
180
            return false;
181
        }        
182
        return true;
183
    }
184

  
185
    @Override
186
    public boolean rename(String newId) {
187
        File dlgfile = this.getFileResource(".dlg");
188
        if( !super.rename(newId) ) {
189
            return false;
190
        }
191
        try {
192
            ScriptingFolder target = this.getParent();
193
            FileUtils.moveFile(dlgfile, new File(target.getFile(),newId+".dlg"));
194
        } catch (IOException ex) {
195
            logger.info("Can't rename script '"+this.getId()+"' to '"+newId+"', "+ex.getMessage(),ex);
196
            return false;
197
        }        
198
        return true;
199
        
200
    }
201

  
202
    
203
    
169 204
}

Also available in: Unified diff