Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / SelectionButtonDialogField.java
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
25 /**
26  * Dialog Field containing a single button such as a radio or checkbox button.
27  */
28 public class SelectionButtonDialogField extends DialogField {
29         
30         private Button fButton;
31         private boolean fIsSelected;
32         private DialogField[] fAttachedDialogFields;
33         private int fButtonStyle;
34
35         /**
36          * Creates a selection button.
37          * Allowed button styles: SWT.RADIO, SWT.CHECK, SWT.TOGGLE, SWT.PUSH
38          */
39         public SelectionButtonDialogField(int buttonStyle) {
40                 super();
41                 fIsSelected= false;
42                 fAttachedDialogFields= null;
43                 fButtonStyle= buttonStyle;
44         }
45         
46         /**
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.
49          */
50         public void attachDialogField(DialogField dialogField) {
51                 attachDialogFields(new DialogField[] { dialogField });
52         }
53
54         /**
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.
57          */     
58         public void attachDialogFields(DialogField[] dialogFields) {
59                 fAttachedDialogFields= dialogFields;
60                 for (int i= 0; i < dialogFields.length; i++) {
61                         dialogFields[i].setEnabled(fIsSelected);
62                 }
63         }       
64         
65         /**
66          * Returns <code>true</code> is  teh gived field is attached to the selection button.
67          */
68         public boolean isAttached(DialogField editor) {
69                 if (fAttachedDialogFields != null) {
70                         for (int i=0; i < fAttachedDialogFields.length; i++) {
71                                 if (fAttachedDialogFields[i] == editor) {
72                                         return true;
73                                 }
74                         }
75                 }
76                 return false;
77         }
78         
79         // ------- layout helpers
80         
81         /*
82          * @see DialogField#doFillIntoGrid
83          */
84         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
85                 assertEnoughColumns(nColumns);
86                 
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);
94                 }                       
95                 
96                 button.setLayoutData(gd);
97                 
98                 return new Control[] { button };
99         }       
100
101         /*
102          * @see DialogField#getNumberOfControls
103          */     
104         public int getNumberOfControls() {
105                 return 1;       
106         }       
107         
108         // ------- ui creation                  
109
110         /**
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>
113          * after.
114          */             
115         public Button getSelectionButton(Composite group) {
116                 if (fButton == null) {
117                         assertCompositeNotNull(group);
118                         
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) {
126                                         doWidgetSelected(e);
127                                 }
128                                 public void widgetSelected(SelectionEvent e) {
129                                         doWidgetSelected(e);
130                                 }
131                         });                             
132                 }
133                 return fButton;
134         }
135         
136         private void doWidgetSelected(SelectionEvent e) {
137                 if (isOkToUse(fButton)) {
138                         changeValue(fButton.getSelection());
139                 }
140         }       
141         
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();
151                                         }
152                                 }
153                         }
154                         dialogFieldChanged();
155                 } else if (fButtonStyle == SWT.PUSH) {
156                         dialogFieldChanged();
157                 }
158         }               
159
160         // ------ model access  
161         
162         /**
163          * Returns the selection state of the button.
164          */
165         public boolean isSelected() {
166                 return fIsSelected;
167         }
168
169         /**
170          * Sets the selection state of the button.
171          */     
172         public void setSelection(boolean selected) {
173                 changeValue(selected);
174                 if (isOkToUse(fButton)) {
175                         fButton.setSelection(selected);
176                 }
177         }
178
179         // ------ enable / disable management
180
181         /*
182          * @see DialogField#updateEnableState
183          */     
184         protected void updateEnableState() {
185                 super.updateEnableState();
186                 if (isOkToUse(fButton)) {
187                         fButton.setEnabled(isEnabled());
188                 }               
189         }
190         
191         
192         
193                 
194 }