KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > CalendarServiceShim


1 /*
2 * Copyright (C) 2007, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 */

5
6 package com.ibm.icu.util;
7
8 import java.util.Locale JavaDoc;
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import com.ibm.icu.impl.ICULocaleService;
13 import com.ibm.icu.impl.ICUResourceBundle;
14 import com.ibm.icu.impl.ICUService;
15 import com.ibm.icu.impl.ICULocaleService.LocaleKey;
16 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
17 import com.ibm.icu.impl.ICUService.Factory;
18 import com.ibm.icu.impl.ICUService.Key;
19 import com.ibm.icu.util.Calendar.CalendarFactory;
20
21 class CalendarServiceShim extends Calendar.CalendarShim {
22
23     Locale JavaDoc[] getAvailableLocales() {
24         if (service.isDefault()) {
25             return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
26         }
27         return service.getAvailableLocales();
28     }
29
30     ULocale[] getAvailableULocales() {
31         if (service.isDefault()) {
32             return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
33         }
34         return service.getAvailableULocales();
35     }
36
37     private static final class CalFactory extends LocaleKeyFactory {
38         private CalendarFactory delegate;
39         CalFactory(CalendarFactory delegate) {
40             super(delegate.visible() ? VISIBLE : INVISIBLE);
41             this.delegate = delegate;
42         }
43
44         public Object JavaDoc create(Key key, ICUService service) {
45             if (handlesKey(key)) {
46                 LocaleKey lkey = (LocaleKey)key;
47                 ULocale loc = lkey.canonicalLocale();
48                 Object JavaDoc result = delegate.createCalendar(loc);
49                 if (result == null) {
50                     result = service.getKey(key, null, this);
51                 }
52                 return result;
53             }
54             return null;
55         }
56
57         protected Set JavaDoc getSupportedIDs() {
58             return delegate.getSupportedLocaleNames();
59         }
60     }
61
62     Calendar createInstance(ULocale desiredLocale) {
63         ULocale[] actualLoc = new ULocale[1];
64         if (desiredLocale.equals(ULocale.ROOT)) {
65             desiredLocale = ULocale.ROOT;
66         }
67         Calendar cal = (Calendar)service.get(desiredLocale, actualLoc);
68         if (cal == null) {
69             throw new MissingResourceException JavaDoc("Unable to construct Calendar", "", "");
70         }
71         cal = (Calendar)cal.clone();
72
73         /* !!! TODO !!! actualLoc returned by service is not properly set.
74          * When this Calendar object is being created, cal.setLocale is called
75          * and proper actual locale is set at that time. Revisit this later.
76          * -yoshito
77          */

78         /*
79         ULocale uloc = actualLoc[0];
80         cal.setLocale(uloc, uloc); // service make no distinction between actual and valid
81         */

82         return cal;
83     }
84
85     Object JavaDoc registerFactory(CalendarFactory factory) {
86         return service.registerFactory(new CalFactory(factory));
87     }
88
89     boolean unregister(Object JavaDoc k) {
90         return service.unregisterFactory((Factory)k);
91     }
92
93     private static class CalService extends ICULocaleService {
94         CalService() {
95             super("Calendar");
96             class RBCalendarFactory extends ICUResourceBundleFactory {
97                 protected Object JavaDoc handleCreate(ULocale loc, int kind, ICUService sercice) {
98                     return Calendar.createInstance(loc);
99                 }
100             }
101             this.registerFactory(new RBCalendarFactory());
102             markDefault();
103         }
104     }
105     
106     private static ICULocaleService service = new CalService();
107 }
108
Popular Tags