001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.interceptor;
016:
017: import java.io.IOException;
018:
019: import net.refractions.udig.catalog.IResolve;
020: import net.refractions.udig.catalog.IService;
021: import net.refractions.udig.project.ILayer;
022: import net.refractions.udig.project.IResourceInterceptor;
023: import net.refractions.udig.project.ProjectBlackboardConstants;
024: import net.refractions.udig.project.internal.ProjectPlugin;
025: import net.refractions.udig.ui.ProgressManager;
026:
027: import org.geotools.data.DataStore;
028: import org.geotools.data.DefaultQuery;
029: import org.geotools.data.FeatureSource;
030: import org.geotools.data.Query;
031: import org.geotools.feature.SchemaException;
032: import org.geotools.filter.Filter;
033:
034: /**
035: * If a filter or a query is in the layer properties or on the map blackboard under the key: the {@link #KEY} then this
036: * interceptor will return the "view" see {@link DataStore#getView(Query)}.
037: * <p>If a filter is in the layer properties then the filter will only apply to that layer. </p>
038: * <p>If a filter is on the map blackboard then it will apply to all layers.</p>
039: * <p>If a query is in the Layer properties then it will be applied to the layer only if the type name is correct</p>
040: * <p>If a query is on the Map blackboard then it will be applied only those layers who's typename is the same as that in the query.</p>
041: * <p>If there is something on both the Map blackboard and the layer properties then the item on the layer blackboard will take precedence.</p>
042: *
043: * @author Jesse
044: * @since 1.1.0
045: */
046: public class ShowViewInterceptor implements
047: IResourceInterceptor<FeatureSource> {
048:
049: /**
050: * The key that is checked to see if a filter is on the Map Blackboard or the Layer Properties.
051: */
052: public static final String KEY = ProjectBlackboardConstants.LAYER__DATA_QUERY;
053:
054: public FeatureSource run(ILayer layer, FeatureSource resource,
055: Class<? super FeatureSource> requestedType) {
056: Object prop = layer.getBlackboard().get(KEY);
057: if (prop == null && layer.getMap() != null)
058: prop = layer.getMap().getBlackboard().get(KEY);
059: if (prop instanceof Filter || prop instanceof Query) {
060: try {
061: IResolve parent = layer.findGeoResource(
062: FeatureSource.class).parent(
063: ProgressManager.instance().get());
064: if (parent instanceof IService) {
065: IService service = (IService) parent;
066: DataStore ds = service.resolve(DataStore.class,
067: ProgressManager.instance().get());
068: if (ds == null)
069: return resource;
070: FeatureSource view;
071: if (prop instanceof Filter)
072: view = ds.getView(new DefaultQuery(resource
073: .getSchema().getTypeName(),
074: (Filter) prop));
075: else {
076: Query query = (Query) prop;
077: if (query.getTypeName() == null
078: || resource.getSchema().getTypeName()
079: .equals(query.getTypeName()))
080: view = ds.getView(query);
081: else {
082: view = null;
083: }
084: }
085: if (view != null) {
086: if (requestedType
087: .isAssignableFrom(FeatureSource.class)) {
088: return view;
089: } else {
090: return null;
091: }
092: }
093: }
094: } catch (IOException e) {
095: ProjectPlugin.log("Error getting view", e); //$NON-NLS-1$
096: } catch (SchemaException e) {
097: ProjectPlugin.log("Error getting view", e); //$NON-NLS-1$
098: }
099: }
100: return resource;
101: }
102:
103: }
|