1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagram;
14 import java.util.Arrays;
16 import org.simantics.utils.strings.format.MetricsFormat;
17 import org.simantics.utils.strings.format.MetricsFormatList;
19 public final class ValueFormatUtil {
21 static String valueStr(Object value_) {
23 return "<null>"; //$NON-NLS-1$
24 //return valueStr(value_, "0.0###");
25 return valueStr(value_, MetricsFormatList.METRICS_GENERIC);
28 public static String valueStr(Object value_, MetricsFormat decimalFormat) {
30 return "<null>"; //$NON-NLS-1$
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_);
50 return value_.toString();
53 if (String[].class == clazz) {
54 return formatString((String[]) value_);
56 return value_.toString();
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);
66 // String.class == clazz
67 // Boolean.class == clazz
68 // Integer.class == clazz
69 // Long.class == clazz
70 // Byte.class == clazz
71 return value_.toString();
74 private static String formatDouble(double[] ds, MetricsFormat f) {
75 StringBuilder sb = new StringBuilder();
76 // DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));
80 sb.append(", "); //$NON-NLS-1$
83 //sb.append(format.format(d));
84 sb.append(f.formatValue(d));
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);
95 private static String formatFloat(float[] ds, MetricsFormat f) {
96 StringBuilder sb = new StringBuilder();
97 //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));
101 sb.append(", "); //$NON-NLS-1$
104 //sb.append(format.format(d));
105 sb.append(f.formatValue(d));
107 return sb.toString();
110 private static String formatString(String[] value) {
111 if (value.length == 1)
114 return Arrays.toString(value);
117 private static String formatBoolean(boolean[] value) {
118 if (value.length == 1)
119 return Boolean.toString(value[0]);
121 return Arrays.toString(value);
124 private static String formatInteger(int[] value) {
125 if (value.length == 1)
126 return Integer.toString(value[0]);
128 return Arrays.toString(value);
131 private static String formatLong(long[] value) {
132 if (value.length == 1)
133 return Long.toString(value[0]);
135 return Arrays.toString(value);
138 private static String formatByte(byte[] value) {
139 if (value.length == 1)
140 return Byte.toString(value[0]);
142 return Arrays.toString(value);