2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpeclipse.preferences;
7 import org.eclipse.jface.resource.JFaceResources;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.DisposeEvent;
10 import org.eclipse.swt.events.DisposeListener;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.ColorDialog;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
26 * A "button" of a certain color determined by the color picker.
28 public class ColorEditor {
30 private Point fExtent;
34 private RGB fColorValue;
38 private Button fButton;
40 public ColorEditor(Composite parent) {
42 fButton = new Button(parent, SWT.PUSH);
43 fExtent = computeImageSize(parent);
44 fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
46 GC gc = new GC(fImage);
47 gc.setBackground(fButton.getBackground());
48 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
51 fButton.setImage(fImage);
52 fButton.addSelectionListener(new SelectionAdapter() {
53 public void widgetSelected(SelectionEvent event) {
54 ColorDialog colorDialog = new ColorDialog(fButton.getShell());
55 colorDialog.setRGB(fColorValue);
56 RGB newColor = colorDialog.open();
57 if (newColor != null) {
58 fColorValue = newColor;
64 fButton.addDisposeListener(new DisposeListener() {
65 public void widgetDisposed(DisposeEvent event) {
78 public RGB getColorValue() {
82 public void setColorValue(RGB rgb) {
87 public Button getButton() {
91 protected void updateColorImage() {
93 Display display = fButton.getDisplay();
95 GC gc = new GC(fImage);
96 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
97 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
102 fColor = new Color(display, fColorValue);
103 gc.setBackground(fColor);
104 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
107 fButton.setImage(fImage);
110 protected Point computeImageSize(Control window) {
111 GC gc = new GC(window);
112 Font f = JFaceResources.getFontRegistry().get(
113 JFaceResources.DEFAULT_FONT);
115 int height = gc.getFontMetrics().getHeight();
117 Point p = new Point(height * 3 - 6, height);