| 1 |
/* |
| 2 |
* @(#) BooleanConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.util.Locale; |
| 9 |
|
| 10 |
/** |
| 11 |
* Converter which converts Boolean to String and converts it back. |
| 12 |
*/ |
| 13 |
public class BooleanConverter implements ObjectConverter { |
| 14 |
|
| 15 |
public BooleanConverter() { |
| 16 |
} |
| 17 |
|
| 18 |
public String toString(Object object, ConverterContext context) { |
| 19 |
if (Boolean.FALSE.equals(object)) { |
| 20 |
return getFalse(); |
| 21 |
} |
| 22 |
else if (Boolean.TRUE.equals(object)) { |
| 23 |
return getTrue(); |
| 24 |
} |
| 25 |
else { |
| 26 |
return getNull(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public boolean supportToString(Object object, ConverterContext context) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
public Object fromString(String string, ConverterContext context) { |
| 35 |
if (string.equalsIgnoreCase(getTrue())) { |
| 36 |
return Boolean.TRUE; |
| 37 |
} |
| 38 |
else |
| 39 |
if (string.equalsIgnoreCase("true")) { // in case the application runs under different locale, we still consider "true" is true. |
| 40 |
return Boolean.TRUE; |
| 41 |
} |
| 42 |
else if (string.equalsIgnoreCase(getFalse())) { |
| 43 |
return Boolean.FALSE; |
| 44 |
} |
| 45 |
else |
| 46 |
if (string.equalsIgnoreCase("false")) { // in case the application runs under different locale, we still consider "false" is false. |
| 47 |
return Boolean.FALSE; |
| 48 |
} |
| 49 |
else { |
| 50 |
return null; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public boolean supportFromString(String string, ConverterContext context) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the string to represent the true value. By default, it's "true". You could override this method to customize |
| 60 |
* the string. |
| 61 |
* |
| 62 |
* @return the string to represent the true value. |
| 63 |
*/ |
| 64 |
protected String getTrue() { |
| 65 |
String s = Resource.getResourceBundle(Locale.getDefault()).getString("Boolean.true"); |
| 66 |
return s != null ? s.trim() : s; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the string to represent the false value. By default, it's "false". You could override this method to customize |
| 71 |
* the string. |
| 72 |
* |
| 73 |
* @return the string to represent the false value. |
| 74 |
*/ |
| 75 |
protected String getFalse() { |
| 76 |
String s = Resource.getResourceBundle(Locale.getDefault()).getString("Boolean.false"); |
| 77 |
return s != null ? s.trim() : s; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the string to represent the null value. By default, it's "". You could override this method to customize |
| 82 |
* the string. |
| 83 |
* |
| 84 |
* @return the string to represent the null value. |
| 85 |
*/ |
| 86 |
protected String getNull() { |
| 87 |
return ""; |
| 88 |
} |
| 89 |
} |