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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.strings.format;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\r
18 * List of metrics formats
\r
20 * @author Toni Kalajainen
\r
22 public class MetricsFormatList {
\r
24 public static MetricsFormat METRICS_GENERIC = new MetricsFormat("%s", 1.0, "Generic");
\r
26 public static MetricsFormat METRICS_EXPONENT2 = new MetricsFormat("%e", 1.0, "Exponent");
\r
28 public static MetricsFormat METRICS_EXPONENT = new MetricsFormat("D0.##E0",1.0, "Exponent");
\r
30 public static MetricsFormat METRICS_DECIMAL = new MetricsFormat("D0.0###", 1.0, "Decimal");
\r
32 public static MetricsFormat METRICS_DECIMAL2 = new MetricsFormat("D0.####", 1.0, "Decimal");
\r
34 public static MetricsFormat METRICS_DECIMAL3 = new MetricsFormat("%,g", 1.0, "Decimal");
\r
36 public static MetricsFormat METRICS_DATE_FI = new MetricsFormat("%td.%tm.%ty", 1.0, "Date dd.mm.yyyy");
\r
38 public static MetricsFormat METRICS_DATE_IMPERIAL = new MetricsFormat("%tm/%td/%ty", 1.0, "Date mm/dd/yyyy");
\r
40 public static MetricsFormat METRICS_TIME_SECONDS = new MetricsFormat("%tS.%tL", 1.0, "Time, ss.ms");
\r
42 public static MetricsFormat METRICS_CLOCK_TIME = new MetricsFormat("%tH:%tM:%tS.%tL", 1.0, "Time, hh:mm:ss.ms");
\r
44 public static MetricsFormat METRICS_CLOCK_TIME2 = new MetricsFormat("%tH:%tM:%tS", 1.0, "Time, hh:mm:ss");
\r
47 private final List<MetricsFormat> formats = new ArrayList<MetricsFormat>();
\r
49 private final List<MetricsFormatListListener> listeners = new ArrayList<MetricsFormatListListener>();
\r
51 public MetricsFormat[] getFormats() {
\r
52 synchronized (formats) {
\r
53 return formats.toArray(new MetricsFormat[0]);
\r
57 public MetricsFormatList() {
\r
60 private void addDefaults() {
\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
77 public void addListener(MetricsFormatListListener listener) {
\r
78 synchronized (this) {
\r
79 listeners.add(listener);
\r
83 public void removeListener(MetricsFormatListListener listener) {
\r
84 synchronized (this) {
\r
85 listeners.remove(listener);
\r
89 private void fireListChanged() {
\r
90 synchronized (this) {
\r
91 for (MetricsFormatListListener listener : listeners)
\r
92 listener.onListChanged(this);
\r
96 public MetricsFormat addFormat(MetricsFormat format) throws Exception {
\r
97 synchronized (this) {
\r
98 MetricsFormat old = findEqual(format);
\r
100 throw new Exception("Format already exists");
\r
101 formats.add(format);
\r
107 public void removeFormat(MetricsFormat format) throws Exception {
\r
108 synchronized (this) {
\r
109 int index = formats.indexOf(format);
\r
111 throw new Exception("Format not found");
\r
112 formats.remove(format);
\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
125 throw new Exception("Format not found");
\r
126 formats.remove(index);
\r
127 formats.add(index, newFormat);
\r
133 public MetricsFormat findEqual(MetricsFormat format) {
\r
134 synchronized (this) {
\r
135 int i = formats.indexOf(format);
\r
138 return formats.get(i);
\r
142 public MetricsFormat findByIndex(int index) {
\r
143 return formats.get(index);
\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
153 private static MetricsFormatList list;
\r
155 public static MetricsFormatList getList() {
\r
160 list = new MetricsFormatList();
\r
161 list.addDefaults();
\r