1 7 8 package org.jdesktop.swing.data; 9 10 import java.net.MalformedURLException ; 11 import java.net.URL ; 12 13 import java.text.ParseException ; 14 import java.text.DateFormat ; 15 import java.text.SimpleDateFormat ; 16 17 import java.util.Date ; 18 import java.util.Map ; 19 import java.util.HashMap ; 20 import java.util.Set ; 21 22 import javax.swing.UIDefaults ; 23 24 57 public class Converters { 58 private static Map map; 59 60 static { 61 map = new HashMap (); 62 map.put(Boolean .class, "org.jdesktop.swing.data.Converters$BooleanConverter"); 63 map.put(Date .class, "org.jdesktop.swing.data.Converters$DateConverter"); 64 map.put(Double .class, "org.jdesktop.swing.data.Converters$DoubleConverter"); 65 map.put(Float .class, "org.jdesktop.swing.data.Converters$FloatConverter"); 66 map.put(Integer .class, "org.jdesktop.swing.data.Converters$IntegerConverter"); 67 map.put(Link.class, "org.jdesktop.swing.data.Converters$LinkConverter"); 68 map.put(Long .class, "org.jdesktop.swing.data.Converters$LongConverter"); 69 map.put(Short .class, "org.jdesktop.swing.data.Converters$ShortConverter"); 70 map.put(String .class, "org.jdesktop.swing.data.Converters$StringConverter"); 71 } 72 73 80 public static Converter get(Class klass) { 81 Object obj = map.get(klass); 82 Converter converter = null; 83 if (obj != null) { 84 if (obj instanceof String ) { 85 try { 86 Class cls = Class.forName((String )obj); 87 converter = (Converter)cls.newInstance(); 88 map.put(klass, converter); 89 } catch (Exception ex) { 90 converter = null; 91 } 92 } else { 93 converter = (Converter)obj; 94 } 95 } 96 return converter; 97 } 98 99 105 public static void put(Class klass, Converter converter) { 106 map.put(klass, converter); 107 } 108 109 114 public static Class [] getTypes() { 115 Set keys = map.keySet(); 116 return (Class [])keys.toArray(new Class [0]); 117 } 118 119 protected Converters() { 120 } 122 126 static class StringConverter implements Converter { 127 128 public String encode(Object value, Object format) 129 throws ConversionException{ 130 if (value != null && value instanceof String ) { 131 return (String )value; 132 } 133 throw new ConversionException(value, String .class); 134 } 135 136 public Object decode(String value, Object format) 137 throws ConversionException { 138 try { 139 return value.toString(); 140 } 141 catch (Exception e) { 142 throw new ConversionException(value, String .class, e); 143 } 144 } 145 } 146 147 154 static class BooleanConverter implements Converter { 155 156 public String encode(Object value, Object format) 157 throws ConversionException { 158 try { 159 Boolean boolValue = (Boolean ) value; 160 return boolValue.toString(); 161 } 162 catch (Exception e) { 163 throw new ConversionException(value, Boolean .class, e); 164 } 165 } 166 167 public Object decode(String value, Object format) 168 throws ConversionException { 169 try { 170 return Boolean.valueOf(value); 173 } 174 catch (Exception e) { 175 throw new ConversionException(value, Boolean .class, e); 176 } 177 } 178 } 179 180 186 static class IntegerConverter implements Converter { 187 188 public String encode(Object value, Object format) 189 throws ConversionException { 190 try { 191 int intValue = ((Integer )value).intValue(); 192 int radix = (format == null? 10 : ((Integer )format).intValue()); 193 return Integer.toString(intValue, radix); 194 } 195 catch (Exception e) { 196 throw new ConversionException(value, Integer .class, e); 197 } 198 } 199 200 public Object decode(String value, Object format) 201 throws ConversionException { 202 try { 203 int radix = (format == null? 10 : ((Integer )format).intValue()); 204 return Integer.valueOf(value, radix); 205 } 206 catch (Exception e) { 207 throw new ConversionException(value, Integer .class, e); 208 } 209 } 210 } 211 212 218 static class LongConverter implements Converter { 219 220 public String encode(Object value, Object format) 221 throws ConversionException { 222 try { 223 long longValue = ((Long )value).longValue(); 224 int radix = (format == null? 10 : ((Integer )format).intValue()); 225 return Long.toString(longValue, radix); 226 } 227 catch (Exception e) { 228 throw new ConversionException(value, Long .class, e); 229 } 230 } 231 232 public Object decode(String value, Object format) 233 throws ConversionException { 234 try { 235 int radix = (format == null? 10 : ((Integer )format).intValue()); 236 return Long.valueOf(value, radix); 237 } 238 catch (Exception e) { 239 throw new ConversionException(value, Long .class, e); 240 } 241 } 242 } 243 244 250 static class ShortConverter implements Converter { 251 252 public String encode(Object value, Object format) throws 253 ConversionException { 254 try { 255 int shortValue = ( (Short ) value).intValue(); 257 int radix = (format == null ? 10 : ( (Integer ) format).intValue()); 258 return Integer.toString(shortValue, radix); 259 } 260 catch (Exception e) { 261 throw new ConversionException(value, Short .class, e); 262 } 263 } 264 265 public Object decode(String value, Object format) throws 266 ConversionException { 267 try { 268 int radix = (format == null ? 10 : ( (Integer ) format).intValue()); 269 return Short.valueOf(value, radix); 270 } 271 catch (Exception e) { 272 throw new ConversionException(value, Short .class, e); 273 } 274 } 275 } 276 277 281 static class FloatConverter implements Converter { 282 283 public String encode(Object value, Object format) 284 throws ConversionException { 285 try { 286 Float floatValue = (Float ) value; 287 return floatValue.toString(); 288 } 289 catch (Exception e) { 290 throw new ConversionException(value, Float .class, e); 291 } 292 } 293 294 public Object decode(String value, Object format) 295 throws ConversionException { 296 try { 297 return Float.valueOf(value); 298 } 299 catch (Exception e) { 300 throw new ConversionException(value, Float .class, e); 301 } 302 } 303 } 304 305 309 static class DoubleConverter implements Converter { 310 311 public String encode(Object value, Object format) 312 throws ConversionException { 313 try { 314 Double doubleValue = (Double ) value; 315 return doubleValue.toString(); 316 } 317 catch (Exception e) { 318 throw new ConversionException(value, Double .class, e); 319 } 320 } 321 322 public Object decode(String value, Object format) 323 throws ConversionException { 324 try { 325 return Double.valueOf(value); 326 } 327 catch (Exception e) { 328 throw new ConversionException(value, Double .class, e); 329 } 330 } 331 } 332 333 334 344 public static class DateConverter implements Converter { 345 private DateFormat defaultInputFormat; 346 private DateFormat defaultOutputFormat; 347 348 public DateConverter() { 349 defaultInputFormat = defaultOutputFormat = 350 new SimpleDateFormat ("EEE MMM dd HH:mm:ss z yyyy"); 351 } 352 353 public DateConverter(DateFormat defaultInputFormat, 354 DateFormat defaultOutputFormat) { 355 this.defaultInputFormat = defaultInputFormat; 356 this.defaultOutputFormat = defaultOutputFormat; 357 } 358 359 public String encode(Object value, Object format) 360 throws ConversionException { 361 try { 362 DateFormat dateFormat = format == null ? defaultOutputFormat : 363 (DateFormat ) format; 364 return dateFormat.format((Date )value); 365 } 366 catch (Exception e) { 367 throw new ConversionException(value, Date .class, e); 368 } 369 } 370 371 public Object decode(String value, Object format) 372 throws ConversionException { 373 try { 374 DateFormat dateFormat = format == null? defaultInputFormat : 375 (DateFormat )format; 376 return dateFormat.parse(value); 377 } 378 catch (Exception e) { 379 throw new ConversionException(value, Date .class, e); 380 } 381 } 382 } 383 384 391 static class LinkConverter implements Converter { 392 393 394 private static final String URL_BEGIN = "<a HREF=\""; 395 private static final String URL_MARKER = "%u"; 396 private static final String URL_END = "\""; 397 private static final String TARGET_BEGIN = " target=\""; 398 private static final String TARGET_MARKER = "%t"; 399 private static final String TARGET_END = "\""; 400 private static final String DISPLAY_BEGIN = ">"; 401 private static final String DISPLAY_MARKER = "%d"; 402 private static final String DISPLAY_END = "</a>"; 403 404 private static final String TEMPLATE = 405 URL_BEGIN + URL_MARKER + URL_END + 406 TARGET_BEGIN + TARGET_MARKER + TARGET_END + 407 DISPLAY_BEGIN + DISPLAY_MARKER + DISPLAY_END; 408 409 private static final String TEMPLATE2 = 410 URL_BEGIN + URL_MARKER + URL_END + 411 DISPLAY_BEGIN + DISPLAY_MARKER + DISPLAY_END; 412 413 public String encode(Object value, Object format) 414 throws ConversionException { 415 try { 416 Link link = (Link) value; 417 String linkString; 418 String target = link.getTarget(); 419 if (target != null) { 420 linkString = TEMPLATE.replaceFirst(URL_MARKER, 421 link.getURL().toExternalForm()); 422 linkString = linkString.replaceFirst(TARGET_MARKER, 423 target); 424 } 425 else { 426 linkString = TEMPLATE2.replaceFirst(URL_MARKER, 427 link.getURL().toExternalForm()); 428 } 429 linkString = linkString.replaceFirst(DISPLAY_MARKER, 430 link.getText()); 431 432 return linkString; 433 } 434 catch (Exception e) { 435 throw new ConversionException(value, Link.class, e); 436 } 437 } 438 439 public Object decode(String value, Object format) 440 throws ConversionException { 441 try { 442 String url = value.substring(URL_BEGIN.length(), 443 value.indexOf(URL_END, URL_BEGIN.length() + 1)); 444 String target = null; 445 int targetIndex = value.indexOf(TARGET_BEGIN); 446 if (targetIndex != -1) { 447 target = value.substring(targetIndex + TARGET_BEGIN.length(), 448 value.indexOf(TARGET_END, 449 targetIndex + TARGET_BEGIN.length() + 1)); 450 } 451 String display = value.substring(value.indexOf(DISPLAY_BEGIN) + 452 DISPLAY_BEGIN.length(), 453 value.indexOf(DISPLAY_END)); 454 455 456 return new Link(display, target, new URL (url)); 457 } 458 catch (Exception e) { 459 throw new ConversionException(value, Link.class, e); 460 } 461 } 462 } 463 } 464 | Popular Tags |