KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > ColorSelector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import org.eclipse.core.commands.common.EventManager;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.accessibility.AccessibleAdapter;
19 import org.eclipse.swt.accessibility.AccessibleEvent;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.GC;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.RGB;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.ColorDialog;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Display;
35
36 /**
37  * The <code>ColorSelector</code> is a wrapper for a button that displays a
38  * selected <code>Color</code> and allows the user to change the selection.
39  */

40 public class ColorSelector extends EventManager {
41     /**
42      * Property name that signifies the selected color of this
43      * <code>ColorSelector</code> has changed.
44      *
45      * @since 3.0
46      */

47     public static final String JavaDoc PROP_COLORCHANGE = "colorValue"; //$NON-NLS-1$
48

49     private Button fButton;
50
51     private Color fColor;
52
53     private RGB fColorValue;
54
55     private Point fExtent;
56
57     private Image fImage;
58
59     /**
60      * Create a new instance of the reciever and the button that it wrappers in
61      * the supplied parent <code>Composite</code>.
62      *
63      * @param parent
64      * The parent of the button.
65      */

66     public ColorSelector(Composite parent) {
67         fButton = new Button(parent, SWT.PUSH);
68         fExtent = computeImageSize(parent);
69         fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
70         GC gc = new GC(fImage);
71         gc.setBackground(fButton.getBackground());
72         gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
73         gc.dispose();
74         fButton.setImage(fImage);
75         fButton.addSelectionListener(new SelectionAdapter() {
76             public void widgetSelected(SelectionEvent event) {
77                 open();
78             }
79         });
80         fButton.addDisposeListener(new DisposeListener() {
81             public void widgetDisposed(DisposeEvent event) {
82                 if (fImage != null) {
83                     fImage.dispose();
84                     fImage = null;
85                 }
86                 if (fColor != null) {
87                     fColor.dispose();
88                     fColor = null;
89                 }
90             }
91         });
92         fButton.getAccessible().addAccessibleListener(new AccessibleAdapter() {
93             /*
94              * (non-Javadoc)
95              *
96              * @see org.eclipse.swt.accessibility.AccessibleAdapter#getName(org.eclipse.swt.accessibility.AccessibleEvent)
97              */

98             public void getName(AccessibleEvent e) {
99                 e.result = JFaceResources.getString("ColorSelector.Name"); //$NON-NLS-1$
100
}
101         });
102     }
103
104     /**
105      * Adds a property change listener to this <code>ColorSelector</code>.
106      * Events are fired when the color in the control changes via the user
107      * clicking an selecting a new one in the color dialog. No event is fired in
108      * the case where <code>setColorValue(RGB)</code> is invoked.
109      *
110      * @param listener
111      * a property change listener
112      * @since 3.0
113      */

114     public void addListener(IPropertyChangeListener listener) {
115         addListenerObject(listener);
116     }
117
118     /**
119      * Compute the size of the image to be displayed.
120      *
121      * @param window -
122      * the window used to calculate
123      * @return <code>Point</code>
124      */

125     private Point computeImageSize(Control window) {
126         GC gc = new GC(window);
127         Font f = JFaceResources.getFontRegistry().get(
128                 JFaceResources.DIALOG_FONT);
129         gc.setFont(f);
130         int height = gc.getFontMetrics().getHeight();
131         gc.dispose();
132         Point p = new Point(height * 3 - 6, height);
133         return p;
134     }
135
136     /**
137      * Get the button control being wrappered by the selector.
138      *
139      * @return <code>Button</code>
140      */

141     public Button getButton() {
142         return fButton;
143     }
144
145     /**
146      * Return the currently displayed color.
147      *
148      * @return <code>RGB</code>
149      */

150     public RGB getColorValue() {
151         return fColorValue;
152     }
153
154     /**
155      * Removes the given listener from this <code>ColorSelector</code>. Has
156      * no affect if the listener is not registered.
157      *
158      * @param listener
159      * a property change listener
160      * @since 3.0
161      */

162     public void removeListener(IPropertyChangeListener listener) {
163         removeListenerObject(listener);
164     }
165
166     /**
167      * Set the current color value and update the control.
168      *
169      * @param rgb
170      * The new color.
171      */

172     public void setColorValue(RGB rgb) {
173         fColorValue = rgb;
174         updateColorImage();
175     }
176
177     /**
178      * Set whether or not the button is enabled.
179      *
180      * @param state
181      * the enabled state.
182      */

183     public void setEnabled(boolean state) {
184         getButton().setEnabled(state);
185     }
186
187     /**
188      * Update the image being displayed on the button using the current color
189      * setting.
190      */

191     protected void updateColorImage() {
192         Display display = fButton.getDisplay();
193         GC gc = new GC(fImage);
194         gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
195         gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4);
196         if (fColor != null) {
197             fColor.dispose();
198         }
199         fColor = new Color(display, fColorValue);
200         gc.setBackground(fColor);
201         gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5);
202         gc.dispose();
203         fButton.setImage(fImage);
204     }
205
206     /**
207      * Activate the editor for this selector. This causes the color selection
208      * dialog to appear and wait for user input.
209      *
210      * @since 3.2
211      */

212     public void open() {
213         ColorDialog colorDialog = new ColorDialog(fButton.getShell());
214         colorDialog.setRGB(fColorValue);
215         RGB newColor = colorDialog.open();
216         if (newColor != null) {
217             RGB oldValue = fColorValue;
218             fColorValue = newColor;
219             final Object JavaDoc[] finalListeners = getListeners();
220             if (finalListeners.length > 0) {
221                 PropertyChangeEvent pEvent = new PropertyChangeEvent(
222                         this, PROP_COLORCHANGE, oldValue, newColor);
223                 for (int i = 0; i < finalListeners.length; ++i) {
224                     IPropertyChangeListener listener = (IPropertyChangeListener) finalListeners[i];
225                     listener.propertyChange(pEvent);
226                 }
227             }
228             updateColorImage();
229         }
230     }
231 }
232
Popular Tags