KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22
23 /**
24  * Implementation of the TemplateOption that allows users to choose a value from
25  * the fixed set of options.
26  *
27  * @since 2.0
28  * @deprecated see {@link RadioChoiceOption} and {@link ComboChoiceOption}
29  */

30 public class ChoiceOption extends TemplateOption {
31     private String JavaDoc[][] choices;
32     private Control labelControl;
33     private Button[] buttons;
34     private boolean blockListener;
35
36     /**
37      * Constructor for ChoiceOption.
38      *
39      * @param section
40      * the parent section.
41      * @param name
42      * the unique name
43      * @param label
44      * the presentable label
45      * @param choices
46      * the list of choices from which the value can be chosen. Each
47      * array entry should be an array of size 2, where position 0
48      * will be interpeted as the choice unique name, and position 1
49      * as the choice presentable label.
50      */

51     public ChoiceOption(BaseOptionTemplateSection section, String JavaDoc name,
52             String JavaDoc label, String JavaDoc[][] choices) {
53         super(section, name, label);
54         this.choices = choices;
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.pde.ui.templates.TemplateField#createControl(org.eclipse.swt.widgets.Composite, int)
59      */

60     public void createControl(Composite parent, int span) {
61         Composite container = createComposite(parent, span);
62         fill(container, span);
63         GridLayout layout = new GridLayout();
64         layout.marginWidth = layout.marginHeight = 0;
65         container.setLayout(layout);
66         labelControl = createLabel(container, span);
67         labelControl.setEnabled(isEnabled());
68         fill(labelControl, span);
69
70         buttons = new Button[choices.length];
71
72         SelectionListener listener = new SelectionAdapter() {
73             public void widgetSelected(SelectionEvent e) {
74                 Button b = (Button) e.widget;
75                 if (blockListener)
76                     return;
77                 if (b.getSelection()) {
78                     ChoiceOption.super.setValue(b.getData().toString());
79                     getSection().validateOptions(ChoiceOption.this);
80                 }
81             }
82         };
83
84         for (int i = 0; i < choices.length; i++) {
85             String JavaDoc[] choice = choices[i];
86             Button button = createRadioButton(parent, span, choice);
87             buttons[i] = button;
88             button.addSelectionListener(listener);
89             button.setEnabled(isEnabled());
90         }
91         if (getChoice() != null)
92             selectChoice(getChoice());
93     }
94     /**
95      * Returns the string value of the current choice.
96      *
97      * @return the current choice or <samp>null </samp> if not initialized.
98      */

99     public String JavaDoc 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 JavaDoc value) {
111         super.setValue(value);
112         if (buttons != null && value != null) {
113             selectChoice(value.toString());
114         }
115     }
116     /**
117      * Implements the superclass method by updating the enable state of the
118      * option's widget.
119      */

120     public void setEnabled(boolean enabled) {
121         super.setEnabled(enabled);
122         if (labelControl != null) {
123             labelControl.setEnabled(enabled);
124             for (int i = 0; i < buttons.length; i++) {
125                 buttons[i].setEnabled(isEnabled());
126             }
127         }
128     }
129
130     private GridData fill(Control control, int span) {
131         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
132         gd.horizontalSpan = span;
133         control.setLayoutData(gd);
134         return gd;
135     }
136
137     private Composite createComposite(Composite parent, int span) {
138         Composite composite = new Composite(parent, SWT.NULL);
139         fill(composite, span);
140         return composite;
141     }
142
143     private Button createRadioButton(Composite parent, int span, String JavaDoc[] choice) {
144         Button button = new Button(parent, SWT.RADIO);
145         button.setData(choice[0]);
146         button.setText(choice[1]);
147         GridData gd = fill(button, span);
148         gd.horizontalIndent = 10;
149         return button;
150     }
151
152     private void selectChoice(String JavaDoc choice) {
153         blockListener = true;
154         for (int i = 0; i < buttons.length; i++) {
155             Button button = buttons[i];
156             String JavaDoc bname = button.getData().toString();
157             if (bname.equals(choice)) {
158                 button.setSelection(true);
159             } else {
160                 button.setSelection(false);
161             }
162         }
163         blockListener = false;
164     }
165 }
166
Popular Tags