/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageSourceEdit.java
 *
 * Created on 04.05.2010, 13:28:32
 */

package csa.config.theme;

import csa.tiletools.graphics.BaseImageData;
import java.awt.image.BufferedImage;
import java.util.Vector;

/**
 *
 * @author malban
 */
public class ImageEdit extends javax.swing.JPanel {

    Vector <BaseImageData> mData = new  Vector <BaseImageData>();
    BaseImageData currentBase = null;

    boolean previewScale = true;
    int inSetting=0;
    int currentSelectedNo=-1;
    boolean newImages = false;


    /** Creates new form ImageSourceEdit */
    public ImageEdit() {
        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() {

        jLabel1 = new javax.swing.JLabel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        singleImagePanel1 = new csa.tiletools.graphics.SingleImagePanel();
        jLabel14 = new javax.swing.JLabel();
        jSliderSourceScale = new javax.swing.JSlider();
        jPanel4 = new javax.swing.JPanel();
        jCheckBoxPreviewScaled = new javax.swing.JCheckBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        singleImagePanel2 = new csa.tiletools.graphics.SingleImagePanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldWidth = new javax.swing.JTextField();
        jTextFieldHeight = new javax.swing.JTextField();
        jTextFieldPosY = new javax.swing.JTextField();
        jTextFieldPosX = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel6.setName("jPanel6"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        singleImagePanel1.setName("singleImagePanel1"); // NOI18N
        singleImagePanel1.setPreferredSize(new java.awt.Dimension(100, 100));
        singleImagePanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                singleImagePanel1MouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                singleImagePanel1MouseMoved(evt);
            }
        });

