Statistics
| Revision:

gvsig-scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / DefaultScriptingFolder.java @ 188

History | View | Annotate | Download (4.09 KB)

1
package org.gvsig.scripting.impl;
2

    
3
import java.io.File;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Map;
9

    
10
import org.gvsig.scripting.ScriptingFolder;
11
import org.gvsig.scripting.ScriptingManager;
12
import org.gvsig.scripting.ScriptingScript;
13
import org.gvsig.scripting.ScriptingUnit;
14

    
15

    
16
public class DefaultScriptingFolder extends AbstractUnit implements ScriptingFolder{
17

    
18
        protected File folder;
19

    
20
        public DefaultScriptingFolder(ScriptingManager manager) {
21
                super(manager);
22
                this.folder = null;
23
        }
24

    
25
        public DefaultScriptingFolder(ScriptingManager manager, File folder) {
26
                super(manager);
27
                this.folder = folder;
28
                this.setId(folder.getName());
29
        }
30
        
31
        public DefaultScriptingFolder(ScriptingManager manager, URL folder) {
32
                super(manager);
33
                if(!folder.getProtocol().equals("file")){
34
                        throw new RuntimeException("Protocol URL not supported");
35
                }
36
                File f = new File(folder.getPath());
37
                this.folder = f;
38
                this.setId(f.getName());
39
        }
40

    
41
        public String getPath(){
42
                return this.folder.getPath();
43
        }
44

    
45
        public Unit getUnit(File afile) {
46
                Unit unit=null;
47
                File file = null;
48
                if( afile.isAbsolute() ) {
49
                        file = new File( /*this.getPath() + */afile.getPath());
50
                } else {
51
                        file = new File( this.getPath()+File.pathSeparator+afile.getPath());
52
                }
53
                
54
                
55
                
56
                if (file.isDirectory()){
57
                        File inf = new File(file.getAbsolutePath()+File.separator+file.getName()+".inf");
58
                        if(inf.isFile()){
59
                                unit = new DefaultScriptingProject(this.manager);
60
                        }else{
61
                                unit = new DefaultScriptingFolder(this.manager);
62
                        }
63
                }else{
64
                        Map extensions = this.manager.getSupportedLanguagesByExtension();                
65
                        Iterator iterator = extensions.keySet().iterator();
66

    
67
                        while (iterator.hasNext()){
68
                                String extension = (String) iterator.next();
69
                                if (file.getName().toLowerCase().endsWith(extension)){
70
                                        String fname = file.getName();
71
                                        String id = fname.substring(0,fname.length()-extension.length());
72
                                        File dlg = new File(file.getParentFile().getAbsolutePath()+File.separator+id+".dlg");
73
                                        if (dlg.isFile()){
74
                                                unit = new DefaultScriptingDialog(this.manager);
75
                                        }else{
76
                                                unit = new DefaultScriptingScript(this.manager);
77
                                        }
78
                                }
79
                        }
80
                }
81
                
82
                if(unit != null){
83
                        unit.load((ScriptingFolder)new DefaultScriptingFolder(this.manager,file.getParentFile()), file.getName());
84
                }
85
                return unit;
86
        }
87

    
88
        public List getUnits() {
89
                // Listar los ficheros del directorio y pasarlo a un vector para ordenarlo
90
                List<ScriptingUnit> ol = new ArrayList<ScriptingUnit>();
91
                File[] files = this.folder.listFiles();
92
                if (files != null){
93
                        for (int i = 0; i < files.length; i++){
94
                                if(files[i].canRead() && !files[i].isHidden()){
95
                                        ScriptingUnit unit = this.getUnit(files[i]);
96
                                        if (unit!=null){
97
                                                ol.add(unit);
98
                                        }
99
                                }
100
                        }
101
                }
102
                return ol;
103
        }
104
        
105
        public void add(ScriptingUnit unit) {
106
                unit.move(this);
107
        }
108

    
109
        public void remove(ScriptingUnit unit) {
110
                String fileName = null;
111
                String s[] = null;
112
                String extension = null;
113
                List units = this.getUnits();
114
                for(int i=0; i<units.size();i++){
115
                        fileName = ((ScriptingUnit)units.get(i)).getId();
116
                        s = fileName.split("\\.");
117
                        extension = "";
118
                        if (s.length>1){
119
                                extension ="." +  s[s.length-1];
120
                                fileName = fileName.substring(0,fileName.length()-extension.length());
121
                        }
122
                        if(extension.equals("") && unit instanceof ScriptingScript){
123
                                extension = manager.getExtensionByLanguage(((ScriptingScript)unit).getLangName());
124
                        }
125
                        if(fileName.equals(unit.getId())){
126
                                //borramos fichero
127
                                File f = new File(this.getPath()+File.separator+fileName+extension);
128
                                f.delete();                                
129
                                f = new File(this.getPath()+File.separator+fileName+".inf");
130
                                if(f.exists()){
131
                                        f.delete();
132
                                }
133
                                f = new File(this.getPath()+File.separator+fileName+".dlg");
134
                                if(f.exists()){
135
                                        f.delete();
136
                                }
137
                        }        
138
                }        
139
        }
140

    
141
        public void load(ScriptingFolder folder, String id) {
142
                DefaultScriptingFolder parent = (DefaultScriptingFolder) folder;
143
                this.setParent(folder);
144
                this.setId(id);        
145
                this.folder =new File(parent.getPath()+File.separator+id);
146
        }
147
        
148
        public String[] getIconNames() {
149
                return new String[]{"folder", "folder-drag-accept"};
150
        }
151

    
152
}