Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cbfa32cadc19c34956edda92d588b1ae05cf31d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.preferences.ui.editor;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;



/**
 * A field editor for a boolean type preference.
 * This field accepts 2 styles :
 * <ul>
 * <li>SWT.LEFT : the CLabel will be on the left</li>
 * <li>SWT.RIGHT : the CLabel will be on the right</li>
 * </ul>
 */
public class CLabelBooleanFieldEditor extends BooleanFieldEditor {

	/**
	 * A CLabel
	 */
	private CLabel cLabel = null;

	/**
	 * the checkbox
	 */
	private Button checkBox = null;

	/**
	 * Style bits. Either <code>SWT.LEFT</code> or <code>SWT.RIGHT</code>.
	 */
	private int style = SWT.LEFT;

	/**
	 * the image to display
	 */
	private Image image = null;

	/**
	 * Creates a boolean field editor in the given style.
	 *
	 * @param preferenceKey
	 *            the name of the preference this field editor works on
	 * @param labelText
	 *            the label text of the field editor
	 * @param style
	 *            the style, either <code>SWT.LEFT</code> or <code>SWT.RIGHT</code>. If style value is different, we use <code>SWT.RIGHT</code> The style
	 *            allows to define if the label is on the left side or in the right side
	 * @param parent
	 *            the parent of the field editor's control
	 */
	public CLabelBooleanFieldEditor(String preferenceKey, String labelText, int style, Image im, Composite parent) {
		this.image = im;
		this.style = (SWT.LEFT == style ? SWT.LEFT : SWT.RIGHT);
		init(preferenceKey, labelText);
		createControl(parent);

	}

	/**
	 *
	 * Constructor. This constructor build a CLabelFieldEditor with the label in the right of the checkbox.
	 *
	 * @param preferenceKey
	 *            the key for the preference
	 * @param labelText
	 *            the label text
	 * @param im
	 *            an image to illustrate the preference
	 * @param parent
	 *            the composite parent
	 */
	public CLabelBooleanFieldEditor(String preferenceKey, String labelText, Image im, Composite parent) {
		this(preferenceKey, labelText, SWT.RIGHT, im, parent);
	}

	/**
	 *
	 * @see org.eclipse.jface.preference.BooleanFieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
	 *
	 * @param parent
	 * @param numColumns
	 */
	@Override
	protected void doFillIntoGrid(Composite parent, int numColumns) {
		GridData gd = new GridData();
		switch (this.style) {
		case SWT.LEFT:
			getCLabelControl(parent);
			checkBox = getChangeControl(parent);
			checkBox.setLayoutData(gd);
			break;
		case SWT.RIGHT:
			checkBox = getChangeControl(parent);
			checkBox.setLayoutData(gd);
			getCLabelControl(parent);
			break;
		default:
			break;
		}
	}

	/**
	 * Returns the label control.
	 *
	 * @return the label control, or <code>null</code> if no label control has been created
	 */
	protected CLabel getCLabelControl() {
		return cLabel;
	}

	/**
	 * Returns this field editor's label component.
	 * <p>
	 * The label is created if it does not already exist
	 * </p>
	 *
	 * @param parent
	 *            the parent
	 * @return the label control
	 */
	public CLabel getCLabelControl(Composite parent) {
		if (cLabel == null) {
			cLabel = new CLabel(parent, SWT.LEFT);
			cLabel.setFont(parent.getFont());

			if (image != null) {
				cLabel.setImage(image);
			}
			String text = getLabelText();
			if (text != null) {
				cLabel.setText(text);
			}
			cLabel.addDisposeListener(new DisposeListener() {

				public void widgetDisposed(DisposeEvent event) {
					cLabel = null;
				}
			});
		} else {
			checkParent(cLabel, parent);
		}
		return cLabel;
	}

	/*
	 * (non-Javadoc) Method declared on FieldEditor.
	 */
	public void setCLabelText(String text) {
		super.setLabelText(text);
		Assert.isNotNull(text);
		cLabel = getCLabelControl();
		if (cLabel == null && checkBox != null) {
			checkBox.setText(text);
		}
	}

	/*
	 *
	 * The following methods are duplicated from BooleanFieldEditor, because, we need the style value before the end of the constructor execution!
	 */

	/**
	 *
	 * @see org.eclipse.jface.preference.BooleanFieldEditor#adjustForNumColumns(int)
	 *
	 * @param numColumns
	 */
	@Override
	protected void adjustForNumColumns(int numColumns) {
		// if(style == SEPARATE_LABEL) {
		// numColumns--;
		// }
		// ((GridData)checkBox.getLayoutData()).horizontalSpan = numColumns;
	}

	/**
	 *
	 * @see org.eclipse.jface.preference.BooleanFieldEditor#getDescriptionControl(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 * @return
	 */
	@Override
	public Control getDescriptionControl(Composite parent) {
		if (style == SEPARATE_LABEL) {
			return getLabelControl(parent);
		}
		return getChangeControl(parent);
	}

	/**
	 *
	 * @see org.eclipse.jface.preference.BooleanFieldEditor#getNumberOfControls()
	 *
	 * @return
	 */
	@Override
	public int getNumberOfControls() {
		return 2;
	}

	/**
	 *
	 * @see org.eclipse.jface.preference.BooleanFieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
	 *
	 * @param enabled
	 * @param parent
	 */
	@Override
	public void setEnabled(boolean enabled, Composite parent) {
		getCLabelControl(parent).setEnabled(enabled);
		getChangeControl(parent).setEnabled(enabled);
	}


}

Back to the top