Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7db537b68fefebe10608a072617c34da997902a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.dialogfield;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * This will display a labal and a group of radio buttons. The group of radio
 * buttons will be layed out horizontally use a RowLayout, and them as a group
 * will use one cell in the GridLayout.
 * 
 * Whenever the radios selection change will fire both dialogFieldChanged() and
 * dialogFieldApplied() event.
 * 
 * @author mengbo
 */
public class RadiosDialogField extends DialogFieldBase {
	final static private String INDEXKEY = "INDEX"; //$NON-NLS-1$

	private Composite _group;

	private String[] _items;

	private Button[] _button;

	private int _selectIndex = -1;

	private boolean _fireEvent = true;

	/**
	 * Default constructor
	 */
	public RadiosDialogField() {
		super();
	}

	/**
	 * this method must be called before create control
	 * 
	 * @param items
	 */
	public void setItems(String[] items) {
		_items = items;
		_button = new Button[_items.length];
	}

	// ------- layout helpers

	/*
	 * @see DialogField#doFillIntoGrid
	 */
	public Control[] doFillIntoGrid(FormToolkit toolkit, Composite parent,
			int nColumns) {
		assertEnoughColumns(nColumns);

		Control requiredLabel = getRequiredLabelControl(toolkit, parent);
		requiredLabel.setLayoutData(gridDataForLabel(1));

		Control label = getLabelControl(toolkit, parent);
		label.setLayoutData(gridDataForLabel(1));

		_group = getGroup(toolkit, parent);
		_group.setLayoutData(gridDataForGroup(nColumns - 2));

		return new Control[] { requiredLabel, label, _group };
	}

	/*
	 * @see DialogField#getNumberOfControls
	 */
	public int getNumberOfControls() {
		return 3;
	}

	/**
	 * @param span
	 * @return the grid data
	 */
	protected static GridData gridDataForGroup(int span) {
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.FILL;
		gd.grabExcessHorizontalSpace = false;
		gd.horizontalSpan = span;
		return gd;
	}

	// ------- focus methods

	/*
	 * @see DialogField#setFocus
	 */
	public boolean setFocus() {
		if (isOkToUse(_group)) {
			_group.setFocus();
		}
		return true;
	}

	// ------- ui creation

	/**
	 * @param toolkit
	 * @param parent
	 * @return the group composite
	 */
	public Composite getGroup(FormToolkit toolkit, Composite parent) {
		if (_group == null || _group.isDisposed()) {
			assertCompositeNotNull(parent);
			if (toolkit != null) {
				_group = toolkit.createComposite(parent);
			} else {
				_group = new Composite(parent, SWT.NONE);
			}
			RowLayout layout = new RowLayout();
			layout.marginBottom = 0;
			_group.setLayout(layout);
			for (int i = 0; i < _items.length; i++) {
				if (toolkit != null) {
					_button[i] = toolkit.createButton(_group, _items[i],
							SWT.RADIO);
				} else {
					_button[i] = new Button(_group, SWT.RADIO);
					_button[i].setText(_items[i]);
				}
				_button[i].setData(INDEXKEY, new Integer(i));
				_button[i].addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						if (_fireEvent) {
							_selectIndex = ((Integer) e.widget
									.getData(INDEXKEY)).intValue();
							// FIXME: seemed will fire widgetSelected twice, one
							// for the deselect one,
							// one for the newly selected one. Need investigate.
							if (((Button) e.widget).getSelection()) {
								dialogFieldChangedAndApplied();
							}
						}
					}

				});
			}
		}
		return _group;
	}

	// ------ enable / disable management

	/*
	 * @see DialogField#updateEnableState
	 */
	protected void updateEnableState() {
		super.updateEnableState();
		if (isOkToUse(_group)) {
			_group.setEnabled(isEnabled());
		}
	}

	// ------ text access

	/**
	 * Sets the text. Triggers a dialog-changed event.
	 * @param index 
	 */
	public void setSelectedIndex(int index) {
		_selectIndex = index;
		if (isOkToUse(_group)) {
			if (_selectIndex >= 0 && _selectIndex < _items.length) {
				_button[_selectIndex].setSelection(true);
			} else {
				for (int i = 0; i < _items.length; i++) {
					if (_button[i].getSelection()) {
						_button[i].setSelection(false);
					}
				}
			}
		}
		dialogFieldChangedAndApplied();
	}

	/**
	 * Sets the text without triggering a dialog-changed event.
	 * @param index 
	 */
	public void setSelectedIndexWithoutUpdate(int index) {
		_selectIndex = index;
		if (isOkToUse(_group)) {
			_fireEvent = false;
			if (_selectIndex >= 0 && _selectIndex < _items.length) {
				_button[_selectIndex].setSelection(true);
			} else {
				for (int i = 0; i < _items.length; i++) {
					if (_button[i].getSelection()) {
						_button[i].setSelection(false);
					}
				}
			}
			_fireEvent = true;
		}
	}

	/**
	 * @return the index selected
	 */
	public int getSelectedIndex() {
		return _selectIndex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.jsf.common.ui.internal.dialogfield.DialogField#handleGrabHorizontal()
	 */
	public void handleGrabHorizontal() {
		LayoutUtil.setGrabHorizontal(this._group, true);
	}
}

Back to the top