KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.SelectionAdapter;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18
19 /**
20  * This implementation of the TemplateOption can be used to represent options
21  * that are boolean choices. Option provides the appropriate visual presentation
22  * that allows users to set the boolean value of the option.
23  *
24  * @since 2.0
25  */

26 public class BooleanOption extends TemplateOption {
27     private Button button;
28     /**
29      * The constructor of the option.
30      *
31      * @param section
32      * the parent section
33      * @param name
34      * the unique name
35      * @param label
36      * the presentable label of the option
37      */

38     public BooleanOption(BaseOptionTemplateSection section, String JavaDoc name,
39             String JavaDoc label) {
40         super(section, name, label);
41     }
42     /**
43      * Returns the current state of the option.
44      *
45      * @return true of the option is selected, false otherwise.
46      */

47     public boolean isSelected() {
48         return getValue() != null && getValue().equals(Boolean.TRUE);
49     }
50     /**
51      * Changes the current state of the option to the provided state.
52      *
53      * @param selected
54      * the new state of the option
55      */

56     public void setSelected(boolean selected) {
57         setValue(selected ? Boolean.TRUE : Boolean.FALSE);
58     }
59     /**
60      * Implementation of the superclass method that updates the option's widget
61      * with the new value.
62      *
63      * @param value
64      * the new option value
65      */

66     public void setValue(Object JavaDoc value) {
67         super.setValue(value);
68         if (button != null)
69             button.setSelection(isSelected());
70     }
71     /**
72      * Creates the boolean option control. Option reserves the right to modify
73      * the actual widget used as long as the user can modify its boolean state.
74      *
75      * @param parent
76      * the parent composite of the option widget
77      * @param span
78      * the number of columns that the widget should span
79      */

80     public void createControl(Composite parent, int span) {
81         button = new Button(parent, SWT.CHECK);
82         button.setText(getLabel());
83         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
84         gd.horizontalSpan = span;
85         button.setLayoutData(gd);
86         button.setSelection(isSelected());
87         button.addSelectionListener(new SelectionAdapter() {
88             public void widgetSelected(SelectionEvent e) {
89                 BooleanOption.super.setValue(button.getSelection()
90                         ? Boolean.TRUE
91                         : Boolean.FALSE);
92                 getSection().validateOptions(BooleanOption.this);
93             }
94         });
95         button.setEnabled(isEnabled());
96     }
97     /**
98      * Implementatin of the superclass method that updates the option widget
99      * with the new enabled state.
100      *
101      * @param enabled
102      * the new enabled state.
103      */

104     public void setEnabled(boolean enabled) {
105         super.setEnabled(enabled);
106         if (button != null)
107             button.setEnabled(enabled);
108     }
109 }
110
Popular Tags