]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2012 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.jdt.internal.ui.wizards.dialogfields; | |
12 | ||
13 | import org.eclipse.swt.SWT; | |
14 | import org.eclipse.swt.layout.GridData; | |
15 | import org.eclipse.swt.widgets.Composite; | |
16 | import org.eclipse.swt.widgets.Control; | |
17 | import org.eclipse.swt.widgets.Display; | |
18 | import org.eclipse.swt.widgets.Label; | |
19 | ||
20 | import org.eclipse.core.runtime.Assert; | |
21 | ||
22 | ||
23 | /** | |
24 | * Base class of all dialog fields. | |
25 | * Dialog fields manage controls together with the model, independent | |
26 | * from the creation time of the widgets. | |
27 | * - support for automated layouting. | |
28 | * - enable / disable, set focus a concept of the base class. | |
29 | * | |
30 | * DialogField have a label. | |
31 | */ | |
32 | public class DialogField { | |
33 | ||
34 | private Label fLabel; | |
35 | protected String fLabelText; | |
36 | ||
37 | private IDialogFieldListener fDialogFieldListener; | |
38 | ||
39 | private boolean fEnabled; | |
40 | ||
41 | public DialogField() { | |
42 | fEnabled= true; | |
43 | fLabel= null; | |
44 | fLabelText= ""; //$NON-NLS-1$ | |
45 | } | |
46 | ||
47 | /** | |
48 | * Sets the label of the dialog field. | |
49 | * | |
50 | * @param labeltext the label text | |
51 | */ | |
52 | public void setLabelText(String labeltext) { | |
53 | fLabelText= labeltext; | |
54 | if (isOkToUse(fLabel)) { | |
55 | fLabel.setText(labeltext); | |
56 | } | |
57 | } | |
58 | ||
59 | // ------ change listener | |
60 | ||
61 | /** | |
62 | * Defines the listener for this dialog field. | |
63 | * | |
64 | * @param listener the dialog field listener | |
65 | */ | |
66 | public final void setDialogFieldListener(IDialogFieldListener listener) { | |
67 | fDialogFieldListener= listener; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Programatical invocation of a dialog field change. | |
72 | */ | |
73 | public void dialogFieldChanged() { | |
74 | if (fDialogFieldListener != null) { | |
75 | fDialogFieldListener.dialogFieldChanged(this); | |
76 | } | |
77 | } | |
78 | ||
79 | // ------- focus management | |
80 | ||
81 | /** | |
82 | * Tries to set the focus to the dialog field. Returns <code>true</code> if the dialog field can | |
83 | * take focus. To be reimplemented by dialog field implementors. | |
84 | * | |
85 | * @return <code>true</code> if the dialog field can take focus | |
86 | */ | |
87 | public boolean setFocus() { | |
88 | return false; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Posts <code>setFocus</code> to the display event queue. | |
93 | * @param display the Display | |
94 | */ | |
95 | public void postSetFocusOnDialogField(Display display) { | |
96 | if (display != null) { | |
97 | display.asyncExec( | |
98 | new Runnable() { | |
99 | public void run() { | |
100 | setFocus(); | |
101 | } | |
102 | } | |
103 | ); | |
104 | } | |
105 | } | |
106 | ||
107 | // ------- layout helpers | |
108 | ||
109 | /** | |
110 | * Creates all controls of the dialog field and fills it to a composite. The composite is | |
111 | * assumed to have <code>GridLayout</code> as layout. The dialog field will adjust its controls' | |
112 | * spans to the number of columns given. To be reimplemented by dialog field implementors. | |
113 | * | |
114 | * @param parent the parent composite | |
115 | * @param nColumns number of columns | |
116 | * @return controls of dialog field | |
117 | */ | |
118 | public Control[] doFillIntoGrid(Composite parent, int nColumns) { | |
119 | assertEnoughColumns(nColumns); | |
120 | ||
121 | Label label= getLabelControl(parent); | |
122 | label.setLayoutData(gridDataForLabel(nColumns)); | |
123 | ||
124 | return new Control[] { label }; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Returns the number of columns of the dialog field. To be reimplemented by dialog field | |
129 | * implementors. | |
130 | * | |
131 | * @return the number of columns of the dialog field | |
132 | */ | |
133 | public int getNumberOfControls() { | |
134 | return 1; | |
135 | } | |
136 | ||
137 | protected static GridData gridDataForLabel(int span) { | |
138 | GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); | |
139 | gd.horizontalSpan= span; | |
140 | return gd; | |
141 | } | |
142 | ||
143 | // ------- ui creation | |
144 | ||
145 | /** | |
146 | * Creates or returns the created label widget. | |
147 | * | |
148 | * @param parent The parent composite or <code>null</code> if the widget has already been | |
149 | * created. | |
150 | * @return the label widget | |
151 | */ | |
152 | public Label getLabelControl(Composite parent) { | |
153 | if (fLabel == null) { | |
154 | assertCompositeNotNull(parent); | |
155 | ||
156 | fLabel= new Label(parent, SWT.LEFT | SWT.WRAP); | |
157 | fLabel.setFont(parent.getFont()); | |
158 | fLabel.setEnabled(fEnabled); | |
159 | if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$ | |
160 | fLabel.setText(fLabelText); | |
161 | } else { | |
162 | // XXX: to avoid a 16 pixel wide empty label - revisit | |
163 | fLabel.setText("."); //$NON-NLS-1$ | |
164 | fLabel.setVisible(false); | |
165 | } | |
166 | } | |
167 | return fLabel; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Creates a spacer control. | |
172 | * | |
173 | * @param parent The parent composite | |
174 | * @return the spacer control | |
175 | */ | |
176 | public static Control createEmptySpace(Composite parent) { | |
177 | return createEmptySpace(parent, 1); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Creates a spacer control with the given span. The composite is assumed to have | |
182 | * <code>GridLayout</code> as layout. | |
183 | * | |
184 | * @param parent The parent composite | |
185 | * @param span the given span | |
186 | * @return the spacer control | |
187 | */ | |
188 | public static Control createEmptySpace(Composite parent, int span) { | |
189 | Label label= new Label(parent, SWT.LEFT); | |
190 | GridData gd= new GridData(); | |
191 | gd.horizontalAlignment= GridData.BEGINNING; | |
192 | gd.grabExcessHorizontalSpace= false; | |
193 | gd.horizontalSpan= span; | |
194 | gd.horizontalIndent= 0; | |
195 | gd.widthHint= 0; | |
196 | gd.heightHint= 0; | |
197 | label.setLayoutData(gd); | |
198 | return label; | |
199 | } | |
200 | ||
201 | /** | |
202 | * Tests is the control is not <code>null</code> and not disposed. | |
203 | * | |
204 | * @param control the Control | |
205 | * @return <code>true</code> if the control is not <code>null</code> and not disposed. | |
206 | */ | |
207 | protected final boolean isOkToUse(Control control) { | |
208 | return (control != null) && (Display.getCurrent() != null) && !control.isDisposed(); | |
209 | } | |
210 | ||
211 | // --------- enable / disable management | |
212 | ||
213 | /** | |
214 | * Sets the enable state of the dialog field. | |
215 | * | |
216 | * @param enabled enable state | |
217 | */ | |
218 | public final void setEnabled(boolean enabled) { | |
219 | if (enabled != fEnabled) { | |
220 | fEnabled= enabled; | |
221 | updateEnableState(); | |
222 | } | |
223 | } | |
224 | ||
225 | /** | |
226 | * Called when the enable state changed. | |
227 | * To be extended by dialog field implementors. | |
228 | */ | |
229 | protected void updateEnableState() { | |
230 | if (fLabel != null) { | |
231 | fLabel.setEnabled(fEnabled); | |
232 | } | |
233 | } | |
234 | ||
235 | /** | |
236 | * Brings the UI in sync with the model. Only needed when model was changed | |
237 | * in different thread whil UI was lready created. | |
238 | */ | |
239 | public void refresh() { | |
240 | updateEnableState(); | |
241 | } | |
242 | ||
243 | /** | |
244 | * Gets the enable state of the dialog field. | |
245 | * | |
246 | * @return the enable state | |
247 | */ | |
248 | public final boolean isEnabled() { | |
249 | return fEnabled; | |
250 | } | |
251 | ||
252 | protected final void assertCompositeNotNull(Composite comp) { | |
253 | Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$ | |
254 | } | |
255 | ||
256 | protected final void assertEnoughColumns(int nColumns) { | |
257 | Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); //$NON-NLS-1$ | |
258 | } | |
259 | ||
260 | ||
261 | ||
262 | ||
263 | } |