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.layout.GridData;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Control;
016:
017: /**
018: * Abstract implementation of the TemplateOption that allows users to choose a value from
019: * the fixed set of options.
020: *
021: * @since 2.0
022: */
023: public abstract class AbstractChoiceOption extends TemplateOption {
024: protected String[][] fChoices;
025: private boolean fBlockListener;
026:
027: /**
028: * Constructor for AbstractChoiceOption.
029: *
030: * @param section
031: * the parent section.
032: * @param name
033: * the unique name
034: * @param label
035: * the presentable label
036: * @param choices
037: * the list of choices from which the value can be chosen. Each
038: * array entry should be an array of size 2, where position 0
039: * will be interpeted as the choice unique name, and position 1
040: * as the choice presentable label.
041: */
042: public AbstractChoiceOption(BaseOptionTemplateSection section,
043: String name, String label, String[][] choices) {
044: super (section, name, label);
045: this .fChoices = choices;
046: }
047:
048: /**
049: * Returns the string value of the current choice.
050: *
051: * @return the current choice or <samp>null </samp> if not initialized.
052: */
053: public String getChoice() {
054: return getValue() != null ? getValue().toString() : null;
055: }
056:
057: /**
058: * Implements the superclass method by passing the new value to the option's
059: * widget.
060: *
061: * @param value
062: * the new value.
063: */
064: public void setValue(Object value) {
065: super .setValue(value);
066: setOptionValue(value);
067: }
068:
069: protected abstract void setOptionValue(Object value);
070:
071: /**
072: * Implements the superclass method by updating the enable state of the
073: * option's widget.
074: */
075: public void setEnabled(boolean enabled) {
076: super .setEnabled(enabled);
077: setOptionEnabled(enabled);
078: }
079:
080: protected abstract void setOptionEnabled(boolean enabled);
081:
082: protected GridData fill(Control control, int span) {
083: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
084: gd.horizontalSpan = span;
085: control.setLayoutData(gd);
086: return gd;
087: }
088:
089: protected Composite createComposite(Composite parent, int span) {
090: Composite composite = new Composite(parent, SWT.NULL);
091: fill(composite, span);
092: return composite;
093: }
094:
095: protected void selectChoice(String choice) {
096: fBlockListener = true;
097: selectOptionChoice(choice);
098: fBlockListener = false;
099: }
100:
101: protected abstract void selectOptionChoice(String choice);
102:
103: protected boolean isBlocked() {
104: return fBlockListener;
105: }
106: }
|