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

/*
 * INetPictureImportPanel.java
 *
 * Created on 04.05.2012, 11:03:48
 */
package csa.config.theme;

import csa.config.Configuration;
import csa.config.Logable;
import csa.gui.components.CSAView;
import csa.gui.components.ModalInternalFrame;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.net.Proxy;
import java.net.URL;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author salchr
 */
public class INetPictureImportPanel extends javax.swing.JPanel implements INetThemeConstants {

    private CSAView mParent = null;
    Vector<InetImageBaseData> data = null; // needed to build on allMana on the fly!
    
    int inEvent = 0;
    int currentId=-1;
    BufferedImage currentImage = null;
    
    Proxy proxy = Proxy.NO_PROXY;
    public void setProxy(Proxy p)
    {
        proxy = p;
    }
    
    /** Creates new form INetPictureImportPanel */
    public INetPictureImportPanel() {
        initComponents();
        jCheckBoxbuildFromMana.setVisible(false);
        jCheckBoxbuildFromBig.setVisible(false);
        
        
    }
    public void setParentWindow(CSAView jpv)
    {
        mParent = jpv;
    }
    
    /** 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();
        jTextFieldURL = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jTextFieldDir = new javax.swing.JTextField();
        jButtonShow = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldHeight = new javax.swing.JTextField();
        jTextFieldWidth = new javax.swing.JTextField();
        jButtonGetDir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldX = new javax.swing.JTextField();
        jTextFieldY = new javax.swing.JTextField();
        jTextFieldH = new javax.swing.JTextField();
        jTextFieldW = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButtonGet = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldScaleW = new javax.swing.JTextField();
        jTextFieldScaleH = new javax.swing.JTextField();
        jCheckBoxDoScale = new javax.swing.JCheckBox();
        jCheckBoxbuildFromMana = new javax.swing.JCheckBox();
        jCheckBoxbuildFromBig = new javax.swing.JCheckBox();

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

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

        jLabel2.setText("Save as:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Save to:");
        jLabel3.setName("jLabel3"); // NOI18N

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

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

        jButtonShow.setText("Show");
        jButtonShow.setName("jButtonShow"); // NOI18N
        jButtonShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonShowActionPerformed(evt);
            }
        });

        jLabel4.setText("Height:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Width:");
        jLabel5.setName("jLabel5"); // NOI18N

        jTextFieldHeight.setEnabled(false);
        jTextFieldHeight.setName("jTextFieldHeight"); // NOI18N

        jTextFieldWidth.setEnabled(false);
        jTextFieldWidth.setName("jTextFieldWidth"); // NOI18N

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

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

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

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
        );

        jLabel6.setText("Preview:");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel8.setText("start X:");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText("start Y:");
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText("height:");
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setText("width:");
        jLabel11.setName("jLabel11"); // NOI18N

        jTextFieldX.setName("jTextFieldX"); // NOI18N
        jTextFieldX.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldXFocusLost(evt);
            }
        });

        jTextFieldY.setName("jTextFieldY"); // NOI18N
        jTextFieldY.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldYFocusLost(evt);
            }
        });

        jTextFieldH.setName("jTextFieldH"); // NOI18N
        jTextFieldH.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldHFocusLost(evt);
            }
        });

        jTextFieldW.setName("jTextFieldW"); // NOI18N
        jTextFieldW.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldWFocusLost(evt);
            }
        });

        jButton2.setText("Manual edit");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButtonGet.setText("Get");
        jButtonGet.setName("jButtonGet"); // NOI18N
        jButtonGet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGetActionPerformed(evt);
            }
        });

        jButtonSave.setText("Save");
        jButtonSave.setName("jButtonSave"); // NOI18N
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jLabel12.setText("scale width:");
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setText("scale height:");
        jLabel13.setName("jLabel13"); // NOI18N

        jTextFieldScaleW.setName("jTextFieldScaleW"); // NOI18N
        jTextFieldScaleW.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldScaleWFocusLost(evt);
            }
        });

        jTextFieldScaleH.setName("jTextFieldScaleH"); // NOI18N
        jTextFieldScaleH.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldScaleHFocusLost(evt);
            }
        });

        jCheckBoxDoScale.setText("scale it:");
        jCheckBoxDoScale.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jCheckBoxDoScale.setName("jCheckBoxDoScale"); // NOI18N
        jCheckBoxDoScale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxDoScaleActionPerformed(evt);
            }
        });

        jCheckBoxbuildFromMana.setText("build from mana");
        jCheckBoxbuildFromMana.setName("jCheckBoxbuildFromMana"); // NOI18N
        jCheckBoxbuildFromMana.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxbuildFromManaActionPerformed(evt);
            }
        });

        jCheckBoxbuildFromBig.setText("build from big version");
        jCheckBoxbuildFromBig.setName("jCheckBoxbuildFromBig"); // NOI18N
        jCheckBoxbuildFromBig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxbuildFromBigActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(261, 261, 261))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jTextFieldDir, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                                            .addComponent(jTextFieldURL, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButtonGet)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(56, 56, 56)
                                                .addComponent(jButtonGetDir)))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButtonShow)
                                    .addComponent(jButtonSave)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextFieldHeight, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldWidth, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextFieldX, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                                    .addComponent(jTextFieldY)
                                    .addComponent(jTextFieldH)
                                    .addComponent(jTextFieldW))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jCheckBoxbuildFromMana)
                                            .addComponent(jCheckBoxbuildFromBig))
                                        .addGap(22, 22, 22))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jCheckBoxDoScale)
                                        .addGap(46, 46, 46))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel12)
                                            .addComponent(jLabel13))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextFieldScaleH, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTextFieldScaleW, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(22, 22, 22))))
                            .addComponent(jButton2))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonShow)
                    .addComponent(jButtonGet))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldHeight, 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(jLabel5)
                    .addComponent(jTextFieldWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldName, 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(jLabel3)
                    .addComponent(jTextFieldDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonGetDir)
                    .addComponent(jButtonSave))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jTextFieldX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12)
                            .addComponent(jTextFieldScaleW, 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(jLabel9)
                            .addComponent(jTextFieldY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel13)
                            .addComponent(jTextFieldScaleH, 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(jLabel10)
                            .addComponent(jTextFieldH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBoxDoScale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jTextFieldW, 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(jButton2)
                            .addComponent(jCheckBoxbuildFromMana))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBoxbuildFromBig)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void hideSave()
    {
        jLabel3.setVisible(false);
        jTextFieldDir.setVisible(false);
        jButtonGetDir.setVisible(false);
        jButtonSave.setVisible(false);
    }
    
    private void jButtonGetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGetActionPerformed
        inEvent++;
        String urlS = jTextFieldURL.getText();
        if (urlS.length() == 0)
        {
            if (jCheckBoxbuildFromMana.isSelected())
            {
                doCurrentCropPreview();
            }
            return;
        }

        BufferedInputStream in;
        BufferedOutputStream out;

        
        try
        {

            in = new BufferedInputStream(new URL(urlS).openConnection(proxy).getInputStream());
            currentImage = javax.imageio.ImageIO.read( in );
            double scale = 212.0/160.0;

            while (currentImage.getHeight(null)  == -1) ;

            int height = currentImage.getHeight(null);
            int width = currentImage.getWidth(null);
            double scaleLoaded = ((double)width) / ((double)height);

            double percent = ((double)212.0) / ((double)width);
            if (scaleLoaded<=scale)  percent = ((double)160.0) / ((double)height);

            jTextFieldHeight.setText(""+height);
            jTextFieldWidth.setText(""+width);

            if (csa.util.UtilityString.Int0(jTextFieldH.getText()) == 0)
            {
                jTextFieldH.setText(""+height);
                jTextFieldW.setText(""+width);
                jTextFieldX.setText("0");
                jTextFieldY.setText("0");

            }

            Image scaled = csa.util.UtilityImage.imageScale(currentImage, (int)(percent*100)); 
            Icon i = new ImageIcon(scaled);
            jLabel7.setIcon(i);

            in.close();

        } catch(Exception e)
        {
            currentImage = null;
            Configuration.getConfiguration().getDebugEntity().addLog(e,Logable.LOG_ERROR);
        }
        inEvent--;
        doCurrentCropPreview();
        
    }//GEN-LAST:event_jButtonGetActionPerformed

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

        if (currentImage==null) return;

        final ModalInternalFrame f;
        javax.swing.ImageIcon i = new ImageIcon(currentImage);
        JLabel label = new JLabel(i);

        f = new ModalInternalFrame("Picture", Configuration.getConfiguration().getMainFrame().getRootPane(), Configuration.getConfiguration().getMainFrame(), label);
        label.addMouseListener(new java.awt.event.MouseAdapter()
        {
            @Override public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                try { f.setClosed(true); }catch(Throwable e){}
            }
        });
        f.setClosable(true);
        f.setVisible(true);

            

    }//GEN-LAST:event_jButtonShowActionPerformed

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        
        if (currentImage==null) return;
        String name = jTextFieldName.getText();
        if (name.length()==0) return;
        
        BufferedImage bimageCropped2 =getCurrentCalculatedImage();
        if (bimageCropped2==null) return;
                
        String dir = jTextFieldDir.getText();
        if (dir.length() != 0) dir += File.separator;

        String imagePath = dir + name;
        if (name.toUpperCase().indexOf(".PNG")==-1)
            imagePath += ".png";

        File f = new File(imagePath);

        try
        {
//            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));

            javax.imageio.ImageIO.write( bimageCropped2, "png", f );
        } catch(Exception e)
        {
            Configuration.getConfiguration().getDebugEntity().addLog(e,Logable.LOG_ERROR);
        }
    
    }//GEN-LAST:event_jButtonSaveActionPerformed

    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        if (currentImage == null) return;
        ImageEdit panel = new ImageEdit();
        panel.setImage(currentImage);
        mParent.showPanelModal(panel, "Manual image selection", 1000, 750);

        jTextFieldX.setText(""+panel.getImageX());
        jTextFieldY.setText(""+panel.getImageY());
        jTextFieldW.setText(""+panel.getImageWidth());
        jTextFieldH.setText(""+panel.getImageHeight());
        doCurrentCropPreview();
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextFieldXFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldXFocusLost
        doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldXFocusLost

    private void jTextFieldYFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldYFocusLost
        doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldYFocusLost

    private void jTextFieldHFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldHFocusLost
        doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldHFocusLost

    private void jTextFieldWFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldWFocusLost
        doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldWFocusLost

    private void jCheckBoxDoScaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxDoScaleActionPerformed
        if (inEvent!=0) return;
        doCurrentCropPreview();
    }//GEN-LAST:event_jCheckBoxDoScaleActionPerformed

    private void jTextFieldScaleWFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldScaleWFocusLost
        if (jCheckBoxDoScale.isSelected())
            doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldScaleWFocusLost

    private void jTextFieldScaleHFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldScaleHFocusLost
        if (jCheckBoxDoScale.isSelected())
            doCurrentCropPreview();
    }//GEN-LAST:event_jTextFieldScaleHFocusLost

    private void jCheckBoxbuildFromManaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxbuildFromManaActionPerformed
        if (inEvent>0) return;
        if (jCheckBoxbuildFromMana.isSelected())
        {
            jTextFieldURL.setText("");
            doCurrentCropPreview();
        }
    }//GEN-LAST:event_jCheckBoxbuildFromManaActionPerformed

    private void jCheckBoxbuildFromBigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxbuildFromBigActionPerformed
        if (inEvent>0) return;
        if (jCheckBoxbuildFromBig.isSelected())
        {
            jTextFieldURL.setText("");
            doCurrentCropPreview();
        }
    }//GEN-LAST:event_jCheckBoxbuildFromBigActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonGet;
    private javax.swing.JButton jButtonGetDir;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JButton jButtonShow;
    private javax.swing.JCheckBox jCheckBoxDoScale;
    private javax.swing.JCheckBox jCheckBoxbuildFromBig;
    private javax.swing.JCheckBox jCheckBoxbuildFromMana;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    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.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldDir;
    private javax.swing.JTextField jTextFieldH;
    private javax.swing.JTextField jTextFieldHeight;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldScaleH;
    private javax.swing.JTextField jTextFieldScaleW;
    private javax.swing.JTextField jTextFieldURL;
    private javax.swing.JTextField jTextFieldW;
    private javax.swing.JTextField jTextFieldWidth;
    private javax.swing.JTextField jTextFieldX;
    private javax.swing.JTextField jTextFieldY;
    // End of variables declaration//GEN-END:variables



   public void writeData(InetImageBaseData iData, Vector<InetImageBaseData> allData)
   {
       inEvent++;
       data = allData;
       currentId = iData.id;
       jTextFieldURL.setText(iData.url);
 
       jTextFieldName.setEnabled(iData.id == THEME_GAMEBACK);
       
       if (iData.id == THEME_ALL_MANA_BIG)
           jCheckBoxbuildFromMana.setVisible(true);
       else
           jCheckBoxbuildFromMana.setVisible(false);
           
       if ((iData.id == THEME_ALL_MANA_SMALL) ||
          (iData.id == THEME_BLACK_MANA_SMALL) ||
          (iData.id == THEME_WHITE_MANA_SMALL) ||
          (iData.id == THEME_GREEN_MANA_SMALL) ||
          (iData.id == THEME_RED_MANA_SMALL) ||
          (iData.id == THEME_BLUE_MANA_SMALL) ||
          (iData.id == THEME_CROWN_SMALL) )
           jCheckBoxbuildFromBig.setVisible(true);
       else
           jCheckBoxbuildFromBig.setVisible(false);
               
       
       jTextFieldName.setText((iData.saveName));
       jTextFieldX.setText(""+iData.x);
       jTextFieldY.setText(""+iData.y);
       jTextFieldW.setText(""+iData.w);
       jTextFieldH.setText(""+iData.h);

       jTextFieldScaleW.setText(""+iData.scaleW);
       jTextFieldScaleH.setText(""+iData.scaleH);
       jCheckBoxDoScale.setSelected(iData.doScale);
       
       jTextFieldScaleW.setEnabled(true);
       jTextFieldScaleH.setEnabled(true);
       jCheckBoxDoScale.setEnabled(true);
       jCheckBoxbuildFromMana.setSelected(iData.fromMana);
       jCheckBoxbuildFromBig.setSelected(iData.fromBig);
               
       inEvent--;
       
   }
   public void readData(InetImageBaseData data)
   {
       data.url = jTextFieldURL.getText();
       data.saveName = jTextFieldName.getText();
       data.x = csa.util.UtilityString.Int0(jTextFieldX.getText());
       data.y = csa.util.UtilityString.Int0(jTextFieldY.getText());
       data.w = csa.util.UtilityString.Int0(jTextFieldW.getText());
       data.h = csa.util.UtilityString.Int0(jTextFieldH.getText());

       
       data.scaleW = csa.util.UtilityString.Int0(jTextFieldScaleW.getText());
       data.scaleH = csa.util.UtilityString.Int0(jTextFieldScaleH.getText());

       data.doScale = jCheckBoxDoScale.isSelected();
       data.fromMana = jCheckBoxbuildFromMana.isSelected();
       data.fromBig = jCheckBoxbuildFromBig.isSelected();
       
   }
   
    public void clearAll() 
    {
       jTextFieldURL.setText("");
 
       jTextFieldName.setEnabled(false);
       jTextFieldName.setText("");
       jTextFieldX.setText("");
       jTextFieldY.setText("");
       jTextFieldW.setText("");
       jTextFieldH.setText("");

       jTextFieldScaleW.setText("");
       jTextFieldScaleH.setText("");

       jCheckBoxDoScale.setSelected(false);
       jCheckBoxbuildFromMana.setSelected(false);
    }


    public boolean importImage(InetImageBaseData idata, String name, Vector<InetImageBaseData> allData)
    {
        boolean ret = true;
        String directoryPath = "theme"+File.separator+name;
        writeData(idata, allData);
        jTextFieldDir.setText(directoryPath);
        jButtonGetActionPerformed(null);
        jButtonSaveActionPerformed(null);

        return ret;
    }
    public void setScale(boolean scale, int height, int width)
    {
        inEvent ++;
        jTextFieldScaleW.setText(""+width);
        jTextFieldScaleH.setText(""+height);
        jCheckBoxDoScale.setSelected(scale);
        jTextFieldScaleW.setEnabled(false);
        jTextFieldScaleH.setEnabled(false);
        jCheckBoxDoScale.setEnabled(false);
        doCurrentCropPreview();
        inEvent --;
    }
    
    void doCurrentCropPreview()
    {
        if (currentImage==null)
        {
            if (!jCheckBoxbuildFromMana.isSelected())
            {
                if (!jCheckBoxbuildFromBig.isSelected())
                    return;
            }
        }

        BufferedImage bimageCropped2 = getCurrentCalculatedImage();
        if (bimageCropped2==null) return;
        double scale = 212.0/160.0;

        int height = bimageCropped2.getHeight(null);
        int width = bimageCropped2.getWidth(null);
        double scaleLoaded = ((double)height) / ((double)width);

        double percent = ((double)212.0) / ((double)width);
        if (scaleLoaded<=scale)  percent = ((double)160.0) / ((double)height);

        Image scaled = csa.util.UtilityImage.imageScale(bimageCropped2, (int)(percent*100)); 
        Icon i = new ImageIcon(scaled);
        jLabel7.setIcon(i);
    }

    InetImageBaseData getDataForId(int id)
    {
        InetImageBaseData ret = null;
        for (int i=0; i< data.size(); i++)
        {
            InetImageBaseData c = data.elementAt(i);
            if(id == c.id)
            {
                ret = c;
                break;
            }
        }
        return ret;
    }
    

	
// taken from http://stackoverflow.com/questions/2825837/java-how-to-do-fast-copy-of-a-bufferedimages-pixels-unit-test-included
    private static void copySrcIntoDstAt(final BufferedImage src,
            final BufferedImage dst, final int dx, final int dy) {
        int[] srcbuf = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
        int[] dstbuf = ((DataBufferInt) dst.getRaster().getDataBuffer()).getData();
        int width = src.getWidth();
        int height = src.getHeight();
        int dstoffs = dx + dy * dst.getWidth();
        int srcoffs = 0;
        for (int y = 0 ; y < height ; y++ , dstoffs+= dst.getWidth(), srcoffs += width ) {
            System.arraycopy(srcbuf, srcoffs , dstbuf, dstoffs, width);
        }
    }

    
    BufferedImage getCurrentCalculatedImage()
    {
        BufferedImage ret=null; 

        if (jCheckBoxbuildFromMana.isSelected())
        {
            if (data == null) return ret;
            // size must be 100x100
            INetPictureImportPanel helper = new INetPictureImportPanel();
            InetImageBaseData black = getDataForId(THEME_BLACK_MANA_BIG); if (black ==null) return ret;
            InetImageBaseData blue = getDataForId(THEME_BLUE_MANA_BIG);if (blue ==null) return ret;
            InetImageBaseData white = getDataForId(THEME_WHITE_MANA_BIG);if (white ==null) return ret;
            InetImageBaseData red = getDataForId(THEME_RED_MANA_BIG);if (red ==null) return ret;
            InetImageBaseData green = getDataForId(THEME_GREEN_MANA_BIG);if (green ==null) return ret;
            
            helper.writeData(white, data); helper.jButtonGetActionPerformed(null); BufferedImage whiteImagePre = helper.getCurrentCalculatedImage();
            helper.writeData(black, data); helper.jButtonGetActionPerformed(null); BufferedImage blackImagePre = helper.getCurrentCalculatedImage();
            helper.writeData(red, data); helper.jButtonGetActionPerformed(null);BufferedImage redImagePre = helper.getCurrentCalculatedImage();
            helper.writeData(green, data); helper.jButtonGetActionPerformed(null);BufferedImage greenImagePre = helper.getCurrentCalculatedImage();
            helper.writeData(blue, data); helper.jButtonGetActionPerformed(null);BufferedImage blueImagePre = helper.getCurrentCalculatedImage();
            
            // resize single images to 27x27
            BufferedImage whiteImage = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(whiteImagePre, 27,27)); 
            BufferedImage blackImage = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(blackImagePre, 27,27)); 
            BufferedImage redImage = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(redImagePre, 27,27)); 
            BufferedImage greenImage = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(greenImagePre, 27,27)); 
            BufferedImage blueImage = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(blueImagePre, 27,27)); 
            
            
            // build a clear 100x100 image
            BufferedImage i = new BufferedImage(100,100, whiteImage.getType());
            // Create a graphics context on the buffered image
            Graphics2D g2d = i.createGraphics();            
    
            // Transparency is created on all the filled pixels
            Color transparent = new Color(0, 0, 0, 0);
            g2d.setColor(transparent);
            g2d.setComposite(AlphaComposite.Src);
            g2d.fill(new Rectangle2D.Float(0, 0, 100, 100));
            g2d.dispose();            

            // blue
            copySrcIntoDstAt(blueImage,i, 18,5);
            // black
            copySrcIntoDstAt(blackImage,i, 58,5);
            // white
            copySrcIntoDstAt(whiteImage,i, 8,42);
            // green
            copySrcIntoDstAt(greenImage,i, 68,43);
            // red
            copySrcIntoDstAt(redImage,i, 36,64);
            
            
            ret = i;
        }
        else if (jCheckBoxbuildFromBig.isSelected())
        {
            int sourceId=-1;
            if (currentId == THEME_ALL_MANA_SMALL) sourceId = THEME_ALL_MANA_BIG;
            if (currentId == THEME_BLACK_MANA_SMALL) sourceId = THEME_BLACK_MANA_BIG;
            if (currentId == THEME_WHITE_MANA_SMALL) sourceId = THEME_WHITE_MANA_BIG;
            if (currentId == THEME_GREEN_MANA_SMALL) sourceId = THEME_GREEN_MANA_BIG;
            if (currentId == THEME_RED_MANA_SMALL) sourceId = THEME_RED_MANA_BIG;
            if (currentId == THEME_BLUE_MANA_SMALL) sourceId = THEME_BLUE_MANA_BIG;
            if (currentId == THEME_CROWN_SMALL) sourceId = THEME_CROWN_BIG;
            if (sourceId==-1) return null;
                            

            INetPictureImportPanel helper = new INetPictureImportPanel();
            InetImageBaseData source = getDataForId(sourceId); if (source ==null) return ret;
            
            helper.writeData(source, data); helper.jButtonGetActionPerformed(null); BufferedImage sourceImagePre = helper.getCurrentCalculatedImage();
            
            // resize single images to 16x16
            ret = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(sourceImagePre, 16,16)); 
        }
        else
        {
            if (currentImage==null) return ret;

            int x = csa.util.UtilityString.IntX(jTextFieldX.getText(), 0);
            int y = csa.util.UtilityString.IntX(jTextFieldY.getText(), 0);
            int w = csa.util.UtilityString.IntX(jTextFieldW.getText(), currentImage.getWidth(null));
            int h = csa.util.UtilityString.IntX(jTextFieldH.getText(), currentImage.getHeight(null));

            if (x<0) x=0;
            if (y<0) y=0;
            if (x>currentImage.getWidth(null)) x=currentImage.getWidth(null);
            if (y>currentImage.getHeight(null)) y=currentImage.getHeight(null);
            if (x+w>currentImage.getWidth(null)) w= currentImage.getWidth(null)-x;
            if (y+h>currentImage.getHeight(null)) h= currentImage.getHeight(null)-y;

            if (w==0) return null;
            if (h==0) return null;
            BufferedImage bimage = csa.util.UtilityImage.toBufferedImage(currentImage);

            BufferedImage bimageCropped = bimage.getSubimage(x, y, w, h) ;

            ret = bimageCropped; 
            if (jCheckBoxDoScale.isSelected())
            {
                int sw = csa.util.UtilityString.IntX(jTextFieldScaleW.getText(), 0);
                int sh = csa.util.UtilityString.IntX(jTextFieldScaleH.getText(), 0);

                if ((sw!=0) && (sh!=0))
                {
                    ret = csa.util.UtilityImage.toBufferedImage(csa.util.UtilityImage.imageScale(bimageCropped, sw,sh)); 
                }
            }            
        }
        

        return ret;
    }
    

}
