]> gerrit.simantics Code Review - simantics/platform.git/blob
4cb51f2896778ec4951ffbebde8f8669183f687e
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram.monitor;
2
3 import java.text.DecimalFormat;
4 import java.text.DecimalFormatSymbols;
5 import java.util.Locale;
6
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;
16
17 public class RealizedFormatterAdapter extends SimpleContextualAdapter<Formatter, RelationContext> {
18
19         @Override
20         public Formatter adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
21
22                 Layer0 L0 = Layer0.getInstance(graph);
23                 DiagramResource DIA = DiagramResource.getInstance(graph);
24                 Resource formatter = context.getStatement().getObject();
25
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 + ")";
30
31                 return new Formatter() {
32
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)) {
38                                         return "NaN";
39                                 } else {
40                                         return format.format(v);
41                                 }
42                         }
43
44                         @Override
45                         public String format(Object object) {
46                                 if (object instanceof Number) {
47                                         Number num = (Number) object;
48                                         return formatNumber(num);
49                                 }
50                                 else if(object instanceof String) {
51                                         try {
52                                                 Double d = Double.parseDouble((String)object);
53                                                 return formatNumber(d);
54                                         } catch (NumberFormatException e) {
55                                                 return (String)object;
56                                         }
57                                 } else {
58                                         return object != null ? object.toString() : "";
59                                 }
60                         }
61
62                         @Override
63                         public String toString() {
64                                 return toString;
65                         }
66
67                 };
68
69         }
70
71 }