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