57b9cd23a3fa68135971b8560f34f711f05b8bc6
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
13 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
14
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;
22
23 /**
24  * Dialog Field containing a single button such as a radio or checkbox button.
25  */
26 public class SelectionButtonDialogField extends DialogField {
27
28         private Button fButton;
29
30         private boolean fIsSelected;
31
32         private DialogField[] fAttachedDialogFields;
33
34         private int fButtonStyle;
35
36         /**
37          * Creates a selection button. Allowed button styles: SWT.RADIO, SWT.CHECK,
38          * SWT.TOGGLE, SWT.PUSH
39          */
40         public SelectionButtonDialogField(int buttonStyle) {
41                 super();
42                 fIsSelected = false;
43                 fAttachedDialogFields = null;
44                 fButtonStyle = buttonStyle;
45         }
46
47         /**
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.
50          */
51         public void attachDialogField(DialogField dialogField) {
52                 attachDialogFields(new DialogField[] { dialogField });
53         }
54
55         /**
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.
58          */
59         public void attachDialogFields(DialogField[] dialogFields) {
60                 fAttachedDialogFields = dialogFields;
61                 for (int i = 0; i < dialogFields.length; i++) {
62                         dialogFields[i].setEnabled(fIsSelected);
63                 }
64         }
65
66         /**
67          * Returns <code>true</code> is teh gived field is attached to the
68          * selection button.
69          */
70         public boolean isAttached(DialogField editor) {
71                 if (fAttachedDialogFields != null) {
72                         for (int i = 0; i < fAttachedDialogFields.length; i++) {
73                                 if (fAttachedDialogFields[i] == editor) {
74                                         return true;
75                                 }
76                         }
77                 }
78                 return false;
79         }
80
81         // ------- layout helpers
82
83         /*
84          * @see DialogField#doFillIntoGrid
85          */
86         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
87                 assertEnoughColumns(nColumns);
88
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);
96                 }
97
98                 button.setLayoutData(gd);
99
100                 return new Control[] { button };
101         }
102
103         /*
104          * @see DialogField#getNumberOfControls
105          */
106         public int getNumberOfControls() {
107                 return 1;
108         }
109
110         // ------- ui creation
111
112         /**
113          * Returns the selection button widget. When called the first time, the
114          * widget will be created.
115          * 
116          * @param The
117          *            parent composite when called the first time, or
118          *            <code>null</code> after.
119          */
120         public Button getSelectionButton(Composite group) {
121                 if (fButton == null) {
122                         assertCompositeNotNull(group);
123
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) {
131                                         doWidgetSelected(e);
132                                 }
133
134                                 public void widgetSelected(SelectionEvent e) {
135                                         doWidgetSelected(e);
136                                 }
137                         });
138                 }
139                 return fButton;
140         }
141
142         private void doWidgetSelected(SelectionEvent e) {
143                 if (isOkToUse(fButton)) {
144                         changeValue(fButton.getSelection());
145                 }
146         }
147
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();
157                                         }
158                                 }
159                         }
160                         dialogFieldChanged();
161                 } else if (fButtonStyle == SWT.PUSH) {
162                         dialogFieldChanged();
163                 }
164         }
165
166         // ------ model access
167
168         /**
169          * Returns the selection state of the button.
170          */
171         public boolean isSelected() {
172                 return fIsSelected;
173         }
174
175         /**
176          * Sets the selection state of the button.
177          */
178         public void setSelection(boolean selected) {
179                 changeValue(selected);
180                 if (isOkToUse(fButton)) {
181                         fButton.setSelection(selected);
182                 }
183         }
184
185         // ------ enable / disable management
186
187         /*
188          * @see DialogField#updateEnableState
189          */
190         protected void updateEnableState() {
191                 super.updateEnableState();
192                 if (isOkToUse(fButton)) {
193                         fButton.setEnabled(isEnabled());
194                 }
195         }
196
197 }