34
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.preference.ColorFieldEditor;
|
|
14
|
|
15 import dwtx.jface.preference.FieldEditor;
|
|
16 import dwtx.jface.preference.ColorSelector;
|
|
17 import dwtx.jface.preference.PreferenceConverter;
|
|
18
|
|
19 import dwt.graphics.Font;
|
|
20 import dwt.graphics.GC;
|
|
21 import dwt.graphics.Point;
|
|
22 import dwt.layout.GridData;
|
|
23 import dwt.widgets.Button;
|
|
24 import dwt.widgets.Composite;
|
|
25 import dwt.widgets.Control;
|
|
26 import dwtx.jface.resource.JFaceResources;
|
|
27 import dwtx.jface.util.IPropertyChangeListener;
|
|
28 import dwtx.jface.util.PropertyChangeEvent;
|
|
29
|
|
30 import dwt.dwthelper.utils;
|
|
31
|
|
32 /**
|
|
33 * A field editor for a color type preference.
|
|
34 */
|
|
35 public class ColorFieldEditor : FieldEditor {
|
|
36
|
|
37 /**
|
|
38 * The color selector, or <code>null</code> if none.
|
|
39 */
|
|
40 private ColorSelector colorSelector;
|
|
41
|
|
42 /**
|
|
43 * Creates a new color field editor
|
|
44 */
|
|
45 protected this() {
|
|
46 //No default behavior
|
|
47 }
|
|
48
|
|
49 /**
|
|
50 * Creates a color field editor.
|
|
51 *
|
|
52 * @param name
|
|
53 * the name of the preference this field editor works on
|
|
54 * @param labelText
|
|
55 * the label text of the field editor
|
|
56 * @param parent
|
|
57 * the parent of the field editor's control
|
|
58 */
|
|
59 public this(String name, String labelText, Composite parent) {
|
|
60 super(name, labelText, parent);
|
|
61 }
|
|
62
|
|
63 /*
|
|
64 * (non-Javadoc) Method declared on FieldEditor.
|
|
65 */
|
|
66 protected void adjustForNumColumns(int numColumns) {
|
|
67 (cast(GridData) colorSelector.getButton().getLayoutData()).horizontalSpan = numColumns - 1;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Computes the size of the color image displayed on the button.
|
|
72 * <p>
|
|
73 * This is an internal method and should not be called by clients.
|
|
74 * </p>
|
|
75 *
|
|
76 * @param window
|
|
77 * the window to create a GC on for calculation.
|
|
78 * @return Point The image size
|
|
79 *
|
|
80 */
|
|
81 protected Point computeImageSize(Control window) {
|
|
82 // Make the image height as high as a corresponding character. This
|
|
83 // makes sure that the button has the same size as a "normal" text
|
|
84 // button.
|
|
85 GC gc = new GC(window);
|
|
86 Font f = JFaceResources.getFontRegistry().get(
|
|
87 JFaceResources.DEFAULT_FONT);
|
|
88 gc.setFont(f);
|
|
89 int height = gc.getFontMetrics().getHeight();
|
|
90 gc.dispose();
|
|
91 Point p = new Point(height * 3 - 6, height);
|
|
92 return p;
|
|
93 }
|
|
94
|
|
95
|
|
96 /* (non-Javadoc)
|
|
97 * @see dwtx.jface.preference.FieldEditor#doFillIntoGrid(dwt.widgets.Composite, int)
|
|
98 */
|
|
99 protected void doFillIntoGrid(Composite parent, int numColumns) {
|
|
100 Control control = getLabelControl(parent);
|
|
101 GridData gd = new GridData();
|
|
102 gd.horizontalSpan = numColumns - 1;
|
|
103 control.setLayoutData(gd);
|
|
104
|
|
105 Button colorButton = getChangeControl(parent);
|
|
106 colorButton.setLayoutData(new GridData());
|
|
107
|
|
108 }
|
|
109
|
|
110
|
|
111 /* (non-Javadoc)
|
|
112 * @see dwtx.jface.preference.FieldEditor#doLoad()
|
|
113 */
|
|
114 protected void doLoad() {
|
|
115 if (colorSelector is null) {
|
|
116 return;
|
|
117 }
|
|
118 colorSelector.setColorValue(PreferenceConverter.getColor(
|
|
119 getPreferenceStore(), getPreferenceName()));
|
|
120 }
|
|
121
|
|
122 /*
|
|
123 * (non-Javadoc) Method declared on FieldEditor.
|
|
124 */
|
|
125 protected void doLoadDefault() {
|
|
126 if (colorSelector is null) {
|
|
127 return;
|
|
128 }
|
|
129 colorSelector.setColorValue(PreferenceConverter.getDefaultColor(
|
|
130 getPreferenceStore(), getPreferenceName()));
|
|
131 }
|
|
132
|
|
133 /*
|
|
134 * (non-Javadoc) Method declared on FieldEditor.
|
|
135 */
|
|
136 protected void doStore() {
|
|
137 PreferenceConverter.setValue(getPreferenceStore(), getPreferenceName(),
|
|
138 colorSelector.getColorValue());
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * Get the color selector used by the receiver.
|
|
143 *
|
|
144 * @return ColorSelector/
|
|
145 */
|
|
146 public ColorSelector getColorSelector() {
|
|
147 return colorSelector;
|
|
148 }
|
|
149
|
|
150 /**
|
|
151 * Returns the change button for this field editor.
|
|
152 *
|
|
153 * @param parent
|
|
154 * The control to create the button in if required.
|
|
155 * @return the change button
|
|
156 */
|
|
157 protected Button getChangeControl(Composite parent) {
|
|
158 if (colorSelector is null) {
|
|
159 colorSelector = new ColorSelector(parent);
|
|
160 colorSelector.addListener(new class IPropertyChangeListener {
|
|
161 // forward the property change of the color selector
|
|
162 public void propertyChange(PropertyChangeEvent event) {
|
|
163 this.outer.fireValueChanged(event.getProperty(),
|
|
164 event.getOldValue(), event.getNewValue());
|
|
165 setPresentsDefaultValue(false);
|
|
166 }
|
|
167 });
|
|
168
|
|
169 } else {
|
|
170 checkParent(colorSelector.getButton(), parent);
|
|
171 }
|
|
172 return colorSelector.getButton();
|
|
173 }
|
|
174
|
|
175 /*
|
|
176 * (non-Javadoc) Method declared on FieldEditor.
|
|
177 */
|
|
178 public int getNumberOfControls() {
|
|
179 return 2;
|
|
180 }
|
|
181
|
|
182 /*
|
|
183 * (non-Javadoc)
|
|
184 *
|
|
185 * @see dwtx.jface.preference.FieldEditor#setEnabled(bool,
|
|
186 * dwt.widgets.Composite)
|
|
187 */
|
|
188 public void setEnabled(bool enabled, Composite parent) {
|
|
189 super.setEnabled(enabled, parent);
|
|
190 getChangeControl(parent).setEnabled(enabled);
|
|
191 }
|
|
192
|
|
193 }
|