Statistics
| Revision:

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 / fmap / dal / impl / DefaultEditingNotificationManager.java @ 44351

History | View | Annotate | Download (11.3 KB)

1 41323 jjdelcerro
/**
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 42775 jjdelcerro
package org.gvsig.fmap.dal.impl;
25 41323 jjdelcerro
26
import java.util.Arrays;
27
import java.util.List;
28 41335 jjdelcerro
import javax.swing.SwingUtilities;
29 42775 jjdelcerro
import org.gvsig.featureform.swing.JFeatureForm;
30 41323 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
31 42775 jjdelcerro
import org.gvsig.fmap.dal.EditingNotification;
32
import org.gvsig.fmap.dal.EditingNotificationManager;
33 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeature;
34 41323 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeatureType;
35
import org.gvsig.fmap.dal.feature.Feature;
36 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
37 41323 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
38 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
39 42775 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
40 41335 jjdelcerro
import org.gvsig.tools.ToolsLocator;
41 41323 jjdelcerro
import org.gvsig.tools.observer.ObservableHelper;
42
import org.gvsig.tools.observer.Observer;
43 41498 jjdelcerro
import org.gvsig.tools.observer.BaseNotification;
44 42775 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
45
import org.gvsig.tools.swing.api.windowmanager.Dialog;
46
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
47
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
48 41323 jjdelcerro
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50
51
/**
52
 *
53
 * Acts as a hub to centralize editing notification events in a single manager.
54
 *
55
 * All objects that modify a store notify this manager to reported the actions
56
 * to the objects interested in knowing when start or end editing, regardless
57
 * of where the editing work is done.
58
 *
59
 */
