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