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 | 1881 | jjdelcerro | package org.gvsig.tools.dynform.spi.dynform; |
---|---|---|---|
2 | 1361 | jjdelcerro | |
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 | 1947 | jjdelcerro | import org.apache.commons.io.IOUtils; |
25 | 1405 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
26 | 1947 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
27 | 1361 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
28 | import org.gvsig.tools.dataTypes.DataType; |
||
29 | 1886 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypes; |
30 | 1361 | jjdelcerro | import org.gvsig.tools.dynform.DynFormDefinition; |
31 | import org.gvsig.tools.dynform.JDynForm; |
||
32 | import org.gvsig.tools.dynform.JDynFormField; |
||
33 | 1881 | jjdelcerro | import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
34 | 1361 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
35 | 1405 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
36 | 1947 | jjdelcerro | import org.gvsig.tools.identitymanagement.SimpleIdentity; |
37 | import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
||
38 | 1948 | jjdelcerro | import org.gvsig.tools.script.Script; |
39 | import org.gvsig.tools.script.ScriptManager; |
||
40 | 1697 | jjdelcerro | import org.gvsig.tools.util.PropertiesSupportHelper; |
41 | 1361 | jjdelcerro | import org.slf4j.Logger; |
42 | import org.slf4j.LoggerFactory; |
||
43 | |||
44 | 1983 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
45 | 1881 | jjdelcerro | public abstract class AbstractJDynForm implements JDynForm { |
46 | 1361 | jjdelcerro | |
47 | 1881 | jjdelcerro | 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 | 1361 | jjdelcerro | |
53 | 1881 | jjdelcerro | private DynFormContext context;
|
54 | |||
55 | private int formWidth = -1; |
||
56 | private int formHeight = -1; |
||
57 | 1361 | jjdelcerro | private int layoutMode = USE_PLAIN; |
58 | private JLabel jlabel_messages = null; |
||
59 | 1862 | jjdelcerro | private boolean readOnly = true; |
60 | 1361 | jjdelcerro | private Set listeners = null; |
61 | private boolean useScrollBars = true; |
||
62 | 1697 | jjdelcerro | private final Map<String, List<Action>> customActions; |
63 | 1361 | jjdelcerro | private JComponent contents = null; |
64 | protected DynObject values = null; |
||
65 | private boolean border; |
||
66 | 1697 | jjdelcerro | private final PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
67 | 1948 | jjdelcerro | private Script script;
|
68 | private boolean script_initialized = false; |
||
69 | 1361 | jjdelcerro | |
70 | 1697 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
71 | 1735 | jjdelcerro | public AbstractJDynForm(
|
72 | 1881 | jjdelcerro | DynFormSPIManager manager, |
73 | JDynFormFactory factory, |
||
74 | 1735 | jjdelcerro | DynFormDefinition definition, |
75 | DynFormContext context |
||
76 | 1881 | jjdelcerro | ) { |
77 | 1361 | jjdelcerro | this.manager = manager;
|
78 | 1881 | jjdelcerro | this.factory = factory;
|
79 | 1361 | jjdelcerro | this.definition = definition;
|
80 | 1735 | jjdelcerro | this.context = context;
|
81 | 1361 | jjdelcerro | this.listeners = new HashSet(); |
82 | this.customActions = new HashMap(); |
||
83 | 1881 | jjdelcerro | if (definition != null) { |
84 | this.loadDefaultValuesFromTags(definition.getTags());
|
||
85 | 1947 | jjdelcerro | SimpleIdentity user = ToolsLocator.getIdentityManager().getCurrentIdentity(); |
86 | if( !user.canWrite(this, definition.getName()) ) { |
||
87 | this.readOnly = true; |
||
88 | } |
||
89 | 1423 | jbadia | } |
90 | 1405 | jjdelcerro | } |
91 | |||
92 | 1948 | jjdelcerro | @Override
|
93 | public Script getScript() {
|
||
94 | if( script_initialized ) {
|
||
95 | return script;
|
||
96 | 1947 | jjdelcerro | } |
97 | try {
|
||
98 | ResourcesStorage resources = this.context.getResourcesStorage();
|
||
99 | if( resources==null ) { |
||
100 | 1948 | jjdelcerro | this.script = null; |
101 | this.script_initialized = true; |
||
102 | 1947 | jjdelcerro | return null; |
103 | } |
||
104 | ResourcesStorage.Resource res = resources.getResource("jfrms");
|
||
105 | if( res == null || !res.exists() ) { |
||
106 | 1948 | jjdelcerro | this.script = null; |
107 | this.script_initialized = true; |
||
108 | 1947 | jjdelcerro | return null; |
109 | } |
||
110 | 1948 | jjdelcerro | String source = IOUtils.toString(res.asInputStream());
|
111 | if( StringUtils.isEmpty(source) ) {
|
||
112 | this.script = null; |
||
113 | this.script_initialized = true; |
||
114 | 1947 | jjdelcerro | return null; |
115 | } |
||
116 | 1948 | jjdelcerro | 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 | 1983 | jjdelcerro | } catch (Exception ex) { |
126 | 1947 | jjdelcerro | LOGGER.warn("Can't load resource jfrms.", ex);
|
127 | 1948 | jjdelcerro | this.script = null; |
128 | this.script_initialized = true; |
||
129 | 1947 | jjdelcerro | return null; |
130 | } |
||
131 | } |
||
132 | |||
133 | protected void callUserEvent(String name, Object...args) { |
||
134 | 1948 | jjdelcerro | Script theScript = this.getScript();
|
135 | if( theScript==null ) { |
||
136 | 1947 | jjdelcerro | return;
|
137 | } |
||
138 | 1948 | jjdelcerro | 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 | 1947 | jjdelcerro | } |
145 | } |
||
146 | |||
147 | @Override
|
||
148 | public Object callUserFunction(String function, Object...args) throws Exception { |
||
149 | 1948 | jjdelcerro | Script theScript = this.getScript();
|
150 | if( theScript==null ) { |
||
151 | 1947 | jjdelcerro | throw new RuntimeException("Can't exists user code associated to this form."); |
152 | } |
||
153 | 1948 | jjdelcerro | return theScript.invokeFunction(function, args);
|
154 | 1947 | jjdelcerro | } |
155 | 1948 | jjdelcerro | |
156 | 1405 | jjdelcerro | public static int getLayoutFromTags(Tags tags) { |
157 | 1886 | jjdelcerro | String layoutMode = (String) tags.get( |
158 | DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE, |
||
159 | DataTypes.STRING, |
||
160 | DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_PLAIN |
||
161 | ); |
||
162 | 1881 | jjdelcerro | if (!StringUtils.isEmpty(layoutMode)) {
|
163 | if (layoutMode.equalsIgnoreCase("0") |
||
164 | || layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_PLAIN)) { |
||
165 | 1886 | jjdelcerro | return JDynForm.USE_PLAIN;
|
166 | 1881 | jjdelcerro | } else if (layoutMode.equalsIgnoreCase("1") |
167 | || layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_TABS)) { |
||
168 | 1886 | jjdelcerro | return JDynForm.USE_TABS;
|
169 | 1881 | jjdelcerro | } else if (layoutMode.equalsIgnoreCase("2") |
170 | || layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_SEPARATORS)) { |
||
171 | 1886 | jjdelcerro | return JDynForm.USE_SEPARATORS;
|
172 | 1361 | jjdelcerro | } |
173 | } |
||
174 | 1886 | jjdelcerro | return JDynForm.USE_PLAIN;
|
175 | 1361 | jjdelcerro | } |
176 | 1881 | jjdelcerro | |
177 | 1405 | jjdelcerro | public void loadDefaultValuesFromTags(Tags tags) { |
178 | this.setLayoutMode(getLayoutFromTags(tags));
|
||
179 | 1881 | jjdelcerro | |
180 | 1405 | jjdelcerro | 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 | 1361 | jjdelcerro | |
202 | public DynFormSPIManager getServiceManager() {
|
||
203 | 1881 | jjdelcerro | return this.manager; |
204 | 1361 | jjdelcerro | } |
205 | |||
206 | @Override
|
||
207 | public JComponent asJComponent() { |
||
208 | if (this.contents == null) { |
||
209 | this.initComponents();
|
||
210 | } |
||
211 | return this.contents; |
||
212 | } |
||
213 | 1368 | fdiaz | |
214 | 1361 | jjdelcerro | protected void initComponents() { |
215 | 1881 | jjdelcerro | if (this.contents == null) { |
216 | 1361 | jjdelcerro | this.contents = addScrollsAndMessageBar(this.getFieldsContainer()); |
217 | 1368 | fdiaz | if (this.values != null) { |
218 | this.setValues(values);
|
||
219 | } |
||
220 | 1881 | jjdelcerro | if (this.border) { |
221 | 1361 | jjdelcerro | this.contents.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
222 | 1368 | fdiaz | } |
223 | 1361 | jjdelcerro | message(); |
224 | } |
||
225 | } |
||
226 | 1368 | fdiaz | |
227 | 1361 | jjdelcerro | protected boolean isContentsInitialized() { |
228 | return this.contents != null; |
||
229 | } |
||
230 | 1368 | fdiaz | |
231 | 1361 | jjdelcerro | @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 | 1881 | jjdelcerro | LOGGER.info("Error calling listener " + listener.toString()
|
286 | 1361 | jjdelcerro | + "(" + listener.getClass().getName() + ") from " |
287 | + this.toString() + "(" + this.getClass().getName() |
||
288 | + ").", ex);
|
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | 1368 | fdiaz | |
293 | 1361 | jjdelcerro | 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 | 1881 | jjdelcerro | LOGGER.info("Error calling listener " + listener.toString()
|
301 | 1361 | jjdelcerro | + "(" + 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 | 1881 | jjdelcerro | 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 | 1361 | jjdelcerro | if (useScrollBars) {
|
328 | 1881 | jjdelcerro | 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 | 1361 | jjdelcerro | } else {
|
332 | 1881 | jjdelcerro | scroll.setPreferredSize(size); |
333 | 1361 | jjdelcerro | } |
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 | 2035 | jjdelcerro | protected List<Action> getCustomActionsForDataType(DataType dataType) { |
397 | 1361 | jjdelcerro | return (List<Action>) customActions.get(dataType.getName()); |
398 | } |
||
399 | |||
400 | @Override
|
||
401 | public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
||
402 | 2035 | jjdelcerro | List<Action> acts = this.getCustomActionsForDataType(tipo); |
403 | 1361 | jjdelcerro | 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 | 2035 | jjdelcerro | List<Action> acts = this.getCustomActionsForDataType(tipo); |
416 | 1361 | jjdelcerro | 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 | 1881 | jjdelcerro | if (this.isContentsInitialized() && border) { |
443 | 1361 | jjdelcerro | this.asJComponent().setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
444 | } |
||
445 | } |
||
446 | |||
447 | @Override
|
||
448 | public JDynFormField getField(String fieldName) { |
||
449 | return null; |
||
450 | } |
||
451 | 1368 | fdiaz | |
452 | 1697 | jjdelcerro | @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 | 1881 | jjdelcerro | public Map<String, Object> getProperties() { |
464 | 1697 | jjdelcerro | return this.propertiesHelper.getProperties(); |
465 | } |
||
466 | 1735 | jjdelcerro | |
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 | 1361 | jjdelcerro | protected abstract JComponent getFieldsContainer(); |
478 | } |