gvsig-scripting / 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 @ 373
History | View | Annotate | Download (6.17 KB)
1 |
package org.gvsig.scripting.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.List; |
6 |
|
7 |
import org.ini4j.Ini; |
8 |
|
9 |
import org.gvsig.scripting.ScriptingFolder; |
10 |
import org.gvsig.scripting.ScriptingManager; |
11 |
import org.gvsig.scripting.ScriptingScript; |
12 |
import org.gvsig.scripting.ScriptingUnit; |
13 |
|
14 |
public abstract class AbstractUnit implements ScriptingUnit, Unit{ |
15 |
|
16 |
protected DefaultScriptingManager manager;
|
17 |
|
18 |
protected String id; |
19 |
protected String name = null; |
20 |
protected String description; |
21 |
protected String createdBy; |
22 |
protected String version; |
23 |
protected ScriptingFolder parent;
|
24 |
|
25 |
public AbstractUnit(ScriptingManager manager){
|
26 |
this.manager = (DefaultScriptingManager) manager;
|
27 |
} |
28 |
|
29 |
/* (non-Javadoc)
|
30 |
* @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String)
|
31 |
*/
|
32 |
public abstract void load(ScriptingFolder folder, String id); |
33 |
|
34 |
@Override
|
35 |
public String toString(){ |
36 |
return this.getName(); |
37 |
} |
38 |
|
39 |
protected void setParent(ScriptingFolder parent) { |
40 |
this.parent = parent;
|
41 |
} |
42 |
|
43 |
protected File getFileResource(String extension){ |
44 |
return new File(this.getParent().getPathFile(),this.id + extension); |
45 |
} |
46 |
|
47 |
/* (non-Javadoc)
|
48 |
* @see org.gvsig.scripting.impl.Unit#getDescription()
|
49 |
*/
|
50 |
public String getDescription() { |
51 |
return this.description; |
52 |
} |
53 |
|
54 |
public String getCreatedBy() { |
55 |
return this.createdBy; |
56 |
} |
57 |
|
58 |
public String getVersion() { |
59 |
return this.version; |
60 |
} |
61 |
|
62 |
/* (non-Javadoc)
|
63 |
* @see org.gvsig.scripting.impl.Unit#getId()
|
64 |
*/
|
65 |
public String getId() { |
66 |
return this.id; |
67 |
} |
68 |
|
69 |
/* (non-Javadoc)
|
70 |
* @see org.gvsig.scripting.impl.Unit#getName()
|
71 |
*/
|
72 |
public String getName() { |
73 |
if (this.name == null){ |
74 |
return this.id; |
75 |
} |
76 |
return this.name; |
77 |
} |
78 |
|
79 |
/* (non-Javadoc)
|
80 |
* @see org.gvsig.scripting.impl.Unit#setDescription(java.lang.String)
|
81 |
*/
|
82 |
public void setDescription(String description) { |
83 |
this.description = description;
|
84 |
} |
85 |
|
86 |
public void setCreatedBy(String createdBy) { |
87 |
this.createdBy = createdBy;
|
88 |
} |
89 |
|
90 |
public void setVersion(String version) { |
91 |
this.version = version;
|
92 |
} |
93 |
|
94 |
/* (non-Javadoc)
|
95 |
* @see org.gvsig.scripting.impl.Unit#setId(java.lang.String)
|
96 |
*/
|
97 |
public void setId(String id) { |
98 |
if(this instanceof ScriptingFolder){ |
99 |
this.id = id;
|
100 |
} else {
|
101 |
String s[] = id.split("\\."); |
102 |
if (s.length<2){ |
103 |
this.id = id;
|
104 |
} else{
|
105 |
String extension ="." + s[s.length-1]; |
106 |
this.id = id.substring(0,id.length()-extension.length()); |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
/* (non-Javadoc)
|
112 |
* @see org.gvsig.scripting.impl.Unit#setName(java.lang.String)
|
113 |
*/
|
114 |
public void setName(String name) { |
115 |
this.name = name;
|
116 |
} |
117 |
|
118 |
/* (non-Javadoc)
|
119 |
* @see org.gvsig.scripting.impl.Unit#getParent()
|
120 |
*/
|
121 |
public ScriptingFolder getParent() {
|
122 |
return this.parent; |
123 |
} |
124 |
|
125 |
/* (non-Javadoc)
|
126 |
* @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
|
127 |
*/
|
128 |
|
129 |
private boolean moveFiles(ScriptingFolder folder, String id){ |
130 |
|
131 |
if(manager.validateUnitId(folder, id)){
|
132 |
String oldId = this.getId(); |
133 |
String fileName = null; |
134 |
String s[] = null; |
135 |
String extension = null; |
136 |
List<ScriptingUnit> units = this.getParent().getUnits(); |
137 |
for(int i=0; i<units.size();i++){ |
138 |
fileName = (units.get(i)).getId(); |
139 |
s = fileName.split("\\.");
|
140 |
extension = "";
|
141 |
if (s.length>1){ |
142 |
extension ="." + s[s.length-1]; |
143 |
fileName = fileName.substring(0,fileName.length()-extension.length());
|
144 |
} |
145 |
if(extension.equals("") && this instanceof ScriptingScript){ |
146 |
extension = manager.getExtensionByLanguage(((ScriptingScript)this).getLangName());
|
147 |
} |
148 |
if(fileName.equals(oldId)){
|
149 |
//renombramos fichero
|
150 |
File f = new File(this.getParent().getPath()+File.separator+fileName+extension); |
151 |
File fDest = new File(folder.getPath()+File.separator+id+extension); |
152 |
if(this instanceof ScriptingScript){ |
153 |
String code = DefaultScriptingManager.getStringFromFile(f.getPath());
|
154 |
((ScriptingScript)this).setCode(code);
|
155 |
} |
156 |
f.renameTo(fDest); |
157 |
|
158 |
File fInf = new File(this.getParent().getPath()+File.separator+fileName+".inf"); |
159 |
if(fInf.exists()){
|
160 |
File fInfDest = new File(folder.getPath()+File.separator+id+".inf"); |
161 |
fInf.renameTo(fInfDest); |
162 |
} |
163 |
File fDialog = new File(this.getParent().getPath()+File.separator+fileName+".dlg"); |
164 |
if(fDialog.exists()){
|
165 |
File fDialogDest = new File(folder.getPath()+File.separator+id+".dlg"); |
166 |
fDialog.renameTo(fDialogDest); |
167 |
} |
168 |
} |
169 |
} |
170 |
this.setParent(folder);
|
171 |
this.setId(id);
|
172 |
return true; |
173 |
} |
174 |
return false; |
175 |
} |
176 |
|
177 |
public boolean move(ScriptingFolder target) { |
178 |
return moveFiles(target, this.getId()); |
179 |
} |
180 |
|
181 |
/* (non-Javadoc)
|
182 |
* @see org.gvsig.scripting.impl.Unit#rename(java.lang.String)
|
183 |
*/
|
184 |
public boolean rename(String newId) { |
185 |
return moveFiles(this.getParent(), newId); |
186 |
} |
187 |
|
188 |
protected void save(Ini prefs){ |
189 |
prefs.put("Unit", "name",this.getName()); |
190 |
if (this.getDescription() == null){ |
191 |
prefs.put("Unit", "description",""); |
192 |
}else {
|
193 |
prefs.put("Unit", "description",this.getDescription()); |
194 |
} |
195 |
|
196 |
if (this.getCreatedBy() == null){ |
197 |
prefs.put("Unit", "createdBy",""); |
198 |
}else {
|
199 |
prefs.put("Unit", "createdBy",this.getCreatedBy()); |
200 |
} |
201 |
|
202 |
if (this.getVersion() == null){ |
203 |
prefs.put("Unit", "version",""); |
204 |
}else {
|
205 |
prefs.put("Unit", "version",this.getVersion()); |
206 |
} |
207 |
|
208 |
try {
|
209 |
prefs.store(); |
210 |
} catch (IOException e) { |
211 |
// TODO Auto-generated catch block
|
212 |
e.printStackTrace(); |
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
protected void loadInf(Ini prefs){ |
218 |
|
219 |
this.setName((String)getInfValue(prefs,"Unit","name", this.getName())); |
220 |
String description = (String) getInfValue(prefs,"Unit","description", null); |
221 |
if (description != null && description.length() == 0){ |
222 |
description = null;
|
223 |
} |
224 |
this.setDescription(description);
|
225 |
|
226 |
String createdBy = (String) getInfValue(prefs,"Unit","createdBy", null); |
227 |
if (createdBy != null && createdBy.length() == 0){ |
228 |
createdBy = null;
|
229 |
} |
230 |
this.setCreatedBy(createdBy);
|
231 |
|
232 |
String version = (String) getInfValue(prefs,"Unit","version", null); |
233 |
if (version != null && version.length() == 0){ |
234 |
version = null;
|
235 |
} |
236 |
this.setVersion(version);
|
237 |
|
238 |
} |
239 |
|
240 |
protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue){ |
241 |
Object r = prefs.get(section,option);
|
242 |
if (r==null) |
243 |
return defaultValue;
|
244 |
else
|
245 |
return r;
|
246 |
} |
247 |
} |