001: /*******************************************************************************
002: * Copyright (c) 2000, 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.swt.SWT;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.events.SelectionListener;
016: import org.eclipse.swt.layout.GridData;
017: import org.eclipse.swt.layout.GridLayout;
018: import org.eclipse.swt.widgets.Button;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021:
022: /**
023: * Implementation of the TemplateOption that allows users to choose a value from
024: * the fixed set of options.
025: *
026: * @since 2.0
027: * @deprecated see {@link RadioChoiceOption} and {@link ComboChoiceOption}
028: */
029: public class ChoiceOption extends TemplateOption {
030: private String[][] choices;
031: private Control labelControl;
032: private Button[] buttons;
033: private boolean blockListener;
034:
035: /**
036: * Constructor for ChoiceOption.
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. Each
046: * array entry should be an array of size 2, where position 0
047: * will be interpeted as the choice unique name, and position 1
048: * as the choice presentable label.
049: */
050: public ChoiceOption(BaseOptionTemplateSection section, String name,
051: String label, String[][] choices) {
052: super (section, name, label);
053: this .choices = choices;
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.pde.ui.templates.TemplateField#createControl(org.eclipse.swt.widgets.Composite, int)
058: */
059: public void createControl(Composite parent, int span) {
060: Composite container = createComposite(parent, span);
061: fill(container, span);
062: GridLayout layout = new GridLayout();
063: layout.marginWidth = layout.marginHeight = 0;
064: container.setLayout(layout);
065: labelControl = createLabel(container, span);
066: labelControl.setEnabled(isEnabled());
067: fill(labelControl, span);
068:
069: buttons = new Button[choices.length];
070:
071: SelectionListener listener = new SelectionAdapter() {
072: public void widgetSelected(SelectionEvent e) {
073: Button b = (Button) e.widget;
074: if (blockListener)
075: return;
076: if (b.getSelection()) {
077: ChoiceOption.super .setValue(b.getData().toString());
078: getSection().validateOptions(ChoiceOption.this );
079: }
080: }
081: };
082:
083: for (int i = 0; i < choices.length; i++) {
084: String[] choice = choices[i];
085: Button button = createRadioButton(parent, span, choice);
086: buttons[i] = button;
087: button.addSelectionListener(listener);
088: button.setEnabled(isEnabled());
089: }
090: if (getChoice() != null)
091: selectChoice(getChoice());
092: }
093:
094: /**
095: * Returns the string value of the current choice.
096: *
097: * @return the current choice or <samp>null </samp> if not initialized.
098: */
099: public String getChoice() {
100: return getValue() != null ? getValue().toString() : null;
101: }
102:
103: /**
104: * Implements the superclass method by passing the new value to the option's
105: * widget.
106: *
107: * @param value
108: * the new value.
109: */
110: public void setValue(Object value) {
111: super .setValue(value);
112: if (buttons != null && value != null) {
113: selectChoice(value.toString());
114: }
115: }
116:
117: /**
118: * Implements the superclass method by updating the enable state of the
119: * option's widget.
120: */
121: public void setEnabled(boolean enabled) {
122: super .setEnabled(enabled);
123: if (labelControl != null) {
124: labelControl.setEnabled(enabled);
125: for (int i = 0; i < buttons.length; i++) {
126: buttons[i].setEnabled(isEnabled());
127: }
128: }
129: }
130:
131: private GridData fill(Control control, int span) {
132: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
133: gd.horizontalSpan = span;
134: control.setLayoutData(gd);
135: return gd;
136: }
137:
138: private Composite createComposite(Composite parent, int span) {
139: Composite composite = new Composite(parent, SWT.NULL);
140: fill(composite, span);
141: return composite;
142: }
143:
144: private Button createRadioButton(Composite parent, int span,
145: String[] choice) {
146: Button button = new Button(parent, SWT.RADIO);
147: button.setData(choice[0]);
148: button.setText(choice[1]);
149: GridData gd = fill(button, span);
150: gd.horizontalIndent = 10;
151: return button;
152: }
153:
154: private void selectChoice(String choice) {
155: blockListener = true;
156: for (int i = 0; i < buttons.length; i++) {
157: Button button = buttons[i];
158: String bname = button.getData().toString();
159: if (bname.equals(choice)) {
160: button.setSelection(true);
161: } else {
162: button.setSelection(false);
163: }
164: }
165: blockListener = false;
166: }
167: }
|