/* * (C) 2004 - Geotechnical Software Services * * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ package no.geosoft.cc.locale; import java.lang.ref.WeakReference; import java.util.Collection; import java.util.Iterator; import java.util.ArrayList; import java.util.TimeZone; /** * A manager class for keeping track of a "current" time zone. * * Reading times from 3rd party sources: * * <pre> * DateFormat format = new SimpleDateFormat (pattern); * format.setTimeZone (TimeZone.getTimeZone ([accroding to input format]); * Date time = format.parse (string); * </pre> * * Reading times from GUI: * * <pre> * DateFormat format = new SimpleDateFormat (pattern); * format.setTimeZone (TimeZone.getTimeZone (TimeZoneManager.getTimeZone()); * Date time = format.parse (string); * </pre> * * Displaying times: * * <pre> * Date date; * DateFormat format = new SimpleDateFormat (pattern); * format.setTimeZone (TimeZoneManager.getTimeZone()); * System.out.println (format.format (date)); * </pre> * * @author <a href="mailto:info@geosoft.no">GeoSoft</a> */ public class TimeZoneManager { private TimeZone timeZone_; private Collection listeners_; // of WeakReference (TimeZoneListener) /** * Create a time manager with the specified current time zone. * * @param timeZone Current time zone. */ public TimeZoneManager (TimeZone timeZone) { timeZone_ = timeZone; } /** * Create a time manager with the default current time zone (UTC). */ public TimeZoneManager() { this (TimeZone.getTimeZone ("UTC")); } /** * Return the current time zone. * * @return Current time zone. */ public TimeZone getTimeZone() { return timeZone_; } /** * Set new current time zone and update listeners. * * @param timeZone New current time zone. */ public void setTimeZone (TimeZone timeZone) { if (timeZone.equals (timeZone_)) return; timeZone_ = timeZone; // Call listeners if (listeners_ != null) { for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); TimeZoneListener listener = (TimeZoneListener) reference.get(); if (listener == null) i.remove(); else listener.timeZoneChanged (timeZone_); } } } /** * Add a time zone listener. * * @param timeZoneListener Listener to add. */ public void addTimeZoneListener (TimeZoneListener timeZoneListener) { if (listeners_ == null) listeners_ = new ArrayList(); // Check to see if it is there already for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); TimeZoneListener listener = (TimeZoneListener) reference.get(); if (listener == timeZoneListener) return; } // Add the listener listeners_.add (new WeakReference (timeZoneListener)); } /** * Remove time zone listener. * * @param timeZoneListener Listener to remove. */ public void removeTimeZoneListener (TimeZoneListener timeZoneListener) { if (listeners_ == null) return; for (Iterator i = listeners_.iterator(); i.hasNext(); ) { WeakReference reference = (WeakReference) i.next(); TimeZoneListener listener = (TimeZoneListener) reference.get(); if (listener == timeZoneListener) { i.remove(); break; } } } }