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