--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.diagram;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.utils.strings.format.MetricsFormat;\r
+import org.simantics.utils.strings.format.MetricsFormatList;\r
+\r
+public final class ValueFormatUtil {\r
+\r
+ static String valueStr(Object value_) {\r
+ if (value_ == null)\r
+ return "<null>";\r
+ //return valueStr(value_, "0.0###");\r
+ return valueStr(value_, MetricsFormatList.METRICS_GENERIC);\r
+ }\r
+\r
+ public static String valueStr(Object value_, MetricsFormat decimalFormat) {\r
+ if (value_ == null)\r
+ return "<null>";\r
+\r
+ Class<?> clazz = value_.getClass();\r
+ //System.out.println("FOO: " + clazz + ": " + value_);\r
+ if (clazz.isArray()) {\r
+ Class<?> componentType = clazz.getComponentType();\r
+ if (componentType.isPrimitive()) {\r
+ if (double[].class == clazz) {\r
+ return formatDouble((double[]) value_, decimalFormat);\r
+ } else if (float[].class == clazz) {\r
+ return formatFloat((float[]) value_, decimalFormat);\r
+ } else if (boolean[].class == clazz) {\r
+ return formatBoolean((boolean[]) value_);\r
+ } else if (int[].class == clazz) {\r
+ return formatInteger((int[]) value_);\r
+ } else if (long[].class == clazz) {\r
+ return formatLong((long[]) value_);\r
+ } else if (byte[].class == clazz) {\r
+ return formatByte((byte[]) value_);\r
+ } else {\r
+ return value_.toString();\r
+ }\r
+ } else {\r
+ if (String[].class == clazz) {\r
+ return formatString((String[]) value_);\r
+ } else {\r
+ return value_.toString();\r
+ }\r
+ }\r
+ }\r
+\r
+ if (Double.class == clazz)\r
+ return formatDouble( (Double) value_, decimalFormat );\r
+ else if (Float.class == clazz)\r
+ return formatFloat(new float[] { (Float) value_ }, decimalFormat);\r
+\r
+ // String.class == clazz\r
+ // Boolean.class == clazz\r
+ // Integer.class == clazz\r
+ // Long.class == clazz\r
+ // Byte.class == clazz\r
+ return value_.toString();\r
+ }\r
+\r
+ private static String formatDouble(double[] ds, MetricsFormat f) {\r
+ StringBuilder sb = new StringBuilder();\r
+// DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
+ boolean first = true;\r
+ for (double d : ds) {\r
+ if (!first)\r
+ sb.append(", ");\r
+ else\r
+ first = false;\r
+ //sb.append(format.format(d));\r
+ sb.append(f.formatValue(d));\r
+ }\r
+ return sb.toString();\r
+ }\r
+\r
+ private static String formatDouble(double d, MetricsFormat f) {\r
+ //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
+ //return format.format(d);\r
+ return f.formatValue(d);\r
+ }\r
+\r
+ private static String formatFloat(float[] ds, MetricsFormat f) {\r
+ StringBuilder sb = new StringBuilder();\r
+ //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US));\r
+ boolean first = true;\r
+ for (float d : ds) {\r
+ if (!first)\r
+ sb.append(", ");\r
+ else\r
+ first = false;\r
+ //sb.append(format.format(d));\r
+ sb.append(f.formatValue(d));\r
+ }\r
+ return sb.toString();\r
+ }\r
+\r
+ private static String formatString(String[] value) {\r
+ if (value.length == 1)\r
+ return value[0];\r
+ else\r
+ return Arrays.toString(value);\r
+ }\r
+\r
+ private static String formatBoolean(boolean[] value) {\r
+ if (value.length == 1)\r
+ return Boolean.toString(value[0]);\r
+ else\r
+ return Arrays.toString(value);\r
+ }\r
+\r
+ private static String formatInteger(int[] value) {\r
+ if (value.length == 1)\r
+ return Integer.toString(value[0]);\r
+ else\r
+ return Arrays.toString(value);\r
+ }\r
+\r
+ private static String formatLong(long[] value) {\r
+ if (value.length == 1)\r
+ return Long.toString(value[0]);\r
+ else\r
+ return Arrays.toString(value);\r
+ }\r
+\r
+ private static String formatByte(byte[] value) {\r
+ if (value.length == 1)\r
+ return Byte.toString(value[0]);\r
+ else\r
+ return Arrays.toString(value);\r
+ }\r
+\r
+}\r