001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.templates;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.events.SelectionAdapter;
015: import org.eclipse.swt.events.SelectionEvent;
016: import org.eclipse.swt.events.SelectionListener;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Button;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Label;
022:
023: /**
024: * Implementation of the AbstractTemplateOption that allows users to choose a value from
025: * the fixed set of options using radio buttons.
026: *
027: * @since 3.2
028: */
029: public class RadioChoiceOption extends AbstractChoiceOption {
030:
031: private Button[] fButtons;
032: private Label fLabel;
033:
034: /**
035: * Constructor for RadioChoiceOption.
036: * Number of choices must be 2, otherwise an assertion will fail.
037: *
038: * @param section
039: * the parent section.
040: * @param name
041: * the unique name
042: * @param label
043: * the presentable label
044: * @param choices
045: * the list of choices from which the value can be chosen. This
046: * list must be of size 2.
047: * Each array entry should be an array of size 2, where position 0
048: * will be interpeted as the choice unique name, and position 1
049: * as the choice presentable label.
050: */
051: public RadioChoiceOption(BaseOptionTemplateSection section,
052: String name, String label, String[][] choices) {
053: super (section, name, label, choices);
054: Assert.isTrue(choices.length == 2);
055: }
056:
057: private Button createRadioButton(Composite parent, int span,
058: String[] choice) {
059: Button button = new Button(parent, SWT.RADIO);
060: button.setData(choice[0]);
061: button.setText(choice[1]);
062: GridData gd = fill(button, span);
063: gd.horizontalIndent = 10;
064: return button;
065: }
066:
067: public void createControl(Composite parent, int span) {
068:
069: fLabel = createLabel(parent, 1);
070: fLabel.setEnabled(isEnabled());
071: fill(fLabel, span);
072:
073: Composite radioComp = createComposite(parent, span);
074: GridData gd = fill(radioComp, span);
075: gd.horizontalIndent = 10;
076: GridLayout layout = new GridLayout(fChoices.length, true);
077: layout.marginWidth = layout.marginHeight = 0;
078: radioComp.setLayout(layout);
079:
080: fButtons = new Button[fChoices.length];
081:
082: SelectionListener listener = new SelectionAdapter() {
083: public void widgetSelected(SelectionEvent e) {
084: Button b = (Button) e.widget;
085: if (isBlocked())
086: return;
087: if (b.getSelection()) {
088: setValue(b.getData().toString());
089: getSection()
090: .validateOptions(RadioChoiceOption.this );
091: }
092: }
093: };
094:
095: for (int i = 0; i < fChoices.length; i++) {
096: fButtons[i] = createRadioButton(radioComp, 1, fChoices[i]);
097: fButtons[i].addSelectionListener(listener);
098: fButtons[i].setEnabled(isEnabled());
099: }
100:
101: if (getChoice() != null)
102: selectChoice(getChoice());
103: }
104:
105: protected void setOptionValue(Object value) {
106: if (fButtons != null && value != null) {
107: selectChoice(value.toString());
108: }
109: }
110:
111: protected void setOptionEnabled(boolean enabled) {
112: if (fLabel != null) {
113: fLabel.setEnabled(enabled);
114: for (int i = 0; i < fButtons.length; i++) {
115: fButtons[i].setEnabled(enabled);
116: }
117: }
118: }
119:
120: protected void selectOptionChoice(String choice) {
121: for (int i = 0; i < fButtons.length; i++) {
122: Button button = fButtons[i];
123: String bname = button.getData().toString();
124: if (bname.equals(choice)) {
125: button.setSelection(true);
126: } else {
127: button.setSelection(false);
128: }
129: }
130: }
131: }
|