001package jmri.jmrit.powerpanel; 002 003import javax.swing.Action; 004import jmri.InstanceManager; 005import jmri.PowerManager; 006 007/** 008 * Swing action to create and register a PowerPanelFrame object. 009 * 010 * @author Bob Jacobsen Copyright (C) 2001, 2010 011 */ 012public class PowerButtonAction extends javax.swing.AbstractAction implements java.beans.PropertyChangeListener { 013 014 public PowerButtonAction(String title) { 015 super(title); 016 checkManager(); 017 updateLabel(); 018 } 019 020 public PowerButtonAction() { 021 this(Bundle.getMessage("ButtonPowerOnOff")); 022 } 023 024 void checkManager() { 025 // disable ourself if there is no power Manager 026 if (InstanceManager.getNullableDefault(PowerManager.class) == null) { 027 setEnabled(false); 028 } else { 029 InstanceManager.getDefault(PowerManager.class).addPropertyChangeListener(this); 030 } 031 } 032 033 void updateLabel() { 034 PowerManager p = InstanceManager.getDefault(PowerManager.class); 035 if (p.getPower() != PowerManager.ON) { 036 putValue(Action.NAME, Bundle.getMessage("ButtonSetOn")); 037 } else { 038 putValue(Action.NAME, Bundle.getMessage("ButtonSetOff")); 039 } 040 firePropertyChange(Action.NAME, "", getValue(Action.NAME)); 041 } 042 043 @Override 044 public void propertyChange(java.beans.PropertyChangeEvent ev) { 045 updateLabel(); 046 } 047 048 @Override 049 public void actionPerformed(java.awt.event.ActionEvent e) { 050 try { 051 // alternate power state, updating name 052 PowerManager p = InstanceManager.getNullableDefault(PowerManager.class); 053 if (p == null) { 054 return; 055 } 056 if (p.getPower() != PowerManager.ON) { 057 p.setPower(PowerManager.ON); 058 } else { 059 p.setPower(PowerManager.OFF); 060 } 061 } catch (Exception ex) { 062 return; 063 } 064 updateLabel(); 065 } 066 067}