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 / DefaultJFeaturesForm.java @ 44346
History | View | Annotate | Download (28.1 KB)
1 | 42112 | jjdelcerro | /* 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 | 42489 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | 44291 | jjdelcerro | import java.awt.event.ComponentAdapter; |
29 | import java.awt.event.ComponentEvent; |
||
30 | 42489 | jjdelcerro | import java.util.ArrayList; |
31 | 44281 | jjdelcerro | import java.util.Collection; |
32 | 42489 | jjdelcerro | import java.util.List; |
33 | import javax.swing.AbstractAction; |
||
34 | import javax.swing.Action; |
||
35 | 44202 | jjdelcerro | import static javax.swing.Action.ACTION_COMMAND_KEY; |
36 | import static javax.swing.Action.NAME; |
||
37 | import static javax.swing.Action.SHORT_DESCRIPTION; |
||
38 | import static javax.swing.Action.SMALL_ICON; |
||
39 | 42112 | jjdelcerro | |
40 | import javax.swing.JComponent; |
||
41 | 42489 | jjdelcerro | import javax.swing.JOptionPane; |
42 | 42112 | jjdelcerro | import javax.swing.JPanel; |
43 | 44281 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
44 | 44128 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
45 | 44339 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
46 | 44239 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
47 | 42112 | jjdelcerro | |
48 | import org.slf4j.Logger; |
||
49 | import org.slf4j.LoggerFactory; |
||
50 | |||
51 | 42775 | jjdelcerro | import org.gvsig.featureform.swing.JFeaturesForm; |
52 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
53 | 44253 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
54 | import org.gvsig.fmap.dal.StoresRepository; |
||
55 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
56 | 44239 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
57 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
58 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.feature.FacadeOfAFeature; |
59 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
60 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
61 | 42489 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
62 | 44262 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
63 | 42112 | jjdelcerro | import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
64 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.swing.AbstractDALActionFactory.AbstractDALActionContext; |
65 | import org.gvsig.fmap.dal.swing.DALActionFactory; |
||
66 | 42489 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
67 | import org.gvsig.fmap.dal.swing.DataSwingManager; |
||
68 | 44202 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager; |
69 | 44281 | jjdelcerro | import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory; |
70 | 44292 | jjdelcerro | import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel; |
71 | 42112 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
72 | 44291 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
73 | 42112 | jjdelcerro | import org.gvsig.tools.dynform.AbortActionException; |
74 | 44128 | jjdelcerro | import org.gvsig.tools.dynform.DynFormDefinition; |
75 | 42112 | jjdelcerro | import org.gvsig.tools.dynform.DynFormLocator; |
76 | import org.gvsig.tools.dynform.DynFormManager; |
||
77 | 44202 | jjdelcerro | import org.gvsig.tools.dynform.JDynForm; |
78 | 42112 | jjdelcerro | import org.gvsig.tools.dynform.JDynFormSet; |
79 | import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
||
80 | import org.gvsig.tools.dynobject.DynClass; |
||
81 | import org.gvsig.tools.dynobject.DynObject; |
||
82 | 44239 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
83 | 42112 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
84 | import org.gvsig.tools.i18n.I18nManager; |
||
85 | 42489 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
86 | import org.gvsig.tools.observer.Observer; |
||
87 | 42112 | jjdelcerro | import org.gvsig.tools.service.ServiceException; |
88 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
89 | 42489 | jjdelcerro | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
90 | import org.gvsig.tools.swing.api.windowmanager.Dialog; |
||
91 | 42112 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
92 | 42489 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
93 | 42112 | jjdelcerro | import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
94 | 42489 | jjdelcerro | import org.gvsig.tools.swing.icontheme.IconTheme; |
95 | 44297 | jjdelcerro | import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
96 | 44339 | jjdelcerro | import org.gvsig.tools.script.ScriptManager; |
97 | 42112 | jjdelcerro | |
98 | /**
|
||
99 | * @author fdiaz
|
||
100 | *
|
||
101 | */
|
||
102 | 44128 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
103 | 42775 | jjdelcerro | public class DefaultJFeaturesForm implements JFeaturesForm { |
104 | 42112 | jjdelcerro | |
105 | 44253 | jjdelcerro | public class DefaultFeaturesFormContext implements FeaturesFormContext { |
106 | |||
107 | private DefaultFeaturesFormContext() {
|
||
108 | |||
109 | } |
||
110 | 44262 | jjdelcerro | |
111 | 44338 | jjdelcerro | @Override
|
112 | 44262 | jjdelcerro | public FeatureStore getFeatureStore() {
|
113 | return store;
|
||
114 | } |
||
115 | 44253 | jjdelcerro | |
116 | 44281 | jjdelcerro | @Override
|
117 | 44262 | jjdelcerro | public FeatureType getFeatureType() {
|
118 | try {
|
||
119 | return store.getDefaultFeatureType();
|
||
120 | } catch (DataException ex) {
|
||
121 | return null; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | 44253 | jjdelcerro | @Override
|
126 | public ResourcesStorage getResourcesStorage() {
|
||
127 | 44338 | jjdelcerro | if( store == null ) { |
128 | return null; |
||
129 | } |
||
130 | 44253 | jjdelcerro | return store.getResourcesStorage();
|
131 | } |
||
132 | |||
133 | 44259 | jjdelcerro | @Override
|
134 | 44262 | jjdelcerro | public StoresRepository getStoresRepository() {
|
135 | 44338 | jjdelcerro | if( store == null ) { |
136 | return null; |
||
137 | } |
||
138 | 44262 | jjdelcerro | return store.getStoresRepository();
|
139 | 44253 | jjdelcerro | } |
140 | 44339 | jjdelcerro | |
141 | @Override
|
||
142 | public ScriptManager getScriptManager() {
|
||
143 | return ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
|
||
144 | } |
||
145 | |||
146 | 44262 | jjdelcerro | |
147 | 44253 | jjdelcerro | } |
148 | |||
149 | 44291 | jjdelcerro | private class FormActionContext extends AbstractDALActionContext { |
150 | 44281 | jjdelcerro | |
151 | 44291 | jjdelcerro | public FormActionContext() {
|
152 | 44281 | jjdelcerro | super(ACTION_CONTEXT_NAME);
|
153 | } |
||
154 | |||
155 | @Override
|
||
156 | public DataStore getStore() {
|
||
157 | 44291 | jjdelcerro | return store;
|
158 | 44281 | jjdelcerro | } |
159 | |||
160 | @Override
|
||
161 | public Expression getFilter() { |
||
162 | 44291 | jjdelcerro | FeatureQuery theCurrentQuery = currentQuery; |
163 | if( theCurrentQuery!=null ) { |
||
164 | Evaluator filter = theCurrentQuery.getFilter(); |
||
165 | 44281 | jjdelcerro | if( filter instanceof ExpressionEvaluator ) { |
166 | Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
||
167 | return expression;
|
||
168 | } |
||
169 | } |
||
170 | return null; |
||
171 | } |
||
172 | |||
173 | @Override
|
||
174 | public int getSelectedsCount() { |
||
175 | return 0; |
||
176 | } |
||
177 | |||
178 | @Override
|
||
179 | public Expression getFilterForSelecteds() { |
||
180 | return null; |
||
181 | } |
||
182 | |||
183 | @Override
|
||
184 | public JComponent getActionButton(String actionName) { |
||
185 | if( formset == null ) { |
||
186 | return null; |
||
187 | } |
||
188 | return formset.getActionButton(actionName);
|
||
189 | } |
||
190 | |||
191 | } |
||
192 | |||
193 | 42775 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJFeaturesForm.class); |
194 | 42112 | jjdelcerro | |
195 | private static final int PAGE_SIZE = 500; |
||
196 | 44291 | jjdelcerro | private JPanel panel; |
197 | 42112 | jjdelcerro | private JDynFormSet formset;
|
198 | private FeatureStore store;
|
||
199 | private FeaturePagingHelper ph;
|
||
200 | 44128 | jjdelcerro | private DynFormDefinition definition = null; |
201 | 42248 | jjdelcerro | private FeatureQuery currentQuery;
|
202 | 44291 | jjdelcerro | private List<Action> otherActions; |
203 | 42714 | dmartinezizquierdo | |
204 | 42775 | jjdelcerro | public DefaultJFeaturesForm() {
|
205 | 44291 | jjdelcerro | this.otherActions = new ArrayList<>(); |
206 | 42112 | jjdelcerro | this.panel = new JPanel(new BorderLayout()); |
207 | 44291 | jjdelcerro | this.panel.addComponentListener(new ComponentAdapter() { |
208 | @Override
|
||
209 | public void componentHidden(ComponentEvent e) { |
||
210 | doDispose(); |
||
211 | } |
||
212 | }); |
||
213 | 42112 | jjdelcerro | } |
214 | |||
215 | 42489 | jjdelcerro | @Override
|
216 | 42112 | jjdelcerro | public void setPreferredSize(Dimension dimension) { |
217 | 44253 | jjdelcerro | // panel.setPreferredSize(dimension);
|
218 | 42112 | jjdelcerro | } |
219 | |||
220 | 42489 | jjdelcerro | private void updateForm() { |
221 | 42473 | fdiaz | if (this.formset == null) { |
222 | this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
||
223 | 42248 | jjdelcerro | } |
224 | 42489 | jjdelcerro | try {
|
225 | 44291 | jjdelcerro | if( this.currentQuery!=null ) { |
226 | this.currentQuery.retrievesAllAttributes();
|
||
227 | } |
||
228 | 42489 | jjdelcerro | this.ph = DALLocator.getDataManager().createFeaturePagingHelper(store, this.currentQuery, PAGE_SIZE); |
229 | this.formset.setValues(ph.asListOfDynObjects());
|
||
230 | } catch (Exception ex) { |
||
231 | throw new RuntimeException("Can't update form", ex); |
||
232 | } |
||
233 | 42248 | jjdelcerro | } |
234 | 42473 | fdiaz | |
235 | 42489 | jjdelcerro | @Override
|
236 | 42112 | jjdelcerro | public JComponent asJComponent() { |
237 | 42248 | jjdelcerro | if (this.ph == null) { |
238 | try {
|
||
239 | updateForm(); |
||
240 | } catch (Exception ex) { |
||
241 | throw new RuntimeException(ex); |
||
242 | } |
||
243 | 42473 | fdiaz | } |
244 | 42112 | jjdelcerro | return this.panel; |
245 | } |
||
246 | |||
247 | 42489 | jjdelcerro | @Override
|
248 | public void bind(FeatureStore store) { |
||
249 | if (store == null) { |
||
250 | throw new IllegalArgumentException("bind need a store as parameter, not a null."); |
||
251 | } |
||
252 | try {
|
||
253 | 44202 | jjdelcerro | DefaultDataSwingManager manager = (DefaultDataSwingManager) DALSwingLocator.getSwingManager(); |
254 | DynClass theDefinition = manager.featureType2DynClass(store, store.getDefaultFeatureType()); |
||
255 | this.bind(store, theDefinition);
|
||
256 | 42489 | jjdelcerro | } catch (Exception ex) { |
257 | throw new RuntimeException("Can't bind store '" + store.getName() + "' to form", ex); |
||
258 | } |
||
259 | 42112 | jjdelcerro | } |
260 | 42473 | fdiaz | |
261 | 42112 | jjdelcerro | public void bind(FeatureStore store, DynClass definition) throws ServiceException, DataException { |
262 | if (this.store == store) { |
||
263 | return;
|
||
264 | } |
||
265 | 44128 | jjdelcerro | DynFormManager formManager = DynFormLocator.getDynFormManager(); |
266 | 44202 | jjdelcerro | this.definition = formManager.getDefinition(definition);
|
267 | 42112 | jjdelcerro | if (formset != null) { |
268 | this.panel.remove(formset.asJComponent());
|
||
269 | this.formset = null; |
||
270 | } |
||
271 | this.store = store;
|
||
272 | 44291 | jjdelcerro | DisposeUtils.bind(store); |
273 | 42112 | jjdelcerro | this.ph = null; |
274 | 44291 | jjdelcerro | } |
275 | 44128 | jjdelcerro | |
276 | 44291 | jjdelcerro | private void doDispose() { |
277 | DisposeUtils.disposeQuietly(store); |
||
278 | this.store = null; |
||
279 | this.panel = null; |
||
280 | this.formset = null; |
||
281 | this.ph = null; |
||
282 | this.definition = null; |
||
283 | this.currentQuery = null; |
||
284 | this.otherActions = null; |
||
285 | 42112 | jjdelcerro | } |
286 | 44291 | jjdelcerro | |
287 | 42489 | jjdelcerro | @Override
|
288 | public JDynFormSet getFormset() {
|
||
289 | 42473 | fdiaz | if (this.formset == null) { |
290 | 42112 | jjdelcerro | DynFormManager formManager = DynFormLocator.getDynFormManager(); |
291 | 44253 | jjdelcerro | this.formset = formManager.createJDynFormSet(
|
292 | new DefaultFeaturesFormContext(),
|
||
293 | this.definition,
|
||
294 | null
|
||
295 | ); |
||
296 | 44202 | jjdelcerro | List<String> groups = this.definition.getGroups(); |
297 | if( groups.size()==1 && groups.get(0)==null ) { |
||
298 | this.formset.setLayoutMode(JDynForm.USE_PLAIN);
|
||
299 | } else {
|
||
300 | this.formset.setLayoutMode(JDynForm.USE_TABS);
|
||
301 | } |
||
302 | this.formset.setAllowNew(true); |
||
303 | this.formset.setAllowDelete(true); |
||
304 | this.formset.setAllowUpdate(true); |
||
305 | 42112 | jjdelcerro | this.formset.setAllowClose(true); |
306 | 42489 | jjdelcerro | this.formset.setAllowSearch(true); |
307 | 42112 | jjdelcerro | this.formset.setAutosave(true); |
308 | 42714 | dmartinezizquierdo | |
309 | 44202 | jjdelcerro | this.formset.addAction(new StartEditingAction()); |
310 | 42489 | jjdelcerro | this.formset.addAction(new FinishEditingAction()); |
311 | 44281 | jjdelcerro | |
312 | 44291 | jjdelcerro | FormActionContext actionContext = new FormActionContext();
|
313 | 44281 | jjdelcerro | Collection<DALActionFactory> factories = DALSwingLocator.getSwingManager().getStoreActions();
|
314 | for (DALActionFactory factory : factories) {
|
||
315 | if( StringUtils.equalsIgnoreCase(factory.getName(), ShowFormActionFactory.ACTION_NAME) ) {
|
||
316 | continue;
|
||
317 | } |
||
318 | Action action = factory.createAction(actionContext);
|
||
319 | this.formset.addAction(action);
|
||
320 | } |
||
321 | 42489 | jjdelcerro | for( Action action : this.otherActions ) { |
322 | this.formset.addAction(action);
|
||
323 | } |
||
324 | 42714 | dmartinezizquierdo | |
325 | 42489 | jjdelcerro | this.formset.addListener(new FormSetListener()); |
326 | 42112 | jjdelcerro | } |
327 | 44202 | jjdelcerro | if( this.store.isEditing() ) { |
328 | 44287 | jjdelcerro | // this.formset.setReadOnly(false);
|
329 | 44202 | jjdelcerro | formset.setActionVisible("startEditing", false); |
330 | formset.setActionEnabled("startEditing", false); |
||
331 | formset.setActionVisible("finishEditing", true); |
||
332 | formset.setActionEnabled("finishEditing", true); |
||
333 | } else {
|
||
334 | 44287 | jjdelcerro | // this.formset.setReadOnly(true);
|
335 | 44202 | jjdelcerro | formset.setActionVisible("startEditing", true); |
336 | formset.setActionEnabled("startEditing", true); |
||
337 | formset.setActionVisible("finishEditing", false); |
||
338 | formset.setActionEnabled("finishEditing", false); |
||
339 | } |
||
340 | 42112 | jjdelcerro | return this.formset; |
341 | } |
||
342 | |||
343 | 42489 | jjdelcerro | @Override
|
344 | public void addAction(Action action) { |
||
345 | this.otherActions.add(action);
|
||
346 | if( this.formset!=null ) { |
||
347 | this.formset.addAction(action);
|
||
348 | } |
||
349 | } |
||
350 | |||
351 | @Override
|
||
352 | public long getCurrentIndex() { |
||
353 | if( this.formset==null ) { |
||
354 | return -1; |
||
355 | } |
||
356 | return this.formset.getCurrentIndex(); |
||
357 | } |
||
358 | |||
359 | @Override
|
||
360 | public Feature get(long index) { |
||
361 | if( this.formset==null || this.ph==null ) { |
||
362 | return null; |
||
363 | } |
||
364 | try {
|
||
365 | return this.ph.getFeatureAt(index); |
||
366 | } catch (BaseException ex) {
|
||
367 | return null; |
||
368 | } |
||
369 | } |
||
370 | 42714 | dmartinezizquierdo | |
371 | 42489 | jjdelcerro | private class FinishEditingAction extends AbstractAction implements Observer { |
372 | |||
373 | 44128 | jjdelcerro | @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
374 | 42489 | jjdelcerro | public FinishEditingAction() {
|
375 | 42714 | dmartinezizquierdo | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
376 | 42489 | jjdelcerro | IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
377 | 42714 | dmartinezizquierdo | |
378 | 42489 | jjdelcerro | this.putValue(NAME,null); |
379 | this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Stop_editing")); |
||
380 | this.putValue(SMALL_ICON, iconTheme.get("table-stop-editing")); |
||
381 | this.putValue(ACTION_COMMAND_KEY, "finishEditing"); |
||
382 | |||
383 | this.setEnabled(store.isEditing());
|
||
384 | store.addObserver(this);
|
||
385 | } |
||
386 | 42714 | dmartinezizquierdo | |
387 | 42489 | jjdelcerro | @Override
|
388 | public void actionPerformed(ActionEvent ae) { |
||
389 | if( store.isEditing() ) {
|
||
390 | try {
|
||
391 | 42714 | dmartinezizquierdo | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
392 | 42489 | jjdelcerro | ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
393 | int x = dialogManager.confirmDialog(
|
||
394 | 42714 | dmartinezizquierdo | "? Desea terminar edicion y guardar los cambios ?\n\nPulse cancelar para cancelar la edicion y los cambios.",
|
395 | 42489 | jjdelcerro | i18nManager.getTranslation("_Stop_editing"),
|
396 | JOptionPane.YES_NO_CANCEL_OPTION,
|
||
397 | JOptionPane.QUESTION_MESSAGE
|
||
398 | ); |
||
399 | switch(x) {
|
||
400 | case JOptionPane.YES_OPTION: |
||
401 | store.finishEditing(); |
||
402 | break;
|
||
403 | case JOptionPane.NO_OPTION: |
||
404 | break;
|
||
405 | case JOptionPane.CANCEL_OPTION: |
||
406 | store.cancelEditing(); |
||
407 | break;
|
||
408 | } |
||
409 | } catch (DataException ex) {
|
||
410 | LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex); |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | @Override
|
||
416 | public void update(Observable observable, Object notification) { |
||
417 | 44346 | jjdelcerro | if( store == null || formset==null ) { |
418 | return;
|
||
419 | } |
||
420 | 42489 | jjdelcerro | if( notification instanceof FeatureStoreNotification ) { |
421 | FeatureStoreNotification n = (FeatureStoreNotification) notification; |
||
422 | switch( n.getType() ) {
|
||
423 | case FeatureStoreNotification.AFTER_STARTEDITING:
|
||
424 | 44202 | jjdelcerro | case FeatureStoreNotification.AFTER_FINISHEDITING:
|
425 | case FeatureStoreNotification.AFTER_CANCELEDITING:
|
||
426 | if( store.isEditing() ) {
|
||
427 | formset.setReadOnly(false);
|
||
428 | formset.setActionVisible("finishEditing", true); |
||
429 | formset.setActionEnabled("finishEditing", true); |
||
430 | } else {
|
||
431 | formset.setReadOnly(true);
|
||
432 | formset.setActionVisible("finishEditing", false); |
||
433 | formset.setActionEnabled("finishEditing", false); |
||
434 | } |
||
435 | 42489 | jjdelcerro | break;
|
436 | 44202 | jjdelcerro | } |
437 | } |
||
438 | } |
||
439 | |||
440 | } |
||
441 | |||
442 | private class StartEditingAction extends AbstractAction implements Observer { |
||
443 | |||
444 | @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
||
445 | public StartEditingAction() {
|
||
446 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
447 | IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
||
448 | |||
449 | this.putValue(NAME,null); |
||
450 | this.putValue(SHORT_DESCRIPTION,i18nManager.getTranslation("_Start_editing")); |
||
451 | this.putValue(SMALL_ICON, iconTheme.get("table-start-editing")); |
||
452 | this.putValue(ACTION_COMMAND_KEY, "startEditing"); |
||
453 | |||
454 | this.setEnabled(!store.isEditing());
|
||
455 | store.addObserver(this);
|
||
456 | } |
||
457 | |||
458 | @Override
|
||
459 | public void actionPerformed(ActionEvent ae) { |
||
460 | if( !store.isEditing() ) {
|
||
461 | try {
|
||
462 | store.edit(); |
||
463 | } catch (DataException ex) {
|
||
464 | LOGGER.warn("Can't finish editing in FeatureForm ("+store.getName()+").",ex); |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | |||
469 | @Override
|
||
470 | public void update(Observable observable, Object notification) { |
||
471 | 44346 | jjdelcerro | if( store == null || formset==null ) { |
472 | return;
|
||
473 | } |
||
474 | 44202 | jjdelcerro | if( notification instanceof FeatureStoreNotification ) { |
475 | FeatureStoreNotification n = (FeatureStoreNotification) notification; |
||
476 | switch( n.getType() ) {
|
||
477 | case FeatureStoreNotification.AFTER_STARTEDITING:
|
||
478 | 42489 | jjdelcerro | case FeatureStoreNotification.AFTER_FINISHEDITING:
|
479 | case FeatureStoreNotification.AFTER_CANCELEDITING:
|
||
480 | 44202 | jjdelcerro | if( store.isEditing() ) {
|
481 | formset.setReadOnly(false);
|
||
482 | formset.setActionVisible("startEditing", false); |
||
483 | formset.setActionEnabled("startEditing", false); |
||
484 | } else {
|
||
485 | formset.setReadOnly(true);
|
||
486 | formset.setActionVisible("startEditing", true); |
||
487 | formset.setActionEnabled("startEditing", true); |
||
488 | } |
||
489 | 42489 | jjdelcerro | break;
|
490 | } |
||
491 | } |
||
492 | } |
||
493 | 42714 | dmartinezizquierdo | |
494 | 42489 | jjdelcerro | } |
495 | |||
496 | @Override
|
||
497 | public void setQuery(FeatureQuery query) { |
||
498 | 42112 | jjdelcerro | if (this.ph != null) { |
499 | 44239 | jjdelcerro | if (this.formset != null && !formset.isReadOnly() && this.formset.isAutosave() && this.formset.countValues() > 0) { |
500 | 42112 | jjdelcerro | if (!store.isEditing()) {
|
501 | try {
|
||
502 | store.edit(); |
||
503 | } catch (DataException e1) {
|
||
504 | 42489 | jjdelcerro | throw new RuntimeException("Can't set query", e1); |
505 | 42112 | jjdelcerro | } |
506 | } |
||
507 | saveChanges(this.formset);
|
||
508 | } |
||
509 | } |
||
510 | 42248 | jjdelcerro | this.currentQuery = query;
|
511 | updateForm(); |
||
512 | 42112 | jjdelcerro | } |
513 | |||
514 | 44239 | jjdelcerro | private FeatureQuery getCurrentQuery() {
|
515 | return this.currentQuery; |
||
516 | } |
||
517 | |||
518 | 42489 | jjdelcerro | @Override
|
519 | public void showForm(MODE mode) { |
||
520 | 42248 | jjdelcerro | this.panel.add(this.getFormset().asJComponent(), BorderLayout.CENTER); |
521 | WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
||
522 | 44202 | jjdelcerro | String title = this.definition.getLabel(); |
523 | 42775 | jjdelcerro | winmgr.showWindow(this.asJComponent(), title, mode);
|
524 | 42112 | jjdelcerro | } |
525 | |||
526 | 42489 | jjdelcerro | private void saveChanges(JDynFormSet dynformSet) { |
527 | 44346 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
528 | 42775 | jjdelcerro | if( dynformSet.isInNewState() ) {
|
529 | Feature feat = null;
|
||
530 | try {
|
||
531 | feat = store.createNewFeature(false);
|
||
532 | } catch (DataException ex) {
|
||
533 | LOGGER.warn("Can't create new feature.",ex);
|
||
534 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
535 | dynformSet.message(i18nManager.getTranslation("error_saving_data_will_not_save"));
|
||
536 | throw new RuntimeException("Can't new save values"); |
||
537 | } |
||
538 | DynObject dynObject = feat.getAsDynObject(); |
||
539 | dynformSet.getFormValues(dynObject); |
||
540 | try {
|
||
541 | ph.insert(((FacadeOfAFeature)dynObject).getEditableFeature()); |
||
542 | } catch (BaseException e) {
|
||
543 | throw new RuntimeException("Can't save values", e); |
||
544 | } |
||
545 | try {
|
||
546 | this.formset.setValues(ph.asListOfDynObjects());
|
||
547 | this.formset.setCurrentIndex((int)(ph.getTotalSize())-1); |
||
548 | } catch(Exception ex) { |
||
549 | LOGGER.warn("Can't reload form data after insert.",ex);
|
||
550 | } |
||
551 | 44346 | jjdelcerro | this.formset.message(i18n.getTranslation("_Record_saved")); |
552 | 42775 | jjdelcerro | } else {
|
553 | int index = dynformSet.getCurrentIndex();
|
||
554 | DynObject dynObject = dynformSet.get(index); |
||
555 | |||
556 | if ( !(dynObject instanceof FacadeOfAFeature) ) { |
||
557 | LOGGER.warn("Can't get the associated feature index " + index);
|
||
558 | 44346 | jjdelcerro | dynformSet.message(i18n.getTranslation("error_saving_data_will_not_save"));
|
559 | 42775 | jjdelcerro | throw new RuntimeException("Can't save values"); |
560 | } |
||
561 | dynformSet.getFormValues(dynObject); |
||
562 | try {
|
||
563 | ph.update(((FacadeOfAFeature)dynObject).getEditableFeature()); |
||
564 | } catch (BaseException e) {
|
||
565 | throw new RuntimeException("Can't save values", e); |
||
566 | } |
||
567 | 42489 | jjdelcerro | } |
568 | 42112 | jjdelcerro | |
569 | } |
||
570 | |||
571 | 42489 | jjdelcerro | @Override
|
572 | public void saveChanges() { |
||
573 | if (this.formset != null && this.formset.countValues() > 0) { |
||
574 | if (store != null && !store.isEditing()) { |
||
575 | try {
|
||
576 | store.edit(); |
||
577 | } catch (DataException e1) {
|
||
578 | LOGGER.warn("Can't edit the store " + store.getName());
|
||
579 | throw new RuntimeException("Can't save changes.", e1); |
||
580 | } |
||
581 | 42112 | jjdelcerro | } |
582 | 42489 | jjdelcerro | this.saveChanges(this.formset); |
583 | 42112 | jjdelcerro | } |
584 | } |
||
585 | |||
586 | 42489 | jjdelcerro | @Override
|
587 | public long getDataSetSize() { |
||
588 | if (this.ph != null) { |
||
589 | return ph.getTotalSize();
|
||
590 | } |
||
591 | return 0; |
||
592 | 42112 | jjdelcerro | } |
593 | |||
594 | 42489 | jjdelcerro | @Override
|
595 | public FeatureStore getFeatureStore() {
|
||
596 | return this.store; |
||
597 | 42112 | jjdelcerro | } |
598 | |||
599 | 42489 | jjdelcerro | private class FormSetListener implements JDynFormSetListener { |
600 | 42112 | jjdelcerro | |
601 | 42489 | jjdelcerro | @Override
|
602 | public void formMessage(String message) { |
||
603 | } |
||
604 | 42112 | jjdelcerro | |
605 | 42489 | jjdelcerro | @Override
|
606 | public void formClose() { |
||
607 | panel.setVisible(false);
|
||
608 | 42112 | jjdelcerro | } |
609 | |||
610 | 42489 | jjdelcerro | @Override
|
611 | public void formMovedTo(int currentPosition) throws AbortActionException { |
||
612 | LOGGER.trace("formMovedTo " + currentPosition);
|
||
613 | } |
||
614 | 42112 | jjdelcerro | |
615 | 42489 | jjdelcerro | @Override
|
616 | public void formBeforeSave(JDynFormSet dynformSet) throws AbortActionException { |
||
617 | LOGGER.trace("formBeforeSave");
|
||
618 | if (!store.isEditing()) {
|
||
619 | try {
|
||
620 | store.edit(); |
||
621 | } catch (DataException e1) {
|
||
622 | throw new StoreEditException(e1, store.getName()); |
||
623 | } |
||
624 | } |
||
625 | } |
||
626 | 42112 | jjdelcerro | |
627 | 44259 | jjdelcerro | @Override
|
628 | 42489 | jjdelcerro | public void formBeforeNew(JDynFormSet dynformSet) throws AbortActionException { |
629 | LOGGER.trace("formBeforeNew");
|
||
630 | 42112 | jjdelcerro | } |
631 | |||
632 | 44259 | jjdelcerro | @Override
|
633 | 42489 | jjdelcerro | public void formBeforeDelete(JDynFormSet dynformSet) throws AbortActionException { |
634 | LOGGER.trace("formBeforeDelete");
|
||
635 | } |
||
636 | 42112 | jjdelcerro | |
637 | 42489 | jjdelcerro | @Override
|
638 | public void formAfterSave(JDynFormSet dynformSet) throws AbortActionException { |
||
639 | LOGGER.trace("formAfterSave");
|
||
640 | saveChanges(dynformSet); |
||
641 | } |
||
642 | 42112 | jjdelcerro | |
643 | 42489 | jjdelcerro | @Override
|
644 | public void formAfterNew(JDynFormSet dynformSet) throws AbortActionException { |
||
645 | LOGGER.trace("formAfterNew");
|
||
646 | 42112 | jjdelcerro | } |
647 | |||
648 | 42489 | jjdelcerro | @Override
|
649 | public void formAfterDelete(JDynFormSet dynformSet) throws AbortActionException { |
||
650 | LOGGER.trace("formAfterDelete");
|
||
651 | 42112 | jjdelcerro | } |
652 | |||
653 | 44292 | jjdelcerro | // @Override
|
654 | // public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
|
||
655 | // LOGGER.trace("formBeforeSearch");
|
||
656 | // DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
|
||
657 | // WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
||
658 | //
|
||
659 | // JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
|
||
660 | // FeatureQuery currentQuery = getCurrentQuery();
|
||
661 | // if( currentQuery!=null ) {
|
||
662 | // Evaluator filter = currentQuery.getFilter();
|
||
663 | // if( filter instanceof ExpressionEvaluator ) {
|
||
664 | // Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
||
665 | // builder.setExpression(expression);
|
||
666 | // }
|
||
667 | // }
|
||
668 | // Dialog dialog = winmgr.createDialog(
|
||
669 | // builder.asJComponent(),
|
||
670 | // "Filtro",
|
||
671 | // "Creacion de filtro sobre '"+store.getName()+"'",
|
||
672 | // WindowManager_v2.BUTTONS_OK_CANCEL
|
||
673 | // );
|
||
674 | // dialog.show(WindowManager.MODE.DIALOG);
|
||
675 | // if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
|
||
676 | // Expression expresion = builder.getExpression();
|
||
677 | // try {
|
||
678 | // FeatureQuery query = store.createFeatureQuery();
|
||
679 | // if( ExpressionUtils.isPhraseEmpty(expresion) ) {
|
||
680 | // query.clearFilter();
|
||
681 | // } else {
|
||
682 | // query.setFilter(expresion);
|
||
683 | // }
|
||
684 | // setQuery(query);
|
||
685 | // } catch (Exception ex) {
|
||
686 | // LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
|
||
687 | // }
|
||
688 | // }
|
||
689 | // }
|
||
690 | |||
691 | 42489 | jjdelcerro | @Override
|
692 | public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException { |
||
693 | LOGGER.trace("formBeforeSearch");
|
||
694 | DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager(); |
||
695 | WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
696 | 42714 | dmartinezizquierdo | |
697 | 44292 | jjdelcerro | final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
|
698 | searchPanel.setShowActions(false);
|
||
699 | 44239 | jjdelcerro | FeatureQuery currentQuery = getCurrentQuery(); |
700 | if( currentQuery!=null ) { |
||
701 | Evaluator filter = currentQuery.getFilter(); |
||
702 | if( filter instanceof ExpressionEvaluator ) { |
||
703 | Expression expression = ((ExpressionEvaluator)filter).getExpression();
|
||
704 | 44292 | jjdelcerro | searchPanel.setFilter(expression); |
705 | 44239 | jjdelcerro | } |
706 | } |
||
707 | 44292 | jjdelcerro | searchPanel.asJComponent().setPreferredSize( |
708 | new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320) |
||
709 | ); |
||
710 | 42489 | jjdelcerro | Dialog dialog = winmgr.createDialog(
|
711 | 44292 | jjdelcerro | searchPanel.asJComponent(), |
712 | 42714 | dmartinezizquierdo | "Filtro",
|
713 | "Creacion de filtro sobre '"+store.getName()+"'", |
||
714 | 42489 | jjdelcerro | WindowManager_v2.BUTTONS_OK_CANCEL |
715 | ); |
||
716 | dialog.show(WindowManager.MODE.DIALOG); |
||
717 | if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
|
||
718 | 44292 | jjdelcerro | Expression expresion = searchPanel.getFilter();
|
719 | 42112 | jjdelcerro | try {
|
720 | 42489 | jjdelcerro | FeatureQuery query = store.createFeatureQuery(); |
721 | 44239 | jjdelcerro | if( ExpressionUtils.isPhraseEmpty(expresion) ) {
|
722 | query.clearFilter(); |
||
723 | } else {
|
||
724 | 44128 | jjdelcerro | query.setFilter(expresion); |
725 | 42489 | jjdelcerro | } |
726 | setQuery(query); |
||
727 | } catch (Exception ex) { |
||
728 | LOGGER.warn("Can't apply filter '" + expresion + "'.", ex); |
||
729 | 42112 | jjdelcerro | } |
730 | } |
||
731 | } |
||
732 | |||
733 | 42489 | jjdelcerro | @Override
|
734 | public void formAfterSearch(JDynFormSet dynformSet) throws AbortActionException { |
||
735 | LOGGER.trace("formAfterSearch");
|
||
736 | } |
||
737 | 42775 | jjdelcerro | |
738 | @Override
|
||
739 | public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
||
740 | LOGGER.trace("formBeforeCancelNew");
|
||
741 | } |
||
742 | |||
743 | @Override
|
||
744 | public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
||
745 | LOGGER.trace("formAfterCancelNew");
|
||
746 | } |
||
747 | 42112 | jjdelcerro | } |
748 | |||
749 | 42489 | jjdelcerro | private static class StoreEditException extends AbortActionException { |
750 | 42112 | jjdelcerro | |
751 | 42489 | jjdelcerro | /**
|
752 | *
|
||
753 | */
|
||
754 | private static final long serialVersionUID = -7682017811778577130L; |
||
755 | 42112 | jjdelcerro | |
756 | 42489 | jjdelcerro | public StoreEditException(Throwable cause, String storename) { |
757 | super("Can't edit the store '%(storename)'", cause, "cant_edit_the store_XstorenameX", serialVersionUID); |
||
758 | setValue("storename", storename);
|
||
759 | 42112 | jjdelcerro | } |
760 | } |
||
761 | } |