1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.util.*; 59 import java.text.*; 60 61 public class FormatFactory { 62 private Map builders = new HashMap(); 63 64 public FormatFactory() { 65 registerBuilder(DecimalFormat.class, new DecimalFormatBuilder()); 66 registerBuilder(NumberFormat.class, new DecimalFormatBuilder()); 67 registerBuilder(SimpleDateFormat.class, new SimpleDateFormatBuilder()); 68 registerBuilder(DateFormat.class, new SimpleDateFormatBuilder()); 69 registerBuilder(ChoiceFormat.class, new ChoiceFormatBuilder()); 70 registerBuilder(MessageFormat.class, new MessageFormatBuilder()); 71 registerBuilder(MapFormat.class, new MapFormatBuilder()); 72 } 73 74 public Format createFormat( 75 Class formatClass, 76 Locale locale, 77 Map parameters) { 78 if (locale == null) 79 locale = Locale.getDefault(); 80 Builder builder = getBuilder(formatClass); 81 Format format = builder.create(locale, parameters); 82 return format; 83 } 84 85 public Builder registerBuilder(Class formatClass, Builder builder) { 86 return (Builder)builders.put(formatClass, builder); 87 } 88 89 public Builder unregisterBuilder(Class formatClass) { 90 return (Builder)builders.remove(formatClass); 91 } 92 93 public Builder getBuilder(Class formatClass) { 94 return (Builder)builders.get(formatClass); 95 } 96 97 public static interface Builder { 98 Format create(Locale locale, Map parameters); 99 } 100 101 public static class DecimalFormatBuilder implements Builder { 102 public Format create(Locale locale, Map parameters) { 103 String pattern = (String )parameters.get("pattern"); 104 DecimalFormatSymbols sym = new DecimalFormatSymbols(locale); 105 DecimalFormat format = new DecimalFormat(); 106 format.setDecimalFormatSymbols(sym); 107 if (pattern != null) 108 format.applyPattern(pattern); 109 return format; 110 } 111 } 112 113 public static class SimpleDateFormatBuilder implements Builder { 114 public Format create(Locale locale, Map parameters) { 115 String pattern = (String )parameters.get("pattern"); 116 DateFormatSymbols sym = new DateFormatSymbols(locale); 117 SimpleDateFormat format = new SimpleDateFormat(); 118 format.setDateFormatSymbols(sym); 119 if (pattern != null) 120 format.applyPattern(pattern); 121 return format; 122 } 123 } 124 125 public static class ChoiceFormatBuilder implements Builder { 126 public Format create(Locale locale, Map parameters) { 127 String pattern = (String )parameters.get("pattern"); 128 ChoiceFormat format = new ChoiceFormat(pattern); 129 return format; 130 } 131 } 132 133 public static class MessageFormatBuilder implements Builder { 134 public Format create(Locale locale, Map parameters) { 135 String pattern = (String )parameters.get("pattern"); 136 MessageFormat format = new MessageFormat(pattern, locale); 137 return format; 138 } 139 } 140 141 public static class MapFormatBuilder implements Builder { 142 public Format create(Locale locale, Map parameters) { 143 MapFormat format = new MapFormat(); 144 String pattern = (String )parameters.get("pattern"); 145 String valueClassName = (String )parameters.get("value-class"); 146 String nullValueDesignation = (String )parameters.get("null-value"); 147 String entryDelimiter = (String )parameters.get("entry-delimiter"); 148 String valueFormatDelimiter = (String )parameters.get("value-delimiter"); 149 if (entryDelimiter != null && entryDelimiter.length() > 0) 150 format.setEntryDelimiter(entryDelimiter.charAt(0)); 151 if (valueFormatDelimiter != null && valueFormatDelimiter.length() > 0) 152 format.setValueFormatDelimiter(valueFormatDelimiter.charAt(0)); 153 if (nullValueDesignation != null) 154 format.setNullValueDesignation(nullValueDesignation); 155 Class valueClass; 156 try { 157 valueClass = Class.forName(valueClassName); 158 format.applyPattern(pattern, valueClass); 159 return format; 160 } 161 catch (ClassNotFoundException ex) { 162 throw new IllegalArgumentException ("Value class " + valueClassName + " not found"); 163 } 164 } 165 } 166 } | Popular Tags |