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.swt.SWT;
012: import org.eclipse.swt.events.DisposeEvent;
013: import org.eclipse.swt.events.DisposeListener;
014: import org.eclipse.swt.events.SelectionAdapter;
015: import org.eclipse.swt.events.SelectionEvent;
016: import org.eclipse.swt.events.SelectionListener;
017: import org.eclipse.swt.graphics.Color;
018: import org.eclipse.swt.graphics.Font;
019: import org.eclipse.swt.graphics.FontData;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.graphics.RGB;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.FontDialog;
025: import org.eclipse.swt.widgets.Shell;
026:
027: /**
028: * <p>
029: * A "button" with an icon/text that pops up the font window.
030: * </p>
031: *
032: * @author aalam
033: * @since 0.6.0
034: */
035: public class FontEditor {
036:
037: private Image fImage = null;
038: private RGB fColorValue;
039: private Color fColor[] = null;
040: private Font fFont[] = null;
041: private FontData[] fFontList = null;
042: private Button fButton;
043: private SelectionListener parentListener;
044: final FontDialog labelFontDialog;
045:
046: public FontEditor(Composite parent) {
047: this (parent, null);
048: }
049:
050: public FontEditor(Composite parent, SelectionListener parentListener) {
051: this .parentListener = parentListener;
052: fButton = new Button(parent, SWT.PUSH);
053: // fExtent= computeImageSize(parent);
054: // fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
055: // fImage = new Image(parent.getDisplay(), "elcl16/up_co.gif"); //$NON-NLS-1$
056: //
057: // GC gc= new GC(fImage);
058: // gc.setBackground(fButton.getBackground());
059: // gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
060: // gc.dispose();
061:
062: fFont = new Font[1];
063: fColor = new Color[1];
064:
065: fButton.setText("Set Font"); //$NON-NLS-1$
066: labelFontDialog = new FontDialog(new Shell());
067: labelFontDialog.setText("Choose a Font"); //$NON-NLS-1$
068:
069: fButton.setImage(fImage);
070: fButton.addSelectionListener(new SelectionAdapter() {
071: public void widgetSelected(SelectionEvent event) {
072: FontDialog fontDialog = new FontDialog(fButton
073: .getShell());
074: fontDialog.setRGB(fColorValue);
075: fontDialog.setFontList(fFontList);
076:
077: if (fontDialog.open() == null)
078: return;
079: if (fFont[0] != null)
080: fFont[0].dispose();
081: FontData[] list = fontDialog.getFontList();
082: if (list != null) {
083: fFont[0] = new Font(fButton.getDisplay(), list);
084: // set the text font here...
085: fFontList = list;
086: }
087: RGB rgb = fontDialog.getRGB();
088: if (rgb != null) {
089: if (fColor[0] != null)
090: fColor[0].dispose();
091: fColor[0] = new Color(fButton.getDisplay(), rgb);
092: // set the text foreground color here...
093: fColorValue = rgb;
094: updateColorImage();
095: }
096: notifyParent(event);
097: }
098: });
099:
100: fButton.addDisposeListener(new DisposeListener() {
101: public void widgetDisposed(DisposeEvent event) {
102: if (fImage != null) {
103: fImage.dispose();
104: fImage = null;
105: }
106: if (fColor != null) {
107: if (fColor[0] != null)
108: fColor[0].dispose();
109: fColor = null;
110: }
111: if (fFont != null) {
112: if (fFont[0] != null)
113: fFont[0].dispose();
114: fFont = null;
115: }
116: }
117: });
118: }
119:
120: public void setListener(SelectionListener parentListener) {
121: this .parentListener = parentListener;
122: }
123:
124: public void clearListener() {
125: this .parentListener = null;
126: }
127:
128: private void notifyParent(SelectionEvent event) {
129: if (parentListener != null)
130: parentListener.widgetSelected(event);
131: }
132:
133: public Color getColor() {
134: return fColor[0];
135: }
136:
137: public java.awt.Color getAWTColor() {
138: Color tmpColor = getColor();
139: if (tmpColor == null)
140: return null;
141: return new java.awt.Color(tmpColor.getRed(), tmpColor
142: .getGreen(), tmpColor.getBlue());
143: }
144:
145: /**
146: * Returns the currently selected font object
147: *
148: * @return Font
149: */
150: public Font getFont() {
151: return fFont[0];
152: }
153:
154: public FontData[] getFontList() {
155: return fFontList;
156: }
157:
158: public void setFontList(FontData[] list) {
159: if (list != null) {
160: fFontList = list;
161: fFont[0] = new Font(fButton.getDisplay(), list);
162: labelFontDialog.setFontList(list);
163: }
164: }
165:
166: public RGB getColorValue() {
167: return fColorValue;
168: }
169:
170: public void setColorValue(RGB rgb) {
171: fColorValue = rgb;
172: fColor[0] = new Color(fButton.getDisplay(), rgb);
173: labelFontDialog.setRGB(rgb);
174: updateColorImage();
175: }
176:
177: public void setColorValue(java.awt.Color colour) {
178: if (colour != null) {
179: setColorValue(new RGB(colour.getRed(), colour.getGreen(),
180: colour.getBlue()));
181: }
182: }
183:
184: public Button getButton() {
185: return fButton;
186: }
187:
188: private void updateColorImage() {
189: /*
190: * Display display= fButton.getDisplay(); GC gc= new GC(fImage);
191: * gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); gc.drawRectangle(0, 2,
192: * fExtent.x - 1, fExtent.y - 4); if (fColor != null) fColor.dispose(); fColor= new
193: * Color(display, fColorValue); gc.setBackground(fColor); gc.fillRectangle(1, 3, fExtent.x -
194: * 2, fExtent.y - 5); gc.dispose(); fButton.setImage(fImage);
195: */
196: }
197:
198: public void setEnabled(boolean enabled) {
199: fButton.setEnabled(enabled);
200: }
201: }
|