KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > wizards > MethodStubsSelectionButtonGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.junit.wizards;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Group;
25 import org.eclipse.swt.widgets.Label;
26
27
28 import org.eclipse.jdt.internal.junit.util.LayoutUtil;
29
30 /**
31  * A group of controls used in the JUnit TestCase and TestSuite wizards
32  * for selecting method stubs to create.
33  */

34 public class MethodStubsSelectionButtonGroup {
35
36     private Label fLabel;
37     protected String JavaDoc fLabelText;
38     
39     private SelectionButtonGroupListener fGroupListener;
40     
41     private boolean fEnabled;
42
43     private Composite fButtonComposite;
44     
45     private Button[] fButtons;
46     private String JavaDoc[] fButtonNames;
47     private boolean[] fButtonsSelected;
48     private boolean[] fButtonsEnabled;
49     
50     private int fGroupBorderStyle;
51     private int fGroupNumberOfColumns;
52     private int fButtonsStyle;
53     
54
55     public interface SelectionButtonGroupListener {
56         /**
57          * The dialog field has changed.
58          * @param field The changed dialog field
59          */

60         void groupChanged(MethodStubsSelectionButtonGroup field);
61     }
62         
63     /**
64      * Creates a group without border.
65      */

66     public MethodStubsSelectionButtonGroup(int buttonsStyle, String JavaDoc[] buttonNames, int nColumns) {
67         this(buttonsStyle, buttonNames, nColumns, SWT.NONE);
68     }
69     
70     /**
71      * Creates a group with border (label in border).
72      * Accepted button styles are: SWT.RADIO, SWT.CHECK, SWT.TOGGLE
73      * For border styles see <code>Group</code>
74      */

75     public MethodStubsSelectionButtonGroup(int buttonsStyle, String JavaDoc[] buttonNames, int nColumns, int borderStyle) {
76         fEnabled= true;
77         fLabel= null;
78         fLabelText= ""; //$NON-NLS-1$
79

80         Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK || buttonsStyle == SWT.TOGGLE);
81         fButtonNames= buttonNames;
82         
83         int nButtons= buttonNames.length;
84         fButtonsSelected= new boolean[nButtons];
85         fButtonsEnabled= new boolean[nButtons];
86         for (int i= 0; i < nButtons; i++) {
87             fButtonsSelected[i]= false;
88             fButtonsEnabled[i]= true;
89         }
90         
91         if (buttonsStyle == SWT.RADIO) {
92             fButtonsSelected[0]= true;
93         }
94         
95         fGroupBorderStyle= borderStyle;
96         fGroupNumberOfColumns= (nColumns <= 0) ? nButtons : nColumns;
97         
98         fButtonsStyle= buttonsStyle;
99     }
100         
101     /*
102      * @see DialogField#doFillIntoGrid
103      */

104     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
105         assertEnoughColumns(nColumns);
106                 
107         if (fGroupBorderStyle == SWT.NONE) {
108             Label label= getLabelControl(parent);
109             label.setLayoutData(gridDataForLabel(1));
110         
111             Composite buttonsgroup= getSelectionButtonsGroup(parent);
112             GridData gd= new GridData();
113             gd.horizontalSpan= nColumns - 1;
114             buttonsgroup.setLayoutData(gd);
115             return new Control[] { label, buttonsgroup };
116         } else {
117             Composite buttonsgroup= getSelectionButtonsGroup(parent);
118             GridData gd= new GridData();
119             gd.horizontalSpan= nColumns;
120             buttonsgroup.setLayoutData(gd);
121             return new Control[] { buttonsgroup };
122         }
123     }
124
125     /*
126      * @see DialogField#doFillIntoGrid
127      */

128     public int getNumberOfControls() {
129         return (fGroupBorderStyle == SWT.NONE) ? 2 : 1;
130     }
131     
132     private Button createSelectionButton(int index, Composite group, SelectionListener listener) {
133         Button button= new Button(group, fButtonsStyle | SWT.LEFT);
134         button.setFont(group.getFont());
135         button.setText(fButtonNames[index]);
136         button.setEnabled(isEnabled() && isEnabled(index));
137         button.setSelection(isSelected(index));
138         button.addSelectionListener(listener);
139         return button;
140     }
141     
142
143     /**
144      * Returns the group widget. When called the first time, the widget will be created.
145      * @param parent composite when called the first time, or <code>null</code>
146      * after.
147      */

