blob: f49e30c1c88b50e6fefeb3f86f0f357c7b9e4b8d [file] [log] [blame]
/*******************************************************************************
* 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 org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.mihalis.opal.preferenceWindow.PreferenceWindow;
/**
* Instances of this class are checkboxes.
*/
public class PWCheckbox extends PWWidget {
/**
* Constructor.
*
* @param label associated label
* @param propertyKey associated key
*/
public PWCheckbox(final String label, final String propertyKey) {
super(label, propertyKey, 1, true);
}
/**
* 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) {
if (getLabel() == null) {
throw new UnsupportedOperationException("Please specify a label for a checkbox");
}
final Button button = new Button(parent, SWT.CHECK);
addControl(button);
button.setText(getLabel());
final boolean originalSelection = (Boolean) PreferenceWindow.getInstance().getValueFor(getPropertyKey());
button.setSelection(originalSelection);
button.addSelectionListener(new SelectionAdapter() {
/**
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
@Override
public void widgetSelected(final SelectionEvent e) {
PreferenceWindow.getInstance().setValue(getPropertyKey(), button.getSelection());
}
});
return button;
}
/**
* 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(), Boolean.valueOf(false));
} else {
if (!(value instanceof Boolean)) {
throw new UnsupportedOperationException("The property '" + getPropertyKey() + "' has to be a Boolean because it is associated to a checkbox");
}
}
}
}