package com.onaro.sanscreen.client; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.Icon; import javax.swing.Timer; import com.jidesoft.status.LabelStatusBarItem; import com.onaro.commons.util.ResourceManager; import com.onaro.sanscreen.client.ejb.ServerStatusValidator; public class RmiMonitorStatusBarItem extends LabelStatusBarItem implements PropertyChangeListener { /** * Flash delay in milliseconds. Changing this will change how long of a wait is done before the status bar item * toggles on/off */ public static final int TOGGLE_DELAY = 100; private static final long serialVersionUID = 1L; private static final Resources RESOURCES = ResourceManager.getResources(Resources.class); private final Timer timer; private final AtomicBoolean inRmiCall = new AtomicBoolean(false); /** Keeps track of the status of the Server */ private final AtomicBoolean isServerRunning = new AtomicBoolean(true); public RmiMonitorStatusBarItem() { initialize(); timer = new Timer(TOGGLE_DELAY, new ActionListener() { public void actionPerformed(ActionEvent e) { Icon rmiIcon = getRmiIcon(inRmiCall.get()); setIcon(rmiIcon); } }); timer.setRepeats(true); timer.start(); // Registers with the "publisher" for notification when the Server status changes. ServerStatusValidator.getInstance().addPropertyChangeListener(this); } private void initialize() { Icon rmiIcon = getRmiIcon(false); setIcon(rmiIcon); } public void setShowingRmiCall(boolean inRmiCall) { this.inRmiCall.set(inRmiCall); } Icon getRmiIcon(boolean inRmiCall) { Icon rmiIcon; boolean serverRunning = isServerRunning.get(); if (inRmiCall) { rmiIcon = RESOURCES.getInRmiCallIcon(); if (!serverRunning) rmiIcon = RESOURCES.getInRmiCallIconNoServer(); } else { rmiIcon = RESOURCES.getNotInRmiCallIcon(); if (!serverRunning) rmiIcon = RESOURCES.getNotInRmiCallIconNoServer(); } return rmiIcon; } public void propertyChange(PropertyChangeEvent evt) { // Synchronized guarantees Atomicity, which allows other threads to see // changes in the variables like "isServerRunning" isServerRunning.set((Boolean) evt.getNewValue()); // System.out.println("--[>isServerRunning: " + isServerRunning); } }