148     public Composite getSelectionButtonsGroup(Composite parent) {
149         if (fButtonComposite == null) {
150             assertCompositeNotNull(parent);
151             
152             GridLayout layout= new GridLayout();
153             //layout.makeColumnsEqualWidth= true;
154
layout.numColumns= fGroupNumberOfColumns;
155             
156             if (fGroupBorderStyle != SWT.NONE) {
157                 Group group= new Group(parent, fGroupBorderStyle);
158                 if (fLabelText != null && fLabelText.length() > 0) {
159                     group.setText(fLabelText);
160                 }
161                 fButtonComposite= group;
162             } else {
163                 fButtonComposite= new Composite(parent, SWT.NULL);
164                 layout.marginHeight= 0;
165                 layout.marginWidth= 0;
166             }
167             fButtonComposite.setLayout(layout);
168             
169             SelectionListener listener= new SelectionListener() {
170                 public void widgetDefaultSelected(SelectionEvent e) {
171                     doWidgetSelected(e);
172                 }
173                 public void widgetSelected(SelectionEvent e) {
174                     doWidgetSelected(e);
175                 }
176             };
177             int nButtons= fButtonNames.length;
178             fButtons= new Button[nButtons];
179
180             for (int i= 0; i < nButtons; i++) {
181                 fButtons[i]= createSelectionButton(i, fButtonComposite, listener);
182             }
183             int nRows= nButtons / fGroupNumberOfColumns;
184             int nFillElements= nRows * fGroupNumberOfColumns - nButtons;
185             for (int i= 0; i < nFillElements; i++) {
186                 createEmptySpace(fButtonComposite);
187             }
188             setSelectionGroupListener(new SelectionButtonGroupListener() {
189                 public void groupChanged(MethodStubsSelectionButtonGroup field) {
190                     field.setEnabled(1, isEnabled() && field.isSelected(0));
191                 }
192             });
193         }
194         return fButtonComposite;
195     }
196
197     /**
198      * Returns a button from the group or <code>null</code> if not yet created.
199      */

200     public Button getSelectionButton(int index) {
201         if (index >= 0 && index < fButtons.length) {
202             return fButtons[index];
203         }
204         return null;
205     }
206     
207     private void doWidgetSelected(SelectionEvent e) {
208         Button button= (Button)e.widget;
209         for (int i= 0; i < fButtons.length; i++) {
210             if (fButtons[i] == button) {
211                 fButtonsSelected[i]= button.getSelection();
212                 dialogFieldChanged();
213                 return;
214             }
215         }
216     }
217     
218     /**
219      * Returns the selection state of a button contained in the group.
220      * @param index of the button
221      */

222     public boolean isSelected(int index) {
223         if (index >= 0 && index < fButtonsSelected.length) {
224             return fButtonsSelected[index] && fButtonsEnabled[index];
225         }
226         return false;
227     }
228     
229     /**
230      * Sets the selection state of a button contained in the group.
231      */

232     public void setSelection(int index, boolean selected) {
233         if (index >= 0 && index < fButtonsSelected.length) {
234             if (fButtonsSelected[index] != selected) {
235                 fButtonsSelected[index]= selected;
236                 if (fButtons != null) {
237                     Button button= fButtons[index];
238                     if (isOkToUse(button) && button.isEnabled()) {
239                         button.setSelection(selected);
240                     }
241                 }
242             }
243         }
244     }
245
246     /**
247      * Returns the enabled state of a button contained in the group.
248      * @param index of the button
249      */

250     public boolean isEnabled(int index) {
251         if (index >= 0 && index < fButtonsEnabled.length) {
252             return fButtonsEnabled[index];
253         }
254         return false;
255     }
256     
257     /**
258      * Sets the selection state of a button contained in the group.
259      */

