| 1 |
/* |
| 2 |
* @(#)QuarterNameConverter.java 5/8/2006 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2006 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.converter; |
| 8 |
|
| 9 |
import java.text.MessageFormat; |
| 10 |
import java.text.ParseException; |
| 11 |
import java.util.Locale; |
| 12 |
import java.util.ResourceBundle; |
| 13 |
|
| 14 |
/** |
| 15 |
* Converter which converts quarter to String and converts it back. |
| 16 |
*/ |
| 17 |
public class QuarterNameConverter implements ObjectConverter { |
| 18 |
|
| 19 |
/** |
| 20 |
* Default ConverterContext for MonthConverter. |
| 21 |
*/ |
| 22 |
public static ConverterContext CONTEXT = new ConverterContext("QuarterName"); |
| 23 |
private static String _quarterNamePattern; |
| 24 |
|
| 25 |
/** |
| 26 |
* Creates a new CalendarConverter. |
| 27 |
*/ |
| 28 |
public QuarterNameConverter() { |
| 29 |
} |
| 30 |
|
| 31 |
public String toString(Object object, ConverterContext context) { |
| 32 |
if (object == null || !(object instanceof Number)) { |
| 33 |
return ""; |
| 34 |
} |
| 35 |
else { |
| 36 |
int qty = ((Number) object).intValue(); |
| 37 |
if (qty >= 0 && qty < 4) { |
| 38 |
return MessageFormat.format(getQuarterNamePattern(), (qty + 1)); |
| 39 |
} |
| 40 |
else { |
| 41 |
return ""; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public boolean supportToString(Object object, ConverterContext context) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
public Object fromString(String string, ConverterContext context) { |
| 51 |
String quarterNamePattern = getQuarterNamePattern(); |
| 52 |
try { |
| 53 |
Object[] values = new MessageFormat(quarterNamePattern).parse(string); |
| 54 |
if (values.length > 0) { |
| 55 |
return Integer.parseInt("" + values[0]) - 1; |
| 56 |
} |
| 57 |
} |
| 58 |
catch (ParseException e) { |
| 59 |
// ignore |
| 60 |
} |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
public boolean supportFromString(String string, ConverterContext context) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
// public static void main(String[] args) { |
| 69 |
// ObjectConverter converter = new QuarterNameConverter(); |
| 70 |
// for (int i = 0; i < 4; i++) { |
| 71 |
// String str = converter.toString(new Integer(i), null); |
| 72 |
// System.out.println(str); |
| 73 |
// System.out.println(converter.fromString(str, null)); |
| 74 |
// } |
| 75 |
// } |
| 76 |
|
| 77 |
/** |
| 78 |
* Gets the quarter name pattern when converting from an int to a String. For example, if the int is 0, it will |
| 79 |
* converted to "Qtr 1" if the quarter name pattern is "Qtr {0}". |
| 80 |
* |
| 81 |
* @return the prefix. |
| 82 |
*/ |
| 83 |
public String getQuarterNamePattern() { |
| 84 |
if (_quarterNamePattern == null) { |
| 85 |
return getResourceString("Quarter.quarter"); |
| 86 |
} |
| 87 |
return _quarterNamePattern; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the quarter name pattern. For example, if the int is 0, it will converted to "Qtr 1" if the pattern is "Qtr |
| 92 |
* {0}". |
| 93 |
* |
| 94 |
* @param quarterName |
| 95 |
*/ |
| 96 |
public void setQuarterNamePattern(String quarterName) { |
| 97 |
_quarterNamePattern = quarterName; |
| 98 |
} |
| 99 |
|
| 100 |
protected String getResourceString(String key) { |
| 101 |
final ResourceBundle resourceBundle = Resource.getResourceBundle(Locale.getDefault()); |
| 102 |
return resourceBundle.getString(key); |
| 103 |
} |
| 104 |
|
| 105 |
} |