KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > templates > ComboChoiceOption


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.ui.templates;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19
20 /**
21  * Implementation of the AbstractTemplateOption that allows users to choose a value from
22  * the fixed set of options using a combo box.
23  *
24  * @since 3.2
25  */

26 public class ComboChoiceOption extends AbstractChoiceOption {
27     
28     private Combo fCombo;
29     private Label fLabel;
30     
31     /**
32      * Constructor for ComboChoiceOption.
33      *
34      * @param section
35      * the parent section.
36      * @param name
37      * the unique name
38      * @param label
39      * the presentable label
40      * @param choices
41      * the list of choices from which the value can be chosen. Each
42      * array entry should be an array of size 2, where position 0
43      * will be interpeted as the choice unique name, and position 1
44      * as the choice presentable label.
45      */

46     public ComboChoiceOption(BaseOptionTemplateSection section, String JavaDoc name, String JavaDoc label, String JavaDoc[][] 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 JavaDoc[] 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 JavaDoc[] choice = fChoices[fCombo.getSelectionIndex()];
68                     setValue(choice[0]);
69                     getSection().validateOptions(ComboChoiceOption.this);
70                 }
71             }
72         });
73         
74         if (getChoice() != null)
75             selectChoice(getChoice());
76     }
77     
78     protected void setOptionValue(Object JavaDoc value) {
79         if (fCombo != null && value != null) {
80             selectChoice(value.toString());
81         }
82     }
83     
84     protected void setOptionEnabled(boolean enabled) {
85         if (fLabel != null) {
86             fLabel.setEnabled(enabled);
87             fCombo.setEnabled(enabled);
88         }
89     }
90     
91     protected void selectOptionChoice(String JavaDoc choice) {
92         fCombo.setText(choice);
93         if (fCombo.getSelectionIndex() == -1)
94             fCombo.select(0);
95     }
96 }
97
Popular Tags