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