blob: a209563d8137fb4480642ba9dc41b497a2a9414e [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.layout.GridData;
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.eclipse.swt.widgets.Text;
import org.mihalis.opal.preferenceWindow.PreferenceWindow;
/**
* Instances of this class are text areas.
*/
public class PWTextarea extends PWWidget {
/**
* Constructor.
*
* @param label associated label
* @param propertyKey associated key
*/
public PWTextarea(final String label, final String propertyKey) {
super(label, propertyKey, label == null ? 1 : 2, false);
setGrabExcessSpace(true);
setHeight(50);
setWidth(350);
}
/**
* 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 Text text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
addControl(text);
text.setText(PreferenceWindow.getInstance().getValueFor(getPropertyKey()).toString());
text.addListener(SWT.FocusOut, new Listener() {
@Override
public void handleEvent(final Event event) {
PreferenceWindow.getInstance().setValue(getPropertyKey(), text.getText());
}
});
return text;
}
/**
* 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(), "");
} else {
if (!(value instanceof String)) {
throw new UnsupportedOperationException("The property '" + getPropertyKey() + "' has to be a String because it is associated to a textarea");
}
}
}
}