/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.eagerlogic.cubee.client.components.attributepanel;
import com.eagerlogic.cubee.client.components.TextBox;
import com.eagerlogic.cubee.client.events.IEventListener;
import com.eagerlogic.cubee.client.events.KeyEventArgs;
import com.eagerlogic.cubee.client.properties.AExpression;
import com.eagerlogic.cubee.client.style.styles.Border;
import com.eagerlogic.cubee.client.style.styles.Padding;
import com.eagerlogic.cubee.shared.utils.AValidator;
import com.eagerlogic.cubee.shared.utils.ValidationException;
import com.google.gwt.event.dom.client.KeyCodes;
/**
*
* @author dipacs
*/
public final class StringAttribute extends AAttribute<String> {
private static final class StringAttributeEditor extends AAttributeEditor<String> {
private TextBox textBox;
public StringAttributeEditor(AAttribute<String> attribute) {
super(attribute);
textBox = new TextBox();
textBox.textProperty().set(getAttribute().getValue());
textBox.widthProperty().bind(new AExpression<Integer>() {
{
bind(clientWidthProperty(), textBox.paddingProperty(), textBox.borderProperty());
}
@Override
public Integer calculate() {
int res = clientWidthProperty().get();
Padding padding = textBox.paddingProperty().get();
Border border = textBox.borderProperty().get();
if (padding != null) {
res = res - padding.getLeftPadding() - padding.getRightPadding();
}
if (border != null) {
res = res - border.getLeftBorderSize() - border.getRightBorderSize();
}
return res;
}
});
textBox.heightProperty().bind(new AExpression<Integer>() {
{
bind(clientHeightProperty(), textBox.paddingProperty(), textBox.borderProperty());
}
@Override
public Integer calculate() {
int res = clientHeightProperty().get();
Padding padding = textBox.paddingProperty().get();
Border border = textBox.borderProperty().get();
if (padding != null) {
res = (res - padding.getTopPadding()) - padding.getBottomPadding();
}
if (border != null) {
res = (res - border.getTopBorderSize()) - border.getBottomBorderSize();
}
return res;
}
});
textBox.onKeyUpEvent().addListener(new IEventListener<KeyEventArgs>() {
@Override
public void onFired(KeyEventArgs args) {
if (args.getKeyCode() == KeyCodes.KEY_ENTER) {
close(true);
} else if (args.getKeyCode() == KeyCodes.KEY_ESCAPE) {
close(false);
}
}
});
this.getChildren().add(textBox);
}
@Override
protected boolean onClose(boolean saveValue) {
if (saveValue) {
String value = textBox.textProperty().get();
try {
getAttribute().setValue(value);
return true;
} catch (ValidationException ex) {
showValidationError(ex.getMessage());
return false;
}
}
return true;
}
}
public StringAttribute(String name, String value, boolean readonly, AValidator<String> validator) throws ValidationException {
super(name, value, readonly, validator);
}
@Override
public AAttributeEditor<String> createEditor() {
return new StringAttributeEditor(this);
}
@Override
public String getStringValue() {
return getValue() == null ? "" : getValue();
}
}