/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unibas.bartgui.view.panel.editor.database;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.WeakListeners;

/**
 *
 * @author Grandinetti Giovanni <[email protected]>
 */
public class MainMemoryPanel extends javax.swing.JPanel {

    /**
     * Creates new form MainMemoryPanel
     */
    public MainMemoryPanel() {
        initComponents();
        init();
        initButton();
    }

    private void init()  {
        getXmlSchemaTextField().setName("XML-Schema");
        getXmlInstanceTextField().setName("XML-Instance");
    }
    
    private void initButton()   {
        choosexmlInstanxceButton.addActionListener(
                WeakListeners.create(ActionListener.class,
                        new buttonListener(xmlInstanceTextField),
                        this));
        choosexmlSchemaButton.addActionListener(
                WeakListeners.create(ActionListener.class,
                        new buttonListener(xmlSchemaTextField),
                        this));
    }
    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        xmlSchemaTextField = new javax.swing.JTextField();
        choosexmlSchemaButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        xmlInstanceTextField = new javax.swing.JTextField();
        choosexmlInstanxceButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 51, 255))); // NOI18N

        xmlSchemaTextField.setText(org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.xmlSchemaTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(choosexmlSchemaButton, org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.choosexmlSchemaButton.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(xmlSchemaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(choosexmlSchemaButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(xmlSchemaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(choosexmlSchemaButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 102, 255))); // NOI18N

        xmlInstanceTextField.setText(org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.xmlInstanceTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(choosexmlInstanxceButton, org.openide.util.NbBundle.getMessage(MainMemoryPanel.class, "MainMemoryPanel.choosexmlInstanxceButton.text")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(xmlInstanceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(choosexmlInstanxceButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(xmlInstanceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(choosexmlInstanxceButton))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        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, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton choosexmlInstanxceButton;
    private javax.swing.JButton choosexmlSchemaButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField xmlInstanceTextField;
    private javax.swing.JTextField xmlSchemaTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the choosexmlInstanxceButton
     */
    public javax.swing.JButton getChoosexmlInstanxceButton() {
        return choosexmlInstanxceButton;
    }

    /**
     * @return the choosexmlSchemaButton
     */
    public javax.swing.JButton getChoosexmlSchemaButton() {
        return choosexmlSchemaButton;
    }

    /**
     * @return the xmlInstanceTextField
     */
    public javax.swing.JTextField getXmlInstanceTextField() {
        return xmlInstanceTextField;
    }

    /**
     * @return the xmlSchemaTextField
     */
    public javax.swing.JTextField getXmlSchemaTextField() {
        return xmlSchemaTextField;
    }
    
    private class buttonListener implements ActionListener   {

        private JTextField field;
        
        public buttonListener(JTextField field) {
            this.field = field;
        }
        
        

        @Override
        public void actionPerformed(ActionEvent e) {
            File home = new File(System.getProperty("user.home"));
            File toLoad = new FileChooserBuilder(" 'Instance/Schema' XML File")
                                        .setTitle("Choose File")
                                        .setDefaultWorkingDirectory(home)
                                        .setApproveText("OK")
                                        .setAcceptAllFileFilterUsed(false)
                                        .addFileFilter(new FileNameExtensionFilter("XML import file", "xml","XML"))
                                        .showOpenDialog();
                        
                if(toLoad==null){
                    return;
                }
                FileObject fo = FileUtil.toFileObject(toLoad);
                if(fo.isFolder()){
                    DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message("Select 'file.xml' no folder", NotifyDescriptor.INFORMATION_MESSAGE));
                    return;
                }
                field.setText(fo.getPath());
        }
        
    }
}