5a2cef9c9fae37563a25223518e9cb7d53d5cea0
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / preferences / ColorEditor.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpeclipse.preferences;
6
7 import org.eclipse.jface.resource.JFaceResources;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.DisposeEvent;
10 import org.eclipse.swt.events.DisposeListener;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.ColorDialog;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
24
25 /**
26  * A "button" of a certain color determined by the color picker.
27  */
28 public class ColorEditor {
29
30         private Point fExtent;
31
32         private Image fImage;
33
34         private RGB fColorValue;
35
36         private Color fColor;
37
38         private Button fButton;
39
40         public ColorEditor(Composite parent) {
41
42                 fButton = new Button(parent, SWT.PUSH);
43                 fExtent = computeImageSize(parent);
44                 fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
45
46                 GC gc = new GC(fImage);
47                 gc.setBackground(fButton.getBackground());
48                 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
49                 gc.dispose();
50
51                 fButton.setImage(fImage);
52                 fButton.addSelectionListener(new SelectionAdapter() {
53                         public void widgetSelected(SelectionEvent event) {
54                                 ColorDialog colorDialog = new ColorDialog(fButton.getShell());
55                                 colorDialog.setRGB(fColorValue);
56                                 RGB newColor = colorDialog.open();
57                                 if (newColor != null) {
58                                         fColorValue = newColor;
59                                         updateColorImage();
60                                 }
61                         }
62                 });
63
64                 fButton.addDisposeListener(new DisposeListener() {
65                         public void widgetDisposed(DisposeEvent event) {
66                                 if (fImage != null) {
67                                         fImage.dispose();
68                                         fImage = null;
69                                 }
70                                 if (fColor != null) {
71                                         fColor.dispose();
72                                         fColor = null;
73                                 }
74                         }
75                 });
76         }
77
78         public RGB getColorValue() {
79                 return fColorValue;
80         }
81
82         public void setColorValue(RGB rgb) {
83                 fColorValue = rgb;
84                 updateColorImage();
85         }
86
87         public Button getButton() {
88                 return fButton;
89         }
90
91         protected void updateColorImage() {
92
93                 Display display = fButton.getDisplay();
94
95                 GC gc = new GC(fImage);
96                 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
97                 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
98
99                 if (fColor != null)
100                         fColor.dispose();
101
102                 fColor = new Color(display, fColorValue);
103                 gc.setBackground(fColor);
104                 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
105                 gc.dispose();
106
107                 fButton.setImage(fImage);
108         }
109
110         protected Point computeImageSize(Control window) {
111                 GC gc = new GC(window);
112                 Font f = JFaceResources.getFontRegistry().get(
113                                 JFaceResources.DEFAULT_FONT);
114                 gc.setFont(f);
115                 int height = gc.getFontMetrics().getHeight();
116                 gc.dispose();
117                 Point p = new Point(height * 3 - 6, height);
118                 return p;
119         }
120 }