001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.mapgraphic.grid;
016:
017: import java.awt.Color;
018:
019: import net.refractions.udig.mapgraphic.grid.GridStyle.Type;
020: import net.refractions.udig.mapgraphic.internal.Messages;
021: import net.refractions.udig.project.internal.Layer;
022: import net.refractions.udig.style.IStyleConfigurator;
023: import net.refractions.udig.ui.graphics.ViewportGraphics;
024:
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.graphics.GC;
027: import org.eclipse.swt.graphics.Image;
028: import org.eclipse.swt.graphics.Rectangle;
029: import org.eclipse.swt.layout.FillLayout;
030: import org.eclipse.swt.layout.GridData;
031: import org.eclipse.swt.layout.GridLayout;
032: import org.eclipse.swt.widgets.Button;
033: import org.eclipse.swt.widgets.Combo;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.Display;
037: import org.eclipse.swt.widgets.Event;
038: import org.eclipse.swt.widgets.Label;
039: import org.eclipse.swt.widgets.Listener;
040: import org.eclipse.swt.widgets.Spinner;
041:
042: /**
043: * Edit GridStyle objects
044: *
045: * @author Jesse
046: * @since 1.1.0
047: */
048: public class GridStyleConfigurator extends IStyleConfigurator implements
049: Listener {
050:
051: private static final String LINE_DASH = "dash"; //$NON-NLS-1$
052: private static final String LINE_DASHDOT = "dash-dot"; //$NON-NLS-1$
053: private static final String LINE_DASHDOTDOT = "dash-dot-dot"; //$NON-NLS-1$
054: private static final String LINE_DOT = "dot"; //$NON-NLS-1$
055: private static final String LINE_SOLID = "solid"; //$NON-NLS-1$
056: private static final String[] LINE_STYLES = new String[] {
057: LINE_SOLID, LINE_DASH, LINE_DOT, LINE_DASHDOT,
058: LINE_DASHDOTDOT };
059:
060: SpacerController xSpacer, ySpacer;
061: Combo lineStyle;
062: Spinner lineWidth;
063: Button color;
064: Label xlabel, ylabel, message, colorLabel, colorDisplay,
065: lineStyleLabel, lineWidthLabel;
066: private GridStyle style;
067: private UnitListener xUnitListener, yUnitListener;
068: private ColorListener colorListener;
069: private Composite comp;
070:
071: @Override
072: public boolean canStyle(Layer aLayer) {
073: return aLayer.hasResource(GridMapGraphic.class)
074: && aLayer.getStyleBlackboard().get(GridStyle.ID) != null;
075: }
076:
077: @Override
078: public void createControl(Composite parent) {
079: parent.setLayout(new FillLayout());
080:
081: Composite comp = createWidgets(parent);
082: addListeners();
083: layoutWidgets(comp);
084: }
085:
086: private void layoutWidgets(Composite comp) {
087: comp.setLayout(new GridLayout(3, false));
088:
089: GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true,
090: false, 3, 1);
091:
092: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
093: 1);
094: xlabel.setLayoutData(layoutData);
095: layoutData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
096: xSpacer.getSpinner().setLayoutData(layoutData);
097: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
098: 1);
099: xSpacer.getUnit().setLayoutData(layoutData);
100:
101: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
102: 1);
103: ylabel.setLayoutData(layoutData);
104: layoutData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
105: ySpacer.getSpinner().setLayoutData(layoutData);
106: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
107: 1);
108: ySpacer.getUnit().setLayoutData(layoutData);
109:
110: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 2,
111: 1);
112: lineStyleLabel.setLayoutData(layoutData);
113: layoutData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
114: lineStyle.setLayoutData(layoutData);
115:
116: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 2,
117: 1);
118: lineWidthLabel.setLayoutData(layoutData);
119: layoutData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
120: lineWidth.setLayoutData(layoutData);
121:
122: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
123: 1);
124: colorLabel.setLayoutData(layoutData);
125: layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
126: 1);
127: colorDisplay.setLayoutData(layoutData);
128: layoutData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
129: color.setLayoutData(layoutData);
130:
131: layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
132: message.setLayoutData(layoutData);
133:
134: }
135:
136: private void addListeners() {
137:
138: if (xUnitListener == null) {
139: xUnitListener = new UnitListener(ySpacer, xSpacer, message);
140: yUnitListener = new UnitListener(xSpacer, ySpacer, message);
141: }
142: xSpacer.addListeners(xUnitListener, this );
143: ySpacer.addListeners(yUnitListener, this );
144:
145: colorListener = new ColorListener(this );
146: color.addSelectionListener(colorListener);
147:
148: lineStyle.addListener(SWT.Modify, this );
149: lineStyle.addListener(SWT.KeyUp, this );
150: lineWidth.addListener(SWT.Modify, this );
151: lineWidth.addListener(SWT.KeyUp, this );
152: }
153:
154: private void removeListeners() {
155: xSpacer.removeListeners(xUnitListener, this );
156: ySpacer.removeListeners(yUnitListener, this );
157:
158: color.removeSelectionListener(colorListener);
159:
160: lineStyle.removeListener(SWT.Modify, this );
161: lineWidth.removeListener(SWT.Modify, this );
162: lineStyle.removeListener(SWT.KeyUp, this );
163: lineWidth.removeListener(SWT.KeyUp, this );
164: }
165:
166: private Composite createWidgets(Composite parent) {
167: comp = new Composite(parent, SWT.NONE);
168:
169: xlabel = new Label(comp, SWT.NONE);
170: xlabel.setText(Messages.GridStyleConfigurator_HSpacing);
171: Spinner x = new Spinner(comp, SWT.NONE);
172: Combo xUnit = new Combo(comp, SWT.READ_ONLY);
173:
174: xSpacer = new SpacerController(x, xUnit);
175:
176: ylabel = new Label(comp, SWT.NONE);
177: ylabel.setText(Messages.GridStyleConfigurator_VSpacing);
178: Spinner y = new Spinner(comp, SWT.NONE);
179: Combo yUnit = new Combo(comp, SWT.READ_ONLY);
180:
181: ySpacer = new SpacerController(y, yUnit);
182:
183: colorLabel = new Label(comp, SWT.NONE);
184: colorLabel.setText(Messages.GridStyleConfigurator_LineColor);
185:
186: colorDisplay = new Label(comp, SWT.NONE);
187: colorDisplay.addListener(SWT.Resize, new Listener() {
188:
189: public void handleEvent(Event event) {
190: updateColorButton();
191: }
192:
193: });
194: color = new Button(comp, SWT.PUSH);
195: color.setText(Messages.GridStyleConfigurator_ChangeColor);
196:
197: lineStyleLabel = new Label(comp, SWT.NONE);
198: lineStyleLabel
199: .setText(Messages.GridStyleConfigurator_LineStyle);
200: lineStyle = new Combo(comp, SWT.NONE);
201: lineStyle.setItems(LINE_STYLES);
202: lineStyle.select(0);
203:
204: lineWidthLabel = new Label(comp, SWT.NONE);
205: lineWidthLabel
206: .setText(Messages.GridStyleConfigurator_LineWidth);
207: lineWidth = new Spinner(comp, SWT.NONE);
208: lineWidth.setIncrement(1);
209: lineWidth.setDigits(0);
210: lineWidth.setMinimum(1);
211:
212: message = new Label(comp, SWT.WRAP);
213:
214: return comp;
215: }
216:
217: @Override
218: protected void refresh() {
219: getApplyAction().setEnabled(false);
220: removeListeners();
221: try {
222: GridStyle oldStyle = (GridStyle) getStyleBlackboard().get(
223: GridStyle.ID);
224: if (oldStyle == null) {
225: oldStyle = GridStyle.DEFAULT_STYLE;
226: }
227:
228: this .style = new GridStyle(oldStyle);
229:
230: message.setText(""); //$NON-NLS-1$
231: lineWidth.setSelection(style.getLineWidth());
232: setLineStyle(style);
233: Color color2 = style.getColor();
234: color.setData(color2);
235: updateColorButton();
236:
237: switch (style.getType()) {
238: case SCREEN:
239: xSpacer.setPixelSpacing(style.getGridSize()[0]);
240: ySpacer.setPixelSpacing(style.getGridSize()[1]);
241: break;
242: case WORLD:
243: xSpacer.setWorldSpacing(style.getGridSize()[0]);
244: ySpacer.setWorldSpacing(style.getGridSize()[1]);
245: break;
246:
247: default:
248: throw new RuntimeException(Messages.bind(
249: Messages.GridStyleConfigurator_0, style
250: .getType()));
251: }
252: xSpacer.getUnit()
253: .setData(selectedString(xSpacer.getUnit()));
254: ySpacer.getUnit()
255: .setData(selectedString(ySpacer.getUnit()));
256: } finally {
257: addListeners();
258: }
259: }
260:
261: private void setLineStyle(GridStyle style2) {
262: switch (style.getLineStyle()) {
263: case ViewportGraphics.LINE_DASH:
264: lineStyle.select(lineStyle.indexOf(LINE_DASH));
265: break;
266: case ViewportGraphics.LINE_DASHDOT:
267: lineStyle.select(lineStyle.indexOf(LINE_DASHDOT));
268: break;
269: case ViewportGraphics.LINE_DASHDOTDOT:
270: lineStyle.select(lineStyle.indexOf(LINE_DASHDOTDOT));
271: break;
272: case ViewportGraphics.LINE_DOT:
273: lineStyle.select(lineStyle.indexOf(LINE_DOT));
274: break;
275: case ViewportGraphics.LINE_SOLID:
276: lineStyle.select(lineStyle.indexOf(LINE_SOLID));
277: break;
278:
279: default:
280:
281: throw new RuntimeException(Messages.bind(
282: Messages.GridStyleConfigurator_1, style
283: .getLineStyle()));
284: }
285: }
286:
287: void updateColorButton() {
288: Image oldImage = colorDisplay.getImage();
289:
290: Rectangle bounds = colorDisplay.getBounds();
291: if (bounds.width == 0 || bounds.height == 0)
292: return;
293:
294: Display display = colorDisplay.getDisplay();
295: Image newImage = new Image(display, bounds.width, bounds.width);
296:
297: Color awtColor = (Color) color.getData();
298: int red = awtColor.getRed();
299: int green = awtColor.getGreen();
300: int blue = awtColor.getBlue();
301: org.eclipse.swt.graphics.Color color2 = new org.eclipse.swt.graphics.Color(
302: display, red, green, blue);
303:
304: GC gc = new GC(newImage);
305: try {
306: gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
307: gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
308: int alpha = awtColor.getAlpha();
309: gc.setAlpha(alpha);
310: gc.setBackground(color2);
311: gc.fillRectangle(1, 1, bounds.width - 2, bounds.height - 2);
312: } finally {
313: gc.dispose();
314: color2.dispose();
315: }
316:
317: colorDisplay.setImage(newImage);
318: if (oldImage != null)
319: oldImage.dispose();
320: colorLabel.redraw();
321:
322: }
323:
324: @Override
325: public void preApply() {
326: if (xSpacer.getSpinner().isFocusControl()) {
327: kickWidget(xSpacer.getSpinner());
328: } else if (ySpacer.getSpinner().isFocusControl()) {
329: kickWidget(ySpacer.getSpinner());
330: } else if (lineWidth.isFocusControl()) {
331: kickWidget(lineWidth);
332: } else if (lineStyle.isFocusControl()) {
333: kickWidget(lineStyle);
334: }
335: }
336:
337: /**
338: * Stupid hack so that when apply is pressed the value in the spinners is set.
339: * Spinners don't have the displayed value until enter or the focus changes
340: * from the spinner. So I'm going to kick the spinner to get it to synchronize with
341: * the displayed value.
342: */
343: private void kickWidget(Control widget) {
344: comp.setFocus();
345: widget.setFocus();
346: while (widget.getDisplay().readAndDispatch())
347: ;
348: }
349:
350: public void handleEvent(Event e) {
351:
352: if (e.type == SWT.KeyUp && e.character == SWT.CR) {
353: makeActionDoStuff();
354: } else {
355: boolean changed = isChanged(e);
356: if (!changed)
357: return;
358: getApplyAction().setEnabled(true);
359: if (e.widget != xSpacer.getUnit()
360: && e.widget != ySpacer.getUnit()) {
361: message.setText(""); //$NON-NLS-1$
362: }
363: // parses all the values and updates the style on the Style blackboard
364:
365: style.setColor((Color) color.getData());
366: style.setLineStyle(parseLineStyle());
367: style.setLineWidth(lineWidth.getSelection());
368:
369: Type type;
370:
371: if (xSpacer.getUnit().getSelectionIndex() == 0) {
372: type = Type.SCREEN;
373: } else {
374: type = Type.WORLD;
375: }
376:
377: style.setType(type);
378:
379: style.setGridSize(xSpacer.getSpacing(), ySpacer
380: .getSpacing());
381: getStyleBlackboard().put(GridStyle.ID, style);
382: }
383:
384: }
385:
386: private boolean isChanged(Event e) {
387:
388: if (style.getLineStyle() != parseLineStyle()) {
389: return true;
390: }
391:
392: if (!style.getColor().equals(color.getData())) {
393: return true;
394: }
395:
396: if (style.getLineWidth() != lineWidth.getSelection()) {
397: return true;
398: }
399:
400: if (!xSpacer.getUnit().getData().equals(
401: selectedString(xSpacer.getUnit()))) {
402: return true;
403: }
404:
405: if (!ySpacer.getUnit().getData().equals(
406: selectedString(ySpacer.getUnit()))) {
407: return true;
408: }
409:
410: if (Math.abs(xSpacer.getSpacing() - style.getGridSize()[0]) > 0.0000000001) {
411: return true;
412: }
413:
414: if (Math.abs(ySpacer.getSpacing() - style.getGridSize()[1]) > 0.0000000001) {
415: return true;
416: }
417:
418: if (e.widget == lineWidth || e.widget == xSpacer.getSpinner()
419: || e.widget == ySpacer.getSpinner()) {
420: return true;
421: }
422:
423: return false;
424: }
425:
426: private int parseLineStyle() {
427: String selectedString = selectedString(lineStyle);
428: if (selectedString.equals(LINE_SOLID)) {
429: return ViewportGraphics.LINE_SOLID;
430: }
431: if (selectedString.equals(LINE_DASH)) {
432: return ViewportGraphics.LINE_DASH;
433: }
434: if (selectedString.equals(LINE_DOT)) {
435: return ViewportGraphics.LINE_DOT;
436: }
437: if (selectedString.equals(LINE_DASHDOT)) {
438: return ViewportGraphics.LINE_DASHDOT;
439: }
440: if (selectedString.equals(LINE_DASHDOTDOT)) {
441: return ViewportGraphics.LINE_DASHDOTDOT;
442: }
443: throw new IllegalArgumentException(Messages.bind(
444: Messages.GridStyleConfigurator_2, selectedString));
445: }
446:
447: static String selectedString(Combo item) {
448: int selectionIndex = item.getSelectionIndex();
449: return item.getItem(selectionIndex);
450: }
451:
452: }
|