intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / ColorEditor.java
1 /*
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
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: ColorEditor.java,v 1.1 2004-09-02 18:26:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui;
15
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;
33
34 /**
35  * A "button" of a certain color determined by the color picker.
36  */
37 public class ColorEditor {
38
39         private Point extent;
40
41         Button button;
42
43         Image image;
44
45         RGB rgb;
46
47         Color color;
48
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);
53
54                 GC gc = new GC(image);
55
56                 gc.setBackground(button.getBackground());
57                 gc.fillRectangle(0, 0, extent.x, extent.y);
58
59                 gc.dispose();
60
61                 button.setImage(image);
62
63                 button.addSelectionListener(new SelectionAdapter() {
64
65                         public void widgetSelected(SelectionEvent event) {
66                                 ColorDialog colorDialog = new ColorDialog(button.getShell());
67                                 colorDialog.setRGB(rgb);
68
69                                 RGB newColor = colorDialog.open();
70                                 if (newColor != null) {
71                                         rgb = newColor;
72                                         updateColorImage();
73                                 }
74                         }
75                 });
76
77                 button.addDisposeListener(new DisposeListener() {
78
79                         public void widgetDisposed(DisposeEvent event) {
80                                 if (image != null) {
81                                         image.dispose();
82                                         image = null;
83                                 }
84
85                                 if (color != null) {
86                                         color.dispose();
87                                         color = null;
88                                 }
89                         }
90                 });
91         }
92
93         public RGB getColorValue() {
94                 return rgb;
95         }
96
97         public void setColorValue(RGB rgb) {
98                 this.rgb = rgb;
99
100                 updateColorImage();
101         }
102
103         public Button getButton() {
104                 return button;
105         }
106
107         protected void updateColorImage() {
108                 Display display = button.getDisplay();
109
110                 GC gc = new GC(image);
111
112                 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
113                 gc.drawRectangle(0, 2, extent.x - 1, extent.y - 4);
114
115                 if (color != null) {
116                         color.dispose();
117                 }
118
119                 color = new Color(display, rgb);
120
121                 gc.setBackground(color);
122                 gc.fillRectangle(1, 3, extent.x - 2, extent.y - 5);
123
124                 gc.dispose();
125
126                 button.setImage(image);
127         }
128
129         protected Point computeImageSize(Control control) {
130                 Font f = JFaceResources.getFontRegistry().get(
131                                 JFaceResources.DEFAULT_FONT);
132
133                 GC gc = new GC(control);
134                 gc.setFont(f);
135
136                 int height = gc.getFontMetrics().getHeight();
137
138                 gc.dispose();
139
140                 return new Point(height * 3 - 6, height);
141         }
142 }