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;
26 * Dialog Field containing a single button such as a radio or checkbox button.
28 public class SelectionButtonDialogField extends DialogField {
30 private Button fButton;
31 private boolean fIsSelected;
32 private DialogField[] fAttachedDialogFields;
33 private int fButtonStyle;
36 * Creates a selection button.
37 * Allowed button styles: SWT.RADIO, SWT.CHECK, SWT.TOGGLE, SWT.PUSH
39 public SelectionButtonDialogField(int buttonStyle) {
42 fAttachedDialogFields= null;
43 fButtonStyle= buttonStyle;
47 * Attaches a field to the selection state of the selection button.
48 * The attached field will be disabled if the selection button is not selected.
50 public void attachDialogField(DialogField dialogField) {
51 attachDialogFields(new DialogField[] { dialogField });
55 * Attaches fields to the selection state of the selection button.
56 * The attached fields will be disabled if the selection button is not selected.
58 public void attachDialogFields(DialogField[] dialogFields) {
59 fAttachedDialogFields= dialogFields;
60 for (int i= 0; i < dialogFields.length; i++) {
61 dialogFields[i].setEnabled(fIsSelected);
66 * Returns <code>true</code> is teh gived field is attached to the selection button.
68 public boolean isAttached(DialogField editor) {
69 if (fAttachedDialogFields != null) {
70 for (int i=0; i < fAttachedDialogFields.length; i++) {
71 if (fAttachedDialogFields[i] == editor) {
79 // ------- layout helpers
82 * @see DialogField#doFillIntoGrid
84 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
85 assertEnoughColumns(nColumns);
87 Button button= getSelectionButton(parent);
88 GridData gd= new GridData();
89 gd.horizontalSpan= nColumns;
90 gd.horizontalAlignment= GridData.FILL;
91 if (fButtonStyle == SWT.PUSH) {
92 gd.heightHint = SWTUtil.getButtonHeigthHint(button);
93 gd.widthHint = SWTUtil.getButtonWidthHint(button);
96 button.setLayoutData(gd);
98 return new Control[] { button };
102 * @see DialogField#getNumberOfControls
104 public int getNumberOfControls() {
108 // ------- ui creation
111 * Returns the selection button widget. When called the first time, the widget will be created.
112 * @param The parent composite when called the first time, or <code>null</code>
115 public Button getSelectionButton(Composite group) {
116 if (fButton == null) {
117 assertCompositeNotNull(group);
119 fButton= new Button(group, fButtonStyle);
120 fButton.setFont(group.getFont());
121 fButton.setText(fLabelText);
122 fButton.setEnabled(isEnabled());
123 fButton.setSelection(fIsSelected);
124 fButton.addSelectionListener(new SelectionListener() {
125 public void widgetDefaultSelected(SelectionEvent e) {
128 public void widgetSelected(SelectionEvent e) {
136 private void doWidgetSelected(SelectionEvent e) {
137 if (isOkToUse(fButton)) {
138 changeValue(fButton.getSelection());
142 private void changeValue(boolean newState) {
143 if (fIsSelected != newState) {
144 fIsSelected= newState;
145 if (fAttachedDialogFields != null) {
146 boolean focusSet= false;
147 for (int i= 0; i < fAttachedDialogFields.length; i++) {
148 fAttachedDialogFields[i].setEnabled(fIsSelected);
149 if (fIsSelected && !focusSet) {
150 focusSet= fAttachedDialogFields[i].setFocus();
154 dialogFieldChanged();
155 } else if (fButtonStyle == SWT.PUSH) {
156 dialogFieldChanged();
160 // ------ model access
163 * Returns the selection state of the button.
165 public boolean isSelected() {
170 * Sets the selection state of the button.
172 public void setSelection(boolean selected) {
173 changeValue(selected);
174 if (isOkToUse(fButton)) {
175 fButton.setSelection(selected);
179 // ------ enable / disable management
182 * @see DialogField#updateEnableState
184 protected void updateEnableState() {
185 super.updateEnableState();
186 if (isOkToUse(fButton)) {
187 fButton.setEnabled(isEnabled());