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 / DefaultJFeatureForm.java @ 42714
History | View | Annotate | Download (15.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.util.ArrayList; |
29 |
import java.util.List; |
30 |
import javax.swing.AbstractAction; |
31 |
import javax.swing.Action; |
32 |
|
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.JPanel; |
36 |
import org.apache.commons.lang3.StringUtils; |
37 |
|
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.featureform.swing.JFeatureForm; |
42 |
import org.gvsig.fmap.dal.DALLocator; |
43 |
import org.gvsig.fmap.dal.DataManager; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
46 |
import org.gvsig.fmap.dal.feature.Feature; |
47 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
48 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
49 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
50 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
51 |
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
52 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
53 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
54 |
import org.gvsig.fmap.dal.swing.queryfilter.QueryFilterExpresion; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.dynform.AbortActionException; |
57 |
import org.gvsig.tools.dynform.DynFormLocator; |
58 |
import org.gvsig.tools.dynform.DynFormManager; |
59 |
import org.gvsig.tools.dynform.JDynForm; |
60 |
import org.gvsig.tools.dynform.JDynFormSet; |
61 |
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
62 |
import org.gvsig.tools.dynobject.DynClass; |
63 |
import org.gvsig.tools.dynobject.DynObject; |
64 |
import org.gvsig.tools.exception.BaseException; |
65 |
import org.gvsig.tools.i18n.I18nManager; |
66 |
import org.gvsig.tools.observer.Observable; |
67 |
import org.gvsig.tools.observer.Observer; |
68 |
import org.gvsig.tools.service.ServiceException; |
69 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
70 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
71 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
72 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
73 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
74 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
75 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
76 |
|
77 |
/**
|
78 |
* @author fdiaz
|
79 |
*
|
80 |
*/
|
81 |
public class DefaultJFeatureForm implements JFeatureForm { |
82 |
|
83 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeatureForm.class); |
84 |
|
85 |
private static final int PAGE_SIZE = 500; |
86 |
private final JPanel panel; |
87 |
private JDynFormSet formset;
|
88 |
private FeatureStore store;
|
89 |
private FeaturePagingHelper ph;
|
90 |
private DynClass definition = null; |
91 |
private FeatureQuery currentQuery;
|
92 |
private List<Action> otherActions; |
93 |
|
94 |
public DefaultJFeatureForm() {
|
95 |
this.panel = new JPanel(new BorderLayout()); |
96 |
this.otherActions = new ArrayList<>(); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void setPreferredSize(Dimension dimension) { |
101 |
panel.setPreferredSize(dimension); |
102 |
} |
103 |
|
104 |
private void updateForm() { |
105 |
if (this.formset == null) { |
106 |
this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
107 |
} |
108 |
try {
|
109 |
this.ph = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE); |
110 |
this.formset.setValues(ph.asListOfDynObjects());
|
111 |
} catch (Exception ex) { |
112 |
throw new RuntimeException("Can't update form", ex); |
113 |
} |
114 |
this.formset.setReadOnly(false); |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public JComponent asJComponent() { |
119 |
if (this.ph == null) { |
120 |
try {
|
121 |
updateForm(); |
122 |
} catch (Exception ex) { |
123 |
throw new RuntimeException(ex); |
124 |
} |
125 |
} |
126 |
return this.panel; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void bind(FeatureStore store) { |
131 |
if (store == null) { |
132 |
throw new IllegalArgumentException("bind need a store as parameter, not a null."); |
133 |
} |
134 |
try {
|
135 |
this.bind(store, store.getDefaultFeatureType());
|
136 |
} catch (Exception ex) { |
137 |
throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex); |
138 |
} |
139 |
} |
140 |
|
141 |
public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException { |
142 |
if (this.store == store) { |
143 |
return;
|
144 |
} |
145 |
if( definition == null ) { |
146 |
definition = store.getDefaultFeatureType(); |
147 |
} |
148 |
if (formset != null) { |
149 |
this.panel.remove(formset.asJComponent());
|
150 |
this.formset = null; |
151 |
} |
152 |
this.store = store;
|
153 |
this.ph = null; |
154 |
this.definition = definition;
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public JDynFormSet getFormset() {
|
159 |
if (this.formset == null) { |
160 |
DynFormManager formManager = DynFormLocator.getDynFormManager(); |
161 |
try {
|
162 |
this.formset = formManager.createJDynFormSet(this.definition); |
163 |
} catch (ServiceException ex) {
|
164 |
throw new RuntimeException("Can't create form set.", ex); |
165 |
} |
166 |
|
167 |
this.formset.setLayoutMode(JDynForm.USE_SEPARATORS);
|
168 |
this.formset.setReadOnly(false); |
169 |
this.formset.setAllowClose(true); |
170 |
this.formset.setAllowDelete(false); |
171 |
this.formset.setAllowNew(false); |
172 |
this.formset.setAllowSearch(true); |
173 |
this.formset.setAllowUpdate(true); |
174 |
this.formset.setAutosave(true); |
175 |
|
176 |
this.formset.addAction(new FinishEditingAction()); |
177 |
for( Action action : this.otherActions ) { |
178 |
this.formset.addAction(action);
|
179 |
} |
180 |
|
181 |
this.formset.addListener(new FormSetListener()); |
182 |
} |
183 |
return this.formset; |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void addAction(Action action) { |
188 |
this.otherActions.add(action);
|
189 |
if( this.formset!=null ) { |
190 |
this.formset.addAction(action);
|
191 |
} |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public long getCurrentIndex() { |
196 |
if( this.formset==null ) { |
197 |
return -1; |
198 |
} |
199 |
return this.formset.getCurrentIndex(); |
200 |
} |
201 |
|
202 |
@Override
|
203 |
public Feature get(long index) { |
204 |
if( this.formset==null || this.ph==null ) { |
205 |
return null; |
206 |
} |
207 |
try {
|
208 |
return this.ph.getFeatureAt(index); |
209 |
} catch (BaseException ex) {
|
210 |
return null; |
211 |
} |
212 |
} |
213 |
|
214 |
private class FinishEditingAction extends AbstractAction implements Observer { |
215 |
|
216 |
public FinishEditingAction() {
|
217 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
218 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
219 |
|
220 |
this.putValue(NAME,null); |
221 |
this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing")); |
222 |
this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing")); |
223 |
this.putValue(ACTION_COMMAND_KEY, "finishEditing"); |
224 |
|
225 |
this.setEnabled(store.isEditing());
|
226 |
store.addObserver(this);
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public void actionPerformed(ActionEvent ae) { |
231 |
if( store.isEditing() ) {
|
232 |
try {
|
233 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
234 |
ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
235 |
int x = dialogManager.confirmDialog(
|
236 |
"? Desea terminar edicion y guardar los cambios ?\n\nPulse cancelar para cancelar la edicion y los cambios.",
|
237 |
i18nManager.getTranslation("_Stop_editing"),
|
238 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
239 |
JOptionPane.QUESTION_MESSAGE
|
240 |
); |
241 |
switch(x) {
|
242 |
case JOptionPane.YES_OPTION: |
243 |
store.finishEditing(); |
244 |
break;
|
245 |
case JOptionPane.NO_OPTION: |
246 |
break;
|
247 |
case JOptionPane.CANCEL_OPTION: |
248 |
store.cancelEditing(); |
249 |
break;
|
250 |
} |
251 |
} catch (DataException ex) {
|
252 |
LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex); |
253 |
} |
254 |
} |
255 |
} |
256 |
|
257 |
@Override
|
258 |
public void update(Observable observable, Object notification) { |
259 |
if( notification instanceof FeatureStoreNotification ) { |
260 |
FeatureStoreNotification n = (FeatureStoreNotification) notification; |
261 |
switch( n.getType() ) {
|
262 |
case FeatureStoreNotification.AFTER_STARTEDITING:
|
263 |
this.setEnabled(true); |
264 |
break;
|
265 |
case FeatureStoreNotification.AFTER_FINISHEDITING:
|
266 |
this.setEnabled(false); |
267 |
break;
|
268 |
case FeatureStoreNotification.AFTER_CANCELEDITING:
|
269 |
this.setEnabled(false); |
270 |
break;
|
271 |
} |
272 |
} |
273 |
} |
274 |
|
275 |
} |
276 |
|
277 |
@Override
|
278 |
public void setQuery(FeatureQuery query) { |
279 |
if (this.ph != null) { |
280 |
if (this.formset != null && this.formset.isAutosave() && this.formset.countValues() > 0) { |
281 |
if (!store.isEditing()) {
|
282 |
try {
|
283 |
store.edit(); |
284 |
} catch (DataException e1) {
|
285 |
throw new RuntimeException("Can't set query", e1); |
286 |
} |
287 |
} |
288 |
saveChanges(this.formset);
|
289 |
} |
290 |
} |
291 |
this.currentQuery = query;
|
292 |
updateForm(); |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void showForm(MODE mode) { |
297 |
this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
298 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
299 |
winmgr.showWindow(this.asJComponent(), store.getName(), mode);
|
300 |
} |
301 |
|
302 |
private void saveChanges(JDynFormSet dynformSet) { |
303 |
int index = dynformSet.getCurrentIndex();
|
304 |
DynObject dynObject = dynformSet.get(index); |
305 |
dynformSet.getFormValues(dynObject); |
306 |
Feature feat = store.getFeature(dynObject); |
307 |
if (feat == null) { |
308 |
LOGGER.warn("Can't get the associated feature index " + index);
|
309 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
310 |
dynformSet.message(i18nManager.getTranslation("error_saving_data_will_not_save"));
|
311 |
throw new RuntimeException("Can't save values"); |
312 |
} |
313 |
|
314 |
try {
|
315 |
ph.update(feat.getEditable()); |
316 |
} catch (BaseException e) {
|
317 |
throw new RuntimeException("Can't save values", e); |
318 |
} |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public void saveChanges() { |
323 |
if (this.formset != null && this.formset.countValues() > 0) { |
324 |
if (store != null && !store.isEditing()) { |
325 |
try {
|
326 |
store.edit(); |
327 |
} catch (DataException e1) {
|
328 |
LOGGER.warn("Can't edit the store " + store.getName());
|
329 |
throw new RuntimeException("Can't save changes.", e1); |
330 |
} |
331 |
} |
332 |
this.saveChanges(this.formset); |
333 |
} |
334 |
} |
335 |
|
336 |
@Override
|
337 |
public long getDataSetSize() { |
338 |
if (this.ph != null) { |
339 |
return ph.getTotalSize();
|
340 |
} |
341 |
return 0; |
342 |
} |
343 |
|
344 |
@Override
|
345 |
public FeatureStore getFeatureStore() {
|
346 |
return this.store; |
347 |
} |
348 |
|
349 |
private class FormSetListener implements JDynFormSetListener { |
350 |
|
351 |
@Override
|
352 |
public void formMessage(String message) { |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public void formClose() { |
357 |
panel.setVisible(false);
|
358 |
} |
359 |
|
360 |
@Override
|
361 |
public void formMovedTo(int currentPosition) throws AbortActionException { |
362 |
LOGGER.trace("formMovedTo " + currentPosition);
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException { |
367 |
LOGGER.trace("formBeforeSave");
|
368 |
if (!store.isEditing()) {
|
369 |
try {
|
370 |
store.edit(); |
371 |
} catch (DataException e1) {
|
372 |
throw new StoreEditException(e1, store.getName()); |
373 |
} |
374 |
} |
375 |
} |
376 |
|
377 |
public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException { |
378 |
LOGGER.trace("formBeforeNew");
|
379 |
} |
380 |
|
381 |
public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException { |
382 |
LOGGER.trace("formBeforeDelete");
|
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException { |
387 |
LOGGER.trace("formAfterSave");
|
388 |
saveChanges(dynformSet); |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException { |
393 |
LOGGER.trace("formAfterNew");
|
394 |
} |
395 |
|
396 |
@Override
|
397 |
public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException { |
398 |
LOGGER.trace("formAfterDelete");
|
399 |
} |
400 |
|
401 |
@Override
|
402 |
public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException { |
403 |
LOGGER.trace("formBeforeSearch");
|
404 |
DataManager dataManager = DALLocator.getDataManager(); |
405 |
DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager(); |
406 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
407 |
|
408 |
QueryFilterExpresion querypanel = dataSwingmanager.createQueryFilterExpresion(store); |
409 |
Dialog dialog = winmgr.createDialog(
|
410 |
querypanel.asJComponent(), |
411 |
"Filtro",
|
412 |
"Creacion de filtro sobre '"+store.getName()+"'", |
413 |
WindowManager_v2.BUTTONS_OK_CANCEL |
414 |
); |
415 |
dialog.show(WindowManager.MODE.DIALOG); |
416 |
|
417 |
if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
|
418 |
String expresion = querypanel.getExpresion();
|
419 |
try {
|
420 |
FeatureQuery query = store.createFeatureQuery(); |
421 |
if( !StringUtils.isEmpty(expresion) ) {
|
422 |
query.setFilter(dataManager.createExpresion(expresion)); |
423 |
} |
424 |
FeatureSet set = store.getFeatureSet(query); |
425 |
setQuery(query); |
426 |
} catch (Exception ex) { |
427 |
LOGGER.warn("Can't apply filter '" + expresion + "'.", ex); |
428 |
return;
|
429 |
} |
430 |
} |
431 |
} |
432 |
|
433 |
@Override
|
434 |
public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException { |
435 |
LOGGER.trace("formAfterSearch");
|
436 |
} |
437 |
} |
438 |
|
439 |
private static class StoreEditException extends AbortActionException { |
440 |
|
441 |
/**
|
442 |
*
|
443 |
*/
|
444 |
private static final long serialVersionUID = -7682017811778577130L; |
445 |
|
446 |
public StoreEditException(Throwable cause, String storename) { |
447 |
super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID); |
448 |
setValue("storename", storename);
|
449 |
} |
450 |
} |
451 |
} |