001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.templates;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.widgets.Button;
017: import org.eclipse.swt.widgets.Composite;
018:
019: /**
020: * This implementation of the TemplateOption can be used to represent options
021: * that are boolean choices. Option provides the appropriate visual presentation
022: * that allows users to set the boolean value of the option.
023: *
024: * @since 2.0
025: */
026: public class BooleanOption extends TemplateOption {
027: private Button button;
028:
029: /**
030: * The constructor of the option.
031: *
032: * @param section
033: * the parent section
034: * @param name
035: * the unique name
036: * @param label
037: * the presentable label of the option
038: */
039: public BooleanOption(BaseOptionTemplateSection section,
040: String name, String label) {
041: super (section, name, label);
042: }
043:
044: /**
045: * Returns the current state of the option.
046: *
047: * @return true of the option is selected, false otherwise.
048: */
049: public boolean isSelected() {
050: return getValue() != null && getValue().equals(Boolean.TRUE);
051: }
052:
053: /**
054: * Changes the current state of the option to the provided state.
055: *
056: * @param selected
057: * the new state of the option
058: */
059: public void setSelected(boolean selected) {
060: setValue(selected ? Boolean.TRUE : Boolean.FALSE);
061: }
062:
063: /**
064: * Implementation of the superclass method that updates the option's widget
065: * with the new value.
066: *
067: * @param value
068: * the new option value
069: */
070: public void setValue(Object value) {
071: super .setValue(value);
072: if (button != null)
073: button.setSelection(isSelected());
074: }
075:
076: /**
077: * Creates the boolean option control. Option reserves the right to modify
078: * the actual widget used as long as the user can modify its boolean state.
079: *
080: * @param parent
081: * the parent composite of the option widget
082: * @param span
083: * the number of columns that the widget should span
084: */
085: public void createControl(Composite parent, int span) {
086: button = new Button(parent, SWT.CHECK);
087: button.setText(getLabel());
088: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
089: gd.horizontalSpan = span;
090: button.setLayoutData(gd);
091: button.setSelection(isSelected());
092: button.addSelectionListener(new SelectionAdapter() {
093: public void widgetSelected(SelectionEvent e) {
094: BooleanOption.super
095: .setValue(button.getSelection() ? Boolean.TRUE
096: : Boolean.FALSE);
097: getSection().validateOptions(BooleanOption.this );
098: }
099: });
100: button.setEnabled(isEnabled());
101: }
102:
103: /**
104: * Implementatin of the superclass method that updates the option widget
105: * with the new enabled state.
106: *
107: * @param enabled
108: * the new enabled state.
109: */
110: public void setEnabled(boolean enabled) {
111: super.setEnabled(enabled);
112: if (button != null)
113: button.setEnabled(enabled);
114: }
115: }
|