KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.resource.JFaceResources;
14 import org.eclipse.jface.util.IPropertyChangeListener;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 /**
25  * A field editor for a color type preference.
26  */

27 public class ColorFieldEditor extends FieldEditor {
28
29     /**
30      * The color selector, or <code>null</code> if none.
31      */

32     private ColorSelector colorSelector;
33
34     /**
35      * Creates a new color field editor
36      */

37     protected ColorFieldEditor() {
38         //No default behavior
39
}
40
41     /**
42      * Creates a color field editor.
43      *
44      * @param name
45      * the name of the preference this field editor works on
46      * @param labelText
47      * the label text of the field editor
48      * @param parent
49      * the parent of the field editor's control
50      */

51     public ColorFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
52         super(name, labelText, parent);
53     }
54
55     /*
56      * (non-Javadoc) Method declared on FieldEditor.
57      */

58     protected void adjustForNumColumns(int numColumns) {
59         ((GridData) colorSelector.getButton().getLayoutData()).horizontalSpan = numColumns - 1;
60     }
61
62     /**
63      * Computes the size of the color image displayed on the button.
64      * <p>
65      * This is an internal method and should not be called by clients.
66      * </p>
67      *
68      * @param window
69      * the window to create a GC on for calculation.
70      * @return Point The image size
71      *
72      */

73     protected Point computeImageSize(Control window) {
74         // Make the image height as high as a corresponding character. This
75
// makes sure that the button has the same size as a "normal" text
76
// button.
77
GC gc = new GC(window);
78         Font f = JFaceResources.getFontRegistry().get(
79                 JFaceResources.DEFAULT_FONT);
80         gc.setFont(f);
81         int height = gc.getFontMetrics().getHeight();
82         gc.dispose();
83         Point p = new Point(height * 3 - 6, height);
84         return p;
85     }
86
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
90      */

91     protected void doFillIntoGrid(Composite parent, int numColumns) {
92         Control control = getLabelControl(parent);
93         GridData gd = new GridData();
94         gd.horizontalSpan = numColumns - 1;
95         control.setLayoutData(gd);
96
97         Button colorButton = getChangeControl(parent);
98         colorButton.setLayoutData(new GridData());
99         
100     }
101
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.jface.preference.FieldEditor#doLoad()
105      */

106     protected void doLoad() {
107         if (colorSelector == null) {
108             return;
109         }
110         colorSelector.setColorValue(PreferenceConverter.getColor(
111                 getPreferenceStore(), getPreferenceName()));
112     }
113
114     /*
115      * (non-Javadoc) Method declared on FieldEditor.
116      */

117     protected void doLoadDefault() {
118         if (colorSelector == null) {
119             return;
120         }
121         colorSelector.setColorValue(PreferenceConverter.getDefaultColor(
122                 getPreferenceStore(), getPreferenceName()));
123     }
124
125     /*
126      * (non-Javadoc) Method declared on FieldEditor.
127      */

128     protected void doStore() {
129         PreferenceConverter.setValue(getPreferenceStore(), getPreferenceName(),
130                 colorSelector.getColorValue());
131     }
132
133     /**
134      * Get the color selector used by the receiver.
135      *
136      * @return ColorSelector/
137      */

138     public ColorSelector getColorSelector() {
139         return colorSelector;
140     }
141
142     /**
143      * Returns the change button for this field editor.
144      *
145      * @param parent
146      * The control to create the button in if required.
147      * @return the change button
148      */

149     protected Button getChangeControl(Composite parent) {
150         if (colorSelector == null) {
151             colorSelector = new ColorSelector(parent);
152             colorSelector.addListener(new IPropertyChangeListener() {
153                 // forward the property change of the color selector
154
public void propertyChange(PropertyChangeEvent event) {
155                     ColorFieldEditor.this.fireValueChanged(event.getProperty(),
156                             event.getOldValue(), event.getNewValue());
157                     setPresentsDefaultValue(false);
158                 }
159             });
160
161         } else {
162             checkParent(colorSelector.getButton(), parent);
163         }
164         return colorSelector.getButton();
165     }
166
167     /*
168      * (non-Javadoc) Method declared on FieldEditor.
169      */

170     public int getNumberOfControls() {
171         return 2;
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean,
178      * org.eclipse.swt.widgets.Composite)
179      */

180     public void setEnabled(boolean enabled, Composite parent) {
181         super.setEnabled(enabled, parent);
182         getChangeControl(parent).setEnabled(enabled);
183     }
184
185 }
186
Popular Tags