org.bidib.wizard.nodes.client.model.AspectRow Maven / Gradle / Ivy
package org.bidib.wizard.nodes.client.model;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.bidib.jbidibc.messages.DccAccessory;
import org.bidib.wizard.api.model.AccessoryAspect;
import org.bidib.wizard.nodes.client.view.DccAspectCellEditor;
import org.bidib.wizard.nodes.client.view.DccAspectConverter;
import com.jidesoft.converter.ConverterContext;
import com.jidesoft.converter.ObjectConverter;
import com.jidesoft.converter.ObjectConverterManager;
import com.jidesoft.grid.DefaultExpandableRow;
import com.jidesoft.grid.EditorContext;
public class AspectRow extends DefaultExpandableRow {
private final NodesAccessoryAspect accessoryAspect;
private final AccessoryAspect aspect;
private Integer aspectNumber;
private Integer dccAccessoryAddress;
private Integer dccAspect;
private boolean dirty;
public AspectRow(final NodesAccessoryAspect accessoryAspect, Integer aspectNumber, final AccessoryAspect aspect) {
this.accessoryAspect = accessoryAspect;
this.aspectNumber = aspectNumber;
this.aspect = aspect;
}
@Override
public ConverterContext getConverterContextAt(int column) {
if (column == 2) {
return DccAspectConverter.CONTEXT;
}
return super.getConverterContextAt(column);
}
@Override
public EditorContext getEditorContextAt(int column) {
if (column == 2) {
return DccAspectCellEditor.CONTEXT;
}
return super.getEditorContextAt(column);
}
@Override
public Object getValueAt(int column) {
switch (column) {
case 0:
String label = Integer.toString(aspectNumber);
if (this.aspect != null) {
label = this.aspect.getLabel();
if (StringUtils.isBlank(label)) {
label = this.aspect.toString();
}
}
return label;
case 1:
return dccAccessoryAddress;
case 2:
return dccAspect; // "green";
default:
break;
}
return null;
}
@Override
public void setValueAt(Object value, int columnIndex) {
switch (columnIndex) {
case AccessoryAspectTreeTableModel.COLUMN_DCC_ADDRESS:
if (value instanceof String) {
String strValue = (String) value;
if (StringUtils.isNotBlank(strValue)) {
this.dccAccessoryAddress = Integer.valueOf(strValue);
}
else {
this.dccAccessoryAddress = null;
}
}
else if (value instanceof Integer) {
this.dccAccessoryAddress = (Integer) value;
}
else if (value == null) {
this.dccAccessoryAddress = null;
}
setDirty(true);
break;
case AccessoryAspectTreeTableModel.COLUMN_ASPECT:
if (value instanceof String) {
final ObjectConverter converter =
ObjectConverterManager.getConverter(Integer.class, DccAspectConverter.CONTEXT);
Object val = converter.fromString((String) value, DccAspectConverter.CONTEXT);
if (val instanceof Integer) {
this.dccAspect = (Integer) val;
}
}
else if (value instanceof Integer) {
this.dccAspect = (Integer) value;
}
else if (value == null) {
this.dccAspect = null;
}
setDirty(true);
break;
default:
break;
}
super.setValueAt(value, columnIndex);
}
@Override
public boolean isCellEditable(int columnIndex) {
switch (columnIndex) {
case 1:
case 2:
case AccessoryAspectTreeTableModel.COLUMN_ACTION:
return true;
default:
break;
}
return false;
}
@Override
public Class> getCellClassAt(int columnIndex) {
switch (columnIndex) {
case 1:
return DccAccessory.class;
case 2:
return Integer.class;
case AccessoryAspectTreeTableModel.COLUMN_ACTION:
return MappingAction.class;
default:
break;
}
return super.getCellClassAt(columnIndex);
}
public boolean hasValidMapping() {
if ((dccAccessoryAddress != null && dccAccessoryAddress.intValue() > 0) && dccAspect != null) {
return true;
}
return false;
}
public long getUniqueId() {
return this.accessoryAspect.getNode().getUniqueId();
}
public Integer getAccessoryNumber() {
return this.accessoryAspect.getAccessory().getId();
}
public String getAccessoryLabel() {
return this.accessoryAspect.getAccessory().getLabel();
}
public Integer getAspectNumber() {
return this.aspectNumber;
}
public String getAspectLabel() {
return this.aspect.getLabel();
}
public AccessoryAspect getAspect() {
return aspect;
}
public Integer getDccAccessoryAddress() {
return dccAccessoryAddress;
}
public Integer getDccAspect() {
return dccAspect;
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
public boolean isDirty() {
return dirty;
}
// public void clear() {
//
//
// }
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}