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

History | View | Annotate | Download (8.01 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.HashSet;
8
import java.util.Set;
9

    
10
import org.ini4j.Ini;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
import org.apache.commons.io.FilenameUtils;
14
import org.apache.commons.lang3.BooleanUtils;
15
import org.gvsig.scripting.ScriptingFolder;
16
import org.gvsig.scripting.ScriptingManager;
17
import org.gvsig.scripting.ScriptingUnit;
18

    
19
public abstract class AbstractUnit implements ScriptingUnit, Unit {
20

    
21
    private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class);
22

    
23
    protected DefaultScriptingManager manager;
24

    
25
    protected String id;
26
    protected String name = null;
27
    protected String description;
28
    protected String createdBy;
29
    protected String version;
30
    protected ScriptingFolder parent;
31
    protected String typename;
32
    protected Set<PropertyChangeListener> changeListener;
33
    protected boolean saved;
34

    
35

    
36
    public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) {
37
        this.parent = parent;
38
        this.manager = (DefaultScriptingManager) manager;
39
        this.typename = typename;
40
        this.id = id;
41
        this.changeListener = null;
42
    }
43

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

    
77
    @Override
78
    public abstract void load(ScriptingFolder folder, String id);
79

    
80
    @Override
81
    public String toString() {
82
        if (this.getName() == null) {
83
            return "(" + this.getClass().getSimpleName() + ")";
84
        }
85
        return this.getName();
86
    }
87

    
88
    protected void setParent(ScriptingFolder parent) {
89
        this.parent = parent;
90
    }
91

    
92
    public ScriptingManager getManager() {
93
        return this.manager;
94
    }
95

    
96
    protected File getFileResource(String extension) {
97
        return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile();
98
    }
99

    
100
    @Override
101
    public String getDescription() {
102
        return this.description;
103
    }
104

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

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

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

    
120
    @Override
121
    public String getName() {
122
        if (this.name == null) {
123
            return this.id;
124
        }
125
        return this.name;
126
    }
127

    
128
    @Override
129
    public void setDescription(String description) {
130
        firePropertyChangeListener("description", description, this.description);
131
        this.description = description;
132
    }
133

    
134
    @Override
135
    public void setCreatedBy(String createdBy) {
136
        firePropertyChangeListener("createdBy", createdBy, this.createdBy);
137
        this.createdBy = createdBy;
138
    }
139

    
140
    @Override
141
    public void setVersion(String version) {
142
        firePropertyChangeListener("version", version, this.version);
143
        this.version = version;
144
    }
145

    
146
    @Override
147
    public void setId(String id) {
148
        firePropertyChangeListener("id", id, this.id);
149
        if (this instanceof ScriptingFolder) {
150
            this.id = id;
151
        } else {
152
            this.id = FilenameUtils.getBaseName(id);
153
        }
154
    }
155

    
156
   @Override
157
    public void setName(String name) {
158
        firePropertyChangeListener("name", name, this.name);
159
        this.name = name;
160
    }
161

    
162
    @Override
163
    public ScriptingFolder getParent() {
164
        return this.parent;
165
    }
166

    
167
    private String toStringNotNull(String s) {
168
        if (s == null) {
169
            return "";
170
        }
171
        return s;
172
    }
173

    
174
    protected void save(Ini prefs) {
175
        prefs.put("Unit", "type", toStringNotNull(this.getTypeName()));
176
        prefs.put("Unit", "name", this.getName());
177
        prefs.put("Unit", "description", toStringNotNull(this.getDescription()));
178
        prefs.put("Unit", "createdBy", toStringNotNull(this.getCreatedBy()));
179
        prefs.put("Unit", "version", toStringNotNull(this.getVersion()));
180

    
181
        try {
182
            prefs.store();
183
        } catch (IOException e) {
184
            File f = prefs.getFile();
185
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
186
            logger.warn("Can't save inf file '" + fname + "'.");
187
        }
188

    
189
    }
190

    
191
    protected void loadInf(Ini prefs) {
192
        String typename = getInfString(prefs, "Unit", "type", this.getTypeName());
193
        if (!this.getTypeName().equalsIgnoreCase(typename)) {
194
            File f = prefs.getFile();
195
            String fname = (f == null) ? "(null)" : f.getAbsolutePath();
196
            logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'.");
197
        }
198
        this.setName(getInfString(prefs, "Unit", "name", this.getName()));
199
        this.setDescription(getInfString(prefs, "Unit", "description", null));
200
        this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null));
201
        this.setVersion(getInfString(prefs, "Unit", "version", null));
202

    
203
    }
204

    
205
    protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) {
206
        Object r = prefs.get(section, option);
207
        if (r == null) {
208
            return defaultValue;
209
        } else {
210
            return r;
211
        }
212
    }
213

    
214
    protected String getInfString(Ini prefs, String section, String option, Object defaultValue) {
215
        String s = (String) getInfValue(prefs, section, option, defaultValue);
216
        if (s != null && s.trim().length() < 1) {
217
            return null;
218
        }
219
        return s;
220
    }
221

    
222
    protected int getInfInt(Ini prefs, String section, String option, int defaultValue) {
223
        String s = (String) getInfValue(prefs, section, option, String.valueOf(defaultValue));
224
        if (s != null && s.trim().length() < 1) {
225
            return defaultValue;
226
        }
227
        return Integer.parseInt(s);
228
    }
229

    
230
    protected boolean getInfBoolean(Ini prefs, String section, String option, boolean defaultValue) {
231
        String s = (String) prefs.get(section, option);
232
        if( s == null ) {
233
            return defaultValue;
234
        }
235
        return BooleanUtils.toBoolean(s);
236
    }
237

    
238
    protected void console_println(String s) {
239
        logger.info(s);
240
    }
241

    
242
    public void create(ScriptingFolder folder, String id) {
243
        this.setParent(folder);
244
        this.setId(id);
245

    
246
        File file = new File(folder.getFile(), id + ".inf");
247
        try {
248
            file.createNewFile();
249
        } catch (IOException e) {
250
            logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e);
251
        }
252
    }
253

    
254
    @Override
255
    public File getFile() {
256
        if (this.getId() == null) {
257
            return null;
258
        }
259
        if (this.getParent() == null) {
260
            return null;
261
        }
262
        if (this.getParent().getFile() == null) {
263
            return null;
264
        }
265
        return new File(getParent().getFile(), this.getId() + ".inf");
266
    }
267
    
268
    @Override
269
    public boolean isSaved() {
270
        return this.saved;
271
    }
272

    
273
    @Override
274
    public void setSaved(boolean saved) {
275
        this.saved = saved;
276
    }
277
}