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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.diagram;
\r
14 import java.util.Arrays;
\r
16 import org.simantics.utils.strings.format.MetricsFormat;
\r
17 import org.simantics.utils.strings.format.MetricsFormatList;
\r
19 public final class ValueFormatUtil {
\r
21 static String valueStr(Object value_) {
\r
24 //return valueStr(value_, "0.0###");
\r
25 return valueStr(value_, MetricsFormatList.METRICS_GENERIC);
\r
28 public static String valueStr(Object value_, MetricsFormat decimalFormat) {
\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
50 return value_.toString();
\r
53 if (String[].class == clazz) {
\r
54 return formatString((String[]) value_);
\r
56 return value_.toString();
\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
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
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
83 //sb.append(format.format(d));
\r
84 sb.append(f.formatValue(d));
\r
86 return sb.toString();
\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
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
104 //sb.append(format.format(d));
\r
105 sb.append(f.formatValue(d));
\r
107 return sb.toString();
\r
110 private static String formatString(String[] value) {
\r
111 if (value.length == 1)
\r
114 return Arrays.toString(value);
\r
117 private static String formatBoolean(boolean[] value) {
\r
118 if (value.length == 1)
\r
119 return Boolean.toString(value[0]);
\r
121 return Arrays.toString(value);
\r
124 private static String formatInteger(int[] value) {
\r
125 if (value.length == 1)
\r
126 return Integer.toString(value[0]);
\r
128 return Arrays.toString(value);
\r
131 private static String formatLong(long[] value) {
\r
132 if (value.length == 1)
\r
133 return Long.toString(value[0]);
\r
135 return Arrays.toString(value);
\r
138 private static String formatByte(byte[] value) {
\r
139 if (value.length == 1)
\r
140 return Byte.toString(value[0]);
\r
142 return Arrays.toString(value);
\r