Revision 1947 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.dynform/org.gvsig.tools.dynform.spi/src/main/java/org/gvsig/tools/dynform/spi/dynform/AbstractJDynForm.java
AbstractJDynForm.java | ||
---|---|---|
6 | 6 |
import java.awt.Dimension; |
7 | 7 |
import java.awt.event.MouseAdapter; |
8 | 8 |
import java.awt.event.MouseEvent; |
9 |
import java.io.IOException; |
|
9 | 10 |
import java.util.ArrayList; |
11 |
import java.util.Collection; |
|
12 |
import java.util.Collections; |
|
10 | 13 |
import java.util.HashMap; |
11 | 14 |
import java.util.HashSet; |
12 | 15 |
import java.util.Iterator; |
13 | 16 |
import java.util.List; |
14 | 17 |
import java.util.Map; |
15 | 18 |
import java.util.Set; |
19 |
import java.util.logging.Level; |
|
16 | 20 |
import javax.swing.Action; |
17 | 21 |
import javax.swing.BorderFactory; |
18 | 22 |
import javax.swing.JComponent; |
... | ... | |
21 | 25 |
import javax.swing.JPanel; |
22 | 26 |
import javax.swing.JScrollPane; |
23 | 27 |
import javax.swing.JViewport; |
28 |
import org.apache.commons.io.IOUtils; |
|
24 | 29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.expressionevaluator.Code; |
|
31 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
32 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
33 |
import org.gvsig.expressionevaluator.Function; |
|
34 |
import org.gvsig.expressionevaluator.Interpreter; |
|
35 |
import org.gvsig.tools.ToolsLocator; |
|
25 | 36 |
import org.gvsig.tools.dataTypes.CoercionException; |
26 | 37 |
import org.gvsig.tools.dataTypes.DataType; |
27 | 38 |
import org.gvsig.tools.dataTypes.DataTypes; |
28 | 39 |
import org.gvsig.tools.dynform.DynFormDefinition; |
29 | 40 |
import org.gvsig.tools.dynform.JDynForm; |
30 | 41 |
import org.gvsig.tools.dynform.JDynFormField; |
31 |
import org.gvsig.tools.dynform.JDynFormSet; |
|
32 | 42 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
33 | 43 |
import org.gvsig.tools.dynobject.DynObject; |
34 | 44 |
import org.gvsig.tools.dynobject.Tags; |
45 |
import org.gvsig.tools.identitymanagement.SimpleIdentity; |
|
46 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
|
35 | 47 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
36 | 48 |
import org.slf4j.Logger; |
37 | 49 |
import org.slf4j.LoggerFactory; |
... | ... | |
58 | 70 |
protected DynObject values = null; |
59 | 71 |
private boolean border; |
60 | 72 |
private final PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
73 |
private Interpreter interpreter; |
|
74 |
private boolean intepreter_initialized = false; |
|
61 | 75 |
|
62 | 76 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
63 | 77 |
public AbstractJDynForm( |
... | ... | |
74 | 88 |
this.customActions = new HashMap(); |
75 | 89 |
if (definition != null) { |
76 | 90 |
this.loadDefaultValuesFromTags(definition.getTags()); |
91 |
SimpleIdentity user = ToolsLocator.getIdentityManager().getCurrentIdentity(); |
|
92 |
if( !user.canWrite(this, definition.getName()) ) { |
|
93 |
this.readOnly = true; |
|
94 |
} |
|
77 | 95 |
} |
78 | 96 |
} |
79 | 97 |
|
98 |
private Interpreter getInterpreter() { |
|
99 |
if( intepreter_initialized ) { |
|
100 |
return interpreter; |
|
101 |
} |
|
102 |
try { |
|
103 |
ResourcesStorage resources = this.context.getResourcesStorage(); |
|
104 |
if( resources==null ) { |
|
105 |
this.interpreter = null; |
|
106 |
this.intepreter_initialized = true; |
|
107 |
return null; |
|
108 |
} |
|
109 |
ResourcesStorage.Resource res = resources.getResource("jfrms"); |
|
110 |
if( res == null || !res.exists() ) { |
|
111 |
this.interpreter = null; |
|
112 |
this.intepreter_initialized = true; |
|
113 |
return null; |
|
114 |
} |
|
115 |
List<String> lines = IOUtils.readLines(res.asInputStream()); |
|
116 |
if( lines == null ) { |
|
117 |
this.interpreter = null; |
|
118 |
this.intepreter_initialized = true; |
|
119 |
return null; |
|
120 |
} |
|
121 |
String source = StringUtils.join(lines, "\n"); |
|
122 |
ExpressionEvaluatorManager expmanager = ExpressionEvaluatorLocator.getManager(); |
|
123 |
Code code = expmanager.compile(source); |
|
124 |
this.interpreter = expmanager.createInterpreter(); |
|
125 |
this.interpreter.run(code); |
|
126 |
this.intepreter_initialized = true; |
|
127 |
return interpreter; |
|
128 |
} catch (IOException ex) { |
|
129 |
LOGGER.warn("Can't load resource jfrms.", ex); |
|
130 |
this.interpreter = null; |
|
131 |
this.intepreter_initialized = true; |
|
132 |
return null; |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public boolean hasUserCode() { |
|
138 |
return this.getInterpreter()!=null; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public boolean hasUserFunction(String name) { |
|
143 |
Interpreter theInterpreter = this.getInterpreter(); |
|
144 |
if( theInterpreter==null ) { |
|
145 |
return false; |
|
146 |
} |
|
147 |
return theInterpreter.hasFunction(name); |
|
148 |
} |
|
149 |
|
|
150 |
protected void callUserEvent(String name, Object...args) { |
|
151 |
Interpreter theInterpreter = this.getInterpreter(); |
|
152 |
if( theInterpreter==null ) { |
|
153 |
return; |
|
154 |
} |
|
155 |
if( theInterpreter.hasFunction(name) ) { |
|
156 |
try { |
|
157 |
theInterpreter.call(name, args); |
|
158 |
} catch(Exception ex) { |
|
159 |
LOGGER.warn("Error calling form event '"+name+"'.", ex); |
|
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
@Override |
|
165 |
public Object callUserFunction(String function, Object...args) throws Exception { |
|
166 |
Interpreter theInterpreter = this.getInterpreter(); |
|
167 |
if( theInterpreter==null ) { |
|
168 |
throw new RuntimeException("Can't exists user code associated to this form."); |
|
169 |
} |
|
170 |
if( !theInterpreter.hasFunction(function) ) { |
|
171 |
throw new IllegalArgumentException("Don't exists function '"+function+"' associated to this form."); |
|
172 |
} |
|
173 |
return theInterpreter.call(function, args); |
|
174 |
} |
|
175 |
|
|
176 |
protected Collection<Function> getUserFunctions() { |
|
177 |
Interpreter theInterpreter = this.getInterpreter(); |
|
178 |
if( theInterpreter==null ) { |
|
179 |
return Collections.EMPTY_LIST; |
|
180 |
} |
|
181 |
return theInterpreter.getSymbolTable().localfunctions(); |
|
182 |
} |
|
183 |
|
|
80 | 184 |
public static int getLayoutFromTags(Tags tags) { |
81 | 185 |
String layoutMode = (String) tags.get( |
82 | 186 |
DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE, |
Also available in: Unified diff