001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials are made 005 * available under the terms of the GNU Lesser General Public License (LGPL) 006 * version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 * 014 * Contributors: Sun Seng David TAN <stan@nuxeo.com> 015 */ 016package org.nuxeo.ecm.user.center.profile; 017 018import java.io.Serializable; 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.Comparator; 022import java.util.List; 023import java.util.TimeZone; 024 025import javax.faces.model.SelectItem; 026 027import org.apache.commons.lang.StringUtils; 028import org.jboss.seam.ScopeType; 029import org.jboss.seam.annotations.In; 030import org.jboss.seam.annotations.Name; 031import org.jboss.seam.annotations.Scope; 032import org.jboss.seam.international.LocaleSelector; 033import org.jboss.seam.international.TimeZoneSelector; 034 035/** 036 * Provide from the system available timezones to be displayed in UI. 037 * 038 * @since 5.6 039 */ 040@Scope(ScopeType.SESSION) 041@Name("timeZones") 042public class TimeZones implements Serializable { 043 044 private static final long serialVersionUID = 1L; 045 046 @In 047 protected LocaleSelector localeSelector; 048 049 private List<SelectItem> timeZoneSelectItems = null; 050 051 public List<SelectItem> getTimeZones() { 052 if (timeZoneSelectItems == null) { 053 initTimeZones(); 054 } 055 return timeZoneSelectItems; 056 } 057 058 public String displayCurrentTimeZone() { 059 TimeZoneSelector tzs = TimeZoneSelector.instance(); 060 String timeZoneId = tzs.getTimeZoneId(); 061 if (StringUtils.isEmpty(timeZoneId)) { 062 TimeZone timeZone = tzs.getTimeZone(); 063 if (timeZone != null) { 064 timeZoneId = timeZone.getID(); 065 } 066 } 067 return displayTimeZone(timeZoneId); 068 } 069 070 public String displayTimeZone(String id) { 071 if (id == null || id.trim().length() == 0 || "none".equals(id)) { 072 return ""; 073 } 074 return id + " - " + TimeZone.getTimeZone(id).getDisplayName(localeSelector.getLocale()); 075 } 076 077 private void initTimeZones() { 078 timeZoneSelectItems = new ArrayList<SelectItem>(); 079 final String[] timeZoneIds = TimeZone.getAvailableIDs(); 080 for (final String id : timeZoneIds) { 081 timeZoneSelectItems.add(new SelectItem(id, displayTimeZone(id))); 082 } 083 Collections.sort(timeZoneSelectItems, new Comparator<SelectItem>() { 084 @Override 085 public int compare(SelectItem o1, SelectItem o2) { 086 return ((String) o1.getValue()).compareTo((String) o2.getValue()); 087 } 088 }); 089 } 090}