Organized imports
[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.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         private Image fImage;
38         private RGB fColorValue;
39         private Color fColor;
40         private Button fButton;
41         
42         public ColorEditor(Composite parent) {
43                 
44                 fButton= new Button(parent, SWT.PUSH);
45                 fExtent= computeImageSize(parent);
46                 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y);
47                 
48                 GC gc= new GC(fImage);
49                 gc.setBackground(fButton.getBackground());
50                 gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
51                 gc.dispose();
52                 
53                 fButton.setImage(fImage);
54                 fButton.addSelectionListener(new SelectionAdapter() {
55                         public void widgetSelected(SelectionEvent event) {
56                                 ColorDialog colorDialog= new ColorDialog(fButton.getShell());
57                                 colorDialog.setRGB(fColorValue);
58                                 RGB newColor = colorDialog.open();
59                                 if (newColor != null) {
60                                         fColorValue= newColor;
61                                         updateColorImage();
62                                 }
63                         }
64                 });
65                 
66                 fButton.addDisposeListener(new DisposeListener() {
67                         public void widgetDisposed(DisposeEvent event) {
68                                 if (fImage != null)  {
69                                         fImage.dispose();
70                                         fImage= null;
71                                 }
72                                 if (fColor != null) {
73                                         fColor.dispose();
74                                         fColor= null;
75                                 }
76                         }
77                 });
78         }
79         
80         public RGB getColorValue() {
81                 return fColorValue;
82         }
83         
84         public void setColorValue(RGB rgb) {
85                 fColorValue= rgb;
86                 updateColorImage();
87         }
88         
89         public Button getButton() {
90                 return fButton;
91         }
92         
93         protected void updateColorImage() {
94                 
95                 Display display= fButton.getDisplay();
96                 
97                 GC gc= new GC(fImage);
98                 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
99                 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
100                 
101                 if (fColor != null)
102                         fColor.dispose();
103                         
104                 fColor= new Color(display, fColorValue);
105                 gc.setBackground(fColor);
106                 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
107                 gc.dispose();
108                 
109                 fButton.setImage(fImage);
110         }
111         
112         protected Point computeImageSize(Control window) {
113                 GC gc= new GC(window);
114                 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT);
115                 gc.setFont(f);
116                 int height= gc.getFontMetrics().getHeight();
117                 gc.dispose();
118                 Point p= new Point(height * 3 - 6, height);
119                 return p;
120         }
121 }