001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.style.sld.internal;
010:
011: import java.awt.Color;
012:
013: import net.refractions.udig.style.sld.SLDEditorPart;
014: import net.refractions.udig.ui.graphics.SLDs;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.events.SelectionListener;
019: import org.eclipse.swt.graphics.RGB;
020: import org.eclipse.swt.layout.RowData;
021: import org.eclipse.swt.layout.RowLayout;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.Label;
026: import org.eclipse.swt.widgets.Spinner;
027: import org.geotools.styling.Fill;
028: import org.geotools.styling.PolygonSymbolizer;
029: import org.geotools.styling.Stroke;
030: import org.geotools.styling.StyleBuilder;
031:
032: /**
033: * Editor for polygon symbolizer
034: *
035: * @author aalam
036: * @since 0.6.0
037: */
038: public class SLDPolygonEditorPart extends SLDEditorPart implements
039: SelectionListener {
040:
041: private StolenColorEditor borderColour;
042: private Button borderEnabled;
043: private Spinner borderWidth;
044:
045: private StolenColorEditor fillColour;
046: private Button fillEnabled;
047: private Spinner borderOpacity;
048:
049: private int opacityMaxValue = 100;
050: private double opacityMaxValueFloat = 100.0;
051: private Spinner fillOpacity;
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see net.refractions.udig.style.sld.SLDEditorPart#getContentType()
057: */
058: public Class getContentType() {
059: return PolygonSymbolizer.class;
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see net.refractions.udig.style.sld.SLDEditorPart#init()
066: */
067: public void init() {
068: // Nothing to do
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see net.refractions.udig.style.sld.SLDEditorPart#reset()
075: */
076: public void reset() {
077: setStylingElements((PolygonSymbolizer) getContent());
078: }
079:
080: private void setStylingElements(PolygonSymbolizer symbolizer) {
081: Color fill = SLDs.polyFill(symbolizer);
082: Color border = SLDs.polyColor(symbolizer);
083: int width = SLDs.polyWidth(symbolizer);
084:
085: if (fill == null) {
086: fill = SymbolizerContent.DEFAULT_FILL_COLOR;
087: fillEnabled.setSelection(false);
088: } else {
089: fillEnabled.setSelection(true);
090: }
091: fillColour.setColorValue(new RGB(fill.getRed(),
092: fill.getGreen(), fill.getBlue()));
093:
094: if (border == null) {
095: border = SymbolizerContent.DEFAULT_BORDER_COLOR;
096: borderEnabled.setSelection(false);
097: } else {
098: borderEnabled.setSelection(true);
099: }
100: borderColour.setColorValue(new RGB(border.getRed(), border
101: .getGreen(), border.getBlue()));
102:
103: if (width == SLDs.NOTFOUND) {
104: width = SymbolizerContent.DEFAULT_BORDER_WIDTH;
105: }
106: borderWidth.setSelection(width);
107:
108: double opacity = SLDs.polyBorderOpacity(symbolizer);
109: if (Double.isNaN(opacity)) {
110: opacity = SymbolizerContent.DEFAULT_POLY_BORDER_OPACITY;
111: }
112: borderOpacity.setSelection((int) (opacity * opacityMaxValue));
113:
114: opacity = SLDs.polyFillOpacity(symbolizer);
115: if (Double.isNaN(opacity)) {
116: opacity = SymbolizerContent.DEFAULT_POLY_FILL_OPACITY;
117: }
118: fillOpacity.setSelection((int) (opacity * opacityMaxValue));
119: }
120:
121: public PolygonSymbolizer getContent() {
122: return (PolygonSymbolizer) super .getContent();
123: }
124:
125: private void applyFill(PolygonSymbolizer polygonSymbolizer,
126: StyleBuilder styleBuilder) {
127: if (fillEnabled.getSelection()) {
128: RGB c = fillColour.getColorValue();
129: Fill fill = polygonSymbolizer.getFill();
130: if (fill == null) {
131: fill = styleBuilder.createFill();
132: polygonSymbolizer.setFill(fill);
133: }
134:
135: fill.setColor(styleBuilder.colorExpression(new Color(c.red,
136: c.green, c.blue)));
137: fill.setOpacity(styleBuilder.literalExpression(fillOpacity
138: .getSelection()
139: / opacityMaxValueFloat));
140: } else {
141: polygonSymbolizer.setFill(null);
142: }
143: }
144:
145: private void applyBorder(PolygonSymbolizer polygonSymbolizer,
146: StyleBuilder styleBuilder) {
147: if (borderEnabled.getSelection()) {
148: RGB c = borderColour.getColorValue();
149: Stroke stroke = polygonSymbolizer.getStroke();
150: if (stroke == null) {
151: stroke = styleBuilder.createStroke();
152: polygonSymbolizer.setStroke(stroke);
153: }
154:
155: stroke.setColor(styleBuilder.colorExpression(new Color(
156: c.red, c.green, c.blue)));
157: stroke.setWidth(styleBuilder.literalExpression(borderWidth
158: .getSelection()));
159: stroke.setOpacity(styleBuilder
160: .literalExpression(borderOpacity.getSelection()
161: / opacityMaxValueFloat));
162: } else {
163: polygonSymbolizer.setStroke(null);
164: }
165: }
166:
167: /**
168: * Construct a subpart labeled with the provided tag.
169: * <p>
170: * Creates a composite with a grid layout of the specifed columns, and a label with text from
171: * tag.
172: * </p>
173: *
174: * @param parent
175: * @param tag
176: * @param numColumns number of columns (usually 2_
177: * @return Composite with one label
178: */
179: private Composite subpart(Composite parent, String tag, int width) {
180: Composite subpart = new Composite(parent, SWT.NONE);
181: RowLayout across = new RowLayout();
182: across.type = SWT.HORIZONTAL;
183: across.wrap = true;
184: across.pack = true;
185: across.fill = true;
186: across.marginBottom = 1;
187: across.marginRight = 2;
188:
189: subpart.setLayout(across);
190:
191: Label label = new Label(subpart, SWT.NONE);
192: label.setText(tag);
193: label.setAlignment(SWT.RIGHT);
194: RowData data = new RowData();
195: data.width = 40;
196: data.height = 10;
197: label.setLayoutData(data);
198:
199: return subpart;
200: }
201:
202: private void borderPart(Composite parent) {
203: Composite border = subpart(parent,
204: Messages.SLDPolygonEditorPart_label_border, 4);
205:
206: borderEnabled = new Button(border, SWT.CHECK);
207: borderEnabled.addSelectionListener(new SelectionListener() {
208: public void widgetSelected(SelectionEvent e) {
209: borderColour.getButton().setEnabled(
210: borderEnabled.getSelection());
211: borderWidth.setEnabled(borderEnabled.getSelection());
212: borderOpacity.setEnabled(borderEnabled.getSelection());
213: }
214:
215: public void widgetDefaultSelected(SelectionEvent e) {
216: }
217: });
218: // borderEnabled.setToolTipText(Messages.SLDMarkerEditorPart_boder_enabled_tooltip);
219:
220: borderColour = new StolenColorEditor(border, this );
221:
222: borderWidth = new Spinner(border, SWT.NONE);
223: borderWidth.setMinimum(1);
224: borderWidth.setMaximum(30);
225: borderWidth.setPageIncrement(5);
226: borderWidth.addSelectionListener(this );
227: // borderWidth.setToolTipText(Messages.SLDMarkerEditorPart_boder_width_tooltip);
228:
229: borderOpacity = new Spinner(border, SWT.NONE);
230: borderOpacity.setMinimum(0);
231: borderOpacity.setMaximum(opacityMaxValue);
232: borderOpacity.setPageIncrement(10);
233: // borderOpacity.setToolTipText(Messages.SLDMarkerEditorPart_boder_opacity_tooltip);
234: }
235:
236: private void fillPart(Composite parent) {
237: Composite fill = subpart(parent,
238: Messages.SLDPolygonEditorPart_label_fill, 3);
239: fillEnabled = new Button(fill, SWT.CHECK);
240: fillEnabled.addSelectionListener(this );
241: // fillEnabled.setToolTipText(Messages.SLDMarkerEditorPart_marker_enabled_tooltip);
242: fillEnabled.addSelectionListener(new SelectionListener() {
243: public void widgetSelected(SelectionEvent e) {
244: fillColour.getButton().setEnabled(
245: fillEnabled.getSelection());
246: fillOpacity.setEnabled(fillEnabled.getSelection());
247: }
248:
249: public void widgetDefaultSelected(SelectionEvent e) {
250: }
251: });
252:
253: fillColour = new StolenColorEditor(fill, this );
254:
255: fillOpacity = new Spinner(fill, SWT.NONE);
256: fillOpacity.setMinimum(0);
257: fillOpacity.setMaximum(opacityMaxValue);
258: fillOpacity.setPageIncrement(10);
259: // fillOpacity.setToolTipText(Messages.SLDMarkerEditorPart_fill_opacity_tooltip);
260: }
261:
262: protected Control createPartControl(Composite parent) {
263: RowLayout layout = new RowLayout();
264: layout.pack = false;
265: layout.wrap = true;
266: layout.type = SWT.HORIZONTAL;
267: layout.fill = true;
268: layout.marginLeft = 0;
269: layout.marginRight = 0;
270: layout.marginTop = 0;
271: layout.marginBottom = 0;
272: layout.spacing = 0;
273: parent.setLayout(layout);
274:
275: borderPart(parent);
276: fillPart(parent);
277:
278: return parent;
279: }
280:
281: public void widgetDefaultSelected(SelectionEvent e) {
282: // TODO: Commit the style
283: }
284:
285: /*
286: * (non-Javadoc)
287: *
288: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
289: */
290: public void widgetSelected(SelectionEvent e) {
291: applyFill(getContent(), getStyleBuilder());
292: applyBorder(getContent(), getStyleBuilder());
293: }
294: }
|