// @topic T11400 MVC Demo - concatenate strings
// @brief Swing window designed in NetBeans (download NetBeans form file here: <a href="http://www.c-jump.com/bcc/c257c/c257jsample/Swing/wk09_mvc_demo/MainWindow.form"><tt>MainWindow.form</tt></a>)
/*
 * @author ik
 *
 */

package hw4;

public class MainWindow extends javax.swing.JFrame {
    private ControllerConcatenation controller;
    private String txtLeftFocusGainedValue;
    private String txtRightFocusGainedValue;

    /**
     * Creates new form MainWindow
     */
    public MainWindow( ControllerConcatenation controller ) {
        this.controller = controller;
        initComponents();
    }

    /**
     * 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() {

        pnlMain = new javax.swing.JPanel();
        txtLeft = new javax.swing.JTextField();
        txtRight = new javax.swing.JTextField();
        btnConcatenate = new javax.swing.JButton();
        txtResult = new javax.swing.JTextField();
        lblInfo = new javax.swing.JLabel();
        lblResult = new javax.swing.JLabel();

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

        txtLeft.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtLeftFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtLeftFocusLost(evt);
            }
        });

        btnConcatenate.setText("Concatenate");
        btnConcatenate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConcatenateActionPerformed(evt);
            }
        });

        lblInfo.setText("Enter two strings to concatenate and show results:");

        lblResult.setText("Result:");

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addComponent(txtLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtRight, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnConcatenate))
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addComponent(lblResult)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConcatenate))
                .addGap(18, 18, 18)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblResult)
                    .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(154, 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(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

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

    private void btnConcatenateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConcatenateActionPerformed
        //ConcatenationEngine engine = new ConcatenationEngine(
        //        txtLeft.getText(), txtRight.getText() );
        //
        //txtResult.setText(
        //    engine.getResult()
        //);
        controller.concatenate();
        
        String result = controller.getResult();
        
        txtResult.setText( result );
        
    }//GEN-LAST:event_btnConcatenateActionPerformed

    private void txtLeftFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLeftFocusGained
        // When it gains focus, preserve the value
        // so that it can be used on focus lost
        // to reset the result s needed:
        txtLeftFocusGainedValue = txtLeft.getText();
    }//GEN-LAST:event_txtLeftFocusGained

    private void txtLeftFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLeftFocusLost
        if ( txtLeftFocusGainedValue != null
                &&
                !txtLeftFocusGainedValue.equals(txtLeft.getText())
                )
        {
            // just lost focus and the value changed!
            // therefore, rest the result:
            txtResult.setText("");
        }
        
    }//GEN-LAST:event_txtLeftFocusLost

    /**
     * @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(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ControllerConcatenation controller = new ControllerConcatenation();
                //new MainWindow().setVisible(true);
                MainWindow window = new MainWindow( controller );
                controller.setWindow(window);
                window.setVisible(true);
            }
        });
    }
    public String getLeftString()
    {
        return txtLeft.getText();
    }
    
    public String getRightString()
    {
        return txtRight.getText();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConcatenate;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblResult;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JTextField txtLeft;
    private javax.swing.JTextField txtResult;
    private javax.swing.JTextField txtRight;
    // End of variables declaration//GEN-END:variables
}