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;
34 private String[] fButtonNames;
36 private boolean[] fButtonsSelected;
38 private boolean[] fButtonsEnabled;
40 private int fGroupBorderStyle;
42 private int fGroupNumberOfColumns;
44 private int fButtonsStyle;
47 * Creates a group without border.
49 public SelectionButtonDialogFieldGroup(int buttonsStyle,
50 String[] buttonNames, int nColumns) {
51 this(buttonsStyle, buttonNames, nColumns, SWT.NONE);
55 * Creates a group with border (label in border). Accepted button styles
56 * are: SWT.RADIO, SWT.CHECK, SWT.TOGGLE For border styles see
59 public SelectionButtonDialogFieldGroup(int buttonsStyle,
60 String[] buttonNames, int nColumns, int borderStyle) {
63 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK
64 || buttonsStyle == SWT.TOGGLE);
65 fButtonNames = buttonNames;
67 int nButtons = buttonNames.length;
68 fButtonsSelected = new boolean[nButtons];
69 fButtonsEnabled = new boolean[nButtons];
70 for (int i = 0; i < nButtons; i++) {
71 fButtonsSelected[i] = false;
72 fButtonsEnabled[i] = true;
74 if (fButtonsStyle == SWT.RADIO) {
75 fButtonsSelected[0] = true;
78 fGroupBorderStyle = borderStyle;
79 fGroupNumberOfColumns = (nColumns <= 0) ? nButtons : nColumns;
81 fButtonsStyle = buttonsStyle;
85 // ------- layout helpers
88 * @see DialogField#doFillIntoGrid
90 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
91 assertEnoughColumns(nColumns);
93 if (fGroupBorderStyle == SWT.NONE) {
94 Label label = getLabelControl(parent);
95 label.setLayoutData(gridDataForLabel(1));
97 Composite buttonsgroup = getSelectionButtonsGroup(parent);
98 GridData gd = new GridData();
99 gd.horizontalSpan = nColumns - 1;
100 buttonsgroup.setLayoutData(gd);
102 return new Control[] { label, buttonsgroup };
104 Composite buttonsgroup = getSelectionButtonsGroup(parent);
105 GridData gd = new GridData();
106 gd.horizontalSpan = nColumns;
107 buttonsgroup.setLayoutData(gd);
109 return new Control[] { buttonsgroup };
114 * @see DialogField#doFillIntoGrid
116 public int getNumberOfControls() {
117 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1;
120 // ------- ui creation
122 private Button createSelectionButton(int index, Composite group,
123 SelectionListener listener) {
124 Button button = new Button(group, fButtonsStyle | SWT.LEFT);
125 button.setFont(group.getFont());
126 button.setText(fButtonNames[index]);
127 button.setEnabled(isEnabled() && fButtonsEnabled[index]);
128 button.setSelection(fButtonsSelected[index]);
129 button.addSelectionListener(listener);
130 button.setLayoutData(new GridData());
135 * Returns the group widget. When called the first time, the widget will be
139 * parent composite when called the first time, or
140 * <code>null</code> after.
142 public Composite getSelectionButtonsGroup(Composite parent) {
143 if (fButtonComposite == null) {
144 assertCompositeNotNull(parent);
146 GridLayout layout = new GridLayout();
147 layout.makeColumnsEqualWidth = true;
148 layout.numColumns = fGroupNumberOfColumns;
150 if (fGroupBorderStyle != SWT.NONE) {
151 Group group = new Group(parent, fGroupBorderStyle);
152 if (fLabelText != null && fLabelText.length() > 0) {
153 group.setText(fLabelText);
155 fButtonComposite = group;
157 fButtonComposite = new Composite(parent, SWT.NULL);
158 layout.marginHeight = 0;
159 layout.marginWidth = 0;
162 fButtonComposite.setLayout(layout);
164 SelectionListener listener = new SelectionListener() {
165 public void widgetDefaultSelected(SelectionEvent e) {
169 public void widgetSelected(SelectionEvent e) {
173 int nButtons = fButtonNames.length;
174 fButtons = new Button[nButtons];
175 for (int i = 0; i < nButtons; i++) {
176 fButtons[i] = createSelectionButton(i, fButtonComposite,
179 int nRows = nButtons / fGroupNumberOfColumns;
180 int nFillElements = nRows * fGroupNumberOfColumns - nButtons;
181 for (int i = 0; i < nFillElements; i++) {
182 createEmptySpace(fButtonComposite);
185 return fButtonComposite;
189 * Returns a button from the group or <code>null</code> if not yet
192 public Button getSelectionButton(int index) {
193 if (index >= 0 && index < fButtons.length) {
194 return fButtons[index];
199 private void doWidgetSelected(SelectionEvent e) {
200 Button button = (Button) e.widget;
201 for (int i = 0; i < fButtons.length; i++) {
202 if (fButtons[i] == button) {
203 fButtonsSelected[i] = button.getSelection();
204 dialogFieldChanged();
210 // ------ model access
213 * Returns the selection state of a button contained in the group.
216 * index of the button
218 public boolean isSelected(int index) {
219 if (index >= 0 && index < fButtonsSelected.length) {
220 return fButtonsSelected[index];
226 * Sets the selection state of a button contained in the group.
228 public void setSelection(int index, boolean selected) {
229 if (index >= 0 && index < fButtonsSelected.length) {
230 if (fButtonsSelected[index] != selected) {
231 fButtonsSelected[index] = selected;
232 if (fButtons != null) {
233 Button button = fButtons[index];
234 if (isOkToUse(button)) {
235 button.setSelection(selected);
242 // ------ enable / disable management
244 protected void updateEnableState() {
245 super.updateEnableState();
246 if (fButtons != null) {
247 boolean enabled = isEnabled();
248 for (int i = 0; i < fButtons.length; i++) {
249 Button button = fButtons[i];
250 if (isOkToUse(button)) {
251 button.setEnabled(enabled && fButtonsEnabled[i]);
258 * Sets the enable state of a button contained in the group.
260 public void enableSelectionButton(int index, boolean enable) {
261 if (index >= 0 && index < fButtonsEnabled.length) {
262 fButtonsEnabled[index] = enable;
263 if (fButtons != null) {
264 Button button = fButtons[index];
265 if (isOkToUse(button)) {
266 button.setEnabled(isEnabled() && enable);