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