Return-Path: Delivered-To: apmail-incubator-jspwiki-commits-archive@minotaur.apache.org Received: (qmail 77308 invoked from network); 8 Jun 2009 01:22:55 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 8 Jun 2009 01:22:55 -0000 Received: (qmail 21256 invoked by uid 500); 8 Jun 2009 01:23:07 -0000 Delivered-To: apmail-incubator-jspwiki-commits-archive@incubator.apache.org Received: (qmail 21237 invoked by uid 500); 8 Jun 2009 01:23:07 -0000 Mailing-List: contact jspwiki-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jspwiki-dev@incubator.apache.org Delivered-To: mailing list jspwiki-commits@incubator.apache.org Received: (qmail 21228 invoked by uid 99); 8 Jun 2009 01:23:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jun 2009 01:23:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jun 2009 01:23:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C267823888BD; Mon, 8 Jun 2009 01:22:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r782491 - in /incubator/jspwiki/trunk/src/java/org/apache/wiki: action/UserPreferencesActionBean.java preferences/Preferences.java Date: Mon, 08 Jun 2009 01:22:44 -0000 To: jspwiki-commits@incubator.apache.org From: ajaquith@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090608012244.C267823888BD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ajaquith Date: Mon Jun 8 01:22:44 2009 New Revision: 782491 URL: http://svn.apache.org/viewvc?rev=782491&view=rev Log: listTimezones/TimeFormats/Locales code moved to Preferences, where it belongs. All of these were only called by one method anyway, so this was a safe thing to do. Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/action/UserPreferencesActionBean.java incubator/jspwiki/trunk/src/java/org/apache/wiki/preferences/Preferences.java Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/action/UserPreferencesActionBean.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/action/UserPreferencesActionBean.java?rev=782491&r1=782490&r2=782491&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/java/org/apache/wiki/action/UserPreferencesActionBean.java (original) +++ incubator/jspwiki/trunk/src/java/org/apache/wiki/action/UserPreferencesActionBean.java Mon Jun 8 01:22:44 2009 @@ -37,7 +37,6 @@ import org.apache.wiki.ui.EditorManager; import org.apache.wiki.ui.TemplateManager; import org.apache.wiki.ui.stripes.HandlerPermission; -import org.apache.wiki.ui.stripes.WikiActionBeanContext; import org.apache.wiki.ui.stripes.WikiRequestContext; import net.sourceforge.stripes.action.*; @@ -202,9 +201,9 @@ // Set request attributes request.setAttribute( "skins", t.listSkins( request, c.getTemplate() ) ); - request.setAttribute( "locales", t.listLocales( request ) ); - request.setAttribute( "timeformats", t.listTimeFormats( getContext() ) ); - request.setAttribute( "timezones", t.listTimeZones( request ) ); + request.setAttribute( "locales", Preferences.listLocales( request ) ); + request.setAttribute( "timeformats", Preferences.listTimeFormats( getContext() ) ); + request.setAttribute( "timezones", Preferences.listTimeZones( request ) ); // Pre-populate values m_editor = Preferences.getPreference( session, Preferences.PREFS_EDITOR ); @@ -352,8 +351,6 @@ @WikiRequestContext( "prefs" ) public Resolution view() { - WikiActionBeanContext context = getContext(); - TemplateManager.addResourceRequest( context, "script", "scripts/jspwiki-prefs.js" ); return new ForwardResolution( "/UserPreferences.jsp" ); } } Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/preferences/Preferences.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/preferences/Preferences.java?rev=782491&r1=782490&r2=782491&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/java/org/apache/wiki/preferences/Preferences.java (original) +++ incubator/jspwiki/trunk/src/java/org/apache/wiki/preferences/Preferences.java Mon Jun 8 01:22:44 2009 @@ -20,6 +20,7 @@ */ package org.apache.wiki.preferences; +import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; @@ -36,6 +37,7 @@ import org.apache.wiki.i18n.InternationalizationManager; import org.apache.wiki.log.Logger; import org.apache.wiki.log.LoggerFactory; +import org.apache.wiki.ui.TemplateManager; import org.apache.wiki.ui.stripes.LocaleConverter; import org.apache.wiki.util.PropertyReader; @@ -59,6 +61,47 @@ */ public class Preferences extends HashMap { + private static final Map LOCALES; + + /** + * Private constructor to prevent direct instantiation. + */ + private Preferences() {} + + /** + * List of time zones, used by {@link #listTimeZones(HttpServletRequest)}. + */ + private static final List TIME_ZONES; + + static + { + // Init time zones + List zones = new ArrayList(); + String[] ids = { "Pacific/Midway", "Pacific/Honolulu", "America/Anchorage", "PST", "MST", "CST", "EST", "America/Caracas", + "Brazil/East", "Atlantic/South_Georgia", "Atlantic/Cape_Verde", "Etc/Greenwich", "CET", "ART", "EAT", + "Asia/Dubai", "IST", "BST", "VST", "CTT", "JST", "Australia/Sydney", "SST", "NZ", "Pacific/Tongatapu", + "Pacific/Kiritimati" }; + for( String id : ids ) + { + java.util.TimeZone zone = java.util.TimeZone.getTimeZone( id ); + zones.add( zone ); + } + TIME_ZONES = Collections.unmodifiableList( zones ); + + // Init locales + Locale[] locales = Locale.getAvailableLocales(); + Map foundLocales = new HashMap(); + for ( Locale locale : locales ) + { + URL url = TemplateManager.class.getClassLoader().getResource( "CoreResources_" + locale.toString() + ".properties" ); + if ( url != null ) + { + foundLocales.put( locale, locale.getDisplayName( locale ) ); + } + } + LOCALES = Collections.unmodifiableMap( foundLocales ); + } + /** *

