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.Combo;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Spinner;
028: import org.geotools.styling.Graphic;
029: import org.geotools.styling.Mark;
030: import org.geotools.styling.PointSymbolizer;
031: import org.geotools.styling.StyleBuilder;
032:
033: /**
034: * Simple view part for editing a Marker.
035: *
036: * @author aalam
037: * @since 1.0.0
038: */
039: public class SLDMarkerEditorPart extends SLDEditorPart implements
040: SelectionListener {
041:
042: private int opacityMaxValue = 100;
043: private double opacityMaxValueFloat = 100.0;
044:
045: private StolenColorEditor borderColour;
046: private Button borderEnabled;
047: private Spinner borderWidth;
048: private Spinner borderOpacity;
049:
050: private StolenColorEditor markerColour;
051: private Button markerEnabled;
052: private Combo markerType;
053: private Spinner markerWidth;
054: private Spinner markerOpacity;
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see net.refractions.udig.style.sld.SLDEditorPart#getContentType()
060: */
061: public Class getContentType() {
062: return PointSymbolizer.class;
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see net.refractions.udig.style.sld.SLDEditorPart#init()
069: */
070: public void init() {
071: // do nothing
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see net.refractions.udig.style.sld.SLDEditorPart#reset()
078: */
079: public void reset() {
080: // initialize the ui
081: setStylingElements((PointSymbolizer) getContent());
082: }
083:
084: private void setStylingElements(PointSymbolizer symbolizer) {
085: Color fill = SLDs.pointFill(symbolizer);
086: Color border = SLDs.pointColor(symbolizer);
087: int markerSize = SLDs.pointSize(symbolizer);
088: int borderSize = SLDs.pointWidth(symbolizer);
089: String wellKnownName = SLDs.pointWellKnownName(symbolizer);
090:
091: if (markerSize == SLDs.NOTFOUND) {
092: markerSize = SymbolizerContent.DEFAULT_MARKER_WIDTH;
093: }
094:
095: markerWidth.setSelection(markerSize);
096:
097: RGB colour = null;
098: if (fill != null) {
099: colour = new RGB(fill.getRed(), fill.getGreen(), fill
100: .getBlue());
101: markerEnabled.setSelection(true);
102: } else {
103: colour = new RGB(SymbolizerContent.DEFAULT_MARKER_COLOR
104: .getRed(), SymbolizerContent.DEFAULT_MARKER_COLOR
105: .getGreen(), SymbolizerContent.DEFAULT_MARKER_COLOR
106: .getBlue());
107: markerEnabled.setSelection(false);
108: }
109: markerColour.setColorValue(colour);
110:
111: borderWidth.setSelection(borderSize);
112:
113: if (border != null) {
114: colour = new RGB(border.getRed(), border.getGreen(), border
115: .getBlue());
116: borderEnabled.setSelection(true);
117: } else {
118: colour = new RGB(SymbolizerContent.DEFAULT_BORDER_COLOR
119: .getRed(), SymbolizerContent.DEFAULT_BORDER_COLOR
120: .getGreen(), SymbolizerContent.DEFAULT_BORDER_COLOR
121: .getBlue());
122: borderEnabled.setSelection(false);
123: }
124: borderColour.setColorValue(colour);
125:
126: markerType.setItems(getStyleBuilder().getWellKnownMarkNames());
127: if (wellKnownName == null) {
128: wellKnownName = SymbolizerContent.DEFAULT_MARKER_TYPE;
129: }
130: int index = markerType.indexOf(wellKnownName);
131: if (index == -1) {
132: markerType.add(wellKnownName);
133: markerType.select(0);
134: } else {
135: markerType.select(index);
136: }
137:
138: double opacity = SLDs.pointBorderOpacity(symbolizer);
139: if (Double.isNaN(opacity)) {
140: opacity = SymbolizerContent.DEFAULT_MARKER_BORDER_OPACITY;
141: }
142: borderOpacity.setSelection((int) (opacity * opacityMaxValue));
143:
144: opacity = SLDs.pointOpacity(symbolizer);
145: if (Double.isNaN(opacity)) {
146: opacity = SymbolizerContent.DEFAULT_MARKER_OPACITY;
147: }
148: markerOpacity.setSelection((int) (opacity * opacityMaxValue));
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see net.refractions.udig.style.StyleConfigurator#apply()
155: */
156: private void apply() {
157: PointSymbolizer symbolizer = (PointSymbolizer) getContent();
158: StyleBuilder styleBuilder = getStyleBuilder();
159:
160: Graphic g = symbolizer.getGraphic();
161:
162: Mark[] mark = new Mark[1];
163: mark[0] = styleBuilder.createMark(markerType.getText());
164: RGB colour = markerColour.getColorValue();
165: if (markerEnabled.getSelection()) {
166: mark[0].setFill(styleBuilder.createFill(new Color(
167: colour.red, colour.green, colour.blue)));
168: mark[0].getFill().setOpacity(
169: styleBuilder.literalExpression(markerOpacity
170: .getSelection()
171: / opacityMaxValueFloat));
172: } else {
173: mark[0].setFill(null);
174: }
175: colour = borderColour.getColorValue();
176: g.setSize(styleBuilder.literalExpression(new Integer(
177: markerWidth.getSelection()).doubleValue()));
178: colour = borderColour.getColorValue();
179: if (borderEnabled.getSelection()) {
180: mark[0].setStroke(styleBuilder.createStroke(new Color(
181: colour.red, colour.green, colour.blue),
182: (new Integer(borderWidth.getSelection()))
183: .doubleValue()));
184: mark[0].getStroke().setOpacity(
185: styleBuilder.literalExpression(borderOpacity
186: .getSelection()
187: / opacityMaxValueFloat));
188: } else {
189: mark[0].setStroke(null);
190: }
191: g.setMarks(mark);
192: }
193:
194: /**
195: * Construct a subpart labeled with the provided tag.
196: * <p>
197: * Creates a composite with a grid layout of the specifed columns, and a label with text from
198: * tag.
199: * </p>
200: *
201: * @param parent
202: * @param tag
203: * @param numColumns number of columns (usually 2_
204: * @return Composite with one label
205: */
206: private Composite subpart(Composite parent, String tag, int width) {
207: Composite subpart = new Composite(parent, SWT.NONE);
208: RowLayout across = new RowLayout();
209: across.type = SWT.HORIZONTAL;
210: across.wrap = true;
211: across.pack = true;
212: across.fill = true;
213: across.marginBottom = 1;
214: across.marginRight = 2;
215:
216: subpart.setLayout(across);
217:
218: Label label = new Label(subpart, SWT.NONE);
219: label.setText(tag);
220: label.setAlignment(SWT.RIGHT);
221: RowData data = new RowData();
222: data.width = 40;
223: data.height = 10;
224: label.setLayoutData(data);
225:
226: return subpart;
227: }
228:
229: /**
230: * Create a row layout, with individual rows provided by sub part.
231: *
232: * @see net.refractions.udig.style.StyleConfigurator#createControl(org.eclipse.swt.widgets.Composite)
233: */
234: protected Control createPartControl(Composite parent) {
235: RowLayout layout = new RowLayout();
236: layout.pack = false;
237: layout.wrap = true;
238: layout.type = SWT.HORIZONTAL;
239: layout.fill = true;
240: layout.marginLeft = 0;
241: layout.marginRight = 0;
242: layout.marginTop = 0;
243: layout.marginBottom = 0;
244: layout.spacing = 0;
245: parent.setLayout(layout);
246:
247: borderPart(parent);
248: fillPart(parent);
249: markerPart(parent);
250:
251: return parent;
252: }
253:
254: private void markerPart(Composite parent) {
255: Composite marker = subpart(parent,
256: Messages.SLDMarkerEditorPart_label_marker, 2);
257:
258: markerType = new Combo(marker, SWT.READ_ONLY);
259: markerType.addSelectionListener(this );
260:
261: markerWidth = new Spinner(marker, SWT.NONE);
262: markerWidth.setMinimum(1);
263: markerWidth.setMaximum(30);
264: markerWidth.setPageIncrement(5);
265: markerWidth.addSelectionListener(this );
266: markerWidth
267: .setToolTipText(Messages.SLDMarkerEditorPart_width_tooltip);
268: }
269:
270: private void borderPart(Composite parent) {
271: Composite border = subpart(parent,
272: Messages.SLDMarkerEditorPart_label_border, 4);
273:
274: borderEnabled = new Button(border, SWT.CHECK);
275: borderEnabled.addSelectionListener(this );
276: borderEnabled
277: .setToolTipText(Messages.SLDMarkerEditorPart_border_enabled_tooltip);
278:
279: borderColour = new StolenColorEditor(border, this );
280:
281: borderWidth = new Spinner(border, SWT.NONE);
282: borderWidth.setMinimum(1);
283: borderWidth.setMaximum(30);
284: borderWidth.setPageIncrement(5);
285: borderWidth.addSelectionListener(this );
286: borderWidth
287: .setToolTipText(Messages.SLDMarkerEditorPart_border_width_tooltip);
288:
289: borderOpacity = new Spinner(border, SWT.NONE);
290: borderOpacity.setMinimum(0);
291: borderOpacity.setMaximum(opacityMaxValue);
292: borderOpacity.setPageIncrement(10);
293: borderOpacity
294: .setToolTipText(Messages.SLDMarkerEditorPart_border_opacity_tooltip);
295: }
296:
297: private void fillPart(Composite parent) {
298: Composite fill = subpart(parent,
299: Messages.SLDMarkerEditorPart_label_fill, 3);
300: markerEnabled = new Button(fill, SWT.CHECK);
301: markerEnabled.addSelectionListener(this );
302: markerEnabled
303: .setToolTipText(Messages.SLDMarkerEditorPart_marker_enabled_tooltip);
304:
305: markerColour = new StolenColorEditor(fill, this );
306:
307: markerOpacity = new Spinner(fill, SWT.NONE);
308: markerOpacity.setMinimum(0);
309: markerOpacity.setMaximum(opacityMaxValue);
310: markerOpacity.setPageIncrement(10);
311: markerOpacity
312: .setToolTipText(Messages.SLDMarkerEditorPart_fill_opacity_tooltip);
313: }
314:
315: /*
316: * (non-Javadoc)
317: *
318: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
319: */
320: public void widgetDefaultSelected(SelectionEvent e) {
321: // Don't care.
322: // TODO: Commit style here
323: }
324:
325: /*
326: * (non-Javadoc)
327: *
328: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
329: */
330: public void widgetSelected(SelectionEvent e) {
331: apply();
332: }
333:
334: }
|