diff options
Diffstat (limited to 'org.mihalis.opal/src/main/java/org/mihalis/opal/preferenceWindow/widgets/PWRadio.java')
-rw-r--r-- | org.mihalis.opal/src/main/java/org/mihalis/opal/preferenceWindow/widgets/PWRadio.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/org.mihalis.opal/src/main/java/org/mihalis/opal/preferenceWindow/widgets/PWRadio.java b/org.mihalis.opal/src/main/java/org/mihalis/opal/preferenceWindow/widgets/PWRadio.java new file mode 100644 index 0000000..f364f26 --- /dev/null +++ b/org.mihalis.opal/src/main/java/org/mihalis/opal/preferenceWindow/widgets/PWRadio.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2011 Laurent CARON + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API + *******************************************************************************/ +package org.mihalis.opal.preferenceWindow.widgets; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.mihalis.opal.preferenceWindow.PreferenceWindow; + +/** + * Instances of this class are a group of radio buttons. + */ +public class PWRadio extends PWWidget { + + /** The data. */ + private final List<Object> data; + + /** The buttons. */ + private final List<Button> buttons; + + /** + * Constructor. + * + * @param label associated label + * @param prop the prop + * @param values the values + */ + public PWRadio(final String label, final String prop, final Object... values) { + super(null, prop, label == null ? 1 : 2, false); + this.data = new ArrayList<Object>(Arrays.asList(values)); + this.buttons = new ArrayList<Button>(); + } + + /** + * Builds the. + * + * @param parent the parent + * @return the control + * @see org.mihalis.opal.preferenceWindow.widgets.PWWidget#build(org.eclipse.swt.widgets.Composite) + */ + @Override + public Control build(final Composite parent) { + buildLabel(parent, GridData.BEGINNING); + + final Composite composite = new Composite(parent, SWT.NONE); + final GridLayout gridLayout = new GridLayout(); + gridLayout.marginHeight = gridLayout.marginWidth = 0; + composite.setLayout(gridLayout); + + for (final Object datum : this.data) { + final Button button = new Button(composite, SWT.RADIO); + addControl(button); + button.setText(datum.toString()); + button.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false)); + button.setSelection(datum.equals(PreferenceWindow.getInstance().getValueFor(getPropertyKey()))); + button.setData(datum); + button.addListener(SWT.Selection, new Listener() { + @Override + public void handleEvent(final Event event) { + if (button.getSelection()) { + PreferenceWindow.getInstance().setValue(getPropertyKey(), button.getData()); + } + } + }); + + this.buttons.add(button); + } + return composite; + } + + /** + * Check. + * + * @see org.mihalis.opal.preferenceWindow.widgets.PWWidget#check() + */ + @Override + public void check() { + final Object value = PreferenceWindow.getInstance().getValueFor(getPropertyKey()); + if (value == null) { + PreferenceWindow.getInstance().setValue(getPropertyKey(), null); + } else { + if (!this.data.isEmpty()) { + if (!value.getClass().equals(this.data.get(0).getClass())) { + throw new UnsupportedOperationException("The property '" + getPropertyKey() + "' has to be a " + this.data.get(0).getClass() + " because it is associated to a combo"); + } + } + } + } + +} |