KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23
24 /**
25  * Implementation of the AbstractTemplateOption that allows users to choose a value from
26  * the fixed set of options using radio buttons.
27  *
28  * @since 3.2
29  */

30 public class RadioChoiceOption extends AbstractChoiceOption {
31     
32     private Button[] fButtons;
33     private Label fLabel;
34
35     /**
36      * Constructor for RadioChoiceOption.
37      * Number of choices must be 2, otherwise an assertion will fail.
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. This
47      * list must be of size 2.
48      * Each array entry should be an array of size 2, where position 0
49      * will be interpeted as the choice unique name, and position 1
50      * as the choice presentable label.
51      */

52     public RadioChoiceOption(BaseOptionTemplateSection section, String JavaDoc name,
53             String JavaDoc label, String JavaDoc[][] choices) {
54         super(section, name, label, choices);
55         Assert.isTrue(choices.length == 2);
56     }
57
58     private Button createRadioButton(Composite parent, int span, String JavaDoc[] choice) {
59         Button button = new Button(parent, SWT.RADIO);
60         button.setData(choice[0]);
61         button.setText(choice[1]);
62         GridData gd = fill(button, span);
63         gd.horizontalIndent = 10;
64         return button;
65     }
66
67
68     public void createControl(Composite parent, int span) {
69         
70         fLabel = createLabel(parent, 1);
71         fLabel.setEnabled(isEnabled());
72         fill(fLabel, span);
73         
74         Composite radioComp = createComposite(parent, span);
75         GridData gd = fill(radioComp, span);
76         gd.horizontalIndent = 10;
77         GridLayout layout = new GridLayout(fChoices.length, true);
78         layout.marginWidth = layout.marginHeight = 0;
79         radioComp.setLayout(layout);
80         
81         fButtons = new Button[fChoices.length];
82
83         SelectionListener listener = new SelectionAdapter() {
84             public void widgetSelected(SelectionEvent e) {
85                 Button b = (Button) e.widget;
86                 if (isBlocked())
87                     return;
88                 if (b.getSelection()) {
89                     setValue(b.getData().toString());
90                     getSection().validateOptions(RadioChoiceOption.this);
91                 }
92             }
93         };
94
95         for (int i = 0; i < fChoices.length; i++) {
96             fButtons[i] = createRadioButton(radioComp, 1, fChoices[i]);
97             fButtons[i].addSelectionListener(listener);
98             fButtons[i].setEnabled(isEnabled());
99         }
100         
101         if (getChoice() != null)
102             selectChoice(getChoice());
103     }
104
105     protected void setOptionValue(Object JavaDoc 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 JavaDoc choice) {
121         for (int i = 0; i < fButtons.length; i++) {
122             Button button = fButtons[i];
123             String JavaDoc bname = button.getData().toString();
124             if (bname.equals(choice)) {
125                 button.setSelection(true);
126             } else {
127                 button.setSelection(false);
128             }
129         }
130     }
131 }
132
Popular Tags