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.swt.SWT;
14 import org.eclipse.swt.events.DisposeEvent;
15 import org.eclipse.swt.events.DisposeListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.ColorDialog;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.jface.resource.JFaceResources;
33 * A "button" of a certain color determined by the color picker.
35 public class ColorEditor {
37 private Point fExtent;
39 private RGB fColorValue;
41 private Button fButton;
43 public ColorEditor(Composite parent) {
45 fButton= new Button(parent, SWT.PUSH);
46 fExtent= computeImageSize(parent);
47 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
49 GC gc= new GC(fImage);
50 gc.setBackground(fButton.getBackground());
51 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
54 fButton.setImage(fImage);
55 fButton.addSelectionListener(new SelectionAdapter() {
56 public void widgetSelected(SelectionEvent event) {
57 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
58 colorDialog.setRGB(fColorValue);
59 RGB newColor = colorDialog.open();
60 if (newColor != null) {
61 fColorValue= newColor;
67 fButton.addDisposeListener(new DisposeListener() {
68 public void widgetDisposed(DisposeEvent event) {
81 public RGB getColorValue() {
85 public void setColorValue(RGB rgb) {
90 public Button getButton() {
94 protected void updateColorImage() {
96 Display display= fButton.getDisplay();
98 GC gc= new GC(fImage);
99 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
100 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
105 fColor= new Color(display, fColorValue);
106 gc.setBackground(fColor);
107 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
110 fButton.setImage(fImage);
113 protected Point computeImageSize(Control window) {
114 GC gc= new GC(window);
115 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
117 int height= gc.getFontMetrics().getHeight();
119 Point p= new Point(height * 3 - 6, height);