001: /**
002: *
003: */package net.refractions.udig.validation;
004:
005: import java.util.Iterator;
006:
007: import net.refractions.udig.project.ILayer;
008: import net.refractions.udig.ui.PlatformGIS;
009: import net.refractions.udig.ui.operations.IOp;
010:
011: import org.eclipse.core.runtime.IProgressMonitor;
012: import org.eclipse.jface.dialogs.Dialog;
013: import org.eclipse.swt.widgets.Display;
014: import org.eclipse.swt.widgets.Shell;
015: import org.geotools.data.FeatureSource;
016: import org.geotools.feature.Feature;
017: import org.geotools.feature.FeatureCollection;
018: import org.geotools.feature.FeatureType;
019: import org.geotools.validation.FeatureValidation;
020:
021: /**
022: * An abstract class for feature validation which uses org.geotools.validation
023: *
024: * @author chorner
025: * @since 1.0.1
026: */
027: abstract class FeatureValidationOp implements IOp {
028: public GenericValidationResults results; //for testing
029:
030: /**
031: *
032: *
033: * @return the appropriate Validating Feature Method Class
034: */
035: abstract FeatureValidation getValidator();
036:
037: /**
038: *
039: * @see net.refractions.udig.ui.operations.IOp#op(org.eclipse.swt.widgets.Display, java.lang.Object, org.eclipse.core.runtime.IProgressMonitor)
040: */
041: public void op(final Display display, Object target,
042: IProgressMonitor monitor) throws Exception {
043: final ILayer layer = (ILayer) target;
044: FeatureSource source = layer.getResource(FeatureSource.class,
045: monitor);
046: FeatureCollection collection;
047: collection = source.getFeatures();
048: results = new GenericValidationResults();
049:
050: PlatformGIS.syncInDisplayThread(new Runnable() {
051:
052: public void run() {
053: Dialog dialog = getDialog(display.getActiveShell(),
054: layer.getSchema());
055: if (dialog != null) {
056: dialog.open();
057: }
058: }
059:
060: });
061:
062: final FeatureValidation featureValidation = getValidator();
063: if (featureValidation == null)
064: return;
065: //IsValidGeometryValidation geometryValidation = new IsValidGeometryValidation();
066: FeatureType type;
067:
068: // iterate through the collection and validate each feature
069: Iterator iterator;
070: for (iterator = collection.iterator(); iterator.hasNext();) {
071: Feature feature = (Feature) iterator.next();
072: type = feature.getFeatureType();
073: if (canValidate(type)) {
074: featureValidation.validate(feature, type, results);
075: }
076: }
077: collection.close(iterator);
078:
079: OpUtils.setSelection(layer, results);
080: //OpUtils.notifyUser(display, results);
081:
082: monitor.internalWorked(1);
083: monitor.done();
084: }
085:
086: /**
087: * This method may be overridden for classes which need a dialog for user input
088: *
089: * @param shell
090: * @param featureType
091: * @return null
092: */
093: protected Dialog getDialog(Shell shell, FeatureType featureType) {
094: return null;
095: }
096:
097: /**
098: * This method may be overridden for classes which only validate certain featureTypes
099: *
100: * @param featureType
101: * @return boolean
102: */
103: protected boolean canValidate(FeatureType featureType) {
104: return true;
105: }
106: }
|