/*
* Copyright 2013 Pavel Stastny <pavel.stastny at gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.aplikator.client.local.widgets;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import org.aplikator.client.shared.descriptor.PropertyDTO;
import org.gwtbootstrap3.client.ui.PageHeader;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
/**
* @author Pavel Stastny <pavel.stastny at gmail.com>
*/
public class PageHeaderWidget<T extends Serializable> extends DataFieldBase implements DataField<T> {
private PageHeader pageHeader = new PageHeader();
public PageHeaderWidget(PropertyDTO property, String localizedName, int size) {
super(localizedName, property);
controlHolder.add(pageHeader);
this.setGridSize(size);
}
public void setValue(T value) {
pageHeader.setText(value != null ? value.toString() : null);
}
@Override
public void setEnabled(boolean enabled) {
}
@Override
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
// ??
return null;
}
@SuppressWarnings({"unchecked", "deprecation"})
public T getValue() {
String strVal = pageHeader.getText();
if (strVal == null)
return null;
String type = getProperty().getType();
if ("java.lang.String".equals(type)) {
return (T) strVal;
} else if ("java.lang.Integer".equals(type)) {
try {
return (T) Integer.decode(strVal);
} catch (NumberFormatException ex) {
return null; //TODO notify user
}
} else if ("java.math.BigDecimal".equals(type)) {
try {
return (T) new BigDecimal(strVal);
} catch (NumberFormatException ex) {
return null; //TODO notify user
}
} else if ("java.util.Date".equals(type)) {
try {
return (T) new Date(strVal);
} catch (IllegalArgumentException ex) {
return null; //TODO notify user
}
} else {
throw new IllegalStateException("TextFieldWidget does not support the data type " + type);
}
}
}