260     public void setEnabled(int index, boolean enabled) {
261         if (index >= 0 && index < fButtonsEnabled.length) {
262             if (fButtonsEnabled[index] != enabled) {
263                 fButtonsEnabled[index]= enabled;
264                 if (fButtons != null) {
265                     Button button= fButtons[index];
266                     if (isOkToUse(button)) {
267                         button.setEnabled(enabled);
268                         if (!enabled) {
269                             button.setSelection(false);
270                         } else {
271                             button.setSelection(fButtonsSelected[index]);
272                         }
273                     }
274                 }
275             }
276         }
277     }
278
279     protected void updateEnableState() {
280         if (fLabel != null) {
281             fLabel.setEnabled(fEnabled);
282         }
283         if (fButtons != null) {
284             boolean enabled= isEnabled();
285             for (int i= 0; i < fButtons.length; i++) {
286                 Button button= fButtons[i];
287                 if (isOkToUse(button)) {
288                     button.setEnabled(enabled && fButtonsEnabled[i]);
289                 }
290             }
291         }
292     }
293
294     /**
295      * Sets the label of the dialog field.
296      */

297     public void setLabelText(String JavaDoc labeltext) {
298         fLabelText= labeltext;
299     }
300         
301     /**
302      * Defines the listener for this dialog field.
303      */

304     public final void setSelectionGroupListener(SelectionButtonGroupListener listener) {
305         fGroupListener= listener;
306     }
307
308     /**
309      * A dialog field has changed.
310      */

311     public void dialogFieldChanged() {
312         if (fGroupListener != null) {
313             fGroupListener.groupChanged(this);
314         }
315     }
316     
317     /**
318      * Tries to set the focus to the dialog field.
319      * Returns <code>true</code> if the dialog field can take focus.
320      * To be re-implemented by dialog field implementors.
321      */

322     public boolean setFocus() {
323         return false;
324     }
325
326     /**
327      * Posts <code>setFocus</code> to the display event queue.
328      */

329     public void postSetFocusOnDialogField(Display display) {
330         if (display != null) {
331             display.asyncExec(
332                 new Runnable JavaDoc() {
333                     public void run() {
334                         setFocus();
335                     }
336                 }
337             );
338         }
339     }
340     
341     protected static GridData gridDataForLabel(int span) {
342         GridData gd= new GridData();
343         gd.horizontalSpan= span;
344         return gd;
345     }
346     
347     /**
348      * Creates or returns the created label widget.
349      * @param parent The parent composite or <code>null</code> if the widget has
350      * already been created.
351      */

352     public Label getLabelControl(Composite parent) {
353         if (fLabel == null) {
354             assertCompositeNotNull(parent);
355             
356             fLabel= new Label(parent, SWT.LEFT | SWT.WRAP);
357             fLabel.setFont(parent.getFont());
358             fLabel.setEnabled(fEnabled);
359             if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$
360
fLabel.setText(fLabelText);
361             } else {
362                 // XXX: to avoid a 16 pixel wide empty label - revisit
363
fLabel.setText("."); //$NON-NLS-1$
364
fLabel.setVisible(false);
365             }
366         }
367         return fLabel;
368     }
369
370     /**
371      * Creates a spacer control.
372      * @param parent The parent composite
373      */

374     public static Control createEmptySpace(Composite parent) {
375         return createEmptySpace(parent, 1);
376     }
377
378     /**
379      * Creates a spacer control with the given span.
380      * The composite is assumed to have <code>MGridLayout</code> as
381      * layout.
382      * @param parent The parent composite
383      */

384     public static Control createEmptySpace(Composite parent, int span) {
385         return LayoutUtil.createEmptySpace(parent, span);
386     }
387     
388     /**
389      * Tests is the control is not <code>null</code> and not disposed.
390     */

391     protected final boolean isOkToUse(Control control) {
392         return (control != null) && !(control.isDisposed());
393     }
394     
395     // --------- enable / disable management
396

397     /**
398      * Sets the enable state of the dialog field.
399      */

400     public final void setEnabled(boolean enabled) {
401         if (enabled != fEnabled) {
402             fEnabled= enabled;
403             updateEnableState();
404         }
405     }
406     
407     /**
408      * Gets the enable state of the dialog field.
409      */

410     public final boolean isEnabled() {
411         return fEnabled;
412     }
413
414     protected final void assertCompositeNotNull(Composite comp) {
415         Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$
416
}
417     
418     protected final void assertEnoughColumns(int nColumns) {
419         Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); //$NON-NLS-1$
420
}
421 }
422
Popular Tags