comparison artifacts/src/main/java/org/dive4elements/river/utils/Formatter.java @ 6172:58a613798386

Implement PDF export for SQ Relation
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 03 Jun 2013 17:15:09 +0200
parents af13ceeba52a
children a82a724356f0
comparison
equal deleted inserted replaced
6171:4a1af135885a 6172:58a613798386
9 package org.dive4elements.river.utils; 9 package org.dive4elements.river.utils;
10 10
11 import java.text.DateFormat; 11 import java.text.DateFormat;
12 import java.text.NumberFormat; 12 import java.text.NumberFormat;
13 import java.text.SimpleDateFormat; 13 import java.text.SimpleDateFormat;
14 import java.text.DecimalFormat;
14 import java.util.Locale; 15 import java.util.Locale;
15 16
16 import org.dive4elements.artifacts.CallContext; 17 import org.dive4elements.artifacts.CallContext;
17 import org.dive4elements.artifacts.CallMeta; 18 import org.dive4elements.artifacts.CallMeta;
18 import org.dive4elements.river.artifacts.resources.Resources; 19 import org.dive4elements.river.artifacts.resources.Resources;
85 public static final int FIX_DELTA_W_DELTA_W_MIN_DIGITS = 3; 86 public static final int FIX_DELTA_W_DELTA_W_MIN_DIGITS = 3;
86 public static final int FIX_DELTA_W_DELTA_W_MAX_DIGITS = 3; 87 public static final int FIX_DELTA_W_DELTA_W_MAX_DIGITS = 3;
87 public static final int FIX_DELTA_W_DELTA_Q_MIN_DIGITS = 0; 88 public static final int FIX_DELTA_W_DELTA_Q_MIN_DIGITS = 0;
88 public static final int FIX_DELTA_W_DELTA_Q_MAX_DIGITS = 2; 89 public static final int FIX_DELTA_W_DELTA_Q_MAX_DIGITS = 2;
89 90
91 public static final int VARIANCE_MIN_DIGITS = 0;
92 public static final int VARIANCE_MAX_DIGITS = 4;
93
94 // SQ Relation
95 public static final int SQ_RELATION_MAX_DIGITS = 8;
96 public static final int SQ_RELATION_MIN_DIGITS = 0;
90 97
91 /** 98 /**
92 * Creates a localized NumberFormatter with given range of decimal digits. 99 * Creates a localized NumberFormatter with given range of decimal digits.
93 * @param m CallMeta to find the locale. 100 * @param m CallMeta to find the locale.
94 * @param min minimum number of decimal ("fraction") digits. 101 * @param min minimum number of decimal ("fraction") digits.
104 nf.setMinimumFractionDigits(min); 111 nf.setMinimumFractionDigits(min);
105 112
106 return nf; 113 return nf;
107 } 114 }
108 115
109
110 public static NumberFormat getFormatter(CallContext c, int min, int max){ 116 public static NumberFormat getFormatter(CallContext c, int min, int max){
111 return getFormatter(c.getMeta(), min, max); 117 return getFormatter(c.getMeta(), min, max);
112 } 118 }
113 119
114 120
122 public static NumberFormat getRawFormatter(CallContext c) { 128 public static NumberFormat getRawFormatter(CallContext c) {
123 Locale locale = Resources.getLocale(c.getMeta()); 129 Locale locale = Resources.getLocale(c.getMeta());
124 return NumberFormat.getInstance(locale); 130 return NumberFormat.getInstance(locale);
125 } 131 }
126 132
133 /**
134 * Returns a formatter in engineering notation
135 */
136 public static NumberFormat getEngFormatter(CallContext c) {
137 NumberFormat nf = getRawFormatter(c);
138 if (nf instanceof DecimalFormat) {
139 DecimalFormat df = (DecimalFormat)nf;
140 df.applyPattern("##0.#####E0");
141 }
142 return nf;
143 }
144
145 /**
146 * Returns a number formatter that uses an exponent after max digits
147 */
148 public static NumberFormat getScientificFormater(CallContext c, int min, int max) {
149 NumberFormat nf = getRawFormatter(c);
150 if (nf instanceof DecimalFormat) {
151 DecimalFormat df = (DecimalFormat)nf;
152 df.applyPattern("0.0E0");
153 df.setMaximumFractionDigits(max);
154 df.setMinimumFractionDigits(min);
155 }
156 return nf;
157 }
158
127 159
128 /** 160 /**
129 * Returns a date formatter with SHORT style. 161 * Returns a date formatter with SHORT style.
130 */ 162 */
131 public static DateFormat getShortDateFormat(CallContext cc) { 163 public static DateFormat getShortDateFormat(CallContext cc) {
396 context, 428 context,
397 FIX_DELTA_W_DELTA_W_MIN_DIGITS, 429 FIX_DELTA_W_DELTA_W_MIN_DIGITS,
398 FIX_DELTA_W_DELTA_W_MAX_DIGITS); 430 FIX_DELTA_W_DELTA_W_MAX_DIGITS);
399 } 431 }
400 432
433 public static NumberFormat getVariance(CallContext context) {
434 return getFormatter(
435 context,
436 VARIANCE_MIN_DIGITS,
437 VARIANCE_MAX_DIGITS);
438 }
439
440 public static NumberFormat getSQRelation(CallContext context) {
441 // Would be nice if E0 would not be shown
442 return getScientificFormater(
443 context,
444 SQ_RELATION_MIN_DIGITS,
445 SQ_RELATION_MAX_DIGITS);
446 }
447
401 public static NumberFormat getMeterFormat(CallContext context) { 448 public static NumberFormat getMeterFormat(CallContext context) {
402 return getFormatter( 449 return getFormatter(
403 context, 450 context,
404 0, 451 0,
405 2); 452 2);

http://dive4elements.wald.intevation.org