[Data Binding] Generic formatted string ComputedValue [message #331861] |
Thu, 25 September 2008 14:38 |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
I've written the following ComputedValue that is intended to be a
generic way to format an arbitrary number of IObservableVaulues
according to a pattern.
I have two questions about this:
A) Does this seem generically useful enough to submit for inclusion in
org.eclipse.core.databinding?
B) Any ideas what to do to gracefully handle null values from the
observables? As the code is now, it just inserts the String "null" but
that is not really acceptable IMO.
Eric
public class FormattedStringObservableValue extends ComputedValue {
private MessageFormat pattern;
private IObservableValue[] observables;
public FormattedStringObservableValue(String formatPattern,
IObservableValue... observables) {
setFormatPattern(formatPattern);
setObservables(observables);
}
public FormattedStringObservableValue(MessageFormat formatPattern,
IObservableValue... observables) {
setFormatPattern(formatPattern);
setObservables(observables);
}
public void setObservables(IObservableValue... observables) {
this.observables = observables;
}
public void setFormatPattern(String formatPattern) {
setFormatPattern(new MessageFormat(formatPattern));
}
public void setFormatPattern(MessageFormat formatPattern) {
this.pattern = formatPattern;
}
@Override
protected Object calculate() {
Object[] values = new Object[observables.length];
for (int i = 0; i < observables.length; i++) {
values[i] = observables[i].getValue();
}
return pattern.format(values);
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03594 seconds