001: package net.refractions.udig.render.internal.wms.basic;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.List;
007:
008: import org.eclipse.core.runtime.NullProgressMonitor;
009: import org.geotools.data.ows.Layer;
010: import org.geotools.data.ows.StyleImpl;
011: import org.geotools.styling.Style;
012:
013: import net.refractions.udig.catalog.IService;
014: import net.refractions.udig.project.ILayer;
015: import net.refractions.udig.project.internal.render.Renderer;
016: import net.refractions.udig.project.render.AbstractRenderMetrics;
017: import net.refractions.udig.project.render.ICompositeRenderContext;
018: import net.refractions.udig.project.render.IRenderContext;
019: import net.refractions.udig.project.render.IRenderMetricsFactory;
020: import net.refractions.udig.render.wms.basic.WMSPlugin;
021: import net.refractions.udig.style.sld.SLDContent;
022: import net.refractions.udig.ui.ProgressManager;
023: import net.refractions.udig.ui.graphics.SLDs;
024:
025: /**
026: * Used to describe the rendering abilities of BasicWMSRenderer2.
027: * @author Richard Gould
028: */
029: public class BasicWMSMetrics2 extends AbstractRenderMetrics {
030:
031: /**
032: * Construct <code>BasicWMSMetrics2</code>.
033: *
034: * @param context2
035: * @param factory
036: */
037: public BasicWMSMetrics2(IRenderContext context2,
038: BasicWMSMetricsFactory2 factory) {
039: super (context2, factory);
040: }
041:
042: public Renderer createRenderer() {
043: Renderer renderer = new BasicWMSRenderer2();
044: renderer.setContext(context);
045: return renderer;
046: }
047:
048: public IRenderContext getRenderContext() {
049: return context;
050: }
051:
052: /**
053: * @see net.refractions.udig.project.render.IRenderMetrics#getRenderMetricsFactory()
054: */
055: public IRenderMetricsFactory getRenderMetricsFactory() {
056: return factory;
057: }
058:
059: public boolean canAddLayer(ILayer layer) {
060:
061: if (!layer.hasResource(Layer.class))
062: return false;
063:
064: try {
065: if (!layer.findGeoResource(Layer.class).parent(
066: ProgressManager.instance().get()).equals(
067: getRenderContext().getGeoResource().parent(
068: ProgressManager.instance().get())))
069: return false;
070: } catch (IOException e2) {
071: return false;
072: }
073:
074: double opacity = Double.NaN;
075:
076: ICompositeRenderContext context1 = (ICompositeRenderContext) context;
077:
078: IRenderContext[] contexts = context1.getContexts().toArray(
079: new IRenderContext[context1.getContexts().size()]);
080: Arrays.sort(contexts);
081: List<Layer> owsLayers = new ArrayList<Layer>();
082: IService currentService;
083: try {
084: owsLayers.add(layer.getResource(Layer.class,
085: new NullProgressMonitor()));
086: currentService = layer.getResource(IService.class, null);
087: } catch (IOException e1) {
088: WMSPlugin.log("", e1); //$NON-NLS-1$
089: return false;
090: }
091: for (IRenderContext renderContext : contexts) {
092: ILayer previousLayer = renderContext.getLayer();
093:
094: try {
095: owsLayers.add(previousLayer.getResource(Layer.class,
096: new NullProgressMonitor()));
097: IService previousService = previousLayer.getResource(
098: IService.class, null);
099: if (currentService != previousService) {
100: return false;
101: }
102: } catch (IOException e) {
103: WMSPlugin.log("Error while retrieving service.", e); //$NON-NLS-1$
104: return false;
105: }
106:
107: if (BasicWMSRenderer2.findRequestCRS(owsLayers, context
108: .getCRS()) == null)
109: return false;
110:
111: Style style = (Style) previousLayer.getStyleBlackboard()
112: .get(SLDContent.ID);
113: if (style != null) {
114: opacity = SLDs.rasterOpacity(SLDs
115: .rasterSymbolizer(style));
116: }
117: }
118:
119: Style style = (Style) layer.getStyleBlackboard().get(
120: SLDContent.ID);
121: if (style == null && Double.isNaN(opacity)) {
122: return true;
123: }
124:
125: double result = SLDs
126: .rasterOpacity(SLDs.rasterSymbolizer(style));
127:
128: if (result == opacity) {
129: return true;
130: }
131: return false;
132: }
133:
134: // XXX: Consider doing more SLD when WMS post is a go
135: public boolean canStyle(String SyleID, Object value) {
136: if (value == null)
137: return false;
138: if (value instanceof Style)
139: return !Double.isNaN(SLDs.rasterOpacity((Style) value));
140:
141: if (value instanceof StyleImpl
142: && getRenderContext().getGeoResource().canResolve(
143: Layer.class)) {
144: try {
145: Layer layer = getRenderContext().getGeoResource()
146: .resolve(Layer.class,
147: ProgressManager.instance().get());
148: if (layer.getStyles().contains(value)) {
149: return true;
150: } else {
151: return false;
152: }
153: } catch (IOException e) {
154: return false;
155: }
156: }
157:
158: return false;
159: }
160:
161: public boolean isOptimized() {
162: return false;
163: }
164: }
|