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