a4c4761b7e80e6e250662b87ae58f6d3599f5cfe
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / preferences / ColorEditor.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
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;
30
31 /**
32  * A "button" of a certain color determined by the color picker.
33  */
34 public class ColorEditor {
35
36         private Point fExtent;
37
38         private Image fImage;
39
40         private RGB fColorValue;
41
42         private Color fColor;
43
44         private Button fButton;
45
46         public ColorEditor(Composite parent) {
47
48                 fButton = new Button(parent, SWT.PUSH);
49                 fExtent = computeImageSize(parent);
50                 fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
51
52                 GC gc = new GC(fImage);
53                 gc.setBackground(fButton.getBackground());
54                 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
55                 gc.dispose();
56
57                 fButton.setImage(fImage);
58                 fButton.addSelectionListener(new SelectionAdapter() {
59                         public void widgetSelected(SelectionEvent event) {
60                                 ColorDialog colorDialog = new ColorDialog(fButton.getShell());
61                                 colorDialog.setRGB(fColorValue);
62                                 RGB newColor = colorDialog.open();
63                                 if (newColor != null) {
64                                         fColorValue = newColor;
65                                         updateColorImage();
66                                 }
67                         }
68                 });
69
70                 fButton.addDisposeListener(new DisposeListener() {
71                         public void widgetDisposed(DisposeEvent event) {
72                                 if (fImage != null) {
73                                         fImage.dispose();
74                                         fImage = null;
75                                 }
76                                 if (fColor != null) {
77                                         fColor.dispose();
78                                         fColor = null;
79                                 }
80                         }
81                 });
82         }
83
84         public RGB getColorValue() {
85                 return fColorValue;
86         }
87
88         public void setColorValue(RGB rgb) {
89                 fColorValue = rgb;
90                 updateColorImage();
91         }
92
93         public Button getButton() {
94                 return fButton;
95         }
96
97         protected void updateColorImage() {
98
99                 Display display = fButton.getDisplay();
100
101                 GC gc = new GC(fImage);
102                 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
103                 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
104
105                 if (fColor != null)
106                         fColor.dispose();
107
108                 fColor = new Color(display, fColorValue);
109                 gc.setBackground(fColor);
110                 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
111                 gc.dispose();
112
113                 fButton.setImage(fImage);
114         }
115
116         protected Point computeImageSize(Control window) {
117                 GC gc = new GC(window);
118                 Font f = JFaceResources.getFontRegistry().get(
119                                 JFaceResources.DEFAULT_FONT);
120                 gc.setFont(f);
121                 int height = gc.getFontMetrics().getHeight();
122                 gc.dispose();
123                 Point p = new Point(height * 3 - 6, height);
124                 return p;
125         }
126 }