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 @ 43362
History | View | Annotate | Download (11.3 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.fmap.dal.impl; |
25 |
|
26 |
import java.util.Arrays; |
27 |
import java.util.List; |
28 |
import javax.swing.SwingUtilities; |
29 |
import org.gvsig.featureform.swing.JFeatureForm; |
30 |
import org.gvsig.fmap.dal.DataStore; |
31 |
import org.gvsig.fmap.dal.EditingNotification; |
32 |
import org.gvsig.fmap.dal.EditingNotificationManager; |
33 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
34 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
39 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.observer.ObservableHelper; |
42 |
import org.gvsig.tools.observer.Observer; |
43 |
import org.gvsig.tools.observer.BaseNotification; |
44 |
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 |
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 |
private static final int AUXDATA = 3; |
69 |
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 |
public class DefaultEditingNotification extends BaseNotification implements EditingNotification { |
78 |
|
79 |
private boolean validateTheFeature = true; |
80 |
|
81 |
DefaultEditingNotification(String type) {
|
82 |
super(type, 7); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public Object getSource() { |
87 |
return this.getValue(SOURCE); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Object getDocument() { |
92 |
return this.getValue(DOCUMENT); |
93 |
} |
94 |
|
95 |
public Object getAuxData() { |
96 |
return this.getValue(AUXDATA); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public DataStore getStore() {
|
101 |
return (DataStore) this.getValue(STORE); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public FeatureStore getFeatureStore() {
|
106 |
return (FeatureStore) this.getValue(STORE); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public Feature getFeature() {
|
111 |
return (Feature) this.getValue(FEATURE); |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public EditableFeatureType getFeatureType() {
|
116 |
return (EditableFeatureType) this.getValue(FEATURETYPE); |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public boolean isCancelable() { |
121 |
if( cancelableNotifications==null ) { |
122 |
String nn[] = new String[] { |
123 |
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 |
}; |
130 |
cancelableNotifications = Arrays.asList(nn);
|
131 |
} |
132 |
return cancelableNotifications.contains(this.getType() ); |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public void setSkipFeatureValidation(boolean skipTheFeatureValidation) { |
137 |
this.validateTheFeature = !skipTheFeatureValidation;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public boolean shouldValidateTheFeature() { |
142 |
return this.validateTheFeature; |
143 |
} |
144 |
} |
145 |
|
146 |
public DefaultEditingNotificationManager() {
|
147 |
this.helper = new ObservableHelper(); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void addObserver(Observer o) { |
152 |
this.helper.addObserver(o);
|
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void deleteObserver(Observer o) { |
157 |
this.helper.deleteObserver(o);
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public void deleteObservers() { |
162 |
this.helper.deleteObservers();
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
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 |
public EditingNotification notifyObservers(Object source, String type, Object document, Object auxdata, DataStore store, Feature feature, EditableFeatureType featureType) { |
176 |
EditingNotification notification = new DefaultEditingNotification(type);
|
177 |
notification.setValue(SOURCE,source); |
178 |
notification.setValue(DOCUMENT,document); |
179 |
notification.setValue(AUXDATA,auxdata); |
180 |
notification.setValue(STORE,store); |
181 |
notification.setValue(FEATURE,feature); |
182 |
notification.setValue(FEATURETYPE,featureType); |
183 |
return this.notifyObservers(notification); |
184 |
} |
185 |
|
186 |
@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 |
} |
190 |
|
191 |
@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 |
} |
195 |
|
196 |
@Override
|
197 |
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 |
@Override
|
202 |
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 |
@Override
|
207 |
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 |
@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 |
} |
215 |
|
216 |
@Override
|
217 |
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 |
|
238 |
private boolean needAskUser(Feature feature) { |
239 |
FeatureType featureType = feature.getType(); |
240 |
FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
|
241 |
|
242 |
for (FeatureAttributeDescriptor attrDesc : attributeDescriptors) {
|
243 |
if ( attrDesc.isAutomatic() ) {
|
244 |
continue;
|
245 |
} |
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 |
@Override
|
267 |
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 |
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 |
this.userCancelValue = false; |
292 |
} else {
|
293 |
this.userCancelValue = true; |
294 |
} |
295 |
|
296 |
} 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 |
|
315 |
|
316 |
} |