svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / DefaultJFeatureReferencesForm.java @ 45827
History | View | Annotate | Download (18.8 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2014 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.featureform.swing.impl; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.awt.event.ComponentAdapter; |
30 |
import java.awt.event.ComponentEvent; |
31 |
import java.util.AbstractList; |
32 |
import java.util.List; |
33 |
import javax.swing.AbstractAction; |
34 |
import static javax.swing.Action.ACTION_COMMAND_KEY; |
35 |
import static javax.swing.Action.NAME; |
36 |
import static javax.swing.Action.SHORT_DESCRIPTION; |
37 |
import static javax.swing.Action.SMALL_ICON; |
38 |
import javax.swing.JComponent; |
39 |
import javax.swing.JPanel; |
40 |
import org.gvsig.featureform.swing.JFeatureReferencesForm; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.feature.FacadeOfAFeature; |
43 |
import org.gvsig.fmap.dal.feature.Feature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
45 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
46 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
48 |
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager; |
49 |
import org.gvsig.tools.ToolsLocator; |
50 |
import org.gvsig.tools.dispose.Disposable; |
51 |
import org.gvsig.tools.dynform.AbortActionException; |
52 |
import org.gvsig.tools.dynform.DynFormDefinition; |
53 |
import org.gvsig.tools.dynform.DynFormLocator; |
54 |
import org.gvsig.tools.dynform.DynFormManager; |
55 |
import org.gvsig.tools.dynform.JDynForm; |
56 |
import org.gvsig.tools.dynform.JDynFormField; |
57 |
import org.gvsig.tools.dynform.JDynFormSet; |
58 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NAVIGATION; |
59 |
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE; |
60 |
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
61 |
import org.gvsig.tools.dynobject.DynClass; |
62 |
import org.gvsig.tools.dynobject.DynObject; |
63 |
import org.gvsig.tools.i18n.I18nManager; |
64 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
65 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
66 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
67 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
69 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
/**
|
74 |
* @author fdiaz
|
75 |
*
|
76 |
*/
|
77 |
@SuppressWarnings("UseSpecificCatch") |
78 |
public class DefaultJFeatureReferencesForm implements Disposable, JFeatureReferencesForm { |
79 |
|
80 |
private final String REFRESHFORM_ACTION = "refreshForm"; |
81 |
|
82 |
public static class FeaturesList extends AbstractList<Feature> { |
83 |
|
84 |
private final List<FeatureReference> references; |
85 |
|
86 |
public FeaturesList(List<FeatureReference> references) { |
87 |
this.references = references;
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Feature get(int index) { |
92 |
try {
|
93 |
return this.references.get(index).getFeature(); |
94 |
} catch (DataException ex) {
|
95 |
throw new RuntimeException("Can't get feature "+index, ex); |
96 |
} |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public int size() { |
101 |
return this.references.size(); |
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
public static class DynObjectsList extends AbstractList<DynObject> { |
107 |
|
108 |
private final List<FeatureReference> references; |
109 |
|
110 |
public DynObjectsList( List<FeatureReference> references) { |
111 |
this.references = references;
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
public DynObject get(int index) { |
116 |
try {
|
117 |
Feature feature = this.references.get(index).getFeature();
|
118 |
return feature.getAsDynObject();
|
119 |
} catch (DataException ex) {
|
120 |
throw new RuntimeException("Can't get dynobject "+index, ex); |
121 |
} |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public int size() { |
126 |
return this.references.size(); |
127 |
} |
128 |
|
129 |
} |
130 |
|
131 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeatureReferencesForm.class); |
132 |
|
133 |
private JPanel panel; |
134 |
private JDynFormSet formset;
|
135 |
private DynFormDefinition definition = null; |
136 |
private final ActionListenerSupport actionListeners; |
137 |
private List<FeatureReference> references; |
138 |
private List<Feature> features; |
139 |
private List<DynObject> dynobjects; |
140 |
private FeatureStore store;
|
141 |
|
142 |
|
143 |
public DefaultJFeatureReferencesForm() {
|
144 |
this.panel = new JPanel(new BorderLayout()); |
145 |
this.panel.addComponentListener(new ComponentAdapter() { |
146 |
@Override
|
147 |
public void componentHidden(ComponentEvent e) { |
148 |
dispose(); |
149 |
} |
150 |
}); |
151 |
this.actionListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void setPreferredSize(Dimension dimension) { |
156 |
panel.setPreferredSize(dimension); |
157 |
} |
158 |
|
159 |
private void updateForm() { |
160 |
if (this.formset == null) { |
161 |
this.getFormset();
|
162 |
} |
163 |
try {
|
164 |
this.formset.setValues(this.dynobjects); |
165 |
} catch (Exception ex) { |
166 |
throw new RuntimeException("Can't update form", ex); |
167 |
} |
168 |
updateButtonEnabledStatus(); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public JComponent asJComponent() { |
173 |
// if (this.features == null) {
|
174 |
// try {
|
175 |
updateForm(); |
176 |
// } catch (Exception ex) {
|
177 |
// throw new RuntimeException(ex);
|
178 |
// }
|
179 |
// }
|
180 |
return this.panel; |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void bind(FeatureStore store, List<FeatureReference> references) { |
185 |
this.bind(store, null, references); |
186 |
} |
187 |
|
188 |
public void bind(FeatureStore store, DynClass definition, List<FeatureReference> references) { |
189 |
if (store == null || references == null) { |
190 |
throw new IllegalArgumentException("bind need a store as parameter, not a null."); |
191 |
} |
192 |
try {
|
193 |
DynFormManager formManager = DynFormLocator.getDynFormManager(); |
194 |
if (definition == null) { |
195 |
DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager(); |
196 |
definition = manager.featureType2DynClass(store, store.getDefaultFeatureType()); |
197 |
} |
198 |
this.store = store;
|
199 |
this.definition = formManager.getDefinition(definition);
|
200 |
this.setFeatures(references);
|
201 |
|
202 |
} catch (Exception ex) { |
203 |
throw new RuntimeException("Can't bind features of '" + store.getName() + "' to form", ex); |
204 |
} |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void dispose() { |
209 |
this.panel = null; |
210 |
this.formset = null; |
211 |
this.features = null; |
212 |
this.definition = null; |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public JDynFormSet getFormset() {
|
217 |
if (this.formset == null) { |
218 |
DynFormManager formManager = DynFormLocator.getDynFormManager(); |
219 |
this.formset = formManager.createJDynFormSet(
|
220 |
new DefaultFeaturesFormContext(this.store), |
221 |
this.definition,
|
222 |
null
|
223 |
); |
224 |
|
225 |
List<String> groups = this.definition.getGroups(); |
226 |
if (groups.size() == 1 && groups.get(0) == null) { |
227 |
this.formset.setLayoutMode(JDynForm.USE_PLAIN);
|
228 |
} else {
|
229 |
this.formset.setLayoutMode(JDynForm.USE_TABS);
|
230 |
} |
231 |
this.formset.setAllowNew(false); |
232 |
this.formset.setAllowDelete(false); |
233 |
this.formset.setAllowUpdate(true); |
234 |
this.formset.setAllowClose(true); |
235 |
this.formset.setAllowSearch(false); |
236 |
this.formset.setAutosave(true); |
237 |
|
238 |
this.formset.addAction(new RefreshAction()); |
239 |
|
240 |
this.formset.addListener(new FormSetListener()); |
241 |
this.formset.getForm().addListener(new JDynForm.JDynFormListener() { |
242 |
@Override
|
243 |
public void message(String string) { |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public void fieldChanged(JDynFormField jdff) { |
248 |
updateButtonEnabledStatus(); |
249 |
} |
250 |
}); |
251 |
|
252 |
ToolsSwingLocator.getToolsSwingManager().removeBorder(this.formset.asJComponent());
|
253 |
this.panel.add(this.formset.asJComponent(), BorderLayout.CENTER); |
254 |
this.panel.revalidate();
|
255 |
ToolsSwingUtils.ensureRowsCols(this.panel, 8, 70, 25, 100); |
256 |
|
257 |
} |
258 |
updateButtonEnabledStatus(); |
259 |
return this.formset; |
260 |
} |
261 |
|
262 |
@Override
|
263 |
public long getCurrentIndex() { |
264 |
if (this.formset == null) { |
265 |
return -1; |
266 |
} |
267 |
return this.formset.getCurrentIndex(); |
268 |
} |
269 |
|
270 |
@Override
|
271 |
public Feature get(long index) { |
272 |
if (this.formset == null || this.features == null) { |
273 |
return null; |
274 |
} |
275 |
return this.features.get((int)index); |
276 |
} |
277 |
|
278 |
private class RefreshAction extends AbstractAction { |
279 |
|
280 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
281 |
public RefreshAction() {
|
282 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
283 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
284 |
|
285 |
this.putValue(NAME, null); |
286 |
this.putValue(SHORT_DESCRIPTION, i18nManager.getTranslation("_Reload_data")); |
287 |
this.putValue(SMALL_ICON, iconTheme.get("form-refresh-data")); |
288 |
this.putValue(ACTION_COMMAND_KEY, REFRESHFORM_ACTION);
|
289 |
this.setEnabled(!formset.isInNewState());
|
290 |
} |
291 |
|
292 |
@Override
|
293 |
public void actionPerformed(ActionEvent ae) { |
294 |
try {
|
295 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
296 |
formset.message(i18n.getTranslation("_Form_reloaded"));
|
297 |
int x = formset.getCurrentIndex();
|
298 |
updateForm(); |
299 |
formset.setCurrentIndex(x); |
300 |
} catch (Exception ex) { |
301 |
LOGGER.warn("Can't reload form", ex);
|
302 |
} |
303 |
} |
304 |
} |
305 |
|
306 |
@Override
|
307 |
public void showForm(MODE mode) { |
308 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
309 |
String title = this.definition.getLabel(); |
310 |
winmgr.showWindow(this.asJComponent(), title, mode);
|
311 |
} |
312 |
|
313 |
private void saveChanges(JDynFormSet theFormSet) { |
314 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
315 |
try {
|
316 |
int index = theFormSet.getCurrentIndex();
|
317 |
DynObject currentElement = theFormSet.get(index); |
318 |
theFormSet.getFormValues(currentElement); |
319 |
store.update(((FacadeOfAFeature) currentElement).getEditableFeature()); |
320 |
this.formset.message(i18n.getTranslation("_Record_saved")); |
321 |
try {
|
322 |
this.formset.setCurrentIndex(index);
|
323 |
} catch (Exception ex) { |
324 |
LOGGER.warn("Can't reload form data after insert.", ex);
|
325 |
} |
326 |
} catch (Exception ex) { |
327 |
theFormSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
|
328 |
throw new RuntimeException("Can't save values", ex); |
329 |
|
330 |
} finally {
|
331 |
updateButtonEnabledStatus(); |
332 |
} |
333 |
|
334 |
} |
335 |
|
336 |
@Override
|
337 |
public void saveChanges() { |
338 |
if (this.formset != null && this.formset.countValues() > 0) { |
339 |
this.saveChanges(this.formset); |
340 |
} |
341 |
} |
342 |
|
343 |
private void updateButtonEnabledStatus() { |
344 |
if (this.formset == null) { |
345 |
return;
|
346 |
} |
347 |
if (this.store == null || store.isBroken() || this.references == null) { |
348 |
this.formset.setReadOnly(true); |
349 |
formset.setActionEnabled(ACTION_SAVE, false);
|
350 |
formset.setActionEnabled(REFRESHFORM_ACTION, false);
|
351 |
formset.setActionEnabled(ACTION_NAVIGATION, false);
|
352 |
return;
|
353 |
} |
354 |
this.formset.setReadOnly(false); |
355 |
if (this.features != null && this.features.isEmpty()) { |
356 |
formset.getForm().setReadOnly(true);
|
357 |
formset.setActionEnabled(ACTION_SAVE, false);
|
358 |
formset.setActionEnabled(REFRESHFORM_ACTION, false);
|
359 |
formset.setActionEnabled(ACTION_NAVIGATION, false);
|
360 |
|
361 |
} else if (formset.getForm().isModified()) { |
362 |
formset.setActionEnabled(ACTION_SAVE, true);
|
363 |
formset.setActionEnabled(REFRESHFORM_ACTION, true);
|
364 |
formset.setActionEnabled(ACTION_NAVIGATION, true);
|
365 |
|
366 |
} else {
|
367 |
formset.setActionEnabled(ACTION_SAVE, false);
|
368 |
formset.setActionEnabled(REFRESHFORM_ACTION, true);
|
369 |
formset.setActionEnabled(ACTION_NAVIGATION, true);
|
370 |
} |
371 |
} |
372 |
|
373 |
private void clearUniqueFields() { |
374 |
for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) { |
375 |
if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
|
376 |
JDynFormField field = formset.getForm().getField(attr.getName()); |
377 |
if (field != null) { |
378 |
field.clear(); |
379 |
} |
380 |
} |
381 |
} |
382 |
} |
383 |
|
384 |
private void setEnabledUniqueFields(boolean enabled) { |
385 |
for (FeatureAttributeDescriptor attr : this.store.getDefaultFeatureTypeQuietly()) { |
386 |
if ((attr.isPrimaryKey() && !attr.isAutomatic()) || (attr.isIndexed() && !attr.allowIndexDuplicateds())) {
|
387 |
JDynFormField field = formset.getForm().getField(attr.getName()); |
388 |
if (field != null) { |
389 |
field.setReadOnly(!enabled); |
390 |
} |
391 |
} |
392 |
} |
393 |
} |
394 |
|
395 |
@Override
|
396 |
public long getDataSetSize() { |
397 |
if (this.references != null) { |
398 |
return references.size();
|
399 |
} |
400 |
return 0; |
401 |
} |
402 |
|
403 |
@Override
|
404 |
public FeatureStore getFeatureStore() {
|
405 |
return this.store; |
406 |
} |
407 |
|
408 |
private class FormSetListener implements JDynFormSetListener { |
409 |
|
410 |
@Override
|
411 |
public void formMessage(String message) { |
412 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMessage")); |
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void formClose() { |
417 |
panel.setVisible(false);
|
418 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formClose")); |
419 |
} |
420 |
|
421 |
@Override
|
422 |
public void formMovedTo(int currentPosition) throws AbortActionException { |
423 |
LOGGER.trace("formMovedTo " + currentPosition);
|
424 |
updateButtonEnabledStatus(); |
425 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formMovedTo")); |
426 |
} |
427 |
|
428 |
@Override
|
429 |
public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException { |
430 |
LOGGER.trace("formBeforeSave");
|
431 |
saveChanges(dynformSet); |
432 |
updateButtonEnabledStatus(); |
433 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeSave")); |
434 |
} |
435 |
|
436 |
@Override
|
437 |
public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException { |
438 |
LOGGER.trace("formBeforeNew");
|
439 |
clearUniqueFields(); |
440 |
updateButtonEnabledStatus(); |
441 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeNew")); |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException { |
446 |
LOGGER.trace("formBeforeDelete");
|
447 |
updateButtonEnabledStatus(); |
448 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeDelete")); |
449 |
} |
450 |
|
451 |
@Override
|
452 |
public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException { |
453 |
LOGGER.trace("formAfterSave");
|
454 |
updateButtonEnabledStatus(); |
455 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterSave")); |
456 |
} |
457 |
|
458 |
@Override
|
459 |
public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException { |
460 |
LOGGER.trace("formAfterNew");
|
461 |
updateButtonEnabledStatus(); |
462 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterNew")); |
463 |
} |
464 |
|
465 |
@Override
|
466 |
public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException { |
467 |
LOGGER.trace("formAfterDelete");
|
468 |
} |
469 |
|
470 |
@Override
|
471 |
public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException { |
472 |
LOGGER.trace("formBeforeSearch");
|
473 |
} |
474 |
|
475 |
@Override
|
476 |
public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException { |
477 |
LOGGER.trace("formAfterSearch");
|
478 |
} |
479 |
|
480 |
@Override
|
481 |
public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
482 |
LOGGER.trace("formBeforeCancelNew");
|
483 |
updateButtonEnabledStatus(); |
484 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formBeforeCancelNew")); |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
489 |
LOGGER.trace("formAfterCancelNew");
|
490 |
updateButtonEnabledStatus(); |
491 |
actionListeners.fireActionEvent(new ActionEvent(this, 1, "formAfterCancelNew")); |
492 |
} |
493 |
} |
494 |
|
495 |
@Override
|
496 |
public void addActionListener(ActionListener listener) { |
497 |
this.actionListeners.addActionListener(listener);
|
498 |
} |
499 |
|
500 |
@Override
|
501 |
public ActionListener[] getActionListeners() { |
502 |
return this.actionListeners.getActionListeners(); |
503 |
} |
504 |
|
505 |
@Override
|
506 |
public void removeActionListener(ActionListener listener) { |
507 |
this.actionListeners.removeActionListener(listener);
|
508 |
} |
509 |
|
510 |
@Override
|
511 |
public void removeAllActionListener() { |
512 |
this.actionListeners.removeAllActionListener();
|
513 |
} |
514 |
|
515 |
@Override
|
516 |
public void fireActionEvent(ActionEvent event) { |
517 |
this.actionListeners.fireActionEvent(event);
|
518 |
} |
519 |
|
520 |
@Override
|
521 |
public boolean hasActionListeners() { |
522 |
return this.actionListeners.hasActionListeners(); |
523 |
} |
524 |
|
525 |
@Override
|
526 |
public void setFeatures(List<FeatureReference> references) { |
527 |
this.references = references;
|
528 |
this.features = new FeaturesList(references); |
529 |
this.dynobjects = new DynObjectsList(references); |
530 |
if (formset != null) { |
531 |
this.panel.remove(formset.asJComponent());
|
532 |
this.panel.revalidate();
|
533 |
this.formset = null; |
534 |
} |
535 |
this.updateForm();
|
536 |
} |
537 |
|
538 |
} |