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;
14 //import org.eclipse.jface.text.Assert;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Group;
25 import org.eclipse.swt.widgets.Label;
28 * Dialog field describing a group with buttons (Checkboxes, radio buttons..)
30 public class SelectionButtonDialogFieldGroup extends DialogField {
32 private Composite fButtonComposite;
34 private Button[] fButtons;
36 private String[] fButtonNames;
38 private boolean[] fButtonsSelected;
40 private boolean[] fButtonsEnabled;
42 private int fGroupBorderStyle;
44 private int fGroupNumberOfColumns;
46 private int fButtonsStyle;
49 * Creates a group without border.
51 public SelectionButtonDialogFieldGroup(int buttonsStyle,
52 String[] buttonNames, int nColumns) {
53 this(buttonsStyle, buttonNames, nColumns, SWT.NONE);
57 * Creates a group with border (label in border). Accepted button styles
58 * are: SWT.RADIO, SWT.CHECK, SWT.TOGGLE For border styles see
61 public SelectionButtonDialogFieldGroup(int buttonsStyle,
62 String[] buttonNames, int nColumns, int borderStyle) {
65 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK
66 || buttonsStyle == SWT.TOGGLE);
67 fButtonNames = buttonNames;
69 int nButtons = buttonNames.length;
70 fButtonsSelected = new boolean[nButtons];
71 fButtonsEnabled = new boolean[nButtons];
72 for (int i = 0; i < nButtons; i++) {
73 fButtonsSelected[i] = false;
74 fButtonsEnabled[i] = true;
76 if (fButtonsStyle == SWT.RADIO) {
77 fButtonsSelected[0] = true;
80 fGroupBorderStyle = borderStyle;
81 fGroupNumberOfColumns = (nColumns <= 0) ? nButtons : nColumns;
83 fButtonsStyle = buttonsStyle;
87 // ------- layout helpers
90 * @see DialogField#doFillIntoGrid
92 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
93 assertEnoughColumns(nColumns);
95 if (fGroupBorderStyle == SWT.NONE) {
96 Label label = getLabelControl(parent);
97 label.setLayoutData(gridDataForLabel(1));
99 Composite buttonsgroup = getSelectionButtonsGroup(parent);
100 GridData gd = new GridData();
101 gd.horizontalSpan = nColumns - 1;
102 buttonsgroup.setLayoutData(gd);
104 return new Control[] { label, buttonsgroup };
106 Composite buttonsgroup = getSelectionButtonsGroup(parent);
107 GridData gd = new GridData();
108 gd.horizontalSpan = nColumns;
109 buttonsgroup.setLayoutData(gd);
111 return new Control[] { buttonsgroup };
116 * @see DialogField#doFillIntoGrid
118 public int getNumberOfControls() {
119 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1;
122 // ------- ui creation
124 private Button createSelectionButton(int index, Composite group,
125 SelectionListener listener) {
126 Button button = new Button(group, fButtonsStyle | SWT.LEFT);
127 button.setFont(group.getFont());
128 button.setText(fButtonNames[index]);
129 button.setEnabled(isEnabled() && fButtonsEnabled[index]);
130 button.setSelection(fButtonsSelected[index]);
131 button.addSelectionListener(listener);
132 button.setLayoutData(new GridData());
137 * Returns the group widget. When called the first time, the widget will be
141 * parent composite when called the first time, or
142 * <code>null</code> after.
144 public Composite getSelectionButtonsGroup(Composite parent) {
145 if (fButtonComposite == null) {
146 assertCompositeNotNull(parent);
148 GridLayout layout = new GridLayout();
149 layout.makeColumnsEqualWidth = true;
150 layout.numColumns = fGroupNumberOfColumns;
152 if (fGroupBorderStyle != SWT.NONE) {
153 Group group = new Group(parent, fGroupBorderStyle);
154 if (fLabelText != null && fLabelText.length() > 0) {
155 group.setText(fLabelText);
157 fButtonComposite = group;
159 fButtonComposite = new Composite(parent, SWT.NULL);
160 layout.marginHeight = 0;
161 layout.marginWidth = 0;
164 fButtonComposite.setLayout(layout);
166 SelectionListener listener = new SelectionListener() {
167 public void widgetDefaultSelected(SelectionEvent e) {
171 public void widgetSelected(SelectionEvent e) {
175 int nButtons = fButtonNames.length;
176 fButtons = new Button[nButtons];
177 for (int i = 0; i < nButtons; i++) {
178 fButtons[i] = createSelectionButton(i, fButtonComposite,
181 int nRows = nButtons / fGroupNumberOfColumns;
182 int nFillElements = nRows * fGroupNumberOfColumns - nButtons;
183 for (int i = 0; i < nFillElements; i++) {
184 createEmptySpace(fButtonComposite);
187 return fButtonComposite;
191 * Returns a button from the group or <code>null</code> if not yet
194 // public Button getSelectionButton(int index) {
195 // if (index >= 0 && index < fButtons.length) {
196 // return fButtons[index];
201 private void doWidgetSelected(SelectionEvent e) {
202 Button button = (Button) e.widget;
203 for (int i = 0; i < fButtons.length; i++) {
204 if (fButtons[i] == button) {
205 fButtonsSelected[i] = button.getSelection();
206 dialogFieldChanged();
212 // ------ model access
215 * Returns the selection state of a button contained in the group.
218 * index of the button
220 public boolean isSelected(int index) {
221 if (index >= 0 && index < fButtonsSelected.length) {
222 return fButtonsSelected[index];
228 * Sets the selection state of a button contained in the group.
230 public void setSelection(int index, boolean selected) {
231 if (index >= 0 && index < fButtonsSelected.length) {
232 if (fButtonsSelected[index] != selected) {
233 fButtonsSelected[index] = selected;
234 if (fButtons != null) {
235 Button button = fButtons[index];
236 if (isOkToUse(button)) {
237 button.setSelection(selected);
244 // ------ enable / disable management
246 protected void updateEnableState() {
247 super.updateEnableState();
248 if (fButtons != null) {
249 boolean enabled = isEnabled();
250 for (int i = 0; i < fButtons.length; i++) {
251 Button button = fButtons[i];
252 if (isOkToUse(button)) {
253 button.setEnabled(enabled && fButtonsEnabled[i]);
260 * Sets the enable state of a button contained in the group.
262 public void enableSelectionButton(int index, boolean enable) {
263 if (index >= 0 && index < fButtonsEnabled.length) {
264 fButtonsEnabled[index] = enable;
265 if (fButtons != null) {
266 Button button = fButtons[index];
267 if (isOkToUse(button)) {
268 button.setEnabled(isEnabled() && enable);