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