        javax.swing.GroupLayout singleImagePanel1Layout = new javax.swing.GroupLayout(singleImagePanel1);
        singleImagePanel1.setLayout(singleImagePanel1Layout);
        singleImagePanel1Layout.setHorizontalGroup(
            singleImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );
        singleImagePanel1Layout.setVerticalGroup(
            singleImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 434, Short.MAX_VALUE)
        );

        jScrollPane2.setViewportView(singleImagePanel1);

        jLabel14.setText("Scale * X");
        jLabel14.setName("jLabel14"); // NOI18N

        jSliderSourceScale.setMajorTickSpacing(1);
        jSliderSourceScale.setMaximum(10);
        jSliderSourceScale.setMinimum(1);
        jSliderSourceScale.setMinorTickSpacing(1);
        jSliderSourceScale.setOrientation(javax.swing.JSlider.VERTICAL);
        jSliderSourceScale.setPaintLabels(true);
        jSliderSourceScale.setPaintTicks(true);
        jSliderSourceScale.setSnapToTicks(true);
        jSliderSourceScale.setValue(1);
        jSliderSourceScale.setName("jSliderSourceScale"); // NOI18N
        jSliderSourceScale.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderSourceScaleStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSliderSourceScale, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderSourceScale, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
        );

        jPanel4.setName("jPanel4"); // NOI18N

        jCheckBoxPreviewScaled.setSelected(true);
        jCheckBoxPreviewScaled.setText("Scaled");
        jCheckBoxPreviewScaled.setName("jCheckBoxPreviewScaled"); // NOI18N
        jCheckBoxPreviewScaled.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxPreviewScaledActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N
        jScrollPane3.setPreferredSize(new java.awt.Dimension(105, 110));

        singleImagePanel2.setName("singleImagePanel2"); // NOI18N
        singleImagePanel2.setPreferredSize(new java.awt.Dimension(104, 106));

        javax.swing.GroupLayout singleImagePanel2Layout = new javax.swing.GroupLayout(singleImagePanel2);
        singleImagePanel2.setLayout(singleImagePanel2Layout);
        singleImagePanel2Layout.setHorizontalGroup(
            singleImagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 201, Short.MAX_VALUE)
        );
        singleImagePanel2Layout.setVerticalGroup(
            singleImagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 124, Short.MAX_VALUE)
        );

        jScrollPane3.setViewportView(singleImagePanel2);

        jLabel8.setText("Height");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel7.setText("Width");
        jLabel7.setName("jLabel7"); // NOI18N

        jTextFieldWidth.setText("32");
        jTextFieldWidth.setName("jTextFieldWidth"); // NOI18N
        jTextFieldWidth.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldWidthFocusLost(evt);
            }
        });

        jTextFieldHeight.setText("32");
        jTextFieldHeight.setName("jTextFieldHeight"); // NOI18N
        jTextFieldHeight.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldHeightFocusLost(evt);
            }
        });

        jTextFieldPosY.setText("0");
        jTextFieldPosY.setName("jTextFieldPosY"); // NOI18N

        jTextFieldPosX.setText("0");
        jTextFieldPosX.setName("jTextFieldPosX"); // NOI18N

        jLabel5.setText("PosX");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("PosY");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel4.setText("<html>Current selection</html>");
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 119, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldPosX)
                            .addComponent(jTextFieldPosY)
                            .addComponent(jTextFieldHeight)
                            .addComponent(jTextFieldWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                        .addComponent(jCheckBoxPreviewScaled)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldPosX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldPosY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jCheckBoxPreviewScaled))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel20.setText("Coords:");
        jLabel20.setName("jLabel20"); // NOI18N

        jLabel21.setText("100 / 200");
        jLabel21.setName("jLabel21"); // NOI18N

        jLabel22.setText("Size:");
        jLabel22.setName("jLabel22"); // NOI18N

        jLabel23.setText("100 / 200");
        jLabel23.setName("jLabel23"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel20)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel21)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel23)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, 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(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jLabel22)
                    .addComponent(jLabel23)
                    .addComponent(jLabel21))
                .addContainerGap(142, Short.MAX_VALUE))
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSliderSourceScaleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderSourceScaleStateChanged
        if (inSetting>0) return;
        singleImagePanel1.setScale(jSliderSourceScale.getValue());
        // jScrollPane2.getViewport().setView(singleImagePanel1);
    }//GEN-LAST:event_jSliderSourceScaleStateChanged

    private void singleImagePanel1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_singleImagePanel1MouseMoved

        int x = evt.getX()/jSliderSourceScale.getValue();
       int y = evt.getY()/jSliderSourceScale.getValue();
       jLabel21.setText(""+x +" / "+y);
       jLabel23.setText(""+singleImagePanel1.selWidth +" / "+singleImagePanel1.selHeight);

    }//GEN-LAST:event_singleImagePanel1MouseMoved


    private void singleImagePanel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_singleImagePanel1MouseDragged

       int x = evt.getX()/jSliderSourceScale.getValue();
       int y = evt.getY()/jSliderSourceScale.getValue();
       jLabel21.setText(""+x +" / "+y);
       jLabel23.setText(""+singleImagePanel1.selWidth +" / "+singleImagePanel1.selHeight);
       currentBase = singleImagePanel1.getSelection();
       jTextFieldPosY.setText(""+currentBase.y);
       jTextFieldPosX.setText(""+currentBase.x);
       jTextFieldWidth.setText(""+currentBase.w);
       jTextFieldHeight.setText(""+currentBase.h);

        if (previewScale)
           singleImagePanel2.setBaseImage(currentBase.image, 100,100);
        else
           singleImagePanel2.setBaseImage(currentBase.image);
    }//GEN-LAST:event_singleImagePanel1MouseDragged

    private void jTextFieldWidthFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldWidthFocusLost
        
    }//GEN-LAST:event_jTextFieldWidthFocusLost

    private void jTextFieldHeightFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldHeightFocusLost
        
    }//GEN-LAST:event_jTextFieldHeightFocusLost

    private void jCheckBoxPreviewScaledActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxPreviewScaledActionPerformed

        previewScale = jCheckBoxPreviewScaled.isSelected();
        if (previewScale)
            singleImagePanel2.setScale(100, 100);
        else
            singleImagePanel2.unsetScale();
    }//GEN-LAST:event_jCheckBoxPreviewScaledActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox jCheckBoxPreviewScaled;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSlider jSliderSourceScale;
    private javax.swing.JTextField jTextFieldHeight;
    private javax.swing.JTextField jTextFieldPosX;
    private javax.swing.JTextField jTextFieldPosY;
    private javax.swing.JTextField jTextFieldWidth;
    private csa.tiletools.graphics.SingleImagePanel singleImagePanel1;
    private csa.tiletools.graphics.SingleImagePanel singleImagePanel2;
    // End of variables declaration//GEN-END:variables

    void setImage(BufferedImage image)
    {
        
        singleImagePanel1.setImage(image);
        if (image != null)
        {
            jTextFieldWidth.setText(""+image.getWidth(null));
            jTextFieldHeight.setText(""+image.getHeight(null));
        }
    }

    Vector <BaseImageData> readAllToCurrent()
    {
        return mData;
    }
    
    public int getImageX()
    {
        return csa.util.UtilityString.Int0(jTextFieldPosX.getText());
    }
    public int getImageY()
    {
        return csa.util.UtilityString.Int0(jTextFieldPosY.getText());
    }
    public int getImageWidth()
    {
        return csa.util.UtilityString.Int0(jTextFieldWidth.getText());
    }
    public int getImageHeight()
    {
        return csa.util.UtilityString.Int0(jTextFieldHeight.getText());
    }
}
