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 @ 2285
History | View | Annotate | Download (14 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.JPopupMenu; |
41 |
import javax.swing.JScrollPane; |
42 |
import javax.swing.JSpinner; |
43 |
import javax.swing.JTable; |
44 |
import javax.swing.JTextArea; |
45 |
import javax.swing.text.DefaultEditorKit; |
46 |
import javax.swing.text.JTextComponent; |
47 |
import org.apache.commons.lang3.StringUtils; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.dynform.DynFormDefinition; |
50 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
51 |
import org.gvsig.tools.dynform.DynFormLocator; |
52 |
import org.gvsig.tools.dynform.DynFormManager; |
53 |
import org.gvsig.tools.dynform.JDynForm.DynFormContext; |
54 |
import org.gvsig.tools.dynform.JDynFormField; |
55 |
import org.gvsig.tools.dynform.spi.DynFormSPILocator; |
56 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
57 |
import org.gvsig.tools.dynform.spi.dynform.JDynFormFactory; |
58 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomSpinner; |
59 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextArea; |
60 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextField; |
61 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
62 |
import org.gvsig.tools.dynform.spi.dynformset.JDynFormSetFactory; |
63 |
import org.gvsig.tools.dynform.spi.dynformfield.JProblemIndicator; |
64 |
import org.gvsig.tools.dynform.spi.dynformfield.JZoomDialog; |
65 |
import org.gvsig.tools.dynobject.Tags; |
66 |
import org.gvsig.tools.i18n.I18nManager; |
67 |
import org.gvsig.tools.service.spi.NotRegisteredException; |
68 |
import org.gvsig.tools.util.IsApplicableSupport; |
69 |
import org.slf4j.Logger; |
70 |
import org.slf4j.LoggerFactory; |
71 |
|
72 |
public class DefaultDynFormSPIManager implements DynFormSPIManager { |
73 |
|
74 |
class DefaultComponentsFactory implements ComponentsFactory { |
75 |
|
76 |
private class DefaultScrolledComponent<T> implements ScrolledComponent<T> { |
77 |
private final T component; |
78 |
private final JScrollPane scrollPane; |
79 |
|
80 |
public DefaultScrolledComponent(JScrollPane scrollPane, T component) { |
81 |
this.component = component;
|
82 |
this.scrollPane = scrollPane;
|
83 |
} |
84 |
|
85 |
@Override
|
86 |
public JScrollPane getScrollPane() { |
87 |
return this.scrollPane; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public T getComponent() {
|
92 |
return this.component; |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public boolean containsComponents(DynFormFieldDefinition definition) { |
98 |
return false; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean containsJComboBox(DynFormFieldDefinition definition, String suffix) { |
103 |
return false; |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public boolean containsJLabel(DynFormFieldDefinition definition, String suffix) { |
108 |
return false; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public boolean containsJButton(DynFormFieldDefinition definition, String suffix) { |
113 |
return false; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public boolean containsJSpinner(DynFormFieldDefinition definition, String suffix) { |
118 |
return false; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public boolean containsJTextField(DynFormFieldDefinition definition, String suffix) { |
123 |
return false; |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public JCheckBox getJCheckBox(DynFormFieldDefinition definition, String suffix) { |
128 |
return new JCheckBox(); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public JLabel getJLabel(DynFormFieldDefinition definition, String suffix) { |
133 |
return new JLabel(); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public JButton getJButton(DynFormFieldDefinition definition, String suffix) { |
138 |
return new JButton(); |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public JSpinner getJSpinner(DynFormFieldDefinition definition, String suffix) { |
143 |
return new JCustomSpinner(definition.getLabel()); |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public JComboBox getJComboBox(DynFormFieldDefinition definition, String suffix) { |
148 |
return new JComboBox(); |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public JTextComponent getJTextField(DynFormFieldDefinition definition, String suffix) { |
153 |
return new JCustomTextField(definition.getLabel()); |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public ScrolledComponent<JTextArea> getJTextArea(DynFormFieldDefinition definition, String suffix) { |
158 |
JCustomTextArea component = new JCustomTextArea(definition.getLabel());
|
159 |
JScrollPane scrollPane = new JScrollPane(component); |
160 |
return new DefaultScrolledComponent<JTextArea>(scrollPane, component); |
161 |
} |
162 |
|
163 |
@Override
|
164 |
public ScrolledComponent<JList> getJList(DynFormFieldDefinition definition, String suffix) { |
165 |
JList component = new JList(); |
166 |
JScrollPane scrollPane = new JScrollPane(component); |
167 |
return new DefaultScrolledComponent<>(scrollPane, component); |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public ScrolledComponent<JTable> getJTable(DynFormFieldDefinition definition, String suffix) { |
172 |
JTable component = new JTable(); |
173 |
JScrollPane scrollPane = new JScrollPane(component); |
174 |
return new DefaultScrolledComponent<>(scrollPane, component); |
175 |
} |
176 |
} |
177 |
|
178 |
|
179 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDynFormSPIManager.class); |
180 |
|
181 |
private DynFormManager manager = null; |
182 |
private final Map<String,JDynFormFactory> forms; |
183 |
private final Map<String,JDynFormSetFactory> formSets; |
184 |
private final List<JDynFormFieldFactory> formFields; |
185 |
private final Map<String, PopupMenuActionFactory> popupMenuActions; |
186 |
|
187 |
public DefaultDynFormSPIManager() {
|
188 |
this.forms = new HashMap<>(); |
189 |
this.formSets = new HashMap<>(); |
190 |
this.formFields = new ArrayList<>(); |
191 |
this.popupMenuActions = new HashMap<>(); |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public ComponentsFactory createDefaultComponentsFactory() {
|
196 |
return new DefaultComponentsFactory(); |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public DynFormManager getDynFormManager() {
|
201 |
if ( manager == null ) { |
202 |
manager = DynFormLocator.getDynFormManager(); |
203 |
} |
204 |
return manager;
|
205 |
} |
206 |
|
207 |
@Override
|
208 |
public JZoomDialog createJZoomDialog(String title, String message, String text) { |
209 |
return new DefaultZoomDialog(title, text); |
210 |
} |
211 |
|
212 |
@Override
|
213 |
public JProblemIndicator createProblemIndicator(JDynFormField field) {
|
214 |
return new DefaultJProblemIndicator(field); |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public JPopupMenu createTextFieldPopupMenu(final String title, final JTextComponent component, boolean zoom) { |
219 |
JPopupMenu popupMenu = new JPopupMenu(); |
220 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
221 |
|
222 |
// JMenuItem textEditorAction = new JMenuItem("Text Editor");
|
223 |
// textEditorAction.addActionListener(new ActionListener() {
|
224 |
// public void actionPerformed(ActionEvent arg0) {
|
225 |
// JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog("Edit the field", null, component.getText());
|
226 |
// dialog.setAlwaysOnTop(true);
|
227 |
// dialog.setVisible(true);
|
228 |
// component.setText(dialog.getText());
|
229 |
// }
|
230 |
// });
|
231 |
Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) { |
232 |
@Override
|
233 |
public void actionPerformed(ActionEvent e) { |
234 |
JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog(title, null, component.getText());
|
235 |
dialog.setEditable(component.isEditable()); |
236 |
dialog.setAlwaysOnTop(true);
|
237 |
dialog.setVisible(true);
|
238 |
if ( component.isEditable() && component.isEnabled() ) {
|
239 |
component.setText(dialog.getText()); |
240 |
} |
241 |
} |
242 |
}; |
243 |
Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction); |
244 |
Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction); |
245 |
Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction); |
246 |
Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction); |
247 |
|
248 |
if ( copyAction == null |
249 |
&& cutAction == null
|
250 |
&& pasteAction == null
|
251 |
&& selectAllAction == null ) {
|
252 |
copyAction = component.getActionMap().get( i18nManager.getTranslation("copy"));
|
253 |
cutAction = component.getActionMap().get( i18nManager.getTranslation("cut"));
|
254 |
pasteAction = component.getActionMap().get( i18nManager.getTranslation("paste"));
|
255 |
selectAllAction = component.getActionMap().get( i18nManager.getTranslation("SelectAll"));
|
256 |
} else {
|
257 |
copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy")); |
258 |
cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut")); |
259 |
pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste")); |
260 |
selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll")); |
261 |
} |
262 |
|
263 |
if ( zoom ) {
|
264 |
popupMenu.add(textEditorAction); |
265 |
popupMenu.addSeparator(); |
266 |
} |
267 |
popupMenu.add(cutAction); |
268 |
popupMenu.add(copyAction); |
269 |
popupMenu.add(pasteAction); |
270 |
popupMenu.add(selectAllAction); |
271 |
|
272 |
return popupMenu;
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
public void configurePopupMenu(JDynFormField jfield) { |
277 |
for (PopupMenuActionFactory factory : getPopupMenuActionFactories()) {
|
278 |
if( factory == null ) { |
279 |
continue;
|
280 |
} |
281 |
if( factory.isApplicable(jfield) ) {
|
282 |
jfield.addActionToPopupMenu(factory.getName(), factory.create(jfield)); |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
@Override
|
288 |
public Iterable<PopupMenuActionFactory> getPopupMenuActionFactories() { |
289 |
return this.popupMenuActions.values(); |
290 |
} |
291 |
|
292 |
public void addPopupMenuAction(PopupMenuActionFactory factory) { |
293 |
this.popupMenuActions.put(factory.getName(), factory);
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void registerDynFieldFactory(JDynFormFieldFactory factory) { |
298 |
this.formFields.add(factory);
|
299 |
this.formFields.sort(null); |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void registerDynFormFactory(JDynFormFactory factory) { |
304 |
this.forms.put(factory.getName(), factory);
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void registerDynFormSetFactory(JDynFormSetFactory factory) { |
309 |
this.formSets.put(factory.getName(), factory);
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public JDynFormFactory getJDynFormFactory(DynFormContext context, DynFormDefinition definition) throws NotRegisteredException { |
314 |
for (JDynFormFactory factory : this.forms.values()) { |
315 |
if( factory.isApplicableTo(context, definition) ) {
|
316 |
return factory;
|
317 |
} |
318 |
} |
319 |
throw new NotRegisteredException(definition.getName()); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public JDynFormSetFactory getJDynFormSetFactory(DynFormContext context, DynFormDefinition definition, Tags contextTags) throws NotRegisteredException { |
324 |
for (JDynFormSetFactory factory : this.formSets.values()) { |
325 |
if( factory.isApplicableTo(context, definition, contextTags) ) {
|
326 |
return factory;
|
327 |
} |
328 |
} |
329 |
throw new NotRegisteredException(definition.getName()); |
330 |
} |
331 |
|
332 |
@Override
|
333 |
public JDynFormFieldFactory getJDynFormFieldFactory(DynFormContext context, DynFormFieldDefinition definition) throws NotRegisteredException { |
334 |
for (JDynFormFieldFactory factory : this.formFields) { |
335 |
if( factory.isApplicableTo(context, definition) ) {
|
336 |
return factory;
|
337 |
} |
338 |
} |
339 |
LOGGER.warn("Can't get JDynFormFieldFactory for '"+definition.getName()+"'."); |
340 |
JDynFormFieldFactory factory = this.getJDynFormFieldFactory("UNKNOWN"); |
341 |
if( factory==null ) { |
342 |
throw new NotRegisteredException(definition.getName()); |
343 |
} |
344 |
return factory;
|
345 |
} |
346 |
|
347 |
@Override
|
348 |
public JDynFormFieldFactory getJDynFormFieldFactory(String name) { |
349 |
for (JDynFormFieldFactory factory : this.formFields) { |
350 |
if( StringUtils.equalsIgnoreCase(name, factory.getName()) ) {
|
351 |
return factory;
|
352 |
} |
353 |
} |
354 |
return null; |
355 |
} |
356 |
|
357 |
@Override
|
358 |
public List<JDynFormFieldFactory> getJDynFormFieldFactories() { |
359 |
return Collections.unmodifiableList(this.formFields); |
360 |
} |
361 |
|
362 |
public String dumpFactories() { |
363 |
StringBuilder builder = new StringBuilder(); |
364 |
builder.append("JDynForm factories:\n");
|
365 |
for (JDynFormFactory factory : this.forms.values()) { |
366 |
builder.append("- ").append(factory).append("\n"); |
367 |
} |
368 |
builder.append("JDynFormSet factories:\n");
|
369 |
for (JDynFormSetFactory factory : this.formSets.values()) { |
370 |
builder.append("- ").append(factory).append("\n"); |
371 |
} |
372 |
builder.append("JDynFormField factories:\n");
|
373 |
for (JDynFormFieldFactory factory : this.formFields) { |
374 |
builder.append("- ").append(factory).append("\n"); |
375 |
} |
376 |
return builder.toString();
|
377 |
} |
378 |
} |