2 * Copyright (c) 2002-2004 Widespace, OU 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://solareclipse.sourceforge.net/legal/cpl-v10.html
9 * Igor Malinin - initial contribution
11 * $Id: ColorEditor.java,v 1.1 2004-09-02 18:26:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.ui;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.ColorDialog;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Display;
35 * A "button" of a certain color determined by the color picker.
37 public class ColorEditor {
49 public ColorEditor(Composite parent) {
50 button = new Button(parent, SWT.PUSH);
51 extent = computeImageSize(parent);
52 image = new Image(parent.getDisplay(), extent.x, extent.y);
54 GC gc = new GC(image);
56 gc.setBackground(button.getBackground());
57 gc.fillRectangle(0, 0, extent.x, extent.y);
61 button.setImage(image);
63 button.addSelectionListener(new SelectionAdapter() {
65 public void widgetSelected(SelectionEvent event) {
66 ColorDialog colorDialog = new ColorDialog(button.getShell());
67 colorDialog.setRGB(rgb);
69 RGB newColor = colorDialog.open();
70 if (newColor != null) {
77 button.addDisposeListener(new DisposeListener() {
79 public void widgetDisposed(DisposeEvent event) {
93 public RGB getColorValue() {
97 public void setColorValue(RGB rgb) {
103 public Button getButton() {
107 protected void updateColorImage() {
108 Display display = button.getDisplay();
110 GC gc = new GC(image);
112 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
113 gc.drawRectangle(0, 2, extent.x - 1, extent.y - 4);
119 color = new Color(display, rgb);
121 gc.setBackground(color);
122 gc.fillRectangle(1, 3, extent.x - 2, extent.y - 5);
126 button.setImage(image);
129 protected Point computeImageSize(Control control) {
130 Font f = JFaceResources.getFontRegistry().get(
131 JFaceResources.DEFAULT_FONT);
133 GC gc = new GC(control);
136 int height = gc.getFontMetrics().getHeight();
140 return new Point(height * 3 - 6, height);