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;
38 private RGB fColorValue;
40 private Button fButton;
42 public ColorEditor(Composite parent) {
44 fButton= new Button(parent, SWT.PUSH);
45 fExtent= computeImageSize(parent);
46 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
48 GC gc= new GC(fImage);
49 gc.setBackground(fButton.getBackground());
50 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
53 fButton.setImage(fImage);
54 fButton.addSelectionListener(new SelectionAdapter() {
55 public void widgetSelected(SelectionEvent event) {
56 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
57 colorDialog.setRGB(fColorValue);
58 RGB newColor = colorDialog.open();
59 if (newColor != null) {
60 fColorValue= newColor;
66 fButton.addDisposeListener(new DisposeListener() {
67 public void widgetDisposed(DisposeEvent event) {
80 public RGB getColorValue() {
84 public void setColorValue(RGB rgb) {
89 public Button getButton() {
93 protected void updateColorImage() {
95 Display display= fButton.getDisplay();
97 GC gc= new GC(fImage);
98 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
99 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
104 fColor= new Color(display, fColorValue);
105 gc.setBackground(fColor);
106 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
109 fButton.setImage(fImage);
112 protected Point computeImageSize(Control window) {
113 GC gc= new GC(window);
114 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
116 int height= gc.getFontMetrics().getHeight();
118 Point p= new Point(height * 3 - 6, height);