KickJava   Java API By Example, From Geeks To Geeks.

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


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.layout.GridData;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 /**
19  * Abstract implementation of the TemplateOption that allows users to choose a value from
20  * the fixed set of options.
21  *
22  * @since 2.0
23  */

24 public abstract class AbstractChoiceOption extends TemplateOption {
25     protected String JavaDoc[][] fChoices;
26     private boolean fBlockListener;
27
28     /**
29      * Constructor for AbstractChoiceOption.
30      *
31      * @param section
32      * the parent section.
33      * @param name
34      * the unique name
35      * @param label
36      * the presentable label
37      * @param choices
38      * the list of choices from which the value can be chosen. Each
39      * array entry should be an array of size 2, where position 0
40      * will be interpeted as the choice unique name, and position 1
41      * as the choice presentable label.
42      */

43     public AbstractChoiceOption(BaseOptionTemplateSection section, String JavaDoc name,
44             String JavaDoc label, String JavaDoc[][] choices) {
45         super(section, name, label);
46         this.fChoices = choices;
47     }
48     
49
50     /**
51      * Returns the string value of the current choice.
52      *
53      * @return the current choice or <samp>null </samp> if not initialized.
54      */

55     public String JavaDoc getChoice() {
56         return getValue() != null ? getValue().toString() : null;
57     }
58
59     /**
60      * Implements the superclass method by passing the new value to the option's
61      * widget.
62      *
63      * @param value
64      * the new value.
65      */

66     public void setValue(Object JavaDoc value) {
67         super.setValue(value);
68         setOptionValue(value);
69     }
70     
71     protected abstract void setOptionValue(Object JavaDoc value);
72
73     /**
74      * Implements the superclass method by updating the enable state of the
75      * option's widget.
76      */

77     public void setEnabled(boolean enabled) {
78         super.setEnabled(enabled);
79         setOptionEnabled(enabled);
80     }
81     
82     protected abstract void setOptionEnabled(boolean enabled);
83     
84     protected GridData fill(Control control, int span) {
85         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
86         gd.horizontalSpan = span;
87         control.setLayoutData(gd);
88         return gd;
89     }
90
91     protected Composite createComposite(Composite parent, int span) {
92         Composite composite = new Composite(parent, SWT.NULL);
93         fill(composite, span);
94         return composite;
95     }
96
97     protected void selectChoice(String JavaDoc choice) {
98         fBlockListener = true;
99         selectOptionChoice(choice);
100         fBlockListener = false;
101     }
102
103     protected abstract void selectOptionChoice(String JavaDoc choice);
104     
105     protected boolean isBlocked() {
106         return fBlockListener;
107     }
108 }
109
Popular Tags