]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/strings/format/MetricsFormatList.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / strings / format / MetricsFormatList.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/strings/format/MetricsFormatList.java b/bundles/org.simantics.utils/src/org/simantics/utils/strings/format/MetricsFormatList.java
new file mode 100644 (file)
index 0000000..0f7afe0
--- /dev/null
@@ -0,0 +1,163 @@
+/*******************************************************************************\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.utils.strings.format;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+/**\r
+ * List of metrics formats\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class MetricsFormatList {\r
+\r
+    public static MetricsFormat METRICS_GENERIC = new MetricsFormat("%s", 1.0, "Generic");\r
+\r
+    public static MetricsFormat METRICS_EXPONENT2 = new MetricsFormat("%e", 1.0, "Exponent");\r
+\r
+    public static MetricsFormat METRICS_EXPONENT = new MetricsFormat("D0.##E0",1.0, "Exponent");\r
+\r
+    public static MetricsFormat METRICS_DECIMAL = new MetricsFormat("D0.0###", 1.0, "Decimal");\r
+\r
+    public static MetricsFormat METRICS_DECIMAL2 = new MetricsFormat("D0.####", 1.0, "Decimal");\r
+\r
+    public static MetricsFormat METRICS_DECIMAL3 = new MetricsFormat("%,g", 1.0, "Decimal");\r
+\r
+    public static MetricsFormat METRICS_DATE_FI = new MetricsFormat("%td.%tm.%ty", 1.0, "Date dd.mm.yyyy");\r
+\r
+    public static MetricsFormat METRICS_DATE_IMPERIAL = new MetricsFormat("%tm/%td/%ty", 1.0, "Date mm/dd/yyyy");\r
+\r
+    public static MetricsFormat METRICS_TIME_SECONDS = new MetricsFormat("%tS.%tL", 1.0, "Time, ss.ms");\r
+\r
+    public static MetricsFormat METRICS_CLOCK_TIME = new MetricsFormat("%tH:%tM:%tS.%tL", 1.0, "Time, hh:mm:ss.ms");\r
+\r
+    public static MetricsFormat METRICS_CLOCK_TIME2 = new MetricsFormat("%tH:%tM:%tS", 1.0, "Time, hh:mm:ss");\r
+\r
+    // Templates\r
+    private final List<MetricsFormat> formats = new ArrayList<MetricsFormat>();\r
+\r
+    private final List<MetricsFormatListListener> listeners = new ArrayList<MetricsFormatListListener>();\r
+\r
+    public MetricsFormat[] getFormats() {\r
+        synchronized (formats) {\r
+            return formats.toArray(new MetricsFormat[0]);\r
+        }\r
+    }\r
+\r
+    public MetricsFormatList() {\r
+    }\r
+\r
+    private void addDefaults() {\r
+        try {\r
+            addFormat(METRICS_DECIMAL);\r
+            addFormat(METRICS_DECIMAL2);\r
+            addFormat(METRICS_DECIMAL3);\r
+            addFormat(METRICS_EXPONENT);\r
+            addFormat(METRICS_EXPONENT2);\r
+            addFormat(METRICS_GENERIC);\r
+            addFormat(METRICS_DATE_FI);\r
+            addFormat(METRICS_DATE_IMPERIAL);\r
+            addFormat(METRICS_TIME_SECONDS);\r
+            addFormat(METRICS_CLOCK_TIME);\r
+            addFormat(METRICS_CLOCK_TIME2);\r
+        } catch (Exception e) {\r
+        }\r
+    }\r
+\r
+    public void addListener(MetricsFormatListListener listener) {\r
+        synchronized (this) {\r
+            listeners.add(listener);\r
+        }\r
+    }\r
+\r
+    public void removeListener(MetricsFormatListListener listener) {\r
+        synchronized (this) {\r
+            listeners.remove(listener);\r
+        }\r
+    }\r
+\r
+    private void fireListChanged() {\r
+        synchronized (this) {\r
+            for (MetricsFormatListListener listener : listeners)\r
+                listener.onListChanged(this);\r
+        }\r
+    }\r
+\r
+    public MetricsFormat addFormat(MetricsFormat format) throws Exception {\r
+        synchronized (this) {\r
+            MetricsFormat old = findEqual(format);\r
+            if (old != null)\r
+                throw new Exception("Format already exists");\r
+            formats.add(format);\r
+            fireListChanged();\r
+            return format;\r
+        }\r
+    }\r
+\r
+    public void removeFormat(MetricsFormat format) throws Exception {\r
+        synchronized (this) {\r
+            int index = formats.indexOf(format);\r
+            if (index < 0)\r
+                throw new Exception("Format not found");\r
+            formats.remove(format);\r
+            fireListChanged();\r
+        }\r
+    }\r
+\r
+    public MetricsFormat replaceFormatWith(MetricsFormat oldFormat, MetricsFormat newFormat) throws Exception {\r
+        synchronized (this) {\r
+            if (formats.contains(newFormat))\r
+                throw new Exception("New format already exists");\r
+            if (oldFormat == newFormat)\r
+                throw new Exception("No changes");\r
+            int index = formats.indexOf(oldFormat);\r
+            if (index < 0)\r
+                throw new Exception("Format not found");\r
+            formats.remove(index);\r
+            formats.add(index, newFormat);\r
+            fireListChanged();\r
+            return newFormat;\r
+        }\r
+    }\r
+\r
+    public MetricsFormat findEqual(MetricsFormat format) {\r
+        synchronized (this) {\r
+            int i = formats.indexOf(format);\r
+            if (i < 0)\r
+                return null;\r
+            return formats.get(i);\r
+        }\r
+    }\r
+\r
+    public MetricsFormat findByIndex(int index) {\r
+        return formats.get(index);\r
+    }\r
+\r
+    public int findIndex(MetricsFormat format) {\r
+        for (int i=0; i<formats.size(); i++)\r
+            if (formats.get(i).equals(format))\r
+                return i;\r
+        return -1;\r
+    }\r
+\r
+    private static MetricsFormatList list;\r
+\r
+    public static MetricsFormatList getList() {\r
+        return list;\r
+    }\r
+\r
+    static {\r
+        list = new MetricsFormatList();\r
+        list.addDefaults();\r
+    }\r
+}\r