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