2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpeclipse.preferences;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.DisposeEvent;
9 import org.eclipse.swt.events.DisposeListener;
10 import org.eclipse.swt.events.SelectionAdapter;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.graphics.Font;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.ColorDialog;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.jface.resource.JFaceResources;
27 * A "button" of a certain color determined by the color picker.
29 public class ColorEditor {
31 private Point fExtent;
33 private RGB fColorValue;
35 private Button fButton;
37 public ColorEditor(Composite parent) {
39 fButton= new Button(parent, SWT.PUSH);
40 fExtent= computeImageSize(parent);
41 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
43 GC gc= new GC(fImage);
44 gc.setBackground(fButton.getBackground());
45 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
48 fButton.setImage(fImage);
49 fButton.addSelectionListener(new SelectionAdapter() {
50 public void widgetSelected(SelectionEvent event) {
51 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
52 colorDialog.setRGB(fColorValue);
53 RGB newColor = colorDialog.open();
54 if (newColor != null) {
55 fColorValue= newColor;
61 fButton.addDisposeListener(new DisposeListener() {
62 public void widgetDisposed(DisposeEvent event) {
75 public RGB getColorValue() {
79 public void setColorValue(RGB rgb) {
84 public Button getButton() {
88 protected void updateColorImage() {
90 Display display= fButton.getDisplay();
92 GC gc= new GC(fImage);
93 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
94 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
99 fColor= new Color(display, fColorValue);
100 gc.setBackground(fColor);
101 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
104 fButton.setImage(fImage);
107 protected Point computeImageSize(Control window) {
108 GC gc= new GC(window);
109 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
111 int height= gc.getFontMetrics().getHeight();
113 Point p= new Point(height * 3 - 6, height);