/* * Created on 12.08.2004 * * @author Jens Guenther */ package de.unirostock.AbstractFactory.example.awt; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import de.unirostock.AbstractFactory.example.ShipFactory; public final class ChooseFactoryPanel extends JPanel implements ActionListener { /** * * @uml.property name="controller" * @uml.associationEnd multiplicity="(0 1)" */ private MainController itsController; /** * * @uml.property name="avaiableFactorys" * @uml.associationEnd multiplicity="(0 -1)" */ private ShipFactory[] itsAvaiableFactorys; private int itsActFactoryNum; private JLabel itsClassNameLabel; private JLabel itsImageLabel; private final static String BT_BACK = "BT_BACK"; private final static String BT_FWD = "BT_FWD"; public ChooseFactoryPanel( MainController controller ) { itsController = controller; itsAvaiableFactorys = itsController.getAvaiableShipFactorys(); itsActFactoryNum = 0; initGraphics(); } public ShipFactory getSelectedShipFactory(){ return itsAvaiableFactorys[itsActFactoryNum]; } public void actionPerformed( ActionEvent e ) { String cmd = e.getActionCommand(); if( cmd.equals( BT_BACK )) prevFactory(); if( cmd.equals( BT_FWD )) nextFactory(); } private void prevFactory() { itsActFactoryNum--; if( itsActFactoryNum == -1 ) itsActFactoryNum = itsAvaiableFactorys.length - 1; itsImageLabel.setIcon( getBookIcon( itsActFactoryNum )); itsClassNameLabel.setText( getFactoryCLassName( itsActFactoryNum )); } private void nextFactory() { itsActFactoryNum++; if( itsActFactoryNum == itsAvaiableFactorys.length ) itsActFactoryNum = 0; itsImageLabel.setIcon( getBookIcon( itsActFactoryNum )); itsClassNameLabel.setText( getFactoryCLassName( itsActFactoryNum )); } private Icon getBookIcon( int num ){ return itsAvaiableFactorys[num].getBookIcon(); } private String getFactoryCLassName( int num ){ String factory = itsAvaiableFactorys[num].getClass().getName(); int lastDot = factory.lastIndexOf( '.' ); return factory.substring( lastDot+1 ); } private void initGraphics() { setBackground( new Color( 255,255,255 )); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout( gridbag ); // image label itsImageLabel = new JLabel( getBookIcon( itsActFactoryNum )); itsImageLabel.setBackground( new Color( 255,255,255 )); c.insets = new Insets( 25,5,5,5 ); c.weightx = 1.0; c.weighty = 0; c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( itsImageLabel, c ); add( itsImageLabel ); // button back JButton btBack = new JButton( " < " ); btBack.setActionCommand( BT_BACK ); btBack.addActionListener( this ); c.insets = new Insets( 25,5,50,5 ); c.weightx = 0; c.weighty = 0; c.fill = GridBagConstraints.NONE; c.gridwidth = 1; gridbag.setConstraints( btBack, c ); add( btBack ); // Label className itsClassNameLabel = new JLabel( getFactoryCLassName( itsActFactoryNum )); itsClassNameLabel.setHorizontalAlignment( JLabel.CENTER ); itsClassNameLabel.setBackground( new Color( 255,255,255 )); c.insets = new Insets( 25,5,50,5 ); c.weightx = 1.0; c.weighty = 0; c.fill = GridBagConstraints.NONE; c.gridwidth = GridBagConstraints.RELATIVE; gridbag.setConstraints( itsClassNameLabel, c ); add( itsClassNameLabel ); // button back JButton btFwd = new JButton( " > " ); btFwd.setActionCommand( BT_FWD ); btFwd.addActionListener( this ); c.insets = new Insets( 25,5,50,5 ); c.weightx = 0; c.weighty = 0; c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( btFwd, c ); add( btFwd ); } }