gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultDynFormSPIManager.java @ 2821
History | View | Annotate | Download (14.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.dynform.impl; |
25 |
|
26 |
import java.awt.event.ActionEvent; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collections; |
29 |
import java.util.HashMap; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import javax.swing.AbstractAction; |
34 |
import javax.swing.Action; |
35 |
import javax.swing.JButton; |
36 |
import javax.swing.JCheckBox; |
37 |
import javax.swing.JComboBox; |
38 |
import javax.swing.JLabel; |
39 |
import javax.swing.JList; |
40 |
import javax.swing.JPasswordField; |
41 |
import javax.swing.JPopupMenu; |
42 |
import javax.swing.JScrollPane; |
43 |
import javax.swing.JSpinner; |
44 |
import javax.swing.JTable; |
45 |
import javax.swing.JTextArea; |
46 |
import javax.swing.JToggleButton; |
47 |
import javax.swing.text.DefaultEditorKit; |
48 |
import javax.swing.text.JTextComponent; |
49 |
import org.apache.commons.lang3.StringUtils; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.dynform.DynFormDefinition; |
52 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
53 |
import org.gvsig.tools.dynform.DynFormLocator; |
54 |
import org.gvsig.tools.dynform.DynFormManager; |
55 |
import org.gvsig.tools.dynform.JDynForm.DynFormContext; |
56 |
import org.gvsig.tools.dynform.JDynFormField; |
57 |
import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
58 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
59 |
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
60 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner; |
61 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextArea; |
62 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextField; |
63 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
64 |
import org.gvsig.tools.dynform.spi.dynformset.JDynFormSetFactory; |
65 |
import org.gvsig.tools.dynform.spi.dynformfield.JProblemIndicator; |
66 |
import org.gvsig.tools.dynform.spi.dynformfield.JZoomDialog; |
67 |
import org.gvsig.tools.dynobject.Tags; |
68 |
import org.gvsig.tools.i18n.I18nManager; |
69 |
import org.gvsig.tools.service.spi.NotRegisteredException; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
public class DefaultDynFormSPIManager implements DynFormSPIManager { |
74 |
|
75 |
class DefaultComponentsFactory implements ComponentsFactory { |
76 |
|
77 |
private class DefaultScrolledComponent<T> implements ScrolledComponent<T> { |
78 |
private final T component; |
79 |
private final JScrollPane scrollPane; |
80 |
|
81 |
public DefaultScrolledComponent(JScrollPane scrollPane, T component) { |
82 |
this.component = component;
|
83 |
this.scrollPane = scrollPane;
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public JScrollPane getScrollPane() { |
88 |
return this.scrollPane; |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public T getComponent() {
|
93 |
return this.component; |
94 |
} |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public boolean containsComponents(DynFormFieldDefinition definition) { |
99 |
return false; |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public boolean containsJComboBox(DynFormFieldDefinition definition, String suffix) { |
104 |
return false; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public boolean containsJLabel(DynFormFieldDefinition definition, String suffix) { |
109 |
return false; |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean containsJButton(DynFormFieldDefinition definition, String suffix) { |
114 |
return false; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public boolean containsJToggleButton(DynFormFieldDefinition definition, String suffix) { |
119 |
return false; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public boolean containsJSpinner(DynFormFieldDefinition definition, String suffix) { |
124 |
return false; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public boolean containsJTextField(DynFormFieldDefinition definition, String suffix) { |
129 |
return false; |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String suffix) { |
134 |
return new JCheckBox(); |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public JLabel getJLabel(DynFormFieldDefinition definition, String suffix) { |
139 |
return new JLabel(); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public JButton getJButton(DynFormFieldDefinition definition, String suffix) { |
144 |
return new JButton(); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public JToggleButton getJToggleButton(DynFormFieldDefinition definition, String suffix) { |
149 |
return new JToggleButton(); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public JSpinner getJSpinner(DynFormFieldDefinition definition, String suffix) { |
154 |
return new JCustomSpinner(definition.getLabel()); |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public JComboBox getJComboBox(DynFormFieldDefinition definition, String suffix) { |
159 |
return new JComboBox(); |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public JTextComponent getJTextField(DynFormFieldDefinition definition, String suffix) { |
164 |
return new JCustomTextField(definition.getLabel()); |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public JTextComponent getJPasswordField(DynFormFieldDefinition definition, String suffix) { |
169 |
return new JPasswordField(definition.getLabel()); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String suffix) { |
174 |
JCustomTextArea component = new JCustomTextArea(definition.getLabel());
|
175 |
JScrollPane scrollPane = new JScrollPane(component); |
176 |
return new DefaultScrolledComponent<>(scrollPane, component); |
177 |
} |
178 |
|
179 |
@Override
|
180 |
public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String suffix) { |
181 |
JList component = new JList(); |
182 |
JScrollPane scrollPane = new JScrollPane(component); |
183 |
return new DefaultScrolledComponent<>(scrollPane, component); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String suffix) { |
188 |
JTable component = new JTable(); |
189 |
JScrollPane scrollPane = new JScrollPane(component); |
190 |
return new DefaultScrolledComponent<>(scrollPane, component); |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDynFormSPIManager.class); |
196 |
|
197 |
private DynFormManager manager = null; |
198 |
private final Map<String,JDynFormFactory> forms; |
199 |
private final Map<String,JDynFormSetFactory> formSets; |
200 |
private final List<JDynFormFieldFactory> formFields; |
201 |
private final Map<String, PopupMenuActionFactory> popupMenuActions; |
202 |
|
203 |
public DefaultDynFormSPIManager() {
|
204 |
this.forms = new HashMap<>(); |
205 |
this.formSets = new HashMap<>(); |
206 |
this.formFields = new ArrayList<>(); |
207 |
this.popupMenuActions = new HashMap<>(); |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public ComponentsFactory createDefaultComponentsFactory() {
|
212 |
return new DefaultComponentsFactory(); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public DynFormManager getDynFormManager() {
|
217 |
if ( manager == null ) { |
218 |
manager = DynFormLocator.getDynFormManager(); |
219 |
} |
220 |
return manager;
|
221 |
} |
222 |
|
223 |
@Override
|
224 |
public JZoomDialog createJZoomDialog(String title, String message, String text) { |
225 |
return new DefaultZoomDialog(title, text); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public JProblemIndicator createProblemIndicator(JDynFormField field) {
|
230 |
return new DefaultJProblemIndicator(field); |
231 |
} |
232 |
|
233 |
@Override
|
234 |
@SuppressWarnings("null") |
235 |
public JPopupMenu createTextFieldPopupMenu(final String title, final JTextComponent component, boolean zoom) { |
236 |
JPopupMenu popupMenu = new JPopupMenu(); |
237 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
238 |
|
239 |
// JMenuItem textEditorAction = new JMenuItem("Text Editor");
|
240 |
// textEditorAction.addActionListener(new ActionListener() {
|
241 |
// public void actionPerformed(ActionEvent arg0) {
|
242 |
// JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog("Edit the field", null, component.getText());
|
243 |
// dialog.setAlwaysOnTop(true);
|
244 |
// dialog.setVisible(true);
|
245 |
// component.setText(dialog.getText());
|
246 |
// }
|
247 |
// });
|
248 |
Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) { |
249 |
@Override
|
250 |
public void actionPerformed(ActionEvent e) { |
251 |
JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog(title, null, component.getText());
|
252 |
dialog.setEditable(component.isEditable()); |
253 |
dialog.setAlwaysOnTop(true);
|
254 |
dialog.setVisible(true);
|
255 |
if ( component.isEditable() && component.isEnabled() ) {
|
256 |
component.setText(dialog.getText()); |
257 |
} |
258 |
} |
259 |
}; |
260 |
Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction); |
261 |
Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction); |
262 |
Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction); |
263 |
Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction); |
264 |
|
265 |
if ( copyAction == null |
266 |
&& cutAction == null
|
267 |
&& pasteAction == null
|
268 |
&& selectAllAction == null ) {
|
269 |
copyAction = component.getActionMap().get( i18nManager.getTranslation("copy"));
|
270 |
cutAction = component.getActionMap().get( i18nManager.getTranslation("cut"));
|
271 |
pasteAction = component.getActionMap().get( i18nManager.getTranslation("paste"));
|
272 |
selectAllAction = component.getActionMap().get( i18nManager.getTranslation("SelectAll"));
|
273 |
} else {
|
274 |
copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy")); |
275 |
cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut")); |
276 |
pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste")); |
277 |
selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll")); |
278 |
} |
279 |
|
280 |
if ( zoom ) {
|
281 |
popupMenu.add(textEditorAction); |
282 |
popupMenu.addSeparator(); |
283 |
} |
284 |
popupMenu.add(cutAction); |
285 |
popupMenu.add(copyAction); |
286 |
popupMenu.add(pasteAction); |
287 |
popupMenu.add(selectAllAction); |
288 |
|
289 |
return popupMenu;
|
290 |
} |
291 |
|
292 |
@Override
|
293 |
public void configurePopupMenu(JDynFormField jfield) { |
294 |
for (PopupMenuActionFactory factory : getPopupMenuActionFactories()) {
|
295 |
if( factory == null ) { |
296 |
continue;
|
297 |
} |
298 |
if( factory.isApplicable(jfield) ) {
|
299 |
jfield.addActionToPopupMenu(factory.getName(), factory.create(jfield)); |
300 |
} |
301 |
} |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public Iterable<PopupMenuActionFactory> getPopupMenuActionFactories() { |
306 |
return this.popupMenuActions.values(); |
307 |
} |
308 |
|
309 |
@Override
|
310 |
public void addPopupMenuAction(PopupMenuActionFactory factory) { |
311 |
this.popupMenuActions.put(factory.getName(), factory);
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public void registerDynFieldFactory(JDynFormFieldFactory factory) { |
316 |
this.formFields.add(factory);
|
317 |
this.formFields.sort(null); |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public void registerDynFormFactory(JDynFormFactory factory) { |
322 |
this.forms.put(factory.getName(), factory);
|
323 |
} |
324 |
|
325 |
@Override
|
326 |
public void registerDynFormSetFactory(JDynFormSetFactory factory) { |
327 |
this.formSets.put(factory.getName(), factory);
|
328 |
} |
329 |
|
330 |
@Override
|
331 |
public JDynFormFactory getJDynFormFactory(DynFormContext context, DynFormDefinition definition) throws NotRegisteredException { |
332 |
for (JDynFormFactory factory : this.forms.values()) { |
333 |
if( factory.isApplicableTo(context, definition) ) {
|
334 |
return factory;
|
335 |
} |
336 |
} |
337 |
throw new NotRegisteredException(definition.getName()); |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public JDynFormSetFactory getJDynFormSetFactory(DynFormContext context, DynFormDefinition definition, Tags contextTags) throws NotRegisteredException { |
342 |
for (JDynFormSetFactory factory : this.formSets.values()) { |
343 |
if( factory.isApplicableTo(context, definition, contextTags) ) {
|
344 |
return factory;
|
345 |
} |
346 |
} |
347 |
throw new NotRegisteredException(definition.getName()); |
348 |
} |
349 |
|
350 |
@Override
|
351 |
public JDynFormFieldFactory getJDynFormFieldFactory(DynFormContext context, DynFormFieldDefinition definition) throws NotRegisteredException { |
352 |
for (JDynFormFieldFactory factory : this.formFields) { |
353 |
if( factory.isApplicableTo(context, definition) ) {
|
354 |
return factory;
|
355 |
} |
356 |
} |
357 |
LOGGER.warn("Can't get JDynFormFieldFactory for '"+definition.getName()+"'."); |
358 |
JDynFormFieldFactory factory = this.getJDynFormFieldFactory("UNKNOWN"); |
359 |
if( factory==null ) { |
360 |
throw new NotRegisteredException(definition.getName()); |
361 |
} |
362 |
return factory;
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public JDynFormFieldFactory getJDynFormFieldFactory(String name) { |
367 |
for (JDynFormFieldFactory factory : this.formFields) { |
368 |
if( StringUtils.equalsIgnoreCase(name, factory.getName()) ) {
|
369 |
return factory;
|
370 |
} |
371 |
} |
372 |
return null; |
373 |
} |
374 |
|
375 |
@Override
|
376 |
public List<JDynFormFieldFactory> getJDynFormFieldFactories() { |
377 |
return Collections.unmodifiableList(this.formFields); |
378 |
} |
379 |
|
380 |
public String dumpFactories() { |
381 |
StringBuilder builder = new StringBuilder(); |
382 |
builder.append("JDynForm factories:\n");
|
383 |
for (JDynFormFactory factory : this.forms.values()) { |
384 |
builder.append("- ").append(factory).append("\n"); |
385 |
} |
386 |
builder.append("JDynFormSet factories:\n");
|
387 |
for (JDynFormSetFactory factory : this.formSets.values()) { |
388 |
builder.append("- ").append(factory).append("\n"); |
389 |
} |
390 |
builder.append("JDynFormField factories:\n");
|
391 |
for (JDynFormFieldFactory factory : this.formFields) { |
392 |
builder.append("- ").append(factory).append("\n"); |
393 |
} |
394 |
return builder.toString();
|
395 |
} |
396 |
} |