// @topic T11736 2D Graphics demo 03 -- mouse events added
// @brief class <tt>ViewDesigner</tt> extends <tt>JFrame</tt>
/*
 * ViewSimulation.java
 *
 * Created on Mar 26, 2013, 10:00:02 AM
 */

package shopsimulation;

import java.awt.Shape;
import java.awt.geom.Line2D;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ViewDesigner extends javax.swing.JFrame {

    /** Creates new form ViewSimulation */
    public ViewDesigner( String title ) {
        super( title );
        initComponents();
        d2CanvasPanel.setWindow(this);
    }

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

        pnlBottom = new javax.swing.JPanel();
        btnDraw = new javax.swing.JButton();
        txtMouseCoord = new javax.swing.JTextField();
        lblMouseCoord = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDrawingPrimitives = new javax.swing.JTextArea();
        lblPrimitives = new javax.swing.JLabel();
        d2CanvasPanel = new shopsimulation.D2Canvas();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnDraw.setText("Draw");
        btnDraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDrawActionPerformed(evt);
            }
        });

        lblMouseCoord.setText("MouseCoord:");

        txtDrawingPrimitives.setColumns(20);
        txtDrawingPrimitives.setRows(5);
        jScrollPane1.setViewportView(txtDrawingPrimitives);

        lblPrimitives.setText("Drawing Primitives:");

        javax.swing.GroupLayout pnlBottomLayout = new javax.swing.GroupLayout(pnlBottom);
        pnlBottom.setLayout(pnlBottomLayout);
        pnlBottomLayout.setHorizontalGroup(
            pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBottomLayout.createSequentialGroup()
                .addGroup(pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlBottomLayout.createSequentialGroup()
                        .addGroup(pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlBottomLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(btnDraw))
                            .addGroup(pnlBottomLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(lblMouseCoord)
                                .addGap(4, 4, 4)
                                .addComponent(txtMouseCoord, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE))
                    .addGroup(pnlBottomLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblPrimitives)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlBottomLayout.setVerticalGroup(
            pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBottomLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlBottomLayout.createSequentialGroup()
                        .addGroup(pnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblMouseCoord)
                            .addComponent(txtMouseCoord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(13, 13, 13)
                        .addComponent(btnDraw)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblPrimitives))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout d2CanvasPanelLayout = new javax.swing.GroupLayout(d2CanvasPanel);
        d2CanvasPanel.setLayout(d2CanvasPanelLayout);
        d2CanvasPanelLayout.setHorizontalGroup(
            d2CanvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        d2CanvasPanelLayout.setVerticalGroup(
            d2CanvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 232, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(d2CanvasPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlBottom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(d2CanvasPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlBottom, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
        );

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

    private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed
        String str = getTxtDrawingPrimitives().getText(); // everything
        String[] primitives = str.split("\n");  // individual lines
        for ( String command : primitives ) {   // for each line
            String[] parameters = command.split( " " ); // get params
            if ( parameters.length == 0 ) {
                continue; // ignore blank lines
            } else if ( parameters[0].length() == 0 ) {
                continue;
            }
            switch ( parameters[0].charAt( 0 ) ) {
                case 'l': // add new line
                case 'L': // add new line
                    if ( parameters.length != 5 ) {
                        getTxtDrawingPrimitives().append(
                                "\n;*** Bad command! ***"
                                );
                    }
                    Shape shape = new Line2D.Double(
                            Double.parseDouble(parameters[ 1 ]),
                            Double.parseDouble(parameters[ 2 ]),
                            Double.parseDouble(parameters[ 3 ]),
                            Double.parseDouble(parameters[ 4 ])
                            );
                    getCanvas().addShape( shape );
                    break;
                case ';': continue; // comment
            }
        }
    }//GEN-LAST:event_btnDrawActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ViewDesigner window = new ViewDesigner(
                        "Scratch Pad Designer" );
                window.setVisible(true);

                // create and start new thread:
                ThreadPainter thp = new ThreadPainter( window );
                Thread th = new Thread( thp );
                th.start();

            }
        });
    }

    // Getters and Setters
    public D2Canvas getCanvas()
    {
        return d2CanvasPanel;
    }

    public JTextField getTxtMouseCoord()
    {
        return txtMouseCoord;
    }

    public JTextArea getTxtDrawingPrimitives()
    {
        return txtDrawingPrimitives;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDraw;
    private shopsimulation.D2Canvas d2CanvasPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblMouseCoord;
    private javax.swing.JLabel lblPrimitives;
    private javax.swing.JPanel pnlBottom;
    private javax.swing.JTextArea txtDrawingPrimitives;
    private javax.swing.JTextField txtMouseCoord;
    // End of variables declaration//GEN-END:variables

}