// @topic S-0303-03-02-02 Java MVC Architecture Demo 3
// @brief Main Application Window (the View)

package radio_button_test;

public class FrmDemo extends javax.swing.JFrame {
    
    //------------------------------
    // data
    //------------------------------
    private ControllerDemo controller;
    private javax.swing.JRadioButton[] arrayRdoFloors;

    //------------------------------
    // constructors
    //------------------------------
    /**
     * Creates new form FrmDemo
     */
    public FrmDemo( ControllerDemo controller ) {
        this.controller = controller;
        initComponents();
        arrayRdoFloors = new javax.swing.JRadioButton[
                grpElevatorShaft1.getButtonCount()
                ];
        arrayRdoFloors[ 0 ] = rdo1;
        arrayRdoFloors[ 1 ] = rdo2;
        arrayRdoFloors[ 2 ] = rdo3;
        arrayRdoFloors[ 3 ] = rdo4;
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grpElevatorShaft1 = new javax.swing.ButtonGroup();
        pnlBackground = new javax.swing.JPanel();
        rdo4 = new javax.swing.JRadioButton();
        rdo3 = new javax.swing.JRadioButton();
        rdo2 = new javax.swing.JRadioButton();
        rdo1 = new javax.swing.JRadioButton();
        btnMove = new javax.swing.JButton();
        btnMoveElevator = new javax.swing.JButton();
        btnAnimate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Radio Button Demo");

        grpElevatorShaft1.add(rdo4);
        rdo4.setText("4");

        grpElevatorShaft1.add(rdo3);
        rdo3.setText("3");

        grpElevatorShaft1.add(rdo2);
        rdo2.setText("2");

        grpElevatorShaft1.add(rdo1);
        rdo1.setText("1");

        btnMove.setText("Move");
        btnMove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveActionPerformed(evt);
            }
        });

        btnMoveElevator.setText("Move");
        btnMoveElevator.setToolTipText("");
        btnMoveElevator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveElevatorActionPerformed(evt);
            }
        });

        btnAnimate.setText("Animate");
        btnAnimate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnimateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBackgroundLayout = new javax.swing.GroupLayout(pnlBackground);
        pnlBackground.setLayout(pnlBackgroundLayout);
        pnlBackgroundLayout.setHorizontalGroup(
            pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBackgroundLayout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addGroup(pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBackgroundLayout.createSequentialGroup()
                        .addComponent(btnMove)
                        .addGap(33, 33, 33)
                        .addComponent(btnMoveElevator)
                        .addGap(18, 18, 18)
                        .addComponent(btnAnimate))
                    .addGroup(pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(rdo1)
                        .addComponent(rdo2)
                        .addComponent(rdo3)
                        .addComponent(rdo4)))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        pnlBackgroundLayout.setVerticalGroup(
            pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBackgroundLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(rdo4)
                .addGap(18, 18, 18)
                .addComponent(rdo3)
                .addGap(18, 18, 18)
                .addComponent(rdo2)
                .addGap(18, 18, 18)
                .addComponent(rdo1)
                .addGap(66, 66, 66)
                .addGroup(pnlBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnMove)
                    .addComponent(btnMoveElevator)
                    .addComponent(btnAnimate))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlBackground, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlBackground, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveActionPerformed
        for ( int idx = 0; idx < arrayRdoFloors.length; ++idx) {
            if ( idx == arrayRdoFloors.length - 1) {
                // top floor
                arrayRdoFloors[0].setSelected(true);
                break;
            }
            if ( arrayRdoFloors[idx].isSelected() ) {
                arrayRdoFloors[idx + 1].setSelected(true);
                break;
            }
        }
        /*
        if ( rdo1.isSelected() ) {
            rdo2.setSelected(true);
        } else if ( rdo2.isSelected() ) {
            rdo3.setSelected(true);
        } else if ( rdo3.isSelected() ) {
            rdo4.setSelected(true);
        } else if ( rdo4.isSelected() ) {
            rdo1.setSelected(true);
        } else {
            rdo1.setSelected(true);
        }
        */
    }//GEN-LAST:event_btnMoveActionPerformed

    public void animate()
    {
        controller.getElevatorBank().getElevator(0/*first available*/).move();
        updateView();
    }
    
    private void btnMoveElevatorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveElevatorActionPerformed
        controller.getElevatorBank().getElevator(0/*first available*/).move();
        updateView();
    }//GEN-LAST:event_btnMoveElevatorActionPerformed

    private void btnAnimateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnimateActionPerformed
        controller.startAnimation();
    }//GEN-LAST:event_btnAnimateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        ControllerDemo controller = new ControllerDemo();

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // this code executes on the swing EDT thread:
                //new FrmDemo().setVisible(true);
                FrmDemo window = new FrmDemo( controller );
                controller.setWindow(window);
                window.updateView();
                window.setVisible(true);
            }
        });
    }
    
    void updateView()
    {
        ElevatorBank bank = controller.getElevatorBank();
        Elevator elevator =
                bank.getElevator( 0/*debug first available elevator*/ );
        arrayRdoFloors[
                elevator.getCurrentFloor() - 1/*because floor numbers are one-based*/
                ]
                .setSelected( true );
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnimate;
    private javax.swing.JButton btnMove;
    private javax.swing.JButton btnMoveElevator;
    private javax.swing.ButtonGroup grpElevatorShaft1;
    private javax.swing.JPanel pnlBackground;
    private javax.swing.JRadioButton rdo1;
    private javax.swing.JRadioButton rdo2;
    private javax.swing.JRadioButton rdo3;
    private javax.swing.JRadioButton rdo4;
    // End of variables declaration//GEN-END:variables
}