Statistics
| Revision:

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 @ 952

History | View | Annotate | Download (9.18 KB)

1
package org.gvsig.scripting.impl;
2

    
3
import java.beans.PropertyChangeEvent;
4
import java.beans.PropertyChangeListener;
5
import java.io.File;
6
import java.io.IOException;
7
import java.util.HashMap;
8
import java.util.HashSet;
9
import java.util.Map;
10
import java.util.Set;
11

    
12
import org.ini4j.Ini;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.apache.commons.io.FilenameUtils;
16
import org.apache.commons.lang3.BooleanUtils;
17
import org.gvsig.scripting.ScriptingFolder;
18
import org.gvsig.scripting.ScriptingManager;
19
import org.gvsig.scripting.ScriptingUnit;
20
import org.ini4j.Profile.Section;
21

    
22
public abstract class AbstractUnit implements ScriptingUnit, Unit {
23

    
24
    protected static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class);
25

    
26
    protected DefaultScriptingManager manager;
27

    
28
    protected String id;
29
    protected String name = null;
30
    protected String description;
31
    protected String createdBy;
32
    protected String version;
33
    protected ScriptingFolder parent;
34
    protected String typename;
35
    protected Set<PropertyChangeListener> changeListener;
36
    protected boolean saved;
37
    private Map<String,String> properties;
38

    
39

    
40
    public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) {
41
        this.parent = parent;
42
        this.manager = (DefaultScriptingManager) manager;
43
        this.typename = typename;
44
        this.id = id;
45
        this.changeListener = null;
46
        this.properties = null;
47
    }
48

    
49
    @Override
50
    public void addPropertyChangeListener(PropertyChangeListener listener) {
51
        if( this.changeListener==null ) {
52
            this.changeListener = new HashSet<>();
53
        }
54
        this.changeListener.add(listener);
55
    }
56
    
57
    public void firePropertyChange(PropertyChangeEvent event) {
58
        if( this.changeListener==null ) {
59
            return;
60
        }
61
        for( PropertyChangeListener listener : this.changeListener ) {
62
            try {
63
                if( listener != null ) {
64
                    listener.propertyChange(event);
65
                }
66
            } catch(Exception ex) {
67
                logger.warn("Problems firing PropertyChangeListener to listener "+listener+".",ex);
68
            }
69
        }
70
    }
71
    
72
    public void firePropertyChangeListener(String name, Object oldValue, Object newValue) {
73
        PropertyChangeEvent event = new PropertyChangeEvent(this,name, oldValue, newValue);
74
        firePropertyChange(event);
75
    }
76
            
77
    @Override
78
    public String getTypeName() {
79
        return typename;
80
    }
81

    
82
    @Override
83
    public abstract void load(ScriptingFolder folder, String id);
84

    
85
    @Override
86
    public String toString() {
87
        if (this.getName() == null) {
88
            return "(" + this.getClass().getSimpleName() + ")";
89
        }
90
        return this.getName();
91
    }
92

    
93
    protected void setParent(ScriptingFolder parent) {
94
        this.parent = parent;
95
    }
96

    
97
    public ScriptingManager getManager() {
98
        return this.manager;
99
    }
100

    
101
    protected File getFileResource(String extension) {
102
        return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile();
103
    }
104

    
105
    @Override
106
    public String getDescription() {
107
        return this.description;
108
    }
109

    
110
    @Override
111
    public String getCreatedBy() {
112
        return this.createdBy;
113
    }
114

    
115
    @Override
116
    public String getVersion() {
117
        return this.version;
118
    }
119

    
120
    @Override
121
    public String getId() {
122
        return this.id;
123
    }
124

    
125
    @Override
126
    public String getName() {
127
        if (this.name == null) {
128
            return this.id;
129
        }
130
        return this.name;
131
    }
132

    
133
    @Override
134
    public void setDescription(String description) {
135
        firePropertyChangeListener("description", description, this.description);
136
        this.description = description;
137
    }
138

    
139
    @Override
140
    public void setCreatedBy(String createdBy) {
141
        firePropertyChangeListener("createdBy", createdBy, this.createdBy);
142
        this.createdBy = createdBy;
143
    }
144

    
145
    @Override
146
    public void setVersion(String version) {
147
        firePropertyChangeListener("version", version, this.version);
148
        this.version = version;
149
    }
150

    
151
    @Override
152
    public void setId(String id) {
153
        firePropertyChangeListener("id", id, this.id);
154
        if (this instanceof ScriptingFolder) {
155
            this.id = id;
156
        } else {
157
            this.id = FilenameUtils.getBaseName(id);
158
        }
159
    }
160

    
161
   @Override
