Last active
December 12, 2015 05:38
-
-
Save cardil/4722673 to your computer and use it in GitHub Desktop.
Vaadin slider with text box for input value, with events and implementing Field so suitable for forms. See: http://img844.imageshack.us/img844/5530/zrzutekranuz20130206145.png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2013 Wave Software. | |
* | |
* The source code contained in this file is owned and/or is subject to | |
* copyright by Wave Software. Only employees of the company, or other | |
* cooperating entities responsible for developing, maintaining and implementing | |
* this code shall be entitled to inspect the contents of the file. If you are | |
* not one of them, delete the file and let us know about a possible theft | |
* attempt by e-mail to: info@wavesoftware.pl. This file may not be reproduced, | |
* used in whole or in part, by persons not listed above. If you need | |
* clarification on permissions in the use of this file please contact via | |
* e-mail. | |
*/ | |
package wave.web.view.helpers; | |
import com.vaadin.data.Property; | |
import com.vaadin.data.Validator; | |
import com.vaadin.data.Validator.InvalidValueException; | |
import com.vaadin.event.FieldEvents; | |
import com.vaadin.event.FieldEvents.TextChangeEvent; | |
import com.vaadin.ui.AbstractTextField; | |
import com.vaadin.ui.Alignment; | |
import com.vaadin.ui.CustomComponent; | |
import com.vaadin.ui.Field; | |
import com.vaadin.ui.HorizontalLayout; | |
import com.vaadin.ui.Slider; | |
import com.vaadin.ui.Slider.ValueOutOfBoundsException; | |
import com.vaadin.ui.TextField; | |
import java.util.Collection; | |
/** | |
* | |
* @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> | |
*/ | |
public class SliderInput extends CustomComponent implements Field { | |
private static final long serialVersionUID = 1L; | |
private final HorizontalLayout layout; | |
private final TextField field = new TextField(); | |
private final Slider slider = new Slider(); | |
public SliderInput() { | |
super(); | |
this.setWidth("154px"); | |
layout = new HorizontalLayout(); | |
layout.setWidth("100%"); | |
layout.setSpacing(true); | |
field.setWidth("30px"); | |
field.setNullRepresentation(""); | |
field.setImmediate(true); | |
field.setTextChangeEventMode(AbstractTextField.TextChangeEventMode.TIMEOUT); | |
field.setTextChangeTimeout(750); | |
slider.setWidth("100%"); | |
slider.setImmediate(true); | |
layout.addComponent(slider); | |
layout.addComponent(field); | |
layout.setExpandRatio(slider, 0.99f); | |
layout.setExpandRatio(field, 0.01f); | |
layout.setComponentAlignment(slider, Alignment.MIDDLE_LEFT); | |
layout.setComponentAlignment(field, Alignment.MIDDLE_RIGHT); | |
setCompositionRoot(layout); | |
slider.addListener(new Property.ValueChangeListener() { | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void valueChange(Property.ValueChangeEvent event) { | |
Object value = event.getProperty().getValue(); | |
if (value != null) { | |
Double d = (Double) value; | |
Integer i = d.intValue(); | |
String is = i.toString(); | |
if (field.getValue() == null) { | |
field.setValue(is); | |
} else { | |
String fVal = field.getValue().toString(); | |
if (!fVal.equals(is)) { | |
field.setValue(is); | |
} | |
} | |
} | |
} | |
}); | |
field.addListener(new FieldEvents.TextChangeListener() { | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void textChange(TextChangeEvent event) { | |
String value = event.getText(); | |
try { | |
if (value != null) { | |
Integer i = Integer.parseInt(value, 10); | |
if (i != null) { | |
if (!i.toString().equals(value)) { | |
throw slider.new ValueOutOfBoundsException(Double.NaN); | |
} | |
if (slider.getValue() == null) { | |
slider.setValue(i.doubleValue()); | |
} else { | |
Double d = (Double) slider.getValue(); | |
if (d.intValue() != i.intValue()) { | |
slider.setValue(i.doubleValue()); | |
} | |
} | |
} | |
} | |
} catch (Exception ex) { | |
Double sVal = (Double) slider.getValue(); | |
Integer i = sVal.intValue(); | |
field.setValue(i.toString()); | |
} | |
} | |
}); | |
} | |
public double getMax() { | |
return slider.getMax(); | |
} | |
public void setMax(double max) { | |
slider.setMax(max); | |
} | |
public double getMin() { | |
return slider.getMin(); | |
} | |
public void setMin(double min) { | |
slider.setMin(min); | |
} | |
public int getResolution() { | |
return slider.getResolution(); | |
} | |
public void setResolution(int resolution) { | |
slider.setResolution(resolution); | |
} | |
public Field getField() { | |
return field; | |
} | |
@Override | |
public String getRequiredError() { | |
return field.getRequiredError(); | |
} | |
@Override | |
public boolean isRequired() { | |
return field.isRequired(); | |
} | |
@Override | |
public void setRequired(boolean required) { | |
field.setRequired(required); | |
} | |
@Override | |
public void setRequiredError(String requiredMessage) { | |
field.setRequiredError(requiredMessage); | |
} | |
@Override | |
public boolean isInvalidCommitted() { | |
return field.isInvalidCommitted(); | |
} | |
@Override | |
public void setInvalidCommitted(boolean isCommitted) { | |
field.setInvalidCommitted(isCommitted); | |
} | |
@Override | |
public void commit() throws SourceException, InvalidValueException { | |
field.commit(); | |
} | |
@Override | |
public void discard() throws SourceException { | |
field.discard(); | |
} | |
@Override | |
public boolean isModified() { | |
return field.isModified(); | |
} | |
@Override | |
public boolean isReadThrough() { | |
return field.isReadThrough(); | |
} | |
@Override | |
public boolean isWriteThrough() { | |
return field.isWriteThrough(); | |
} | |
@Override | |
public void setReadThrough(boolean readThrough) throws SourceException { | |
field.setReadThrough(readThrough); | |
} | |
@Override | |
public void setWriteThrough(boolean writeThrough) throws SourceException, | |
InvalidValueException { | |
field.setWriteThrough(writeThrough); | |
} | |
@Override | |
public void addValidator(Validator validator) { | |
field.addValidator(validator); | |
} | |
@Override | |
public Collection<Validator> getValidators() { | |
return field.getValidators(); | |
} | |
@Override | |
public boolean isInvalidAllowed() { | |
return field.isInvalidAllowed(); | |
} | |
@Override | |
public boolean isValid() { | |
return field.isValid(); | |
} | |
@Override | |
public void removeValidator(Validator validator) { | |
field.removeValidator(validator); | |
} | |
@Override | |
public void setInvalidAllowed(boolean invalidValueAllowed) | |
throws UnsupportedOperationException { | |
field.setInvalidAllowed(invalidValueAllowed); | |
} | |
@Override | |
public void validate() throws InvalidValueException { | |
field.validate(); | |
} | |
@Override | |
public Class<?> getType() { | |
return field.getType(); | |
} | |
@Override | |
public void setValue(Object newValue) throws ReadOnlyException, | |
ConversionException { | |
field.setValue(newValue); | |
} | |
@Override | |
public void addListener(ValueChangeListener listener) { | |
field.addListener(listener); | |
} | |
@Override | |
public void removeListener(ValueChangeListener listener) { | |
field.removeListener(listener); | |
} | |
@Override | |
public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) { | |
field.valueChange(event); | |
} | |
@Override | |
public Property getPropertyDataSource() { | |
return field.getPropertyDataSource(); | |
} | |
@Override | |
public void setPropertyDataSource(Property newDataSource) { | |
field.setPropertyDataSource(newDataSource); | |
} | |
@Override | |
public void focus() { | |
field.focus(); | |
} | |
@Override | |
public int getTabIndex() { | |
return field.getTabIndex(); | |
} | |
@Override | |
public void setTabIndex(int tabIndex) { | |
field.setTabIndex(tabIndex); | |
} | |
@Override | |
public Object getValue() { | |
return field.getValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment