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