1 package org.simantics.modeling.ui.diagram.monitor;
3 import java.text.DecimalFormat;
4 import java.text.DecimalFormatSymbols;
5 import java.util.Locale;
7 import org.simantics.common.format.Formatter;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.RelationContext;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.adaption.SimpleContextualAdapter;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.layer0.Layer0;
17 public class RealizedFormatterAdapter extends SimpleContextualAdapter<Formatter, RelationContext> {
20 public Formatter adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
22 Layer0 L0 = Layer0.getInstance(graph);
23 DiagramResource DIA = DiagramResource.getInstance(graph);
24 Resource formatter = context.getStatement().getObject();
26 String definition = graph.getRelatedValue(formatter, DIA.RealizedFormatter_HasDefinition, Bindings.STRING);
27 final String label = graph.getPossibleRelatedAdapter(formatter, L0.HasLabel, String.class);
28 final DecimalFormat format = new DecimalFormat(definition, DecimalFormatSymbols.getInstance(Locale.US));
29 final String toString = label + " (" + definition + ")";
31 return new Formatter() {
33 private String formatNumber(Number v) {
34 double dv = v.doubleValue();
35 if (Double.isInfinite(dv)) {
36 return (dv == Double.POSITIVE_INFINITY) ? "\u221E" : "-\u221E";
37 } else if (Double.isNaN(dv)) {
40 return format.format(v);
45 public String format(Object object) {
46 if (object instanceof Number) {
47 Number num = (Number) object;
48 return formatNumber(num);
50 else if(object instanceof String) {
52 Double d = Double.parseDouble((String)object);
53 return formatNumber(d);
54 } catch (NumberFormatException e) {
55 return (String)object;
58 return object != null ? object.toString() : "";
63 public String toString() {