X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2FValueFormatUtil.java;h=8a541eefcd67c643d56c63a5cdc4a52550e81e3e;hp=c3a0a7375661373af8a81304def1f3d18910ce37;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ValueFormatUtil.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ValueFormatUtil.java index c3a0a7375..8a541eefc 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ValueFormatUtil.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ValueFormatUtil.java @@ -1,145 +1,145 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.modeling.ui.diagram; - -import java.util.Arrays; - -import org.simantics.utils.strings.format.MetricsFormat; -import org.simantics.utils.strings.format.MetricsFormatList; - -public final class ValueFormatUtil { - - static String valueStr(Object value_) { - if (value_ == null) - return ""; - //return valueStr(value_, "0.0###"); - return valueStr(value_, MetricsFormatList.METRICS_GENERIC); - } - - public static String valueStr(Object value_, MetricsFormat decimalFormat) { - if (value_ == null) - return ""; - - Class clazz = value_.getClass(); - //System.out.println("FOO: " + clazz + ": " + value_); - if (clazz.isArray()) { - Class componentType = clazz.getComponentType(); - if (componentType.isPrimitive()) { - if (double[].class == clazz) { - return formatDouble((double[]) value_, decimalFormat); - } else if (float[].class == clazz) { - return formatFloat((float[]) value_, decimalFormat); - } else if (boolean[].class == clazz) { - return formatBoolean((boolean[]) value_); - } else if (int[].class == clazz) { - return formatInteger((int[]) value_); - } else if (long[].class == clazz) { - return formatLong((long[]) value_); - } else if (byte[].class == clazz) { - return formatByte((byte[]) value_); - } else { - return value_.toString(); - } - } else { - if (String[].class == clazz) { - return formatString((String[]) value_); - } else { - return value_.toString(); - } - } - } - - if (Double.class == clazz) - return formatDouble( (Double) value_, decimalFormat ); - else if (Float.class == clazz) - return formatFloat(new float[] { (Float) value_ }, decimalFormat); - - // String.class == clazz - // Boolean.class == clazz - // Integer.class == clazz - // Long.class == clazz - // Byte.class == clazz - return value_.toString(); - } - - private static String formatDouble(double[] ds, MetricsFormat f) { - StringBuilder sb = new StringBuilder(); -// DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); - boolean first = true; - for (double d : ds) { - if (!first) - sb.append(", "); - else - first = false; - //sb.append(format.format(d)); - sb.append(f.formatValue(d)); - } - return sb.toString(); - } - - private static String formatDouble(double d, MetricsFormat f) { - //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); - //return format.format(d); - return f.formatValue(d); - } - - private static String formatFloat(float[] ds, MetricsFormat f) { - StringBuilder sb = new StringBuilder(); - //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); - boolean first = true; - for (float d : ds) { - if (!first) - sb.append(", "); - else - first = false; - //sb.append(format.format(d)); - sb.append(f.formatValue(d)); - } - return sb.toString(); - } - - private static String formatString(String[] value) { - if (value.length == 1) - return value[0]; - else - return Arrays.toString(value); - } - - private static String formatBoolean(boolean[] value) { - if (value.length == 1) - return Boolean.toString(value[0]); - else - return Arrays.toString(value); - } - - private static String formatInteger(int[] value) { - if (value.length == 1) - return Integer.toString(value[0]); - else - return Arrays.toString(value); - } - - private static String formatLong(long[] value) { - if (value.length == 1) - return Long.toString(value[0]); - else - return Arrays.toString(value); - } - - private static String formatByte(byte[] value) { - if (value.length == 1) - return Byte.toString(value[0]); - else - return Arrays.toString(value); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.diagram; + +import java.util.Arrays; + +import org.simantics.utils.strings.format.MetricsFormat; +import org.simantics.utils.strings.format.MetricsFormatList; + +public final class ValueFormatUtil { + + static String valueStr(Object value_) { + if (value_ == null) + return ""; + //return valueStr(value_, "0.0###"); + return valueStr(value_, MetricsFormatList.METRICS_GENERIC); + } + + public static String valueStr(Object value_, MetricsFormat decimalFormat) { + if (value_ == null) + return ""; + + Class clazz = value_.getClass(); + //System.out.println("FOO: " + clazz + ": " + value_); + if (clazz.isArray()) { + Class componentType = clazz.getComponentType(); + if (componentType.isPrimitive()) { + if (double[].class == clazz) { + return formatDouble((double[]) value_, decimalFormat); + } else if (float[].class == clazz) { + return formatFloat((float[]) value_, decimalFormat); + } else if (boolean[].class == clazz) { + return formatBoolean((boolean[]) value_); + } else if (int[].class == clazz) { + return formatInteger((int[]) value_); + } else if (long[].class == clazz) { + return formatLong((long[]) value_); + } else if (byte[].class == clazz) { + return formatByte((byte[]) value_); + } else { + return value_.toString(); + } + } else { + if (String[].class == clazz) { + return formatString((String[]) value_); + } else { + return value_.toString(); + } + } + } + + if (Double.class == clazz) + return formatDouble( (Double) value_, decimalFormat ); + else if (Float.class == clazz) + return formatFloat(new float[] { (Float) value_ }, decimalFormat); + + // String.class == clazz + // Boolean.class == clazz + // Integer.class == clazz + // Long.class == clazz + // Byte.class == clazz + return value_.toString(); + } + + private static String formatDouble(double[] ds, MetricsFormat f) { + StringBuilder sb = new StringBuilder(); +// DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); + boolean first = true; + for (double d : ds) { + if (!first) + sb.append(", "); + else + first = false; + //sb.append(format.format(d)); + sb.append(f.formatValue(d)); + } + return sb.toString(); + } + + private static String formatDouble(double d, MetricsFormat f) { + //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); + //return format.format(d); + return f.formatValue(d); + } + + private static String formatFloat(float[] ds, MetricsFormat f) { + StringBuilder sb = new StringBuilder(); + //DecimalFormat format = new DecimalFormat(f, DecimalFormatSymbols.getInstance(Locale.US)); + boolean first = true; + for (float d : ds) { + if (!first) + sb.append(", "); + else + first = false; + //sb.append(format.format(d)); + sb.append(f.formatValue(d)); + } + return sb.toString(); + } + + private static String formatString(String[] value) { + if (value.length == 1) + return value[0]; + else + return Arrays.toString(value); + } + + private static String formatBoolean(boolean[] value) { + if (value.length == 1) + return Boolean.toString(value[0]); + else + return Arrays.toString(value); + } + + private static String formatInteger(int[] value) { + if (value.length == 1) + return Integer.toString(value[0]); + else + return Arrays.toString(value); + } + + private static String formatLong(long[] value) { + if (value.length == 1) + return Long.toString(value[0]); + else + return Arrays.toString(value); + } + + private static String formatByte(byte[] value) { + if (value.length == 1) + return Byte.toString(value[0]); + else + return Arrays.toString(value); + } + +}