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 / AbstractJDynForm.java @ 1862
History | View | Annotate | Download (12 KB)
1 | 1361 | jjdelcerro | package org.gvsig.tools.dynform.spi; |
---|---|---|---|
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 | 1405 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
25 | 1361 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
26 | import org.gvsig.tools.dataTypes.DataType; |
||
27 | import org.gvsig.tools.dynform.DynFormDefinition; |
||
28 | import org.gvsig.tools.dynform.DynFormManager; |
||
29 | import org.gvsig.tools.dynform.JDynForm; |
||
30 | import org.gvsig.tools.dynform.JDynFormField; |
||
31 | 1405 | jjdelcerro | import org.gvsig.tools.dynform.JDynFormSet; |
32 | 1361 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
33 | 1405 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
34 | 1361 | jjdelcerro | import org.gvsig.tools.service.ServiceException; |
35 | 1697 | jjdelcerro | import org.gvsig.tools.util.PropertiesSupportHelper; |
36 | 1361 | jjdelcerro | import org.slf4j.Logger; |
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | |||
40 | public abstract class AbstractJDynForm implements JDynForm { |
||
41 | |||
42 | protected static final Logger logger = LoggerFactory.getLogger(AbstractJDynForm.class); |
||
43 | private int formWidth = 320; |
||
44 | private int formHeight = 540; |
||
45 | private DynFormManager manager = null; |
||
46 | private DynFormDefinition definition = null; |
||
47 | private int layoutMode = USE_PLAIN; |
||
48 | private JLabel jlabel_messages = null; |
||
49 | 1862 | jjdelcerro | private boolean readOnly = true; |
50 | 1361 | jjdelcerro | private Set listeners = null; |
51 | private boolean useScrollBars = true; |
||
52 | 1697 | jjdelcerro | private final Map<String, List<Action>> customActions; |
53 | 1361 | jjdelcerro | private JComponent contents = null; |
54 | protected DynObject values = null; |
||
55 | private boolean border; |
||
56 | private DynFormSPIManager serviceManager;
|
||
57 | 1697 | jjdelcerro | private final PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
58 | 1735 | jjdelcerro | private DynFormContext context = null; |
59 | 1361 | jjdelcerro | |
60 | 1697 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
61 | 1735 | jjdelcerro | public AbstractJDynForm(
|
62 | DynFormManager manager, |
||
63 | DynFormDefinition definition, |
||
64 | DynFormContext context |
||
65 | ) throws ServiceException {
|
||
66 | 1361 | jjdelcerro | this.manager = manager;
|
67 | this.definition = definition;
|
||
68 | 1735 | jjdelcerro | this.context = context;
|
69 | 1361 | jjdelcerro | this.listeners = new HashSet(); |
70 | this.customActions = new HashMap(); |
||
71 | 1423 | jbadia | if(definition != null){ |
72 | this.loadDefaultValuesFromTags(definition.getTags());
|
||
73 | } |
||
74 | 1405 | jjdelcerro | } |
75 | |||
76 | public static int getLayoutFromTags(Tags tags) { |
||
77 | String layoutMode = (String) tags.get(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE); |
||
78 | if( !StringUtils.isEmpty(layoutMode) ) {
|
||
79 | if( layoutMode.equalsIgnoreCase("0") || |
||
80 | layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_PLAIN) ) { |
||
81 | return JDynFormSet.USE_PLAIN;
|
||
82 | } else if( layoutMode.equalsIgnoreCase("1") || |
||
83 | layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_TABS) ) { |
||
84 | return JDynFormSet.USE_TABS;
|
||
85 | } else if( layoutMode.equalsIgnoreCase("2") || |
||
86 | layoutMode.equalsIgnoreCase(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_SEPARATORS) ) { |
||
87 | return JDynFormSet.USE_SEPARATORS;
|
||
88 | 1361 | jjdelcerro | } |
89 | } |
||
90 | 1405 | jjdelcerro | return JDynFormSet.USE_PLAIN;
|
91 | 1361 | jjdelcerro | } |
92 | 1405 | jjdelcerro | |
93 | public void loadDefaultValuesFromTags(Tags tags) { |
||
94 | this.setLayoutMode(getLayoutFromTags(tags));
|
||
95 | |||
96 | try {
|
||
97 | this.formWidth = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH);
|
||
98 | } catch (CoercionException ex) {
|
||
99 | } |
||
100 | try {
|
||
101 | this.formHeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT);
|
||
102 | } catch (CoercionException ex) {
|
||
103 | } |
||
104 | try {
|
||
105 | this.border = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_BORDER);
|
||
106 | } catch (CoercionException ex) {
|
||
107 | } |
||
108 | try {
|
||
109 | this.useScrollBars = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_USESCROLLBARS);
|
||
110 | } catch (CoercionException ex) {
|
||
111 | } |
||
112 | try {
|
||
113 | this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY);
|
||
114 | } catch (CoercionException ex) {
|
||
115 | } |
||
116 | } |
||
117 | 1361 | jjdelcerro | |
118 | public DynFormSPIManager getServiceManager() {
|
||
119 | if ( serviceManager == null ) { |
||
120 | serviceManager = DynFormSPILocator.getDynFormSPIManager(); |
||
121 | } |
||
122 | return serviceManager;
|
||
123 | } |
||
124 | |||
125 | |||
126 | @Override
|
||
127 | public JComponent asJComponent() { |
||
128 | if (this.contents == null) { |
||
129 | this.initComponents();
|
||
130 | } |
||
131 | return this.contents; |
||
132 | } |
||
133 | 1368 | fdiaz | |
134 | 1361 | jjdelcerro | protected void initComponents() { |
135 | if( this.contents == null ) { |
||
136 | this.contents = addScrollsAndMessageBar(this.getFieldsContainer()); |
||
137 | 1368 | fdiaz | if (this.values != null) { |
138 | this.setValues(values);
|
||
139 | } |
||
140 | 1361 | jjdelcerro | if( this.border ) { |
141 | this.contents.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
||
142 | 1368 | fdiaz | } |
143 | 1361 | jjdelcerro | message(); |
144 | } |
||
145 | } |
||
146 | 1368 | fdiaz | |
147 | 1361 | jjdelcerro | protected boolean isContentsInitialized() { |
148 | return this.contents != null; |
||
149 | } |
||
150 | 1368 | fdiaz | |
151 | 1361 | jjdelcerro | @Override
|
152 | public void addListener(JDynFormListener listener) { |
||
153 | this.listeners.add(listener);
|
||
154 | } |
||
155 | |||
156 | @Override
|
||
157 | public void removeListener(JDynFormListener listener) { |
||
158 | this.listeners.remove(listener);
|
||
159 | } |
||
160 | |||
161 | public JLabel getMessagesJLabel() { |
||
162 | if (this.jlabel_messages == null) { |
||
163 | this.jlabel_messages = new JLabel(); |
||
164 | this.jlabel_messages.addMouseListener(new MouseAdapter() { |
||
165 | @Override
|
||
166 | public void mouseClicked(MouseEvent evt) { |
||
167 | int count = evt.getClickCount();
|
||
168 | if (count == 2) { |
||
169 | JOptionPane.showMessageDialog(contents, jlabel_messages.getText(), "Status", JOptionPane.INFORMATION_MESSAGE); |
||
170 | } |
||
171 | } |
||
172 | }); |
||
173 | } |
||
174 | return this.jlabel_messages; |
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public void setShowMessageStatus(boolean showMessageStatus) { |
||
179 | this.getMessagesJLabel().setVisible(showMessageStatus);
|
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public boolean isShowMessageStatus() { |
||
184 | return this.getMessagesJLabel().isVisible(); |
||
185 | } |
||
186 | |||
187 | @Override
|
||
188 | public void message() { |
||
189 | this.getMessagesJLabel().setText(" "); |
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public void message(String msg) { |
||
194 | this.getMessagesJLabel().setText(msg);
|
||
195 | fireMessageEvent(msg); |
||
196 | } |
||
197 | |||
198 | protected void fireMessageEvent(String message) { |
||
199 | Iterator it = this.listeners.iterator(); |
||
200 | while (it.hasNext()) {
|
||
201 | JDynFormListener listener = (JDynFormListener) it.next(); |
||
202 | try {
|
||
203 | listener.message(message); |
||
204 | } catch (Exception ex) { |
||
205 | logger.info("Error calling listener " + listener.toString()
|
||
206 | + "(" + listener.getClass().getName() + ") from " |
||
207 | + this.toString() + "(" + this.getClass().getName() |
||
208 | + ").", ex);
|
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | 1368 | fdiaz | |
213 | 1361 | jjdelcerro | protected void fireFieldChangeEvent(JDynFormField field) { |
214 | Iterator it = this.listeners.iterator(); |
||
215 | while (it.hasNext()) {
|
||
216 | JDynFormListener listener = (JDynFormListener) it.next(); |
||
217 | try {
|
||
218 | listener.fieldChanged(field); |
||
219 | } catch (Exception ex) { |
||
220 | logger.info("Error calling listener " + listener.toString()
|
||
221 | + "(" + listener.getClass().getName() + ") from " |
||
222 | + this.toString() + "(" + this.getClass().getName() |
||
223 | + ").", ex);
|
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | protected JPanel addScrollsAndMessageBar(JComponent formPanel) { |
||
229 | formPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); |
||
230 | JScrollPane scroll = new JScrollPane(formPanel); |
||
231 | if (useScrollBars) {
|
||
232 | scroll.setPreferredSize(new Dimension(formWidth, formHeight)); |
||
233 | } else {
|
||
234 | scroll.setPreferredSize(formPanel.getPreferredSize()); |
||
235 | } |
||
236 | scroll.setBorder(BorderFactory.createEmptyBorder());
|
||
237 | JPanel jpanel = new JPanel(); |
||
238 | jpanel.setLayout(new BorderLayout()); |
||
239 | jpanel.add(scroll, BorderLayout.CENTER);
|
||
240 | jpanel.add(getMessagesJLabel(), BorderLayout.PAGE_END);
|
||
241 | return jpanel;
|
||
242 | } |
||
243 | |||
244 | @Override
|
||
245 | public int getLayoutMode() { |
||
246 | return this.layoutMode; |
||
247 | } |
||
248 | |||
249 | @Override
|
||
250 | public void setLayoutMode(int layoutMode) { |
||
251 | if (layoutMode < 0 || layoutMode > USE_SEPARATORS) { |
||
252 | throw new IllegalArgumentException("layoutMode (" + layoutMode + ") out of range. Valid values are 0 .. " + USE_SEPARATORS + "."); |
||
253 | } |
||
254 | this.layoutMode = layoutMode;
|
||
255 | } |
||
256 | |||
257 | @Override
|
||
258 | public boolean isReadOnly() { |
||
259 | return readOnly;
|
||
260 | } |
||
261 | |||
262 | @Override
|
||
263 | public void setReadOnly(boolean readOnly) { |
||
264 | this.readOnly = readOnly;
|
||
265 | } |
||
266 | |||
267 | @Override
|
||
268 | public int getFormWidth() { |
||
269 | if (this.contents == null) { |
||
270 | return this.formWidth; |
||
271 | } else {
|
||
272 | JViewport port = ((JScrollPane) this.asJComponent()).getViewport(); |
||
273 | Component comp = (Component) port.getView(); |
||
274 | return comp.getWidth();
|
||
275 | } |
||
276 | } |
||
277 | |||
278 | @Override
|
||
279 | public int getFormHeight() { |
||
280 | if (this.contents == null) { |
||
281 | return this.formHeight; |
||
282 | } else {
|
||
283 | JViewport port = ((JScrollPane) this.asJComponent()).getViewport(); |
||
284 | Component comp = (Component) port.getView(); |
||
285 | return comp.getHeight();
|
||
286 | } |
||
287 | } |
||
288 | |||
289 | @Override
|
||
290 | public void setFormSize(int width, int height) { |
||
291 | this.formHeight = height;
|
||
292 | this.formWidth = width;
|
||
293 | if (this.contents != null) { |
||
294 | this.asJComponent().setPreferredSize(new Dimension(width, height)); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | protected List<Action> getCustomFields(DataType dataType) { |
||
299 | return (List<Action>) customActions.get(dataType.getName()); |
||
300 | } |
||
301 | |||
302 | @Override
|
||
303 | public void addActionToPopupMenu(DataType tipo, String name, Action action) { |
||
304 | List<Action> acts = this.getCustomFields(tipo); |
||
305 | action.putValue(Action.NAME, name);
|
||
306 | if (acts == null) { |
||
307 | List<Action> aux = new ArrayList<>(); |
||
308 | aux.add(action); |
||
309 | customActions.put(tipo.getName(), aux); |
||
310 | } else {
|
||
311 | acts.add(action); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | @Override
|
||
316 | public void addSeparatorToPopupMenu(DataType tipo) { |
||
317 | List<Action> acts = this.getCustomFields(tipo); |
||
318 | if (acts == null) { |
||
319 | List<Action> aux = new ArrayList<>(); |
||
320 | aux.add(null);
|
||
321 | customActions.put(tipo.getName(), aux); |
||
322 | } else {
|
||
323 | acts.add(null);
|
||
324 | } |
||
325 | } |
||
326 | |||
327 | @Override
|
||
328 | public DynFormDefinition getDefinition() {
|
||
329 | return this.definition; |
||
330 | } |
||
331 | |||
332 | @Override
|
||
333 | public boolean getUseScrollBars() { |
||
334 | return useScrollBars;
|
||
335 | } |
||
336 | |||
337 | |||
338 | @Override
|
||
339 | public void setUseScrollBars(boolean usesScrolls) { |
||
340 | this.useScrollBars = usesScrolls;
|
||
341 | } |
||
342 | |||
343 | public void setBorder(boolean border) { |
||
344 | this.border = border;
|
||
345 | if( this.isContentsInitialized() && border ) { |
||
346 | this.asJComponent().setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | @Override
|
||
351 | public JDynFormField getField(String fieldName) { |
||
352 | return null; |
||
353 | } |
||
354 | 1368 | fdiaz | |
355 | 1697 | jjdelcerro | @Override
|
356 | public Object getProperty(String key) { |
||
357 | return this.propertiesHelper.getProperty(key); |
||
358 | } |
||
359 | |||
360 | @Override
|
||
361 | public void setProperty(String key, Object obj) { |
||
362 | this.propertiesHelper.setProperty(key, obj);
|
||
363 | } |
||
364 | |||
365 | @Override
|
||
366 | public Map<String,Object> getProperties() { |
||
367 | return this.propertiesHelper.getProperties(); |
||
368 | } |
||
369 | |||
370 | 1735 | jjdelcerro | |
371 | @Override
|
||
372 | public DynFormContext getContext() {
|
||
373 | return this.context; |
||
374 | } |
||
375 | |||
376 | @Override
|
||
377 | public void setContext(DynFormContext context) { |
||
378 | this.context = context;
|
||
379 | } |
||
380 | |||
381 | 1361 | jjdelcerro | protected abstract JComponent getFieldsContainer(); |
382 | } |