* Enumeration of three different date formats that JSPWiki supports. @@ -130,6 +173,9 @@ */ public static final String PREFS_LOCALE = "Locale"; + /** I18N string to mark the server timezone */ + public static final String I18NSERVER_TIMEZONE = "prefs.user.timezone.server"; + /** * Cookie name for the user's preferred template skin. */ @@ -141,6 +187,9 @@ private static final LocaleConverter LOCALE_CONVERTER = new LocaleConverter(); + /** Prefix of the default timeformat properties. */ + public static final String TIMEFORMATPROPERTIES = "jspwiki.defaultprefs.timeformat."; + /** * Get SimpleTimeFormat according to user browser locale and preferred time * formats. If not found, it will revert to whichever format is set for the @@ -408,4 +457,126 @@ } return cookieValue == null ? defaultValue : cookieValue; } + + /** + * Returns a Map of localized time zones, with the TimeZone IDs as keys and + * localized formatted names as the values. + * + * @param request the HTTP request + * @return map of TimeZones + * @since 2.7.x + */ + public static Map listTimeZones( HttpServletRequest request ) + { + WikiEngine engine = WikiEngine.getInstance( request.getSession().getServletContext(), null ); + LinkedHashMap resultMap = new LinkedHashMap(); + java.util.TimeZone serverZone = java.util.TimeZone.getDefault(); + Date now = new Date(); + + // Build a map of TimeZones and their localized display names + for( TimeZone zone : TIME_ZONES ) + { + int offset = zone.getRawOffset() / 3600000; + String zoneLabel = "[GMT" + (offset > 0 ? "+" : "") + offset + "] " + + zone.getDisplayName( zone.inDaylightTime( now ), TimeZone.LONG, request.getLocale() ); + if( serverZone.getRawOffset() == zone.getRawOffset() ) + { + InternationalizationManager i18n = engine.getInternationalizationManager(); + String serverLabel = i18n.get( InternationalizationManager.TEMPLATES_BUNDLE, request.getLocale(), + I18NSERVER_TIMEZONE ); + zoneLabel = zoneLabel + " " + serverLabel; + } + resultMap.put( zone.getID(), zoneLabel ); + } + return resultMap; + } + + /** + * List all installed i18n language properties + * + * @param request the HTTP request + * @return map of installed Languages (with help of Juan Pablo Santos + * Rodriguez) + * @since 2.7.x + */ + public static Map listLocales( HttpServletRequest request ) + { + return LOCALES; + } + + /** + * List all available timeformats, read from the jspwiki.properties + * + * @param context the wiki context + * @return map of TimeFormats + * @since 2.7.x + */ + public static Map listTimeFormats( WikiContext context ) + { + Properties props = context.getEngine().getWikiProperties(); + ArrayList timeFormats = new ArrayList( 40 ); + LinkedHashMap resultMap = new LinkedHashMap(); + + /* filter timeformat properties */ + for( Enumeration e = props.propertyNames(); e.hasMoreElements(); ) + { + String name = (String) e.nextElement(); + + if( name.startsWith( TIMEFORMATPROPERTIES ) ) + { + timeFormats.add( name ); + } + } + + /* fetch actual formats */ + if( timeFormats.size() == 0 ) /* + * no props found - make sure some default + * formats are avail + */ + { + timeFormats.add( "dd-MMM-yy" ); + timeFormats.add( "d-MMM-yyyy" ); + timeFormats.add( "EEE, dd-MMM-yyyy, zzzz" ); + } + else + { + Collections.sort( timeFormats ); + + for( int i = 0; i < timeFormats.size(); i++ ) + { + timeFormats.set( i, props.getProperty( timeFormats.get( i ) ) ); + } + } + + String prefTimeZone = Preferences.getPreference( context.getHttpRequest().getSession(), "TimeZone" ); + TimeZone timeZone = TimeZone.getTimeZone( prefTimeZone ); + + Date d = new Date(); // current date + try + { + // dummy format pattern + SimpleDateFormat format = Preferences.getDateFormat( context, TimeFormat.DATETIME ); + format.setTimeZone( timeZone ); + + for( int i = 0; i < timeFormats.size(); i++ ) + { + try + { + String f = timeFormats.get( i ); + format.applyPattern( f ); + + resultMap.put( f, format.format( d ) ); + } + catch( IllegalArgumentException e ) + { + } // skip parameter + } + } + catch( IllegalArgumentException e ) + { + } // skip parameter + + return resultMap; + } + }