Revision 41335 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/editing/impl/DefaultEditingNotificationManager.java
DefaultEditingNotificationManager.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.util.Arrays; |
27 | 27 |
import java.util.List; |
28 |
import javax.swing.SwingUtilities; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
28 | 30 |
import org.gvsig.editing.EditingNotificationManager; |
29 | 31 |
import org.gvsig.editing.EditingNotification; |
30 | 32 |
import org.gvsig.fmap.dal.DataStore; |
33 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
31 | 34 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
32 | 35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
33 | 37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
34 | 39 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
35 | 40 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
41 |
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor; |
|
42 |
import org.gvsig.tools.ToolsLocator; |
|
43 |
import org.gvsig.tools.dynobject.DynObject; |
|
36 | 44 |
import org.gvsig.tools.observer.ObservableHelper; |
37 | 45 |
import org.gvsig.tools.observer.Observer; |
38 | 46 |
import org.slf4j.Logger; |
... | ... | |
66 | 74 |
|
67 | 75 |
public class DefaultEditingNotification extends ObservableHelper.BaseNotification implements EditingNotification { |
68 | 76 |
|
77 |
private boolean validateTheFeature = true; |
|
78 |
|
|
69 | 79 |
DefaultEditingNotification(String type) { |
70 | 80 |
super(type, 7); |
71 | 81 |
} |
... | ... | |
112 | 122 |
} |
113 | 123 |
return cancelableNotifications.contains(this.getType() ); |
114 | 124 |
} |
125 |
|
|
126 |
public void setSkipFeatureValidation(boolean skipTheFeatureValidation) { |
|
127 |
this.validateTheFeature = !skipTheFeatureValidation; |
|
128 |
} |
|
129 |
|
|
130 |
public boolean shouldValidateTheFeature() { |
|
131 |
return !this.validateTheFeature; |
|
132 |
} |
|
115 | 133 |
} |
116 | 134 |
|
117 | 135 |
public DefaultEditingNotificationManager() { |
... | ... | |
177 | 195 |
public EditingNotification notifyObservers(Object source, String type, Object document, FLayer layer, DataStore store, Feature feature) { |
178 | 196 |
return this.notifyObservers(source, type, document, layer, store, feature, null); |
179 | 197 |
} |
198 |
|
|
199 |
public boolean validateFeature(Feature feature) { |
|
200 |
while( true ) { |
|
201 |
if( !needAskUser(feature) ) { |
|
202 |
// La feature se puede validar y no precisa de intervencion del |
|
203 |
// usuario. Retornamos true. |
|
204 |
return true; |
|
205 |
} |
|
206 |
// No se habia podido validar la feature, se piden al |
|
207 |
// usuario los datos que faltan. |
|
208 |
AskRequiredAttributtes ask = new AskRequiredAttributtes(); |
|
209 |
ask.showDialog(feature); |
|
210 |
if( ask.userCancel() ) { |
|
211 |
// No se habia podido validar la feature, se le han pedido al |
|
212 |
// usuario los datos que faltan y este ha pulsado en cancel, |
|
213 |
// asi que la feature no se puede validar, y retornamos |
|
214 |
// false. |
|
215 |
return false; |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
180 | 219 |
|
220 |
private boolean needAskUser(Feature feature) { |
|
221 |
FeatureType featureType = feature.getType(); |
|
222 |
FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors(); |
|
223 |
|
|
224 |
for ( int i = 0; i < attributeDescriptors.length; i++ ) { |
|
225 |
FeatureAttributeDescriptor attrDesc = attributeDescriptors[i]; |
|
226 |
if ( attrDesc.isAutomatic() ) { |
|
227 |
break; |
|
228 |
} |
|
229 |
if ( (attrDesc.isPrimaryKey() || !attrDesc.allowNull()) |
|
230 |
&& feature.get(attrDesc.getName()) == null ) { |
|
231 |
return true; |
|
232 |
} |
|
233 |
} |
|
234 |
return false; |
|
235 |
} |
|
236 |
|
|
237 |
private class AskRequiredAttributtes { |
|
238 |
|
|
239 |
private boolean userCancelValue = true; |
|
240 |
|
|
241 |
public boolean userCancel() { |
|
242 |
return this.userCancelValue; |
|
243 |
} |
|
244 |
|
|
245 |
public void showDialog(final Feature feature) { |
|
246 |
if ( !SwingUtilities.isEventDispatchThread() ) { |
|
247 |
try { |
|
248 |
SwingUtilities.invokeAndWait(new Runnable() { |
|
249 |
public void run() { |
|
250 |
showDialog(feature); |
|
251 |
} |
|
252 |
} |
|
253 |
); |
|
254 |
} catch (Exception e1) { |
|
255 |
message("Can't show form to fill need data.",e1); |
|
256 |
this.userCancelValue = true; |
|
257 |
return; |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
try { |
|
262 |
DynObject data = feature.getAsDynObject(); |
|
263 |
DynObjectEditor editor; |
|
264 |
editor = new DynObjectEditor(data); |
|
265 |
|
|
266 |
String title = data.getDynClass().getDescription(); |
|
267 |
if( StringUtils.isEmpty(title) ) { |
|
268 |
title = data.getDynClass().getName(); |
|
269 |
} |
|
270 |
editor.setTitle( |
|
271 |
ToolsLocator.getI18nManager().getTranslation("_Fill_the_required_fields")+ |
|
272 |
" - "+ |
|
273 |
title); |
|
274 |
|
|
275 |
editor.editObject(true); |
|
276 |
if ( editor.isCanceled() ) { |
|
277 |
this.userCancelValue = true; |
|
278 |
} else { |
|
279 |
editor.getData(data); |
|
280 |
this.userCancelValue = false; |
|
281 |
} |
|
282 |
} catch (Exception ex) { |
|
283 |
message("Can't show form to fill need data.",ex); |
|
284 |
this.userCancelValue = true; |
|
285 |
} |
|
286 |
} |
|
287 |
|
|
288 |
private void message(String msg, Throwable ex) { |
|
289 |
if( ex==null ) { |
|
290 |
logger.warn(msg); |
|
291 |
} else { |
|
292 |
logger.warn(msg,ex); |
|
293 |
} |
|
294 |
// msg = msg + "\nSee to application log for more information."; |
|
295 |
// ApplicationManager application = ApplicationLocator.getManager(); |
|
296 |
// application.message(msg,JOptionPane.WARNING_MESSAGE); |
|
297 |
} |
|
298 |
} |
|
299 |
|
|
181 | 300 |
|
301 |
|
|
182 | 302 |
} |
Also available in: Unified diff