162
    public void setName(String name) {
163
        firePropertyChangeListener("name", name, this.name);
164
        this.name = name;
165
    }
166

    
167
    @Override
168
    public ScriptingFolder getParent() {
169
        return this.parent;
170
    }
171

    
172
    private String toStringNotNull(String s) {
173
        if (s == null) {
174
            return "";
175
        }
176
        return s;
177
    }
178

    
179
    protected void save(Ini prefs) {
180
        prefs.put("Unit", "type", toStringNotNull(this.getTypeName()));
181
        prefs.put("Unit", "name", this.getName());
182
        prefs.put("Unit", "description", toStringNotNull(this.getDescription()));
183
        prefs.put("Unit", "createdBy", toStringNotNull(this.getCreatedBy()));
184
        prefs.put("Unit", "version", toStringNotNull(this.getVersion()));
185

    
186
        if( this.properties!=null ) {
187
            for (Map.Entry<String, String> property : properties.entrySet()) {
188
                String name = property.getKey();
189
                String value = property.getValue();
190
                prefs.put("properties", name, value);
191
            }
192
        }
193
        try {
194
            prefs.store();
195
        } catch (IOException e) {
196
            File f = prefs.getFile();
197
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
198
            logger.warn("Can't save inf file '" + fname + "'.");
199
        }
200

    
201
    }
202

    
203
    protected void loadInf(Ini prefs) {
204
        String typename = getInfString(prefs, "Unit", "type", this.getTypeName());
205
        if (!this.getTypeName().equalsIgnoreCase(typename)) {
206
            File f = prefs.getFile();
207
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
208
            logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'.");
209
        }
210
        this.setName(getInfString(prefs, "Unit", "name", this.getName()));
211
        this.setDescription(getInfString(prefs, "Unit", "description", null));
212
        this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null));
213
        this.setVersion(getInfString(prefs, "Unit", "version", null));
214
        
215
        Section sec = prefs.get("properties");
216
        if( sec != null ) {
217
            for(Map.Entry<String,String> entry : sec.entrySet() ) {
218
                this.setProperty(entry.getKey(), entry.getValue());
219
            }
220
        }
221
    }
222

    
223
    protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) {
224
        Object r = prefs.get(section, option);
225
        if (r == null) {
226
            return defaultValue;
227
        } else {
228
            return r;
229
        }
230
    }
231

    
232
    protected String getInfString(Ini prefs, String section, String option, Object defaultValue) {
233
        String s = (String) getInfValue(prefs, section, option, defaultValue);
234
        if (s != null && s.trim().length() < 1) {
235
            return null;
236
        }
237
        return s;
238
    }
239

    
240
    protected int getInfInt(Ini prefs, String section, String option, int defaultValue) {
241
        String s = (String) getInfValue(prefs, section, option, String.valueOf(defaultValue));
242
        if (s != null && s.trim().length() < 1) {
243
            return defaultValue;
244
        }
245
        return Integer.parseInt(s);
246
    }
247

    
248
    protected boolean getInfBoolean(Ini prefs, String section, String option, boolean defaultValue) {
249
        String s = (String) prefs.get(section, option);
250
        if( s == null ) {
251
            return defaultValue;
252
        }
253
        return BooleanUtils.toBoolean(s);
254
    }
255

    
256
    protected void console_println(String s) {
257
        logger.info(s);
258
    }
259

    
260
    public void create(ScriptingFolder folder, String id) {
261
        this.setParent(folder);
262
        this.setId(id);
263

    
264
        File file = new File(folder.getFile(), id + ".inf");
265
        try {
266
            file.createNewFile();
267
        } catch (IOException e) {
268
            logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e);
269
        }
270
    }
271

    
272
    @Override
273
    public File getFile() {
274
        if (this.getId() == null) {
275
            return null;
276
        }
277
        if (this.getParent() == null) {
278
            return null;
279
        }
280
        if (this.getParent().getFile() == null) {
281
            return null;
282
        }
283
        return new File(getParent().getFile(), this.getId() + ".inf");
284
    }
285
    
286
    @Override
287
    public boolean isSaved() {
288
        return this.saved;
289
    }
290

    
291
    @Override
292
    public void setSaved(boolean saved) {
293
        this.saved = saved;
294
    }
295

    
296
    @Override
297
    public void setProperty(String name, String value) {
298
        if( this.properties == null ) {
299
            this.properties = new HashMap<>();
300
        }
301
        this.properties.put(name, value);
302
    }
303

    
304
    @Override
305
    public String getProperty(String name) {
306
        if( this.properties == null ) {
307
            return null;
308
        }
309
        return this.properties.get(name);
310
    }
311
    
312
    public Map<String,String> getProperties() {
313
        return this.properties;
314
    }
315
    
316
}