]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.strings.format;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 /**\r
18  * List of metrics formats\r
19  * \r
20  * @author Toni Kalajainen\r
21  */\r
22 public class MetricsFormatList {\r
23 \r
24     public static MetricsFormat METRICS_GENERIC = new MetricsFormat("%s", 1.0, "Generic");\r
25 \r
26     public static MetricsFormat METRICS_EXPONENT2 = new MetricsFormat("%e", 1.0, "Exponent");\r
27 \r
28     public static MetricsFormat METRICS_EXPONENT = new MetricsFormat("D0.##E0",1.0, "Exponent");\r
29 \r
30     public static MetricsFormat METRICS_DECIMAL = new MetricsFormat("D0.0###", 1.0, "Decimal");\r
31 \r
32     public static MetricsFormat METRICS_DECIMAL2 = new MetricsFormat("D0.####", 1.0, "Decimal");\r
33 \r
34     public static MetricsFormat METRICS_DECIMAL3 = new MetricsFormat("%,g", 1.0, "Decimal");\r
35 \r
36     public static MetricsFormat METRICS_DATE_FI = new MetricsFormat("%td.%tm.%ty", 1.0, "Date dd.mm.yyyy");\r
37 \r
38     public static MetricsFormat METRICS_DATE_IMPERIAL = new MetricsFormat("%tm/%td/%ty", 1.0, "Date mm/dd/yyyy");\r
39 \r
40     public static MetricsFormat METRICS_TIME_SECONDS = new MetricsFormat("%tS.%tL", 1.0, "Time, ss.ms");\r
41 \r
42     public static MetricsFormat METRICS_CLOCK_TIME = new MetricsFormat("%tH:%tM:%tS.%tL", 1.0, "Time, hh:mm:ss.ms");\r
43 \r
44     public static MetricsFormat METRICS_CLOCK_TIME2 = new MetricsFormat("%tH:%tM:%tS", 1.0, "Time, hh:mm:ss");\r
45 \r
46     // Templates\r
47     private final List<MetricsFormat> formats = new ArrayList<MetricsFormat>();\r
48 \r
49     private final List<MetricsFormatListListener> listeners = new ArrayList<MetricsFormatListListener>();\r
50 \r
51     public MetricsFormat[] getFormats() {\r
52         synchronized (formats) {\r
53             return formats.toArray(new MetricsFormat[0]);\r
54         }\r
55     }\r
56 \r
57     public MetricsFormatList() {\r
58     }\r
59 \r
60     private void addDefaults() {\r
61         try {\r
62             addFormat(METRICS_DECIMAL);\r
63             addFormat(METRICS_DECIMAL2);\r
64             addFormat(METRICS_DECIMAL3);\r
65             addFormat(METRICS_EXPONENT);\r
66             addFormat(METRICS_EXPONENT2);\r
67             addFormat(METRICS_GENERIC);\r
68             addFormat(METRICS_DATE_FI);\r
69             addFormat(METRICS_DATE_IMPERIAL);\r
70             addFormat(METRICS_TIME_SECONDS);\r
71             addFormat(METRICS_CLOCK_TIME);\r
72             addFormat(METRICS_CLOCK_TIME2);\r
73         } catch (Exception e) {\r
74         }\r
75     }\r
76 \r
77     public void addListener(MetricsFormatListListener listener) {\r
78         synchronized (this) {\r
79             listeners.add(listener);\r
80         }\r
81     }\r
82 \r
83     public void removeListener(MetricsFormatListListener listener) {\r
84         synchronized (this) {\r
85             listeners.remove(listener);\r
86         }\r
87     }\r
88 \r
89     private void fireListChanged() {\r
90         synchronized (this) {\r
91             for (MetricsFormatListListener listener : listeners)\r
92                 listener.onListChanged(this);\r
93         }\r
94     }\r
95 \r
96     public MetricsFormat addFormat(MetricsFormat format) throws Exception {\r
97         synchronized (this) {\r
98             MetricsFormat old = findEqual(format);\r
99             if (old != null)\r
100                 throw new Exception("Format already exists");\r
101             formats.add(format);\r
102             fireListChanged();\r
103             return format;\r
104         }\r
105     }\r
106 \r
107     public void removeFormat(MetricsFormat format) throws Exception {\r
108         synchronized (this) {\r
109             int index = formats.indexOf(format);\r
110             if (index < 0)\r
111                 throw new Exception("Format not found");\r
112             formats.remove(format);\r
113             fireListChanged();\r
114         }\r
115     }\r
116 \r
117     public MetricsFormat replaceFormatWith(MetricsFormat oldFormat, MetricsFormat newFormat) throws Exception {\r
118         synchronized (this) {\r
119             if (formats.contains(newFormat))\r
120                 throw new Exception("New format already exists");\r
121             if (oldFormat == newFormat)\r
122                 throw new Exception("No changes");\r
123             int index = formats.indexOf(oldFormat);\r
124             if (index < 0)\r
125                 throw new Exception("Format not found");\r
126             formats.remove(index);\r
127             formats.add(index, newFormat);\r
128             fireListChanged();\r
129             return newFormat;\r
130         }\r
131     }\r
132 \r
133     public MetricsFormat findEqual(MetricsFormat format) {\r
134         synchronized (this) {\r
135             int i = formats.indexOf(format);\r
136             if (i < 0)\r
137                 return null;\r
138             return formats.get(i);\r
139         }\r
140     }\r
141 \r
142     public MetricsFormat findByIndex(int index) {\r
143         return formats.get(index);\r
144     }\r
145 \r
146     public int findIndex(MetricsFormat format) {\r
147         for (int i=0; i<formats.size(); i++)\r
148             if (formats.get(i).equals(format))\r
149                 return i;\r
150         return -1;\r
151     }\r
152 \r
153     private static MetricsFormatList list;\r
154 \r
155     public static MetricsFormatList getList() {\r
156         return list;\r
157     }\r
158 \r
159     static {\r
160         list = new MetricsFormatList();\r
161         list.addDefaults();\r
162     }\r
163 }\r