Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / SelectionButtonDialogFieldGroup.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 org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.swt.widgets.Label;
24
25 /**
26  * Dialog field describing a group with buttons (Checkboxes, radio buttons..)
27  */
28 public class SelectionButtonDialogFieldGroup extends DialogField {
29         
30         private Composite fButtonComposite;
31         
32         private Button[] fButtons;
33         private String[] fButtonNames;
34         private boolean[] fButtonsSelected;
35         private boolean[] fButtonsEnabled;
36         
37         private int fGroupBorderStyle;
38         private int fGroupNumberOfColumns;
39         private int fButtonsStyle;      
40                 
41         /**
42          * Creates a group without border.
43          */
44         public SelectionButtonDialogFieldGroup(int buttonsStyle, String[] buttonNames, int nColumns) {
45                 this(buttonsStyle, buttonNames, nColumns, SWT.NONE);            
46         }       
47         
48         
49         /**
50          * Creates a group with border (label in border).
51          * Accepted button styles are: SWT.RADIO, SWT.CHECK, SWT.TOGGLE
52          * For border styles see <code>Group</code>
53          */     
54         public SelectionButtonDialogFieldGroup(int buttonsStyle, String[] buttonNames, int nColumns, int borderStyle) {
55                 super();
56                 
57                 Assert.isTrue(buttonsStyle == SWT.RADIO || buttonsStyle == SWT.CHECK || buttonsStyle == SWT.TOGGLE);
58                 fButtonNames= buttonNames;
59                 
60                 int nButtons= buttonNames.length;
61                 fButtonsSelected= new boolean[nButtons];
62                 fButtonsEnabled= new boolean[nButtons];
63                 for (int i= 0; i < nButtons; i++) {
64                         fButtonsSelected[i]= false;
65                         fButtonsEnabled[i]= true;
66                 }
67                 if (fButtonsStyle == SWT.RADIO) {
68                         fButtonsSelected[0]= true;
69                 }
70                 
71                 fGroupBorderStyle= borderStyle;
72                 fGroupNumberOfColumns= (nColumns <= 0) ? nButtons : nColumns;
73                 
74                 fButtonsStyle= buttonsStyle;
75                 
76         }
77         
78         // ------- layout helpers
79                 
80         /*
81          * @see DialogField#doFillIntoGrid
82          */
83         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
84                 assertEnoughColumns(nColumns);
85                                 
86                 if (fGroupBorderStyle == SWT.NONE) {
87                         Label label= getLabelControl(parent);
88                         label.setLayoutData(gridDataForLabel(1));
89                 
90                         Composite buttonsgroup= getSelectionButtonsGroup(parent);
91                         GridData gd= new GridData();
92                         gd.horizontalSpan= nColumns - 1;
93                         buttonsgroup.setLayoutData(gd);
94                         
95                         return new Control[] { label, buttonsgroup };
96                 } else {
97                         Composite buttonsgroup= getSelectionButtonsGroup(parent);
98                         GridData gd= new GridData();
99                         gd.horizontalSpan= nColumns;
100                         buttonsgroup.setLayoutData(gd);
101                 
102                         return new Control[] { buttonsgroup };
103                 }
104         }       
105
106         /*
107          * @see DialogField#doFillIntoGrid
108          */     
109         public int getNumberOfControls() {
110                 return (fGroupBorderStyle == SWT.NONE) ? 2 : 1;
111         }
112         
113         // ------- ui creation
114         
115         private Button createSelectionButton(int index, Composite group, SelectionListener listener) {
116                 Button button= new Button(group, fButtonsStyle | SWT.LEFT);
117                 button.setFont(group.getFont());                        
118                 button.setText(fButtonNames[index]);
119                 button.setEnabled(isEnabled() && fButtonsEnabled[index]);
120                 button.setSelection(fButtonsSelected[index]);
121                 button.addSelectionListener(listener);
122                 button.setLayoutData(new GridData());
123                 return button;
124         }
125
126         /**
127          * Returns the group widget. When called the first time, the widget will be created.
128          * @param The parent composite when called the first time, or <code>null</code>
129          * after.
130          */
131         public Composite getSelectionButtonsGroup(Composite parent) {
132                 if (fButtonComposite == null) {
133                         assertCompositeNotNull(parent);
134                         
135                         GridLayout layout= new GridLayout();
136                         layout.makeColumnsEqualWidth= true;
137                         layout.numColumns= fGroupNumberOfColumns;                       
138                         
139                         if (fGroupBorderStyle != SWT.NONE) {
140                                 Group group= new Group(parent, fGroupBorderStyle);
141                                 if (fLabelText != null && fLabelText.length() > 0) {
142                                         group.setText(fLabelText);
143                                 }
144                                 fButtonComposite= group;
145                         } else {
146                                 fButtonComposite= new Composite(parent, SWT.NULL);
147                                 layout.marginHeight= 0;
148                                 layout.marginWidth= 0;
149                         }
150
151                         fButtonComposite.setLayout(layout);
152                         
153                         SelectionListener listener= new SelectionListener() {
154                                 public void widgetDefaultSelected(SelectionEvent e) {
155                                         doWidgetSelected(e);
156                                 }
157                                 public void widgetSelected(SelectionEvent e) {
158                                         doWidgetSelected(e);
159                                 }
160                         };      
161                         int nButtons= fButtonNames.length;
162                         fButtons= new Button[nButtons]; 
163                         for (int i= 0; i < nButtons; i++) {
164                                 fButtons[i]= createSelectionButton(i, fButtonComposite, listener);
165                         }
166                         int nRows= nButtons / fGroupNumberOfColumns;
167                         int nFillElements= nRows * fGroupNumberOfColumns - nButtons;
168                         for (int i= 0; i < nFillElements; i++) {
169                                 createEmptySpace(fButtonComposite);
170                         }
171                 }
172                 return fButtonComposite;
173         }
174
175         /**
176          * Returns a button from the group or <code>null</code> if not yet created.
177          */     
178         public Button getSelectionButton(int index) {
179                 if (index >= 0 && index < fButtons.length) {
180                         return fButtons[index];
181                 }
182                 return null;
183         }
184         
185         private void doWidgetSelected(SelectionEvent e) {
186                 Button button= (Button)e.widget;
187                 for (int i= 0; i < fButtons.length; i++) {
188                         if (fButtons[i] == button) {
189                                 fButtonsSelected[i]= button.getSelection();
190                                 dialogFieldChanged();
191                                 return;
192                         }
193                 }
194         }       
195         
196         // ------ model access  
197
198         /**
199          * Returns the selection state of a button contained in the group.
200          * @param The index of the button
201          */
202         public boolean isSelected(int index) {
203                 if (index >= 0 && index < fButtonsSelected.length) {
204                         return fButtonsSelected[index];
205                 }
206                 return false;
207         }
208         
209         /**
210          * Sets the selection state of a button contained in the group.
211          */
212         public void setSelection(int index, boolean selected) {
213                 if (index >= 0 && index < fButtonsSelected.length) {
214                         if (fButtonsSelected[index] != selected) {
215                                 fButtonsSelected[index]= selected;
216                                 if (fButtons != null) {
217                                         Button button= fButtons[index];
218                                         if (isOkToUse(button)) {
219                                                 button.setSelection(selected);
220                                         }
221                                 }
222                         }
223                 }
224         }
225
226         // ------ enable / disable management
227         
228         protected void updateEnableState() {
229                 super.updateEnableState();
230                 if (fButtons != null) {
231                         boolean enabled= isEnabled();
232                         for (int i= 0; i < fButtons.length; i++) {
233                                 Button button= fButtons[i];
234                                 if (isOkToUse(button)) {
235                                         button.setEnabled(enabled && fButtonsEnabled[i]);
236                                 }
237                         }
238                 }
239         }
240         
241         /**
242          * Sets the enable state of a button contained in the group.
243          */     
244         public void enableSelectionButton(int index, boolean enable) {
245                 if (index >= 0 && index < fButtonsEnabled.length) {
246                         fButtonsEnabled[index]= enable;
247                         if (fButtons != null) {
248                                 Button button= fButtons[index];
249                                 if (isOkToUse(button)) {
250                                         button.setEnabled(isEnabled() && enable);
251                                 }
252                         }
253                 }
254         }       
255 }