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 org.eclipse.jface.resource.JFaceResources;
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.DisposeEvent;
014: import org.eclipse.swt.events.DisposeListener;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.events.SelectionListener;
018: import org.eclipse.swt.graphics.Color;
019: import org.eclipse.swt.graphics.Font;
020: import org.eclipse.swt.graphics.GC;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.swt.graphics.Point;
023: import org.eclipse.swt.graphics.RGB;
024: import org.eclipse.swt.widgets.Button;
025: import org.eclipse.swt.widgets.ColorDialog;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Display;
029:
030: /**
031: * A "button" of a certain color determined by the color picker.
032: */
033: public class StolenColorEditor {
034:
035: private Point fExtent;
036: private Image fImage;
037: private RGB fColorValue;
038: private Color fColor;
039: private Button fButton;
040: private SelectionListener listener;
041:
042: public StolenColorEditor(Composite parent) {
043: this (parent, null);
044: }
045:
046: public StolenColorEditor(Composite parent,
047: SelectionListener parentListener) {
048: this .listener = parentListener;
049: fButton = new Button(parent, SWT.PUSH);
050: fExtent = computeImageSize(parent);
051: fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
052:
053: GC gc = new GC(fImage);
054: gc.setBackground(fButton.getBackground());
055: gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
056: gc.dispose();
057:
058: fButton.setImage(fImage);
059: fButton.addSelectionListener(new SelectionAdapter() {
060: public void widgetSelected(SelectionEvent event) {
061: ColorDialog colorDialog = new ColorDialog(fButton
062: .getShell());
063: colorDialog.setRGB(fColorValue);
064: RGB newColor = colorDialog.open();
065: if (newColor != null) {
066: fColorValue = newColor;
067: updateColorImage();
068: }
069: notifyParent(event);
070: }
071: });
072:
073: fButton.addDisposeListener(new DisposeListener() {
074: public void widgetDisposed(DisposeEvent event) {
075: if (fImage != null) {
076: fImage.dispose();
077: fImage = null;
078: }
079: if (fColor != null) {
080: fColor.dispose();
081: fColor = null;
082: }
083: }
084: });
085: }
086:
087: public void setListener(SelectionListener newListener) {
088: listener = newListener;
089: }
090:
091: private void notifyParent(SelectionEvent event) {
092: if (listener != null)
093: listener.widgetSelected(event);
094: }
095:
096: public java.awt.Color getColor() {
097: RGB rgb = getColorValue();
098: return new java.awt.Color(rgb.red, rgb.green, rgb.blue);
099: }
100:
101: public void setColor(java.awt.Color color) {
102: if (color == null) {
103: setColorValue(null);
104: } else {
105: RGB rgb = new RGB(color.getRed(), color.getGreen(), color
106: .getBlue());
107: setColorValue(rgb);
108: }
109: }
110:
111: public void setEnabled(boolean isEnabled) {
112: getButton().setEnabled(isEnabled);
113: }
114:
115: public RGB getColorValue() {
116: return fColorValue;
117: }
118:
119: public void setColorValue(RGB rgb) {
120: if (rgb == null) {
121: rgb = new RGB(0, 0, 0);
122: }
123: fColorValue = rgb;
124: updateColorImage();
125: }
126:
127: public Button getButton() {
128: return fButton;
129: }
130:
131: protected void updateColorImage() {
132:
133: Display display = fButton.getDisplay();
134:
135: GC gc = new GC(fImage);
136: gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
137: gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
138:
139: if (fColor != null)
140: fColor.dispose();
141:
142: fColor = new Color(display, fColorValue);
143: gc.setBackground(fColor);
144: gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
145: gc.dispose();
146:
147: fButton.setImage(fImage);
148: }
149:
150: protected Point computeImageSize(Control window) {
151: GC gc = new GC(window);
152: Font f = JFaceResources.getFontRegistry().get(
153: JFaceResources.DEFAULT_FONT);
154: gc.setFont(f);
155: int height = gc.getFontMetrics().getHeight();
156: gc.dispose();
157: Point p = new Point(height * 3 - 6, height);
158: return p;
159: }
160: }
|