001: package net.refractions.udig.style.sld.internal;
002:
003: import java.awt.Color;
004:
005: import net.refractions.udig.project.internal.Layer;
006: import net.refractions.udig.style.sld.SLDEditorPart;
007: import net.refractions.udig.ui.graphics.SLDs;
008:
009: import org.eclipse.swt.SWT;
010: import org.eclipse.swt.events.SelectionAdapter;
011: import org.eclipse.swt.events.SelectionEvent;
012: import org.eclipse.swt.graphics.FontData;
013: import org.eclipse.swt.graphics.RGB;
014: import org.eclipse.swt.layout.RowData;
015: import org.eclipse.swt.layout.RowLayout;
016: import org.eclipse.swt.widgets.Button;
017: import org.eclipse.swt.widgets.Combo;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Label;
021: import org.eclipse.swt.widgets.Spinner;
022: import org.geotools.data.FeatureSource;
023: import org.geotools.feature.AttributeType;
024: import org.geotools.feature.FeatureType;
025: import org.geotools.filter.Expression;
026: import org.geotools.filter.FilterFactoryFinder;
027: import org.geotools.styling.HaloImpl;
028: import org.geotools.styling.StyleBuilder;
029: import org.geotools.styling.TextSymbolizer;
030:
031: /**
032: * summary sentence.
033: * <p>
034: * Paragraph ...
035: * </p>
036: * <p>
037: * Responsibilities:
038: * <ul>
039: * <li>
040: * <li>
041: * </ul>
042: * </p>
043: * <p>
044: * Example:
045: *
046: * <pre><code>
047: *
048: * SLDTextEditorPart x = new SLDTextEditorPart( ... );
049: * TODO code example
050: *
051: * </code></pre>
052: *
053: * </p>
054: *
055: * @author aalam
056: * @since 0.6.0
057: */
058: public class SLDTextEditorPart extends SLDEditorPart {
059:
060: private static final int LABEL = 1;
061: private static final int FONTCOLOR = 2;
062: private static final int HALOCOLOR = 2;
063:
064: private StolenColorEditor labelHaloColorEditor;
065: private FontEditor labelFont;
066: private Combo labelCombo;
067: private Button labelHaloEnabled;
068: private Spinner haloWidthScale;
069: private Spinner haloOpacityScale;
070: private int opacityMaxValue = 100;
071: private double opacityMaxValueFloat = 100.0;
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see net.refractions.udig.style.sld.SLDEditorPart#getContentType()
077: */
078: public Class getContentType() {
079: return TextSymbolizer.class;
080: }
081:
082: /**
083: * Construct a subpart labeled with the provided tag.
084: * <p>
085: * Creates a composite with a grid layout of the specifed columns,
086: * and a label with text from tag.
087: * </p>
088: * @param parent
089: * @param tag
090: * @param numColumns number of columns (usually 2_
091: * @return Composite with one label
092: */
093: private Composite subpart(Composite parent, String tag, int width) {
094: Composite subpart = new Composite(parent, SWT.NONE);
095: RowLayout across = new RowLayout();
096: across.type = SWT.HORIZONTAL;
097: across.wrap = true;
098: across.pack = true;
099: across.fill = true;
100: across.marginBottom = 1;
101: across.marginRight = 2;
102:
103: subpart.setLayout(across);
104:
105: Label label = new Label(subpart, SWT.NONE);
106: label.setText(tag);
107: label.setAlignment(SWT.RIGHT);
108: RowData data = new RowData();
109: data.width = 40;
110: data.height = 10;
111: label.setLayoutData(data);
112:
113: return subpart;
114: }
115:
116: private void labelPart(Composite parent) {
117: Composite part = subpart(parent,
118: Messages.SLDTextEditorPart_label_label, 2);
119:
120: labelCombo = new Combo(part, SWT.READ_ONLY);
121: labelCombo.addSelectionListener(new SelectionAdapter() {
122: public void widgetSelected(SelectionEvent e) {
123: apply(LABEL);
124: }
125: });
126:
127: labelFont = new FontEditor(part, new SelectionAdapter() {
128: public void widgetSelected(SelectionEvent event) {
129: apply(FONTCOLOR);
130: }
131: });
132: }
133:
134: private void haloPart(Composite parent) {
135: Composite halo = subpart(parent,
136: Messages.SLDTextEditorPart_label_halo, 3);
137:
138: labelHaloEnabled = new Button(halo, SWT.CHECK);
139: labelHaloEnabled.addSelectionListener(new SelectionAdapter() {
140: public void widgetSelected(SelectionEvent event) {
141: apply(HALOCOLOR);
142: }
143: });
144:
145: labelHaloColorEditor = new StolenColorEditor(halo,
146: new SelectionAdapter() {
147: public void widgetSelected(SelectionEvent event) {
148: apply(HALOCOLOR);
149: }
150: });
151:
152: haloWidthScale = new Spinner(halo, SWT.HORIZONTAL);
153: haloWidthScale.setMinimum(0);
154: haloWidthScale.setMaximum(10);
155: haloWidthScale.setPageIncrement(1);
156: haloWidthScale.addSelectionListener(new SelectionAdapter() {
157: public void widgetSelected(SelectionEvent e) {
158: apply(HALOCOLOR);
159: }
160: });
161:
162: haloOpacityScale = new Spinner(halo, SWT.HORIZONTAL);
163: haloOpacityScale.setMinimum(0);
164: haloOpacityScale.setMaximum(opacityMaxValue);
165: haloOpacityScale.setPageIncrement(10);
166: haloOpacityScale.addSelectionListener(new SelectionAdapter() {
167: public void widgetSelected(SelectionEvent e) {
168: apply(HALOCOLOR);
169: }
170: });
171:
172: }
173:
174: /*
175: * (non-Javadoc)
176: *
177: * @see net.refractions.udig.style.sld.SLDEditorPart#createPartControl(org.eclipse.swt.widgets.Composite)
178: */
179: protected Control createPartControl(Composite parent) {
180: RowLayout layout = new RowLayout();
181: layout.pack = false;
182: layout.wrap = true;
183: layout.type = SWT.HORIZONTAL;
184: layout.fill = true;
185: layout.marginLeft = 0;
186: layout.marginRight = 0;
187: layout.marginTop = 0;
188: layout.marginBottom = 0;
189: layout.spacing = 0;
190: parent.setLayout(layout);
191:
192: labelPart(parent);
193: haloPart(parent);
194:
195: return parent;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see net.refractions.udig.style.sld.SLDEditorPart#init()
202: */
203: public void init() {
204: // do nothing
205: }
206:
207: private void setStylingElements(TextSymbolizer symbolizer) {
208:
209: Color fontFill = SLDs.textFontFill(symbolizer);
210: FontData[] tempFD = SLDs.textFont(symbolizer);
211: Expression label = SLDs.textLabel(symbolizer);
212: if (fontFill == null) {
213: fontFill = SymbolizerContent.DEFAULT_FONT_COLOR;
214: }
215: labelFont.setColorValue(new RGB(fontFill.getRed(), fontFill
216: .getGreen(), fontFill.getBlue()));
217: if (tempFD == null) {
218: tempFD = new FontData[1];
219: tempFD[0] = new FontData(
220: SymbolizerContent.DEFAULT_FONT_FACE,
221: SymbolizerContent.DEFAULT_FONT_SIZE,
222: SymbolizerContent.DEFAULT_FONT_STYLE);
223: }
224: labelFont.setFontList(tempFD);
225:
226: // Need to get all available labels
227: //check if this layer has a feature
228: Layer currLayer = getLayer();
229: AttributeType[] attributeList = null;
230: AttributeType defaultGeom = null;
231: if (currLayer.hasResource(FeatureSource.class)) {
232: FeatureType ft = currLayer.getSchema();
233: attributeList = ft.getAttributeTypes();
234: defaultGeom = ft.getDefaultGeometry();
235: }
236: labelCombo.removeAll();
237: if (attributeList != null) {
238: for (int i = 0; i < attributeList.length; i++) {
239: if (attributeList[i] != defaultGeom)
240: labelCombo.add(attributeList[i].getName());
241: if (label != null
242: && attributeList[i] != null
243: && attributeList[i].getName().equals(
244: label.toString())) {
245: //Set the correct initial label
246: labelCombo.select(i);
247: } else if (i == 0) {
248: labelCombo.select(i);
249: }
250: }
251: }
252: labelCombo.pack(true);
253:
254: Color haloFill = SLDs.textHaloFill(symbolizer);
255: if (haloFill == null) {
256: haloFill = SymbolizerContent.DEFAULT_HALO_COLOR;
257: labelHaloEnabled.setSelection(false);
258: } else {
259: labelHaloEnabled.setSelection(true);
260: }
261: labelHaloColorEditor.setColorValue(new RGB(haloFill.getRed(),
262: haloFill.getGreen(), haloFill.getBlue()));
263:
264: int width = SLDs.textHaloWidth(symbolizer);
265: if (width == 0) {
266: width = (int) (SymbolizerContent.DEFAULT_HALO_WIDTH);
267: }
268: haloWidthScale.setSelection(width);
269:
270: double opacity = SLDs.textHaloOpacity(symbolizer);
271: if (Double.isNaN(opacity)) {
272: opacity = SymbolizerContent.DEFAULT_HALO_OPACITY;
273: }
274: haloOpacityScale
275: .setSelection((int) (opacity * opacityMaxValue));
276: }
277:
278: /*
279: * (non-Javadoc)
280: *
281: * @see net.refractions.udig.style.sld.SLDEditorPart#reset()
282: */
283: public void reset() {
284: setStylingElements((TextSymbolizer) getContent());
285: }
286:
287: /*
288: * (non-Javadoc)
289: *
290: * @see net.refractions.udig.style.IStyleConfigurator#apply()
291: */
292: private void apply(int mask) {
293: TextSymbolizer textSymbolizer = (TextSymbolizer) getContent();
294: if ((mask & LABEL) != 0) {
295: applyLabel(textSymbolizer, getStyleBuilder());
296: }
297: if ((mask & FONTCOLOR) != 0) {
298: applyLabel(textSymbolizer, getStyleBuilder());
299: }
300: }
301:
302: private void applyLabel(TextSymbolizer textSymbolizer,
303: StyleBuilder styleBuilder) {
304:
305: Expression currLabel = null;
306: if (labelCombo.getSelectionIndex() != -1) {
307: String selectedLabel = labelCombo.getItem(labelCombo
308: .getSelectionIndex());
309:
310: RGB xfontColor = labelFont.getColorValue();
311: textSymbolizer
312: .setFill(styleBuilder.createFill(new Color(
313: xfontColor.red, xfontColor.green,
314: xfontColor.blue)));
315:
316: FontData[] fd = labelFont.getFontList();
317: String fontName = fd[0].getName();
318: boolean fontBold = (fd[0].getStyle() == SWT.BOLD);
319: boolean fontItalic = (fd[0].getStyle() == SWT.ITALIC);
320: double fontSize = fd[0].getHeight();
321: org.geotools.styling.Font[] font = new org.geotools.styling.Font[1];
322: font[0] = styleBuilder.createFont(fontName, fontItalic,
323: fontBold, fontSize);
324: textSymbolizer.setFonts(font);
325:
326: if (labelHaloEnabled.getSelection()) {
327: RGB haloColor = labelHaloColorEditor.getColorValue();
328: if (textSymbolizer.getHalo() != null) {
329: if (textSymbolizer.getHalo().getFill() != null) {
330: textSymbolizer.getHalo().getFill().setColor(
331: styleBuilder.colorExpression(new Color(
332: haloColor.red, haloColor.green,
333: haloColor.blue)));
334: } else {
335: textSymbolizer.getHalo().setFill(
336: styleBuilder.createFill(new Color(
337: haloColor.red, haloColor.green,
338: haloColor.blue)));
339: }
340: textSymbolizer.getHalo().setRadius(
341: styleBuilder
342: .literalExpression(haloWidthScale
343: .getSelection()));
344: textSymbolizer.getHalo().getFill().setOpacity(
345: styleBuilder
346: .literalExpression(haloOpacityScale
347: .getSelection()
348: / opacityMaxValueFloat));
349: } else {
350: HaloImpl halo = new HaloImpl();
351: textSymbolizer.setHalo(halo);
352: textSymbolizer.getHalo().setFill(
353: styleBuilder.createFill(new Color(
354: haloColor.red, haloColor.green,
355: haloColor.blue)));
356: textSymbolizer.getHalo().setRadius(
357: styleBuilder
358: .literalExpression(haloWidthScale
359: .getSelection()));
360: textSymbolizer.getHalo().getFill().setOpacity(
361: styleBuilder
362: .literalExpression(haloOpacityScale
363: .getSelection()
364: / opacityMaxValueFloat));
365: }
366: } else {
367: textSymbolizer.setHalo(null);
368: }
369:
370: currLabel = FilterFactoryFinder.createFilterFactory()
371: .createAttributeExpression(selectedLabel);
372: textSymbolizer.setLabel(currLabel);
373: }
374: }
375: }
|