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 net.sourceforge.phpdt.internal.ui.util.SWTUtil;
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.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
24 * Dialog Field containing a single button such as a radio or checkbox button.
26 public class SelectionButtonDialogField extends DialogField {
28 private Button fButton;
30 private boolean fIsSelected;
32 private DialogField[] fAttachedDialogFields;
34 private int fButtonStyle;
37 * Creates a selection button. Allowed button styles: SWT.RADIO, SWT.CHECK,
38 * SWT.TOGGLE, SWT.PUSH
40 public SelectionButtonDialogField(int buttonStyle) {
43 fAttachedDialogFields = null;
44 fButtonStyle = buttonStyle;
48 * Attaches a field to the selection state of the selection button. The
49 * attached field will be disabled if the selection button is not selected.
51 public void attachDialogField(DialogField dialogField) {
52 attachDialogFields(new DialogField[] { dialogField });
56 * Attaches fields to the selection state of the selection button. The
57 * attached fields will be disabled if the selection button is not selected.
59 public void attachDialogFields(DialogField[] dialogFields) {
60 fAttachedDialogFields = dialogFields;
61 for (int i = 0; i < dialogFields.length; i++) {
62 dialogFields[i].setEnabled(fIsSelected);
67 * Returns <code>true</code> is teh gived field is attached to the
70 public boolean isAttached(DialogField editor) {
71 if (fAttachedDialogFields != null) {
72 for (int i = 0; i < fAttachedDialogFields.length; i++) {
73 if (fAttachedDialogFields[i] == editor) {
81 // ------- layout helpers
84 * @see DialogField#doFillIntoGrid
86 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
87 assertEnoughColumns(nColumns);
89 Button button = getSelectionButton(parent);
90 GridData gd = new GridData();
91 gd.horizontalSpan = nColumns;
92 gd.horizontalAlignment = GridData.FILL;
93 if (fButtonStyle == SWT.PUSH) {
94 gd.heightHint = SWTUtil.getButtonHeightHint(button);
95 gd.widthHint = SWTUtil.getButtonWidthHint(button);
98 button.setLayoutData(gd);
100 return new Control[] { button };
104 * @see DialogField#getNumberOfControls
106 public int getNumberOfControls() {
110 // ------- ui creation
113 * Returns the selection button widget. When called the first time, the
114 * widget will be created.
117 * parent composite when called the first time, or
118 * <code>null</code> after.
120 public Button getSelectionButton(Composite group) {
121 if (fButton == null) {
122 assertCompositeNotNull(group);
124 fButton = new Button(group, fButtonStyle);
125 fButton.setFont(group.getFont());
126 fButton.setText(fLabelText);
127 fButton.setEnabled(isEnabled());
128 fButton.setSelection(fIsSelected);
129 fButton.addSelectionListener(new SelectionListener() {
130 public void widgetDefaultSelected(SelectionEvent e) {
134 public void widgetSelected(SelectionEvent e) {
142 private void doWidgetSelected(SelectionEvent e) {
143 if (isOkToUse(fButton)) {
144 changeValue(fButton.getSelection());
148 private void changeValue(boolean newState) {
149 if (fIsSelected != newState) {
150 fIsSelected = newState;
151 if (fAttachedDialogFields != null) {
152 boolean focusSet = false;
153 for (int i = 0; i < fAttachedDialogFields.length; i++) {
154 fAttachedDialogFields[i].setEnabled(fIsSelected);
155 if (fIsSelected && !focusSet) {
156 focusSet = fAttachedDialogFields[i].setFocus();
160 dialogFieldChanged();
161 } else if (fButtonStyle == SWT.PUSH) {
162 dialogFieldChanged();
166 // ------ model access
169 * Returns the selection state of the button.
171 public boolean isSelected() {
176 * Sets the selection state of the button.
178 public void setSelection(boolean selected) {
179 changeValue(selected);
180 if (isOkToUse(fButton)) {
181 fButton.setSelection(selected);
185 // ------ enable / disable management
188 * @see DialogField#updateEnableState
190 protected void updateEnableState() {
191 super.updateEnableState();
192 if (isOkToUse(fButton)) {
193 fButton.setEnabled(isEnabled());