001: package net.refractions.udig.style.sld.internal;
002:
003: import java.awt.Color;
004:
005: import net.refractions.udig.style.sld.SLDEditorPart;
006: import net.refractions.udig.ui.graphics.SLDs;
007:
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.SelectionEvent;
010: import org.eclipse.swt.events.SelectionListener;
011: import org.eclipse.swt.graphics.RGB;
012: import org.eclipse.swt.layout.RowData;
013: import org.eclipse.swt.layout.RowLayout;
014: import org.eclipse.swt.widgets.Button;
015: import org.eclipse.swt.widgets.Combo;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.swt.widgets.Label;
019: import org.eclipse.swt.widgets.Spinner;
020: import org.geotools.styling.LineSymbolizer;
021: import org.geotools.styling.Stroke;
022: import org.geotools.styling.StyleBuilder;
023:
024: /**
025: * Edit a line symbolizer.
026: *
027: * @author aalam
028: * @since 0.6.0
029: */
030: public class SLDLineEditorPart extends SLDEditorPart implements
031: SelectionListener {
032:
033: private StolenColorEditor lineColourEditor;
034: Spinner lineWidth;
035: Spinner lineOpacity;
036: Combo linejoinCombo;
037: Combo linecapCombo;
038: int opacityMaxValue = 100;
039: double opacityMaxValueFloat = 100.0;
040:
041: /*
042: * (non-Javadoc)
043: *
044: * @see net.refractions.udig.style.sld.SLDEditorPart#getContentType()
045: */
046: public Class getContentType() {
047: return LineSymbolizer.class;
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see net.refractions.udig.style.sld.SLDEditorPart#init()
054: */
055: public void init() {
056: //Nothing to do
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see net.refractions.udig.style.sld.SLDEditorPart#reset()
063: */
064: public void reset() {
065: // initialize the ui
066: setStylingElements((LineSymbolizer) getContent());
067:
068: }
069:
070: private void setStylingElements(LineSymbolizer symbolizer) {
071:
072: Color colour = null;
073: int width = SLDs.NOTFOUND;
074:
075: Stroke stroke = symbolizer.getStroke();
076: if (stroke != null) {
077: colour = SLDs.lineColor(symbolizer);
078: width = SLDs.lineWidth(symbolizer);
079:
080: }
081: if (colour == null) {
082: colour = SymbolizerContent.DEFAULT_LINE_COLOR;
083: }
084: lineColourEditor.setColorValue(new RGB(colour.getRed(), colour
085: .getGreen(), colour.getBlue()));
086: if (width == SLDs.NOTFOUND) {
087: width = SymbolizerContent.DEFAULT_LINE_WIDTH;
088: }
089: lineWidth.setSelection(width);
090: //lineWidthText.setText(Integer.toString(width));
091: //lineWidthText.pack(true);
092:
093: double opacity = SLDs.lineOpacity(symbolizer);
094: if (Double.isNaN(opacity)) {
095: opacity = SymbolizerContent.DEFAULT_LINE_OPACITY;
096: }
097: lineOpacity.setSelection((int) (opacity * opacityMaxValue));
098: //lineOpacityText.setText(Integer.toString((int)(opacity*opacityMaxValue)) + "%"); //$NON-NLS-1$
099: //lineOpacityText.pack(true);
100:
101: //TODO: fix all these hard coded defaults etc...
102: String linejoin = SLDs.lineLinejoin(symbolizer);
103: String[] options = new String[] { "mitre", "round", "bevel" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
104: linejoinCombo.setItems(options);
105: if (linejoin == null) {
106: linejoin = SymbolizerContent.DEFAULT_LINE_LINEJOIN;
107: }
108: int index = linejoinCombo.indexOf(linejoin);
109: if (index == -1) {
110: linejoinCombo.add(linejoin);
111: linejoinCombo.select(0);
112: } else {
113: linejoinCombo.select(index);
114: }
115:
116: // TODO: fix all these hard coded defaults etc...
117: String linecap = SLDs.lineLinecap(symbolizer);
118: String[] lcoptions = new String[] { "butt", "round", "square" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
119: linecapCombo.setItems(lcoptions);
120: if (linecap == null) {
121: linecap = SymbolizerContent.DEFAULT_LINE_LINECAP;
122: }
123: int lcindex = linecapCombo.indexOf(linecap);
124: if (lcindex == -1) {
125: linecapCombo.add(linecap);
126: linecapCombo.select(0);
127: } else {
128: linecapCombo.select(lcindex);
129: }
130:
131: // TODO: This functionality to be added later with a better UI...goes with other task
132: // float[] lineDash = SLDs.lineDash(symbolizer);
133: // if( lineDash.length == 2 ) {
134: // lineDashText.setText(Float.toString(lineDash[0]));
135: // lineSpaceText.setText(Float.toString(lineDash[1]));
136: // } else {
137: // lineDashText.setText(""); //$NON-NLS-1$
138: // lineSpaceText.setText(""); //$NON-NLS-1$
139: // }
140: }
141:
142: /**
143: * Construct a subpart labeled with the provided tag.
144: * <p>
145: * Creates a composite with a grid layout of the specifed columns,
146: * and a label with text from tag.
147: * </p>
148: * @param parent
149: * @param tag
150: * @param numColumns number of columns (usually 2_
151: * @return Composite with one label
152: */
153: private Composite subpart(Composite parent, String tag, int width) {
154: Composite subpart = new Composite(parent, SWT.NONE);
155: RowLayout across = new RowLayout();
156: across.type = SWT.HORIZONTAL;
157: across.wrap = true;
158: across.pack = true;
159: across.fill = true;
160: across.marginBottom = 1;
161: across.marginRight = 2;
162:
163: subpart.setLayout(across);
164:
165: Label label = new Label(subpart, SWT.NONE);
166: label.setText(tag);
167: label.setAlignment(SWT.RIGHT);
168: RowData data = new RowData();
169: data.width = 40;
170: data.height = 10;
171: label.setLayoutData(data);
172:
173: return subpart;
174: }
175:
176: private void strokePart(Composite parent) {
177: Composite stroke = subpart(parent,
178: Messages.SLDLineEditorPart_label_stroke, 2);
179:
180: linejoinCombo = new Combo(stroke, SWT.READ_ONLY);
181: linejoinCombo.addSelectionListener(this );
182:
183: linecapCombo = new Combo(stroke, SWT.READ_ONLY);
184: linecapCombo.addSelectionListener(this );
185: }
186:
187: private void borderPart(Composite parent) {
188: Composite border = subpart(parent,
189: Messages.SLDLineEditorPart_label_border, 4);
190:
191: Button borderEnabled = new Button(border, SWT.CHECK);
192: borderEnabled.setEnabled(false);
193: borderEnabled.setSelection(true);
194:
195: lineColourEditor = new StolenColorEditor(border, this );
196:
197: lineWidth = new Spinner(border, SWT.HORIZONTAL);
198: lineWidth.setMinimum(1);
199: lineWidth.setMaximum(30);
200: lineWidth.setPageIncrement(5);
201: lineWidth.addSelectionListener(this );
202: lineWidth
203: .setToolTipText(Messages.SLDLineEditorPart_border_width_tooltip);
204:
205: lineOpacity = new Spinner(border, SWT.HORIZONTAL);
206: lineOpacity.setMinimum(0);
207: lineOpacity.setMaximum(opacityMaxValue);
208: lineOpacity.setPageIncrement(10);
209: lineOpacity.addSelectionListener(this );
210: lineOpacity
211: .setToolTipText(Messages.SLDLineEditorPart_border_opacity_tooltip);
212: }
213:
214: protected Control createPartControl(Composite parent) {
215: RowLayout layout = new RowLayout();
216: layout.pack = false;
217: layout.wrap = true;
218: layout.type = SWT.HORIZONTAL;
219: layout.fill = true;
220: layout.marginLeft = 0;
221: layout.marginRight = 0;
222: layout.marginTop = 0;
223: layout.marginBottom = 0;
224: layout.spacing = 0;
225: parent.setLayout(layout);
226:
227: borderPart(parent);
228: strokePart(parent);
229:
230: return parent;
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
237: */
238: public void widgetDefaultSelected(SelectionEvent e) {
239: // Meh! Meh I say!
240: }
241:
242: /*
243: * (non-Javadoc)
244: *
245: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
246: */
247: public void widgetSelected(SelectionEvent e) {
248: apply();
249: }
250:
251: /**
252: * Reflects the changes from the UI to the symbolizer
253: *
254: */
255: public void apply() {
256: LineSymbolizer symbolizer = (LineSymbolizer) getContent();
257: StyleBuilder styleBuilder = getStyleBuilder();
258:
259: Stroke stroke = symbolizer.getStroke();
260: if (stroke == null) {
261: stroke = getStyleBuilder().createStroke();
262: symbolizer.setStroke(stroke);
263: }
264:
265: Color c = lineColourEditor.getColor();
266:
267: stroke.setWidth(styleBuilder.literalExpression(lineWidth
268: .getSelection()));
269: stroke.setColor(styleBuilder.colorExpression(c));
270: stroke.setOpacity(styleBuilder.literalExpression(lineOpacity
271: .getSelection()
272: / opacityMaxValueFloat));
273: stroke.setLineJoin(styleBuilder.literalExpression(linejoinCombo
274: .getText()));
275: stroke.setLineCap(styleBuilder.literalExpression(linecapCombo
276: .getText()));
277:
278: // TODO: This functionality to be added later with a better UI...goes with other task
279: // if (!lineDashText.getText().equalsIgnoreCase("")
280: // && !lineSpaceText.getText().equalsIgnoreCase("")) {
281: // float dash = Float.parseFloat(lineDashText.getText());
282: // float space = Float.parseFloat(lineSpaceText.getText());
283: // if (dash > 0) {
284: // stroke.setDashArray(new float[]{dash, space});
285: // } else {
286: // stroke.setDashArray(new float[]{10.0f, 0.0f});
287: // }
288: // } else {
289: // stroke.setDashArray(new float[]{10.0f, 0.0f});
290: // }
291: }
292: }
|