001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.providers.userinfo;
007:
008: import java.util.TimeZone;
009: import javax.servlet.http.HttpServletRequest;
010:
011: import com.sun.portal.providers.userinfo.tag.AttributeTag;
012: import com.sun.portal.providers.userinfo.tag.TagException;
013: import com.sun.portal.providers.userinfo.tag.ReadTag;
014: import com.sun.portal.providers.userinfo.tag.WriteTag;
015: import com.sun.portal.providers.userinfo.tag.UnhandledTagException;
016:
017: import java.util.Locale;
018: import java.util.ResourceBundle;
019: import com.sun.portal.desktop.util.I18n;
020: import com.sun.portal.providers.context.ProviderContext;
021: import com.sun.portal.providers.context.ProviderContextException;
022:
023: /**
024: * this class handles getting and setting: preferredtimezone<br>
025: * and handles getting: timezoneList
026: */
027:
028: public class TimezoneTag implements ReadTag, WriteTag {
029: private ProviderContext context = null;
030: private String channel = null;
031: private ResourceBundle bundle = null;
032: private Locale locale = null;
033:
034: public void init(String channel, ProviderContext context,
035: HttpServletRequest req) throws TagException {
036:
037: this .channel = channel;
038:
039: if (context == null) {
040: throw new TagException(
041: "TimezoneTag.init(): provider context was null");
042: }
043:
044: // set providercontext
045: this .context = context;
046:
047: // get locale
048: locale = context.getLocale();
049:
050: // get resource bundle
051: try {
052: bundle = ResourceBundle.getBundle(context
053: .getProviderName(channel), locale);
054: } catch (ProviderContextException pce) {
055: throw new TagException(
056: "TimezoneTag.init(): Failed to get resource bundle for "
057: + channel + " with locale " + locale);
058: }
059:
060: }
061:
062: public void set(String key, String newValue) throws TagException {
063: if (!key.equals("preferredtimezone")) {
064: throw new UnhandledTagException("unhandled tag=" + key);
065: }
066:
067: context.setStringAttribute(key, newValue);
068: }
069:
070: public String get(String key) throws TagException {
071: if (key.equals("timezoneList")) {
072:
073: String timezoneFormat = bundle.getString("timezoneFormat");
074:
075: String[] ids = TimeZone.getAvailableIDs();
076: String selectedTimezone = "";
077: StringBuffer timezoneList = new StringBuffer(
078: "<SELECT NAME=\"preferredtimezone\">");
079:
080: String userTZ = context
081: .getStringAttribute("preferredtimezone");
082:
083: if (userTZ == null || userTZ.length() == 0) {
084: userTZ = TimeZone.getDefault().getID();
085: }
086:
087: for (int i = 0; i < ids.length; i++) {
088: TimeZone tz = TimeZone.getTimeZone(ids[i]);
089: if (ids[i].equals(userTZ)) {
090: selectedTimezone = "SELECTED";
091: } else {
092: selectedTimezone = "";
093: }
094:
095: timezoneList.append("<OPTION ")
096: .append(selectedTimezone).append(" VALUE=\"")
097: .append(ids[i]).append("\">");
098:
099: //Check whether this timezone uses daylight savings time.
100: if (tz.useDaylightTime()) {
101: timezoneList.append('*');
102: }
103:
104: timezoneList.append(
105: I18n.format(timezoneFormat, tz, locale))
106: .append("</OPTION>");
107: }
108:
109: timezoneList.append("</SELECT>");
110: return timezoneList.toString();
111: } else {
112: throw new UnhandledTagException("unknown key=" + key);
113: }
114: }
115: }
|