]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ValueFormatUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / ValueFormatUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.diagram;\r
13 \r
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.utils.strings.format.MetricsFormat;\r
17 import org.simantics.utils.strings.format.MetricsFormatList;\r
18 \r
19 public final class ValueFormatUtil {\r
20 \r
21     static String valueStr(Object value_) {\r
22         if (value_ == null)\r
23             return "<null>";\r
24         //return valueStr(value_, "0.0###");\r
25         return valueStr(value_, MetricsFormatList.METRICS_GENERIC);\r
26     }\r
27 \r
28     public static String valueStr(Object value_, MetricsFormat decimalFormat) {\r
29         if (value_ == null)\r
30             return "<null>";\r
31 \r
32         Class<?> clazz = value_.getClass();\r
33         //System.out.println("FOO: " + clazz + ": " + value_);\r
34         if (clazz.isArray()) {\r
35             Class<?> componentType = clazz.getComponentType();\r
36             if (componentType.isPrimitive()) {\r
37                 if (double[].class == clazz) {\r
38                     return formatDouble((double[]) value_, decimalFormat);\r
39                 } else if (float[].class == clazz) {\r
40                     return formatFloat((float[]) value_, decimalFormat);\r
41                 } else if (boolean[].class == clazz) {\r
42                     return formatBoolean((boolean[]) value_);\r
43                 } else if (int[].class == clazz) {\r
44                     return formatInteger((int[]) value_);\r
45                 } else if (long[].class == clazz) {\r
46                     return formatLong((long[]) value_);\r
47                 } else if (byte[].class == clazz) {\r
48                     return formatByte((byte[]) value_);\r
49                 } else {\r
50                     return value_.toString();\r
51                 }\r
52             } else {\r
53                 if (String[].class == clazz) {\r
54                     return formatString((String[]) value_);\r
55                 } else {\r
56                     return value_.toString();\r
57                 }\r
58             }\r
59         }\r
60 \r
61         if (Double.class == clazz)\r
62             return formatDouble( (Double) value_, decimalFormat );\r
63         else if (Float.class == clazz)\r
64             return formatFloat(new float[] { (Float) value_ }, decimalFormat);\r
65 \r
66         // String.class == clazz\r
67         // Boolean.class == clazz\r
68         // Integer.class == clazz\r
69         // Long.class == clazz\r
70         // Byte.class == clazz\r
71         return value_.toString();\r
72     }\r
73 \r
74     private static String formatDouble(double[] ds, MetricsFormat f) {\r
75         StringBuilder sb = new StringBuilder();\r
76 //        DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
77         boolean first = true;\r
78         for (double d : ds) {\r
79             if (!first)\r
80                 sb.append(", ");\r
81             else\r
82                 first = false;\r
83             //sb.append(format.format(d));\r
84             sb.append(f.formatValue(d));\r
85         }\r
86         return sb.toString();\r
87     }\r
88 \r
89     private static String formatDouble(double d, MetricsFormat f) {\r
90         //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
91         //return format.format(d);\r
92         return f.formatValue(d);\r
93     }\r
94 \r
95     private static String formatFloat(float[] ds, MetricsFormat f) {\r
96         StringBuilder sb = new StringBuilder();\r
97         //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
98         boolean first = true;\r
99         for (float d : ds) {\r
100             if (!first)\r
101                 sb.append(", ");\r
102             else\r
103                 first = false;\r
104             //sb.append(format.format(d));\r
105             sb.append(f.formatValue(d));\r
106         }\r
107         return sb.toString();\r
108     }\r
109 \r
110     private static String formatString(String[] value) {\r
111         if (value.length == 1)\r
112             return value[0];\r
113         else\r
114             return Arrays.toString(value);\r
115     }\r
116 \r
117     private static String formatBoolean(boolean[] value) {\r
118         if (value.length == 1)\r
119             return Boolean.toString(value[0]);\r
120         else\r
121             return Arrays.toString(value);\r
122     }\r
123 \r
124     private static String formatInteger(int[] value) {\r
125         if (value.length == 1)\r
126             return Integer.toString(value[0]);\r
127         else\r
128             return Arrays.toString(value);\r
129     }\r
130 \r
131     private static String formatLong(long[] value) {\r
132         if (value.length == 1)\r
133             return Long.toString(value[0]);\r
134         else\r
135             return Arrays.toString(value);\r
136     }\r
137 \r
138     private static String formatByte(byte[] value) {\r
139         if (value.length == 1)\r
140             return Byte.toString(value[0]);\r
141         else\r
142             return Arrays.toString(value);\r
143     }\r
144 \r
145 }\r