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;
32 private RGB fColorValue;
34 private Button fButton;
36 public ColorEditor(Composite parent) {
38 fButton= new Button(parent, SWT.PUSH);
39 fExtent= computeImageSize(parent);
40 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
42 GC gc= new GC(fImage);
43 gc.setBackground(fButton.getBackground());
44 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
47 fButton.setImage(fImage);
48 fButton.addSelectionListener(new SelectionAdapter() {
49 public void widgetSelected(SelectionEvent event) {
50 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
51 colorDialog.setRGB(fColorValue);
52 RGB newColor = colorDialog.open();
53 if (newColor != null) {
54 fColorValue= newColor;
60 fButton.addDisposeListener(new DisposeListener() {
61 public void widgetDisposed(DisposeEvent event) {
74 public RGB getColorValue() {
78 public void setColorValue(RGB rgb) {
83 public Button getButton() {
87 protected void updateColorImage() {
89 Display display= fButton.getDisplay();
91 GC gc= new GC(fImage);
92 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
93 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
98 fColor= new Color(display, fColorValue);
99 gc.setBackground(fColor);
100 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
103 fButton.setImage(fImage);
106 protected Point computeImageSize(Control window) {
107 GC gc= new GC(window);
108 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
110 int height= gc.getFontMetrics().getHeight();
112 Point p= new Point(height * 3 - 6, height);