001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: Sun Seng David TAN <[email protected]> 017 */ 018package org.nuxeo.ecm.user.center.profile; 019 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.Comparator; 024import java.util.List; 025import java.util.TimeZone; 026 027import javax.faces.model.SelectItem; 028 029import org.apache.commons.lang3.StringUtils; 030import org.jboss.seam.ScopeType; 031import org.jboss.seam.annotations.In; 032import org.jboss.seam.annotations.Name; 033import org.jboss.seam.annotations.Scope; 034import org.jboss.seam.international.LocaleSelector; 035import org.jboss.seam.international.TimeZoneSelector; 036 037/** 038 * Provide from the system available timezones to be displayed in UI. 039 * 040 * @since 5.6 041 */ 042@Scope(ScopeType.SESSION) 043@Name("timeZones") 044public class TimeZones implements Serializable { 045 046 private static final long serialVersionUID = 1L; 047 048 @In 049 protected LocaleSelector localeSelector; 050 051 private List<SelectItem> timeZoneSelectItems = null; 052 053 public List<SelectItem> getTimeZones() { 054 if (timeZoneSelectItems == null) { 055 initTimeZones(); 056 } 057 return timeZoneSelectItems; 058 } 059 060 public String displayCurrentTimeZone() { 061 TimeZoneSelector tzs = TimeZoneSelector.instance(); 062 String timeZoneId = tzs.getTimeZoneId(); 063 if (StringUtils.isEmpty(timeZoneId)) { 064 TimeZone timeZone = tzs.getTimeZone(); 065 if (timeZone != null) { 066 timeZoneId = timeZone.getID(); 067 } 068 } 069 return displayTimeZone(timeZoneId); 070 } 071 072 public String displayTimeZone(String id) { 073 if (id == null || id.trim().length() == 0 || "none".equals(id)) { 074 return ""; 075 } 076 return id + " - " + TimeZone.getTimeZone(id).getDisplayName(localeSelector.getLocale()); 077 } 078 079 private void initTimeZones() { 080 timeZoneSelectItems = new ArrayList<SelectItem>(); 081 final String[] timeZoneIds = TimeZone.getAvailableIDs(); 082 for (final String id : timeZoneIds) { 083 timeZoneSelectItems.add(new SelectItem(id, displayTimeZone(id))); 084 } 085 Collections.sort(timeZoneSelectItems, new Comparator<SelectItem>() { 086 @Override 087 public int compare(SelectItem o1, SelectItem o2) { 088 return ((String) o1.getValue()).compareTo((String) o2.getValue()); 089 } 090 }); 091 } 092}