/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 *
 * Copyright (C) 2004-2023 Wandora Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * SimpleEmailExtractorPanel.java
 *
 * Created on 9.12.2010, 12:42:50
 */

package org.wandora.application.tools.extractors.email;

import java.io.File;

import javax.swing.JDialog;

import org.wandora.application.Wandora;
import org.wandora.application.gui.UIBox;
import org.wandora.application.gui.UIConstants;
import org.wandora.application.gui.simple.SimpleButton;
import org.wandora.application.gui.simple.SimpleComboBox;
import org.wandora.application.gui.simple.SimpleField;
import org.wandora.application.gui.simple.SimpleFileChooser;
import org.wandora.application.gui.simple.SimpleLabel;

/**
 *
 * @author akivela
 */
public class SimpleEmailExtractorPanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;

	public static int EMAIL_RESOURCE = 1;
    public static int MBOX_EMAIL_REPOSITORY = 2;
    public static int DBX_EMAIL_REPOSITORY = 3;


    private int emailResourceType = EMAIL_RESOURCE;

    private Wandora wandora = null;
    private JDialog dialog = null;
    private boolean isAccepted =  false;
    private static String currentFile = null;
    
    


    /** Creates new form SimpleEmailExtractorPanel */
    public SimpleEmailExtractorPanel(Wandora wandora) {
        this.wandora = wandora;
        initComponents();
        typeComboBox.setEditable(false);
    }



    public void showDialog() {
        if(dialog == null) {
            dialog = new JDialog(wandora, true);
            dialog.setTitle("Simple email extractor");
            dialog.add(this);
            dialog.setSize(650,200);
            UIBox.centerWindow(dialog, wandora);
        }
        isAccepted = false;
        dialog.setVisible(true);
    }



    public int getEmailResourceType() {
        Object ro = typeComboBox.getSelectedItem();
        if(ro != null) {
            String rs = ro.toString();
            if("Single email file".equals(rs)) {
                return EMAIL_RESOURCE;
            }
            else if("MBOX email repository".equals(rs)) {
                return MBOX_EMAIL_REPOSITORY;
            }
            else if("DBX email repository".equals(rs)) {
                return DBX_EMAIL_REPOSITORY;
            }
        }
        return emailResourceType;
    }
    
    
    
    public String getResourceAddress() {
        return resourceTextField.getText();
    }


    public boolean wasAccepted() {
        return isAccepted;
    }


    

    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        emailPanelInner = new javax.swing.JPanel();
        descriptionLabel = new SimpleLabel();
        optionsPanel = new javax.swing.JPanel();
        typeLabel = new SimpleLabel();
        typeComboBox = new SimpleComboBox();
        resourceLabel = new SimpleLabel();
        resourceTextField = new SimpleField();
        fileButton = new SimpleButton();
        buttonPanel = new javax.swing.JPanel();
        buttonFillerPanel = new javax.swing.JPanel();
        extractButton = new SimpleButton();
        cancelButton2 = new SimpleButton();

        setLayout(new java.awt.GridBagLayout());

        emailPanelInner.setLayout(new java.awt.GridBagLayout());

        descriptionLabel.setText("<html>Simple email extractor can extract single email files and MBOX email repositories. First select resource type. Then locate the email resource file and press Extract button.</html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
        emailPanelInner.add(descriptionLabel, gridBagConstraints);

        optionsPanel.setLayout(new java.awt.GridBagLayout());

        typeLabel.setText("Resource type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 5);
        optionsPanel.add(typeLabel, gridBagConstraints);

        typeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single email file", "MBOX email repository" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        optionsPanel.add(typeComboBox, gridBagConstraints);

        resourceLabel.setText("Email resource");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        optionsPanel.add(resourceLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        optionsPanel.add(resourceTextField, gridBagConstraints);

        fileButton.setText("Browse");
        fileButton.setMargin(new java.awt.Insets(0, 2, 0, 2));
        fileButton.setMaximumSize(new java.awt.Dimension(70, 23));
        fileButton.setMinimumSize(new java.awt.Dimension(70, 23));
        fileButton.setPreferredSize(new java.awt.Dimension(70, 23));
        fileButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                fileButtonMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        optionsPanel.add(fileButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 12, 0);
        emailPanelInner.add(optionsPanel, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        javax.swing.GroupLayout buttonFillerPanelLayout = new javax.swing.GroupLayout(buttonFillerPanel);
        buttonFillerPanel.setLayout(buttonFillerPanelLayout);
        buttonFillerPanelLayout.setHorizontalGroup(
            buttonFillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        buttonFillerPanelLayout.setVerticalGroup(
            buttonFillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        buttonPanel.add(buttonFillerPanel, gridBagConstraints);

        extractButton.setText("Extract");
        extractButton.setMaximumSize(new java.awt.Dimension(70, 23));
        extractButton.setMinimumSize(new java.awt.Dimension(70, 23));
        extractButton.setPreferredSize(new java.awt.Dimension(70, 23));
        extractButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                extractButtonMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
        buttonPanel.add(extractButton, gridBagConstraints);

        cancelButton2.setText("Cancel");
        cancelButton2.setMaximumSize(new java.awt.Dimension(70, 23));
        cancelButton2.setMinimumSize(new java.awt.Dimension(70, 23));
        cancelButton2.setPreferredSize(new java.awt.Dimension(70, 23));
        cancelButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cancelButton2MouseReleased(evt);
            }
        });
        buttonPanel.add(cancelButton2, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        emailPanelInner.add(buttonPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        add(emailPanelInner, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents



    
    private void extractButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_extractButtonMouseReleased
        isAccepted = true;
        if(dialog != null) dialog.setVisible(false);
    }//GEN-LAST:event_extractButtonMouseReleased

    private void cancelButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButton2MouseReleased
        isAccepted = false;
        if(dialog != null) dialog.setVisible(false);
    }//GEN-LAST:event_cancelButton2MouseReleased

    private void fileButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileButtonMouseReleased
        SimpleFileChooser fc = UIConstants.getFileChooser();
        fc.setDialogTitle("Select email resource");
        if(currentFile != null) {
            fc.setSelectedFile(new File(currentFile));
        }
        int answer = fc.open(dialog, SimpleFileChooser.OPEN_DIALOG, "Select");
        if(answer == SimpleFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            if(f != null) {
                resourceTextField.setText(f.getAbsolutePath());
                currentFile = f.getAbsolutePath();
            }
        }
    }//GEN-LAST:event_fileButtonMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonFillerPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton2;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JPanel emailPanelInner;
    private javax.swing.JButton extractButton;
    private javax.swing.JButton fileButton;
    private javax.swing.JPanel optionsPanel;
    private javax.swing.JLabel resourceLabel;
    private javax.swing.JTextField resourceTextField;
    private javax.swing.JComboBox typeComboBox;
    private javax.swing.JLabel typeLabel;
    // End of variables declaration//GEN-END:variables

}
