use of org.diirt.vtype.VEnum in project yamcs-studio by yamcs.
the class LocalChannelHandler method write.
@Override
public void write(Object newValue, ChannelWriteCallback callback) {
try {
if (VEnum.class.equals(type)) {
// Handle enum writes
int newIndex = -1;
VEnum firstEnum = (VEnum) initialValue;
List<String> labels = firstEnum.getLabels();
if (newValue instanceof Number) {
newIndex = ((Number) newValue).intValue();
} else if (newValue instanceof String) {
newIndex = labels.indexOf((String) newValue);
// parse a number.
if (newIndex == -1) {
String value = (String) newValue;
try {
newIndex = Double.valueOf(value).intValue();
} catch (NumberFormatException ex) {
}
}
} else {
throw new IllegalArgumentException("Value" + newValue + " can not be accepted by VEnum.");
}
newValue = ValueFactory.newVEnum(newIndex, firstEnum.getLabels(), alarmNone(), timeNow());
} else if (VString.class.equals(type) && newValue instanceof String) {
newValue = ValueFactory.newVString((String) newValue, alarmNone(), timeNow());
} else {
// If the string can be parsed to a number, do it
if (newValue instanceof String) {
String value = (String) newValue;
try {
newValue = Double.valueOf(value);
} catch (NumberFormatException ex) {
// Not a double - continue
}
}
// If new value is not a VType, try to convert it
if (!(newValue instanceof VType)) {
newValue = checkValue(ValueFactory.toVTypeChecked(newValue));
}
}
processMessage(newValue);
callback.channelWritten(null);
} catch (Exception ex) {
callback.channelWritten(ex);
}
}
use of org.diirt.vtype.VEnum in project org.csstudio.display.builder by kasemir.
the class RadioRepresentation method selectionChanged.
private void selectionChanged(final ObservableValue<? extends Toggle> obs, final Toggle oldval, final Toggle newval) {
if (!active && newval != null) {
active = true;
try {
// For now reset to old value.
// New value will be shown if the PV accepts it and sends a value update.
toggle.selectToggle(oldval);
final Object value;
final VType pv_value = model_widget.runtimePropValue().getValue();
if (pv_value instanceof VEnum || pv_value instanceof VNumber)
// PV uses enumerated or numeric type, so write the index
value = toggle.getToggles().indexOf(newval);
else
// PV uses text
value = FormatOptionHandler.parse(pv_value, ((RadioButton) newval).getText(), FormatOption.DEFAULT);
logger.log(Level.FINE, "Writing " + value);
toolkit.fireWrite(model_widget, value);
} finally {
active = false;
}
}
}
use of org.diirt.vtype.VEnum in project org.csstudio.display.builder by kasemir.
the class ValueUtil method getLabels.
/**
* Get labels for a {@link VEnum} value, or headers for a {@link VTable}.
* @param value Value of a PV
* @return Enum labels or empty array if not enum nor table
*/
public static String[] getLabels(final VType value) {
if (value instanceof VEnum) {
final List<String> labels = ((VEnum) value).getLabels();
return labels.toArray(new String[labels.size()]);
}
if (value instanceof VTable) {
final VTable table = (VTable) value;
final int num = table.getColumnCount();
final String[] headers = new String[num];
for (int i = 0; i < num; ++i) headers[i] = table.getColumnName(i);
return headers;
}
return new String[0];
}
use of org.diirt.vtype.VEnum in project yamcs-studio by yamcs.
the class AbstractChoiceEditPart method registerLoadItemsListener.
/**
*/
private void registerLoadItemsListener() {
// load items from PV
if (getExecutionMode() == ExecutionMode.RUN_MODE) {
if (getWidgetModel().isItemsFromPV()) {
IPV pv = getPV(AbstractPVWidgetModel.PROP_PVNAME);
if (pv != null) {
if (loadItemsFromPVListener == null)
loadItemsFromPVListener = new IPVListener.Stub() {
@Override
public void valueChanged(IPV pv) {
VType value = pv.getValue();
if (value != null && value instanceof VEnum) {
List<String> new_meta = ((VEnum) value).getLabels();
getWidgetModel().setPropertyValue(AbstractChoiceModel.PROP_ITEMS, new_meta);
}
}
};
pv.addListener(loadItemsFromPVListener);
}
}
}
}
use of org.diirt.vtype.VEnum in project yamcs-studio by yamcs.
the class ComboEditPart method registerLoadItemsListener.
/**
*/
private void registerLoadItemsListener() {
// load items from PV
if (getExecutionMode() == ExecutionMode.RUN_MODE) {
if (getWidgetModel().isItemsFromPV()) {
IPV pv = getPV(AbstractPVWidgetModel.PROP_PVNAME);
if (pv != null) {
if (loadItemsFromPVListener == null)
loadItemsFromPVListener = new IPVListener.Stub() {
@Override
public void valueChanged(IPV pv) {
VType value = pv.getValue();
if (value != null && value instanceof VEnum) {
List<String> items = ((VEnum) value).getLabels();
getWidgetModel().setPropertyValue(ComboModel.PROP_ITEMS, items);
}
}
};
pv.addListener(loadItemsFromPVListener);
}
}
}
}
Aggregations