1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.swt.widgets.Label;
26 * Dialog field describing a group with buttons (Checkboxes, radio buttons..)
28 public class SelectionButtonDialogFieldGroup extends DialogField {
30 private Composite fButtonComposite;
32 private Button[] fButtons;
33 private String[] fButtonNames;
34 private boolean[] fButtonsSelected;
35 private boolean[] fButtonsEnabled;
37 private int fGroupBorderStyle;
38 private int fGroupNumberOfColumns;
39 private int fButtonsStyle;
42 * Creates a group without border.
44 public SelectionButtonDialogFieldGroup(int buttonsStyle, String[] buttonNames, int nColumns) {
45 this(buttonsStyle, buttonNames, nColumns, SWT.NONE);
50 * Creates a group with border (label in border).
51 * Accepted button styles are: SWT.RADIO, SWT.CHECK, SWT.TOGGLE
52 * For border styles see <code>Group</code>
54 public SelectionButtonDialogFieldGroup(int buttonsStyle, String[] buttonNames, int nColumns, int borderStyle) {
57 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK || buttonsStyle == SWT.TOGGLE);
58 fButtonNames= buttonNames;
60 int nButtons= buttonNames.length;
61 fButtonsSelected= new boolean[nButtons];
62 fButtonsEnabled= new boolean[nButtons];
63 for (int i= 0; i < nButtons; i++) {
64 fButtonsSelected[i]= false;
65 fButtonsEnabled[i]= true;
67 if (fButtonsStyle == SWT.RADIO) {
68 fButtonsSelected[0]= true;
71 fGroupBorderStyle= borderStyle;
72 fGroupNumberOfColumns= (nColumns <= 0) ? nButtons : nColumns;
74 fButtonsStyle= buttonsStyle;
78 // ------- layout helpers
81 * @see DialogField#doFillIntoGrid
83 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
84 assertEnoughColumns(nColumns);
86 if (fGroupBorderStyle == SWT.NONE) {
87 Label label= getLabelControl(parent);
88 label.setLayoutData(gridDataForLabel(1));
90 Composite buttonsgroup= getSelectionButtonsGroup(parent);
91 GridData gd= new GridData();
92 gd.horizontalSpan= nColumns - 1;
93 buttonsgroup.setLayoutData(gd);
95 return new Control[] { label, buttonsgroup };
97 Composite buttonsgroup= getSelectionButtonsGroup(parent);
98 GridData gd= new GridData();
99 gd.horizontalSpan= nColumns;
100 buttonsgroup.setLayoutData(gd);
102 return new Control[] { buttonsgroup };
107 * @see DialogField#doFillIntoGrid
109 public int getNumberOfControls() {
110 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1;
113 // ------- ui creation
115 private Button createSelectionButton(int index, Composite group, SelectionListener listener) {
116 Button button= new Button(group, fButtonsStyle | SWT.LEFT);
117 button.setFont(group.getFont());
118 button.setText(fButtonNames[index]);
119 button.setEnabled(isEnabled() && fButtonsEnabled[index]);
120 button.setSelection(fButtonsSelected[index]);
121 button.addSelectionListener(listener);
122 button.setLayoutData(new GridData());
127 * Returns the group widget. When called the first time, the widget will be created.
128 * @param The parent composite when called the first time, or <code>null</code>
131 public Composite getSelectionButtonsGroup(Composite parent) {
132 if (fButtonComposite == null) {
133 assertCompositeNotNull(parent);
135 GridLayout layout= new GridLayout();
136 layout.makeColumnsEqualWidth= true;
137 layout.numColumns= fGroupNumberOfColumns;
139 if (fGroupBorderStyle != SWT.NONE) {
140 Group group= new Group(parent, fGroupBorderStyle);
141 if (fLabelText != null && fLabelText.length() > 0) {
142 group.setText(fLabelText);
144 fButtonComposite= group;
146 fButtonComposite= new Composite(parent, SWT.NULL);
147 layout.marginHeight= 0;
148 layout.marginWidth= 0;
151 fButtonComposite.setLayout(layout);
153 SelectionListener listener= new SelectionListener() {
154 public void widgetDefaultSelected(SelectionEvent e) {
157 public void widgetSelected(SelectionEvent e) {
161 int nButtons= fButtonNames.length;
162 fButtons= new Button[nButtons];
163 for (int i= 0; i < nButtons; i++) {
164 fButtons[i]= createSelectionButton(i, fButtonComposite, listener);
166 int nRows= nButtons / fGroupNumberOfColumns;
167 int nFillElements= nRows * fGroupNumberOfColumns - nButtons;
168 for (int i= 0; i < nFillElements; i++) {
169 createEmptySpace(fButtonComposite);
172 return fButtonComposite;
176 * Returns a button from the group or <code>null</code> if not yet created.
178 public Button getSelectionButton(int index) {
179 if (index >= 0 && index < fButtons.length) {
180 return fButtons[index];
185 private void doWidgetSelected(SelectionEvent e) {
186 Button button= (Button)e.widget;
187 for (int i= 0; i < fButtons.length; i++) {
188 if (fButtons[i] == button) {
189 fButtonsSelected[i]= button.getSelection();
190 dialogFieldChanged();
196 // ------ model access
199 * Returns the selection state of a button contained in the group.
200 * @param The index of the button
202 public boolean isSelected(int index) {
203 if (index >= 0 && index < fButtonsSelected.length) {
204 return fButtonsSelected[index];
210 * Sets the selection state of a button contained in the group.
212 public void setSelection(int index, boolean selected) {
213 if (index >= 0 && index < fButtonsSelected.length) {
214 if (fButtonsSelected[index] != selected) {
215 fButtonsSelected[index]= selected;
216 if (fButtons != null) {
217 Button button= fButtons[index];
218 if (isOkToUse(button)) {
219 button.setSelection(selected);
226 // ------ enable / disable management
228 protected void updateEnableState() {
229 super.updateEnableState();
230 if (fButtons != null) {
231 boolean enabled= isEnabled();
232 for (int i= 0; i < fButtons.length; i++) {
233 Button button= fButtons[i];
234 if (isOkToUse(button)) {
235 button.setEnabled(enabled && fButtonsEnabled[i]);
242 * Sets the enable state of a button contained in the group.
244 public void enableSelectionButton(int index, boolean enable) {
245 if (index >= 0 && index < fButtonsEnabled.length) {
246 fButtonsEnabled[index]= enable;
247 if (fButtons != null) {
248 Button button= fButtons[index];
249 if (isOkToUse(button)) {
250 button.setEnabled(isEnabled() && enable);