001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028package edu.wisc.ssec.mcidasv.monitors.time;
029
030import java.text.SimpleDateFormat;
031import java.util.Date;
032import java.util.List;
033import java.util.concurrent.CopyOnWriteArrayList;
034
035import javax.swing.SwingUtilities;
036
037import ucar.unidata.util.GuiUtils;
038
039import edu.wisc.ssec.mcidasv.monitors.Monitorable;
040import edu.wisc.ssec.mcidasv.monitors.Monitoring;
041
042public class TimeMonitor implements Monitorable {
043
044    private final List<Monitoring> listeners = new CopyOnWriteArrayList<>();
045    private final SimpleDateFormat clockFormat = new SimpleDateFormat("HH:mm:ss z");
046
047    public TimeMonitor() {
048        // nothin!
049    }
050
051    public void addMonitor(final Monitoring listener) {
052        listeners.add(listener);
053    }
054
055    public void removeMonitor(final Monitoring listener) {
056        if (!listeners.isEmpty()) {
057            listeners.remove(listener);
058        }
059    }
060
061    public boolean hasMonitors() {
062        return !listeners.isEmpty();
063    }
064
065    public void run() {
066        Date date = new Date();
067        clockFormat.setTimeZone(GuiUtils.getTimeZone());
068        String output = "  "+clockFormat.format(date);
069        final TimeMonitorEvent event = new TimeMonitorEvent(this, output);
070        for (final Monitoring listener : listeners) {
071            SwingUtilities.invokeLater(new Runnable() {
072                public void run() {
073                    listener.monitorUpdated(event);
074                }
075            });
076        }
077    }
078}