/**
* Copyright 2014 Comcast Cable Communications Management, LLC
*
* This file is part of CATS.
*
* CATS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CATS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CATS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.comcast.cats.vision.panel.power;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import org.apache.log4j.Logger;
import com.comcast.cats.Settop;
import com.comcast.cats.event.CatsEventDispatcher;
import com.comcast.cats.vision.event.ActionCommand;
import com.comcast.cats.vision.event.PowerEvent;
import com.comcast.cats.vision.panel.videogrid.model.GridDataModel;
import com.comcast.cats.vision.util.CatsVisionUtils;
import static com.comcast.cats.vision.util.CatsVisionConstants.PLEASE_ALLOCATE_MSG;
import static com.comcast.cats.vision.util.CatsVisionConstants.POWER_OFF;
import static com.comcast.cats.vision.util.CatsVisionConstants.POWER_ON;
import static com.comcast.cats.vision.util.CatsVisionConstants.POWER_REBOOT;
/**
* PowerPanel
*
* @author cfrede001
*/
public class PowerPanel extends javax.swing.JPanel implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -1960618190919879257L;
public static final Font DEFAULT_FONT = new Font( "Arial", Font.BOLD, 10 );
public static final Integer BUTTON_WIDTH = 70;
public static final Integer BUTTON_HEIGHT = 20;
private Dimension BUTTON_DIMENSION = new Dimension( BUTTON_WIDTH, BUTTON_HEIGHT );
private JButton jOffButton;
private JButton jOnButton;
private JButton jRebootButton;
private static final Dimension DIMENSION = new Dimension( 275, 45 );
private static final Dimension POWER_PANEL_SIZE = new Dimension( 270, 45 );
private CatsEventDispatcher dispatcherThreaded;
private GridDataModel gridDataModel;
private Set< Settop > allocatedSettops = new LinkedHashSet< Settop >();
/**
* default logger.
*/
private static final Logger logger = Logger.getLogger( PowerPanel.class );
/**
* Constructor for GridPowerPanel
*
* @param gridDataModel
* instance of GridDataModel
* @param dispatcher
* instance of CatsEventDispatcher
*/
public PowerPanel( GridDataModel gridDataModel, CatsEventDispatcher dispatcher )
{
if ( logger.isDebugEnabled() )
{
logger.debug( "Creating GridPowerPanel (panel which holds power buttons)." );
}
dispatcherThreaded = dispatcher;
this.gridDataModel = gridDataModel;
initComponents();
setPreferredSize( POWER_PANEL_SIZE );
setMinimumSize( DIMENSION );
setSize( DIMENSION );
setName( "PowerPanel" );
setBorder( BorderFactory.createLineBorder( Color.BLACK, 1 ) );
setBackground( Color.LIGHT_GRAY );
setName( "gridPowerPanel" );
}
public void pressPowerButton( String buttonName )
{
allocatedSettops = gridDataModel.getAllocatedAndSelectedSettops();
if ( ( allocatedSettops != null ) && !( allocatedSettops.isEmpty() ) )
{
if ( logger.isDebugEnabled() )
{
logger.debug( "Calling GridPowerPanel- pressPowerButton" );
}
dispatcherThreaded
.sendCatsEvent( new PowerEvent( ActionCommand.parse( buttonName ), "GridPowerPanel", this ) );
}
else
{
CatsVisionUtils
.showWarning( "Unable to perform the operation", PLEASE_ALLOCATE_MSG + "Hard power buttons." );
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
private void initComponents()
{
jOnButton = new JButton();
jOffButton = new JButton();
jRebootButton = new JButton();
this.setLayout( new GridBagLayout() );
JLabel label = new JLabel( "Hard Power Commands" );
this.setSize( 150, 30 );
jOnButton.setText( POWER_ON );
jOnButton.setSize( BUTTON_DIMENSION );
jOnButton.setPreferredSize( BUTTON_DIMENSION );
jOnButton.setFont( DEFAULT_FONT );
jOnButton.setFocusable( false );
jOnButton.addActionListener( this );
jOffButton.setText( POWER_OFF );
jOffButton.setSize( BUTTON_DIMENSION );
jOffButton.setPreferredSize( BUTTON_DIMENSION );
jOffButton.setFont( DEFAULT_FONT );
jOffButton.setFocusable( false );
jOffButton.addActionListener( this );
jRebootButton.setText( POWER_REBOOT );
jRebootButton.setSize( BUTTON_DIMENSION );
jRebootButton.setPreferredSize( BUTTON_DIMENSION );
jRebootButton.setFont( DEFAULT_FONT );
jRebootButton.setFocusable( false );
jRebootButton.addActionListener( this );
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 3;
this.add( label, c );
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
this.add( jOnButton, c );
c.gridx = 1;
this.add( jOffButton, c );
c.gridx = 2;
this.add( jRebootButton, c );
}
@Override
public void actionPerformed( java.awt.event.ActionEvent evt )
{
if ( evt.getSource() instanceof JButton )
{
JButton powerButton = ( JButton ) evt.getSource();
String text = powerButton.getText();
if ( ( text.equals( POWER_ON ) ) || ( text.equals( POWER_OFF ) ) || ( text.equals( POWER_REBOOT ) ) )
{
pressPowerButton( text );
}
}
}
}