001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.messages.util;
014:
015: import java.text.DateFormat;
016: import java.util.Locale;
017:
018: public class LocaleHelper {
019:
020: private static final ThreadLocal threadLocales = new ThreadLocal();
021:
022: public static final int FORMAT_SHORT = DateFormat.SHORT;
023:
024: public static final int FORMAT_MEDIUM = DateFormat.MEDIUM;
025:
026: public static final int FORMAT_LONG = DateFormat.LONG;
027:
028: public static final int FORMAT_FULL = DateFormat.FULL;
029:
030: public static final int FORMAT_IGNORE = -1;
031:
032: private static Locale defaultLocale;
033:
034: public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
035:
036: private static String encoding = UTF_8;
037:
038: public static final String LEFT_TO_RIGHT = "LTR"; //$NON-NLS-1$
039:
040: private static String textDirection = LEFT_TO_RIGHT;
041:
042: public static void setDefaultLocale(Locale newLocale) {
043: defaultLocale = newLocale;
044: }
045:
046: public static Locale getDefaultLocale() {
047: return defaultLocale;
048: }
049:
050: public static void setLocale(Locale newLocale) {
051: threadLocales.set(newLocale);
052: }
053:
054: public static Locale getLocale() {
055: Locale rtn = (Locale) threadLocales.get();
056: if (rtn != null) {
057: return rtn;
058: }
059: defaultLocale = Locale.getDefault();
060: setLocale(defaultLocale);
061: return defaultLocale;
062: }
063:
064: public static void setSystemEncoding(String encoding) {
065: LocaleHelper.encoding = encoding;
066: }
067:
068: public static void setTextDirection(String textDirection) {
069: // TODO make this ThreadLocal
070: LocaleHelper.textDirection = textDirection;
071: }
072:
073: public static String getSystemEncoding() {
074: return encoding;
075: }
076:
077: public static String getTextDirection() {
078: // TODO make this ThreadLocal
079: return textDirection;
080: }
081:
082: public static DateFormat getDateFormat(int dateFormat,
083: int timeFormat) {
084:
085: if (dateFormat != FORMAT_IGNORE && timeFormat != FORMAT_IGNORE) {
086: return DateFormat.getDateTimeInstance(dateFormat,
087: timeFormat, getLocale());
088: } else if (dateFormat != FORMAT_IGNORE) {
089: return DateFormat.getDateInstance(dateFormat, getLocale());
090: } else if (timeFormat != FORMAT_IGNORE) {
091: return DateFormat.getTimeInstance(timeFormat, getLocale());
092: } else {
093: return null;
094: }
095:
096: }
097:
098: public static DateFormat getShortDateFormat(boolean date,
099: boolean time) {
100: if (date && time) {
101: return DateFormat.getDateTimeInstance(DateFormat.SHORT,
102: DateFormat.SHORT, getLocale());
103: } else if (date) {
104: return DateFormat.getDateInstance(DateFormat.SHORT,
105: getLocale());
106: } else if (time) {
107: return DateFormat.getTimeInstance(DateFormat.SHORT,
108: getLocale());
109: } else {
110: return null;
111: }
112: }
113:
114: public static DateFormat getMediumDateFormat(boolean date,
115: boolean time) {
116: if (date && time) {
117: return DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
118: DateFormat.MEDIUM, getLocale());
119: } else if (date) {
120: return DateFormat.getDateInstance(DateFormat.MEDIUM,
121: getLocale());
122: } else if (time) {
123: return DateFormat.getTimeInstance(DateFormat.MEDIUM,
124: getLocale());
125: } else {
126: return null;
127: }
128: }
129:
130: public static DateFormat getLongDateFormat(boolean date,
131: boolean time) {
132: if (date && time) {
133: return DateFormat.getDateTimeInstance(DateFormat.LONG,
134: DateFormat.LONG, getLocale());
135: } else if (date) {
136: return DateFormat.getDateInstance(DateFormat.LONG,
137: getLocale());
138: } else if (time) {
139: return DateFormat.getTimeInstance(DateFormat.LONG,
140: getLocale());
141: } else {
142: return null;
143: }
144: }
145:
146: public static DateFormat getFullDateFormat(boolean date,
147: boolean time) {
148: if (date && time) {
149: return DateFormat.getDateTimeInstance(DateFormat.FULL,
150: DateFormat.FULL, getLocale());
151: } else if (date) {
152: return DateFormat.getDateInstance(DateFormat.FULL,
153: getLocale());
154: } else if (time) {
155: return DateFormat.getTimeInstance(DateFormat.FULL,
156: getLocale());
157: } else {
158: return null;
159: }
160: }
161:
162: }
|