gvsig-tools / 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 @ 2035
History | View | Annotate | Download (15.4 KB)
1 |
package org.gvsig.tools.dynform.spi.dynform; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.Component; |
6 |
import java.awt.Dimension; |
7 |
import java.awt.event.MouseAdapter; |
8 |
import java.awt.event.MouseEvent; |
9 |
import java.util.ArrayList; |
10 |
import java.util.HashMap; |
11 |
import java.util.HashSet; |
12 |
import java.util.Iterator; |
13 |
import java.util.List; |
14 |
import java.util.Map; |
15 |
import java.util.Set; |
16 |
import javax.swing.Action; |
17 |
import javax.swing.BorderFactory; |
18 |
import javax.swing.JComponent; |
19 |
import javax.swing.JLabel; |
20 |
import javax.swing.JOptionPane; |
21 |
import javax.swing.JPanel; |
22 |
import javax.swing.JScrollPane; |
23 |
import javax.swing.JViewport; |
24 |
import org.apache.commons.io.IOUtils; |
25 |
import org.apache.commons.lang3.StringUtils; |
26 |
import org.gvsig.tools.ToolsLocator; |
27 |
import org.gvsig.tools.dataTypes.CoercionException; |
28 |
import org.gvsig.tools.dataTypes.DataType; |
29 |
import org.gvsig.tools.dataTypes.DataTypes; |
30 |
import org.gvsig.tools.dynform.DynFormDefinition; |
31 |
import org.gvsig.tools.dynform.JDynForm; |
32 |
import org.gvsig.tools.dynform.JDynFormField; |
33 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
34 |
import org.gvsig.tools.dynobject.DynObject; |
35 |
import org.gvsig.tools.dynobject.Tags; |
36 |
import org.gvsig.tools.identitymanagement.SimpleIdentity; |
37 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
38 |
import org.gvsig.tools.script.Script; |
39 |
import org.gvsig.tools.script.ScriptManager; |
40 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
@SuppressWarnings("UseSpecificCatch") |
45 |
public abstract class AbstractJDynForm implements JDynForm { |
46 |
|
47 |
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractJDynForm.class); |
48 |
|
49 |
private final DynFormSPIManager manager; |
50 |
private final DynFormDefinition definition; |
51 |
private final JDynFormFactory factory; |
52 |
|
53 |
private DynFormContext context;
|
54 |
|
55 |
private int formWidth = -1; |
56 |
private int formHeight = -1; |
57 |
private int layoutMode = USE_PLAIN; |
58 |
private JLabel jlabel_messages = null; |
59 |
private boolean readOnly = true; |
60 |
private Set listeners = null; |
61 |
private boolean useScrollBars = true; |
62 |
private final Map<String, List<Action>> customActions; |
63 |
private JComponent contents = null; |
64 |
protected DynObject values = null; |
65 |
private boolean border; |
66 |
private final PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
67 |
private Script script;
|
68 |
private boolean script_initialized = false; |
69 |
|
70 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
71 |
public AbstractJDynForm(
|
72 |
DynFormSPIManager manager, |
73 |
JDynFormFactory factory, |
74 |
DynFormDefinition definition, |
75 |
DynFormContext context |
76 |
) { |
77 |
this.manager = manager;
|
78 |
this.factory = factory;
|
79 |
this.definition = definition;
|
80 |
this.context = context;
|
81 |
this.listeners = new HashSet(); |
82 |
this.customActions = new HashMap(); |
83 |
if (definition != null) { |
84 |
this.loadDefaultValuesFromTags(definition.getTags());
|
85 |
SimpleIdentity user = ToolsLocator.getIdentityManager().getCurrentIdentity(); |
86 |
if( !user.canWrite(this, definition.getName()) ) { |
87 |
this.readOnly = true; |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public Script getScript() {
|
94 |
if( script_initialized ) {
|
95 |
return script;
|
96 |
} |
97 |
try {
|
98 |
ResourcesStorage resources = this.context.getResourcesStorage();
|
99 |
if( resources==null ) { |
100 |
this.script = null; |
101 |
this.script_initialized = true; |
102 |
return null; |
103 |
} |
104 |
ResourcesStorage.Resource res = resources.getResource("jfrms");
|
105 |
if( res == null || !res.exists() ) { |
106 |
this.script = null; |
107 |
this.script_initialized = true; |
108 |
return null; |
109 |
} |
110 |
String source = IOUtils.toString(res.asInputStream());
|
111 |
if( StringUtils.isEmpty(source) ) {
|
112 |
this.script = null; |
113 |
this.script_initialized = true; |
114 |
return null; |
115 |
} |
116 |
ScriptManager scriptManager = this.context.getScriptManager();
|
117 |
if( scriptManager==null ) { |
118 |
this.script = null; |
119 |
this.script_initialized = true; |
120 |
return null; |
121 |
} |
122 |
this.script = scriptManager.createScript(this.definition.getName(), source, null); |
123 |
this.script_initialized = true; |
124 |
return script;
|
125 |
} catch (Exception ex) { |
126 |
LOGGER.warn("Can't load resource jfrms.", ex);
|
127 |
this.script = null; |
128 |
this.script_initialized = true; |
129 |
return null; |
130 |
} |
131 |
} |
132 |
|
133 |
protected void callUserEvent(String name, Object...args) { |
134 |
Script theScript = this.getScript();
|
135 |
if( theScript==null ) { |
136 |
return;
|
137 |
} |
138 |
try {
|
139 |
theScript.invokeFunction(name, args); |
140 |
} catch(NoSuchMethodException ex) { |
141 |
// Do nothing
|
142 |
} catch(Exception ex) { |
143 |
LOGGER.warn("Error calling form event '"+name+"'.", ex); |
144 |
} |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public Object callUserFunction(String function, Object...args) throws Exception { |
149 |
Script theScript = this.getScript();
|
150 |
if( theScript==null ) { |
151 |
throw new RuntimeException("Can't exists user code associated to this form."); |
152 |
} |
153 |
return theScript.invokeFunction(function, args);
|
154 |
} |
155 |
|
156 |
public static int getLayoutFromTags(Tags tags) { |
157 |
String layoutMode = (String) tags.get( |
158 |
DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE, |
159 |
DataTypes.STRING, |
160 |
DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_PLAIN |
161 |
); |
162 |
if (!StringUtils.isEmpty(layoutMode)) {
|
163 |
if (layoutMode.equalsIgnoreCase("0") |
164 |
|| layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_PLAIN)) { |
165 |
return JDynForm.USE_PLAIN;
|
166 |
} else if (layoutMode.equalsIgnoreCase("1") |
167 |
|| layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_TABS)) { |
168 |
return JDynForm.USE_TABS;
|
169 |
} else if (layoutMode.equalsIgnoreCase("2") |
170 |
|| layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_SEPARATORS)) { |
171 |
return JDynForm.USE_SEPARATORS;
|
172 |
} |
173 |
} |
174 |
return JDynForm.USE_PLAIN;
|
175 |
} |
176 |
|
177 |
public void loadDefaultValuesFromTags(Tags tags) { |
178 |
this.setLayoutMode(getLayoutFromTags(tags));
|
179 |
|
180 |
try {
|
181 |
this.formWidth = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH);
|
182 |
} catch (CoercionException ex) {
|
183 |
} |
184 |
try {
|
185 |
this.formHeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT);
|
186 |
} catch (CoercionException ex) {
|
187 |
} |
188 |
try {
|
189 |
this.border = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_BORDER);
|
190 |
} catch (CoercionException ex) {
|
191 |
} |
192 |
try {
|
193 |
this.useScrollBars = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_USESCROLLBARS);
|
194 |
} catch (CoercionException ex) {
|
195 |
} |
196 |
try {
|
197 |
this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY);
|
198 |
} catch (CoercionException ex) {
|
199 |
} |
200 |
} |
201 |
|
202 |
public DynFormSPIManager getServiceManager() {
|
203 |
return this.manager; |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public JComponent asJComponent() { |
208 |
if (this.contents == null) { |
209 |
this.initComponents();
|
210 |
} |
211 |
return this.contents; |
212 |
} |
213 |
|
214 |
protected void initComponents() { |
215 |
if (this.contents == null) { |
216 |
this.contents = addScrollsAndMessageBar(this.getFieldsContainer()); |
217 |
if (this.values != null) { |
218 |
this.setValues(values);
|
219 |
} |
220 |
if (this.border) { |
221 |
this.contents.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
222 |
} |
223 |
message(); |
224 |
} |
225 |
} |
226 |
|
227 |
protected boolean isContentsInitialized() { |
228 |
return this.contents != null; |
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void addListener(JDynFormListener listener) { |
233 |
this.listeners.add(listener);
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public void removeListener(JDynFormListener listener) { |
238 |
this.listeners.remove(listener);
|
239 |
} |
240 |
|
241 |
public JLabel getMessagesJLabel() { |
242 |
if (this.jlabel_messages == null) { |
243 |
this.jlabel_messages = new JLabel(); |
244 |
this.jlabel_messages.addMouseListener(new MouseAdapter() { |
245 |
@Override
|
246 |
public void mouseClicked(MouseEvent evt) { |
247 |
int count = evt.getClickCount();
|
248 |
if (count == 2) { |
249 |
JOptionPane.showMessageDialog(contents, jlabel_messages.getText(), "Status", JOptionPane.INFORMATION_MESSAGE); |
250 |
} |
251 |
} |
252 |
}); |
253 |
} |
254 |
return this.jlabel_messages; |
255 |
} |
256 |
|
257 |
@Override
|
258 |
public void setShowMessageStatus(boolean showMessageStatus) { |
259 |
this.getMessagesJLabel().setVisible(showMessageStatus);
|
260 |
} |
261 |
|
262 |
@Override
|
263 |
public boolean isShowMessageStatus() { |
264 |
return this.getMessagesJLabel().isVisible(); |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public void message() { |
269 |
this.getMessagesJLabel().setText(" "); |
270 |
} |
271 |
|
272 |
@Override
|
273 |
public void message(String msg) { |
274 |
this.getMessagesJLabel().setText(msg);
|
275 |
fireMessageEvent(msg); |
276 |
} |
277 |
|
278 |
protected void fireMessageEvent(String message) { |
279 |
Iterator it = this.listeners.iterator(); |
280 |
while (it.hasNext()) {
|
281 |
JDynFormListener listener = (JDynFormListener) it.next(); |
282 |
try {
|
283 |
listener.message(message); |
284 |
} catch (Exception ex) { |
285 |
LOGGER.info("Error calling listener " + listener.toString()
|
286 |
+ "(" + listener.getClass().getName() + ") from " |
287 |
+ this.toString() + "(" + this.getClass().getName() |
288 |
+ ").", ex);
|
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
protected void fireFieldChangeEvent(JDynFormField field) { |
294 |
Iterator it = this.listeners.iterator(); |
295 |
while (it.hasNext()) {
|
296 |
JDynFormListener listener = (JDynFormListener) it.next(); |
297 |
try {
|
298 |
listener.fieldChanged(field); |
299 |
} catch (Exception ex) { |
300 |
LOGGER.info("Error calling listener " + listener.toString()
|
301 |
+ "(" + listener.getClass().getName() + ") from " |
302 |
+ this.toString() + "(" + this.getClass().getName() |
303 |
+ ").", ex);
|
304 |
} |
305 |
} |
306 |
} |
307 |
|
308 |
protected JPanel addScrollsAndMessageBar(JComponent formPanel) { |
309 |
formPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); |
310 |
JScrollPane scroll = new JScrollPane(formPanel); |
311 |
Dimension size = formPanel.getPreferredSize();
|
312 |
if( this.formWidth<100 ) { |
313 |
if( size.width>800 ) { |
314 |
this.formWidth = 800; |
315 |
} else {
|
316 |
this.formWidth = size.width;
|
317 |
} |
318 |
} |
319 |
if( this.formHeight<100 ) { |
320 |
if( size.height>450 ) { |
321 |
this.formHeight = 450; |
322 |
} else {
|
323 |
this.formHeight = size.height;
|
324 |
} |
325 |
} |
326 |
|
327 |
if (useScrollBars) {
|
328 |
int scrollBarWidth = scroll.getVerticalScrollBar().getPreferredSize().width;
|
329 |
scroll.setPreferredSize(new Dimension(formWidth+4, formHeight+4)); |
330 |
formPanel.setPreferredSize(new Dimension(formWidth-scrollBarWidth, size.height)); |
331 |
} else {
|
332 |
scroll.setPreferredSize(size); |
333 |
} |
334 |
scroll.setBorder(BorderFactory.createEmptyBorder());
|
335 |
JPanel jpanel = new JPanel(); |
336 |
jpanel.setLayout(new BorderLayout()); |
337 |
jpanel.add(scroll, BorderLayout.CENTER);
|
338 |
jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
|
339 |
return jpanel;
|
340 |
} |
341 |
|
342 |
@Override
|
343 |
public int getLayoutMode() { |
344 |
return this.layoutMode; |
345 |
} |
346 |
|
347 |
@Override
|
348 |
public void setLayoutMode(int layoutMode) { |
349 |
if (layoutMode < 0 || layoutMode > USE_SEPARATORS) { |
350 |
throw new IllegalArgumentException("layoutMode (" + layoutMode + ") out of range. Valid values are 0 .. " + USE_SEPARATORS + "."); |
351 |
} |
352 |
this.layoutMode = layoutMode;
|
353 |
} |
354 |
|
355 |
@Override
|
356 |
public boolean isReadOnly() { |
357 |
return readOnly;
|
358 |
} |
359 |
|
360 |
@Override
|
361 |
public void setReadOnly(boolean readOnly) { |
362 |
this.readOnly = readOnly;
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public int getFormWidth() { |
367 |
if (this.contents == null) { |
368 |
return this.formWidth; |
369 |
} else {
|
370 |
JViewport port = ((JScrollPane) this.asJComponent()).getViewport(); |
371 |
Component comp = (Component) port.getView(); |
372 |
return comp.getWidth();
|
373 |
} |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public int getFormHeight() { |
378 |
if (this.contents == null) { |
379 |
return this.formHeight; |
380 |
} else {
|
381 |
JViewport port = ((JScrollPane) this.asJComponent()).getViewport(); |
382 |
Component comp = (Component) port.getView(); |
383 |
return comp.getHeight();
|
384 |
} |
385 |
} |
386 |
|
387 |
@Override
|
388 |
public void setFormSize(int width, int height) { |
389 |
this.formHeight = height;
|
390 |
this.formWidth = width;
|
391 |
if (this.contents != null) { |
392 |
this.asJComponent().setPreferredSize(new Dimension(width, height)); |
393 |
} |
394 |
} |
395 |
|
396 |
protected List<Action> getCustomActionsForDataType(DataType dataType) { |
397 |
return (List<Action>) customActions.get(dataType.getName()); |
398 |
} |
399 |
|
400 |
@Override
|
401 |
public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
402 |
List<Action> acts = this.getCustomActionsForDataType(tipo); |
403 |
action.putValue(Action.NAME, name);
|
404 |
if (acts == null) { |
405 |
List<Action> aux = new ArrayList<>(); |
406 |
aux.add(action); |
407 |
customActions.put(tipo.getName(), aux); |
408 |
} else {
|
409 |
acts.add(action); |
410 |
} |
411 |
} |
412 |
|
413 |
@Override
|
414 |
public void addSeparatorToPopupMenu(DataType tipo) { |
415 |
List<Action> acts = this.getCustomActionsForDataType(tipo); |
416 |
if (acts == null) { |
417 |
List<Action> aux = new ArrayList<>(); |
418 |
aux.add(null);
|
419 |
customActions.put(tipo.getName(), aux); |
420 |
} else {
|
421 |
acts.add(null);
|
422 |
} |
423 |
} |
424 |
|
425 |
@Override
|
426 |
public DynFormDefinition getDefinition() {
|
427 |
return this.definition; |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public boolean getUseScrollBars() { |
432 |
return useScrollBars;
|
433 |
} |
434 |
|
435 |
@Override
|
436 |
public void setUseScrollBars(boolean usesScrolls) { |
437 |
this.useScrollBars = usesScrolls;
|
438 |
} |
439 |
|
440 |
public void setBorder(boolean border) { |
441 |
this.border = border;
|
442 |
if (this.isContentsInitialized() && border) { |
443 |
this.asJComponent().setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
444 |
} |
445 |
} |
446 |
|
447 |
@Override
|
448 |
public JDynFormField getField(String fieldName) { |
449 |
return null; |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public Object getProperty(String key) { |
454 |
return this.propertiesHelper.getProperty(key); |
455 |
} |
456 |
|
457 |
@Override
|
458 |
public void setProperty(String key, Object obj) { |
459 |
this.propertiesHelper.setProperty(key, obj);
|
460 |
} |
461 |
|
462 |
@Override
|
463 |
public Map<String, Object> getProperties() { |
464 |
return this.propertiesHelper.getProperties(); |
465 |
} |
466 |
|
467 |
@Override
|
468 |
public DynFormContext getContext() {
|
469 |
return this.context; |
470 |
} |
471 |
|
472 |
@Override
|
473 |
public void setContext(DynFormContext context) { |
474 |
this.context = context;
|
475 |
} |
476 |
|
477 |
protected abstract JComponent getFieldsContainer(); |
478 |
} |