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