60
public class DefaultEditingNotificationManager implements EditingNotificationManager {
61
62
    private static final Logger logger = LoggerFactory.getLogger(DefaultEditingNotificationManager.class);
63
64
    private ObservableHelper helper = null;
65
66
    private static final int SOURCE = 1;
67
    private static final int DOCUMENT = 2;
68 42775 jjdelcerro
    private static final int AUXDATA = 3;
69 41323 jjdelcerro
    private static final int STORE = 4;
70
    private static final int FEATURE = 5;
71
    private static final int FEATURETYPE = 6;
72
73
    private static List cancelableNotifications = null;
74
75
76
77 41498 jjdelcerro
    public class DefaultEditingNotification extends BaseNotification implements EditingNotification {
78 41323 jjdelcerro
79 41335 jjdelcerro
        private boolean validateTheFeature = true;
80
81 41323 jjdelcerro
        DefaultEditingNotification(String type) {
82
            super(type, 7);
83
        }
84
85 42775 jjdelcerro
        @Override
86 41323 jjdelcerro
        public Object getSource() {
87
            return this.getValue(SOURCE);
88
        }
89
90 42775 jjdelcerro
        @Override
91 41323 jjdelcerro
        public Object getDocument() {
92
            return this.getValue(DOCUMENT);
93
        }
94
95 42775 jjdelcerro
        public Object getAuxData() {
96
            return this.getValue(AUXDATA);
97 41323 jjdelcerro
        }
98
99 42775 jjdelcerro
        @Override
100 41323 jjdelcerro
        public DataStore getStore() {
101
            return (DataStore) this.getValue(STORE);
102
        }
103
104 42775 jjdelcerro
        @Override
105 41323 jjdelcerro
        public FeatureStore getFeatureStore() {
106
            return (FeatureStore) this.getValue(STORE);
107
        }
108
109 42775 jjdelcerro
        @Override
110 41323 jjdelcerro
        public Feature getFeature() {
111
            return (Feature) this.getValue(FEATURE);
112
        }
113
114 42775 jjdelcerro
        @Override
115 41323 jjdelcerro
        public EditableFeatureType getFeatureType() {
116
            return (EditableFeatureType) this.getValue(FEATURETYPE);
117
        }
118
119 42775 jjdelcerro
        @Override
120 41323 jjdelcerro
        public boolean isCancelable() {
121
            if( cancelableNotifications==null ) {
122
                String nn[] = new String[] {
123 41330 jjdelcerro
                    BEFORE_ENTER_EDITING_STORE,
124
                    BEFORE_EXIT_EDITING_STORE,
125
                    BEFORE_INSERT_FEATURE,
126
                    BEFORE_REMOVE_FEATURE,
127
                    BEFORE_UPDATE_FEATURE,
128
                    BEFORE_UPDATE_FEATURE_TYPE
129 41323 jjdelcerro
                };
130
                cancelableNotifications = Arrays.asList(nn);
131
            }
132
            return cancelableNotifications.contains(this.getType() );
133
        }
134 41335 jjdelcerro
135 42775 jjdelcerro
        @Override
136 41335 jjdelcerro
        public void setSkipFeatureValidation(boolean skipTheFeatureValidation) {
137
            this.validateTheFeature = !skipTheFeatureValidation;
138
        }
139
140 42775 jjdelcerro
        @Override
141 41335 jjdelcerro
        public boolean shouldValidateTheFeature() {
142 41741 jjdelcerro
            return this.validateTheFeature;
143 41335 jjdelcerro
        }
144 41323 jjdelcerro
    }
145
146
    public DefaultEditingNotificationManager() {
147
        this.helper = new ObservableHelper();
148
    }
149
150 42775 jjdelcerro
    @Override
151 41323 jjdelcerro
    public void addObserver(Observer o) {
152
        this.helper.addObserver(o);
153
    }
154
155 42775 jjdelcerro
    @Override
156 41323 jjdelcerro
    public void deleteObserver(Observer o) {
157
        this.helper.deleteObserver(o);
158
    }
159
160 42775 jjdelcerro
    @Override
161 41323 jjdelcerro
    public void deleteObservers() {
162
        this.helper.deleteObservers();
163
    }
164
165 42775 jjdelcerro
    @Override
166 41323 jjdelcerro
    public EditingNotification notifyObservers(EditingNotification notification) {
167
        try {
168
            this.helper.notifyObservers(this, notification);
169
        } catch(Exception ex) {
170
            logger.warn("Problems notifing to observers of DefaultEditingNotificationManager.",ex);
171
        }
172
        return notification;
173
    }
174
175 42775 jjdelcerro
    public EditingNotification notifyObservers(Object source, String type, Object document, Object auxdata, DataStore store, Feature feature, EditableFeatureType featureType) {
176 41323 jjdelcerro
        EditingNotification notification = new DefaultEditingNotification(type);
177
        notification.setValue(SOURCE,source);
178
        notification.setValue(DOCUMENT,document);
179 42775 jjdelcerro
        notification.setValue(AUXDATA,auxdata);
180 41323 jjdelcerro
        notification.setValue(STORE,store);
181
        notification.setValue(FEATURE,feature);
182
        notification.setValue(FEATURETYPE,featureType);
183
        return this.notifyObservers(notification);
184
    }
185
186 42775 jjdelcerro
    @Override
187
    public EditingNotification notifyObservers(Object source, String type, Object document, Object auxdata, DataStore store) {
188
        return this.notifyObservers(source, type, document, auxdata, store, null, null);
189 41323 jjdelcerro
    }
190
191 42775 jjdelcerro
    @Override
192
    public EditingNotification notifyObservers(Object source, String type, Object document, Object auxdata) {
193
        return this.notifyObservers(source, type, document, auxdata, null, null, null);
194 41323 jjdelcerro
    }
195
196 42775 jjdelcerro
    @Override
197 41323 jjdelcerro
    public EditingNotification notifyObservers(Object source, String type, Object document, DataStore store) {
198
        return this.notifyObservers(source, type, document, null, store, null, null);
199
    }
200
201 42775 jjdelcerro
    @Override
202 41323 jjdelcerro
    public EditingNotification notifyObservers(Object source, String type, Object document, DataStore store, Feature feature) {
203
        return this.notifyObservers(source, type, document, null, store, feature, null);
204
    }
205
206 42775 jjdelcerro
    @Override
207 41323 jjdelcerro
    public EditingNotification notifyObservers(Object source, String type, Object document, DataStore store, EditableFeatureType featureType) {
208
        return this.notifyObservers(source, type, document, null, store, null, featureType);
209
    }
210
211 42775 jjdelcerro
    @Override
212
    public EditingNotification notifyObservers(Object source, String type, Object document, Object auxdata, DataStore store, Feature feature) {
213
        return this.notifyObservers(source, type, document, auxdata, store, feature, null);
214 41323 jjdelcerro
    }
215 41335 jjdelcerro
216 42775 jjdelcerro
    @Override
217 41335 jjdelcerro
    public boolean validateFeature(Feature feature) {
218
        while( true ) {
219
            if( !needAskUser(feature) ) {
220
                // La feature se puede validar y no precisa de intervencion del
221
                // usuario. Retornamos true.
222
                return true;
223
            }
224
            // No se habia podido validar la feature, se piden al
225
            // usuario los datos que faltan.
226
            AskRequiredAttributtes ask = new AskRequiredAttributtes();
227
            ask.showDialog(feature);
228
            if( ask.userCancel() ) {
229
                // No se habia podido validar la feature, se le han pedido al
230
                // usuario los datos que faltan y este ha pulsado en cancel,
231
                // asi que la feature no se puede validar, y retornamos
232
                // false.
233
                return false;
234
            }
235
        }
236
    }
237 41323 jjdelcerro
238 41335 jjdelcerro
    private boolean needAskUser(Feature feature) {
239
        FeatureType featureType = feature.getType();
240
        FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
241
242 42775 jjdelcerro
        for (FeatureAttributeDescriptor attrDesc : attributeDescriptors) {
243 41335 jjdelcerro
            if ( attrDesc.isAutomatic() ) {
244 43362 jjdelcerro
                continue;
245 41335 jjdelcerro
            }
246
            if ( (attrDesc.isPrimaryKey() || !attrDesc.allowNull())
247
                    && feature.get(attrDesc.getName()) == null ) {
248
                return true;
249
            }
250
        }
251
        return false;
252
    }
253
254
    private class AskRequiredAttributtes {
255
256
        private boolean userCancelValue = true;
257
258
        public boolean userCancel() {
259
            return this.userCancelValue;
260
        }
261
262
        public void showDialog(final Feature feature) {
263
            if ( !SwingUtilities.isEventDispatchThread() ) {
264
                try {
265
                    SwingUtilities.invokeAndWait(new Runnable() {
266 42775 jjdelcerro
                        @Override
267 41335 jjdelcerro
                        public void run() {
268
                            showDialog(feature);
269
                        }
270
                    }
271
                    );
272
                } catch (Exception e1) {
273
                    message("Can't show form to fill need data.",e1);
274
                    this.userCancelValue = true;
275
                    return;
276
                }
277
            }
278
279
            try {
280 42775 jjdelcerro
                JFeatureForm form = DALSwingLocator.getSwingManager().createJFeatureForm(feature);
281
                WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
282
                Dialog dialog = winManager.createDialog(
283
                        form.asJComponent(),
284
                        form.getDynForm().getDefinition().getLabel(),
285
                        ToolsLocator.getI18nManager().getTranslation("_Fill_the_required_fields"),
286
                        WindowManager_v2.BUTTONS_OK_CANCEL
287
                );
288
                dialog.show(WindowManager.MODE.DIALOG);
289
                if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
290
                    form.fetch((EditableFeature) feature);
291 41335 jjdelcerro
                    this.userCancelValue = false;
292 43362 jjdelcerro
                } else {
293
                    this.userCancelValue = true;
294 41335 jjdelcerro
                }
295 42775 jjdelcerro
296 41335 jjdelcerro
            } catch (Exception ex) {
297
                message("Can't show form to fill need data.",ex);
298
                this.userCancelValue = true;
299
            }
300
        }
301
302
        private void message(String msg, Throwable ex) {
303
            if( ex==null ) {
304
                logger.warn(msg);
305
            } else {
306
                logger.warn(msg,ex);
307
            }
308
//            msg = msg + "\nSee to application log for more information.";
309
//            ApplicationManager application = ApplicationLocator.getManager();
310
//            application.message(msg,JOptionPane.WARNING_MESSAGE);
311
        }
312
    }
313
314 41323 jjdelcerro
315 41335 jjdelcerro
316 41323 jjdelcerro
}