01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.ui.templates;
11:
12: import org.eclipse.swt.SWT;
13: import org.eclipse.swt.events.SelectionAdapter;
14: import org.eclipse.swt.events.SelectionEvent;
15: import org.eclipse.swt.widgets.Combo;
16: import org.eclipse.swt.widgets.Composite;
17: import org.eclipse.swt.widgets.Label;
18:
19: /**
20: * Implementation of the AbstractTemplateOption that allows users to choose a value from
21: * the fixed set of options using a combo box.
22: *
23: * @since 3.2
24: */
25: public class ComboChoiceOption extends AbstractChoiceOption {
26:
27: private Combo fCombo;
28: private Label fLabel;
29:
30: /**
31: * Constructor for ComboChoiceOption.
32: *
33: * @param section
34: * the parent section.
35: * @param name
36: * the unique name
37: * @param label
38: * the presentable label
39: * @param choices
40: * the list of choices from which the value can be chosen. Each
41: * array entry should be an array of size 2, where position 0
42: * will be interpeted as the choice unique name, and position 1
43: * as the choice presentable label.
44: */
45: public ComboChoiceOption(BaseOptionTemplateSection section,
46: String name, String label, String[][] choices) {
47: super (section, name, label, choices);
48: }
49:
50: public void createControl(Composite parent, int span) {
51: fLabel = createLabel(parent, 1);
52: fLabel.setEnabled(isEnabled());
53: fill(fLabel, 1);
54:
55: fCombo = new Combo(parent, SWT.READ_ONLY);
56: fill(fCombo, 1);
57: for (int i = 0; i < fChoices.length; i++) {
58: String[] choice = fChoices[i];
59: fCombo.add(choice[1], i);
60: fCombo.setEnabled(isEnabled());
61: }
62: fCombo.addSelectionListener(new SelectionAdapter() {
63: public void widgetSelected(SelectionEvent e) {
64: if (isBlocked())
65: return;
66: if (fCombo.getSelectionIndex() != -1) {
67: String[] choice = fChoices[fCombo
68: .getSelectionIndex()];
69: setValue(choice[0]);
70: getSection()
71: .validateOptions(ComboChoiceOption.this );
72: }
73: }
74: });
75:
76: if (getChoice() != null)
77: selectChoice(getChoice());
78: }
79:
80: protected void setOptionValue(Object value) {
81: if (fCombo != null && value != null) {
82: selectChoice(value.toString());
83: }
84: }
85:
86: protected void setOptionEnabled(boolean enabled) {
87: if (fLabel != null) {
88: fLabel.setEnabled(enabled);
89: fCombo.setEnabled(enabled);
90: }
91: }
92:
93: protected void selectOptionChoice(String choice) {
94: fCombo.setText(choice);
95: if (fCombo.getSelectionIndex() == -1)
96: fCombo.select(0);
97: }
98: }
|