001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.style.sld;
018:
019: import net.refractions.udig.project.internal.Layer;
020: import net.refractions.udig.project.internal.StyleBlackboard;
021: import net.refractions.udig.style.sld.simple.RasterViewer;
022: import net.refractions.udig.style.sld.simple.ScaleViewer;
023:
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.events.KeyAdapter;
026: import org.eclipse.swt.events.KeyEvent;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.events.SelectionListener;
029: import org.eclipse.swt.widgets.Composite;
030: import org.geotools.data.wms.WebMapServer;
031: import org.geotools.styling.FeatureTypeStyle;
032: import org.geotools.styling.RasterSymbolizer;
033: import org.geotools.styling.Rule;
034: import org.geotools.styling.SLD;
035: import org.geotools.styling.Style;
036: import org.geotools.styling.Symbolizer;
037: import org.opengis.coverage.grid.GridCoverage;
038:
039: /**
040: * Allow editing of a RasterSymbolizaer.
041: * <p>
042: * Currently this is focused on opacity and scale settings
043: * </p>
044: * @author mleslie
045: * @since 1.0.
046: */
047: public class SimpleRasterConfigurator extends
048: AbstractSimpleConfigurator {
049: private RasterViewer raster = new RasterViewer();
050: ScaleViewer minScale = new ScaleViewer(ScaleViewer.MIN);
051: ScaleViewer maxScale = new ScaleViewer(ScaleViewer.MAX);
052:
053: SelectionListener synchronize = new SelectionListener() {
054:
055: public void widgetSelected(SelectionEvent e) {
056: synchronize();
057: }
058:
059: public void widgetDefaultSelected(SelectionEvent e) {
060: synchronize();
061: // apply(); // and apply?
062: }
063:
064: };
065:
066: /**
067: * Construct <code>SimpleRasterConfigurator</code>.
068: *
069: */
070: public SimpleRasterConfigurator() {
071: super ();
072: this .raster.addListener(this .synchronize);
073: this .minScale.addListener(this .synchronize);
074: this .maxScale.addListener(this .synchronize);
075: }
076:
077: @Override
078: public boolean canStyle(Layer aLayer) {
079: if (aLayer.hasResource(GridCoverage.class)
080: || aLayer.hasResource(WebMapServer.class))
081: return true;
082: return false;
083: }
084:
085: @Override
086: protected void refresh() {
087: Style style = getStyle();
088: RasterSymbolizer sym = SLD.rasterSymbolizer(style);
089: this .raster.set(sym);
090:
091: Rule r = style.getFeatureTypeStyles()[0].getRules()[0];
092: double minScaleDen = r.getMinScaleDenominator();
093: double maxScaleDen = r.getMaxScaleDenominator();
094: this .minScale.setScale(minScaleDen, Math.round(getLayer()
095: .getMap().getViewportModel().getScaleDenominator()));
096: this .maxScale.setScale(maxScaleDen, Math.round(getLayer()
097: .getMap().getViewportModel().getScaleDenominator()));
098: }
099:
100: @Override
101: public void createControl(Composite parent) {
102: setLayout(parent);
103: KeyAdapter adapter = new KeyAdapter() {
104: @Override
105: public void keyReleased(KeyEvent e) {
106: /*
107: * I don't like having different ways of checking for keypad enter
108: * and the normal one. Using the keyCode would be better, but
109: * I couldn't readily find the value for CR.
110: */
111: if (e.keyCode == SWT.KEYPAD_CR || e.character == SWT.CR) {
112: makeActionDoStuff();
113: }
114: }
115: };
116: this .raster.createControl(parent, adapter);
117: this .minScale.createControl(parent, adapter);
118: this .maxScale.createControl(parent, adapter);
119: }
120:
121: @Override
122: public void synchronize() {
123: RasterSymbolizer sym = this .raster.get(this .build);
124: Rule rule = this .build.createRule(new Symbolizer[] { sym });
125: double minScaleDen = minScale.getScale();
126: double maxScaleDen = maxScale.getScale();
127: if (minScale.isEnabled())
128: rule.setMinScaleDenominator(minScaleDen);
129: if (maxScale.isEnabled())
130: rule.setMaxScaleDenominator(maxScaleDen);
131: FeatureTypeStyle featureTypeStyle = this .build
132: .createFeatureTypeStyle("Feature", rule); //$NON-NLS-1$
133: Style style = this .build.createStyle();
134: style.setDefault(true);
135: style.setName("simple"); //$NON-NLS-1$
136: style
137: .setFeatureTypeStyles(new FeatureTypeStyle[] { featureTypeStyle, });
138: //attach the SLD
139: SLDContent.createDefaultStyledLayerDescriptor(style);
140: //put style on blackboard
141: getStyleBlackboard().put(SLDContent.ID, style);
142:
143: ((StyleBlackboard) getStyleBlackboard())
144: .setSelected(new String[] { SLDContent.ID });
145: }
146:
147: }
|