1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import org.eclipse.jface.resource.JFaceResources;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.RGB;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.ColorDialog;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Display;
32 * A "button" of a certain color determined by the color picker.
34 public class ColorEditor {
36 private Point fExtent;
40 private RGB fColorValue;
44 private Button fButton;
46 public ColorEditor(Composite parent) {
48 fButton = new Button(parent, SWT.PUSH);
49 fExtent = computeImageSize(parent);
50 fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
52 GC gc = new GC(fImage);
53 gc.setBackground(fButton.getBackground());
54 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
57 fButton.setImage(fImage);
58 fButton.addSelectionListener(new SelectionAdapter() {
59 public void widgetSelected(SelectionEvent event) {
60 ColorDialog colorDialog = new ColorDialog(fButton.getShell());
61 colorDialog.setRGB(fColorValue);
62 RGB newColor = colorDialog.open();
63 if (newColor != null) {
64 fColorValue = newColor;
70 fButton.addDisposeListener(new DisposeListener() {
71 public void widgetDisposed(DisposeEvent event) {
84 public RGB getColorValue() {
88 public void setColorValue(RGB rgb) {
93 public Button getButton() {
97 protected void updateColorImage() {
99 Display display = fButton.getDisplay();
101 GC gc = new GC(fImage);
102 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
103 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
108 fColor = new Color(display, fColorValue);
109 gc.setBackground(fColor);
110 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
113 fButton.setImage(fImage);
116 protected Point computeImageSize(Control window) {
117 GC gc = new GC(window);
118 Font f = JFaceResources.getFontRegistry().get(
119 JFaceResources.DEFAULT_FONT);
121 int height = gc.getFontMetrics().getHeight();
123 Point p = new Point(height * 3 - 6, height);