/*
 * Copyright (c) 1998-2014 ChemAxon Ltd. All Rights Reserved.
 */

import chemaxon.marvin.beans.MSketchPane;
import com.jgoodies.forms.factories.Borders;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * The main goal of this example is to show how to create and embed the
 * MarvinSketch bean in a Swing container with some
 * additional functionalities such as getting and setting molecules.
 *
 * For the detailed description of this example, please visit
 * http://www.chemaxon.com/marvin/examples/beans/sketch-simple/index.html
 *
 * @author  Judit Vasko-Szedlar
 * @version 5.0.3 Apr 10, 2008
 * @since   Marvin 5.0.3
 */
public class SketchSimple extends JPanel {

    public static final int MANUAL = 0;
    public static final int IMPORTER = 1;
    public static final int EXPORTER = 2;

    private int behavior = MANUAL;
    private String format = "mol";
    private JTextArea textArea;

    private MSketchPane sketchPane;

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                createAndShowGUI();
            }
        });
    }

    /**
     * Create the GUI and show it. For thread safety,
     * this method should be invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame sketch = new JFrame();
        sketch.setTitle("MarvinSketch Simple Beans Example");
        sketch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SketchSimple sketchSimple = new SketchSimple();
        sketch.getContentPane().setLayout(new BorderLayout());
        sketch.getContentPane().add(sketchSimple, BorderLayout.CENTER);

        sketch.pack();
        sketch.setLocationRelativeTo(null);
        sketch.setVisible(true);
        sketch.requestFocus();
    }

    public SketchSimple() {
        // Creating the MarvinSketch JavaBean component.
        // The component is ready and can simply added to
        // any Swing container.
        sketchPane = new MSketchPane();

        // Listening any molecule changes using
        // "mol" property change listener.
        // In this example, we "auto-exported" the molecule in this case.
        sketchPane.addPropertyChangeListener("mol",
            new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if(behavior == EXPORTER) {
                        exportActionPerformed();
                    }
                }
        });

        JPanel importExportPanel = createImportExportPanel();

        setLayout(new BorderLayout());
        add(sketchPane, BorderLayout.CENTER);
        add(importExportPanel, BorderLayout.EAST);
    }

    // Imports the content of the textarea to the MarvinSketch bean
    // component as molecule string, without specifying format.
    // MSketchPane automatically detects the format of the molecule.
    private void importActionPerformed() {
        String molS = textArea.getText();
        sketchPane.setMol(molS);
    }

    // Exports the current molecule being visible on the MarvinSketch
    // canvas in the desired format.
    private void exportActionPerformed() {
        // getting the current molecule from the bean in required format
        String molS = sketchPane.getMol(format);
        // setting the molecule source string to textarea
        textArea.setText( molS );
    }

    // Creating and laying out GUI components.
    // The code was generated by JFormDesigner
    private JPanel createImportExportPanel() {
        JPanel buttonPanel = new JPanel();
        GridBagLayout gbLayout = new GridBagLayout();
        buttonPanel.setLayout(gbLayout);
        gbLayout.columnWidths = new int[] {80, 0, 80, 75, 0};
        gbLayout.rowHeights =new int[] {0, 0, 0};
        gbLayout.columnWeights = new double[] {0.0, 1.0, 1.0, 0.0, 1.0E-4};
        gbLayout.rowWeights = new double[] {0.0, 0.0, 1.0E-4};

        JLabel behaviorLabel = new JLabel("Behavior:");
        buttonPanel.add(behaviorLabel,
                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));
        JComboBox behaviorComboBox = new JComboBox();
        behaviorComboBox.setModel(new DefaultComboBoxModel(new String[] {
                "Manual",
                "Auto-Importer",
                "Auto-Exporter"
        }));
        behaviorComboBox.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                behavior = cb.getSelectedIndex();
            }
        });

        buttonPanel.add(behaviorComboBox,
                new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 5), 0, 0));

        JButton importButton = new JButton();
        importButton.setText("Import");
        importButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                importActionPerformed();
            }
        });
        buttonPanel.add(importButton,
                new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));

        JLabel formatLabel = new JLabel("Export format:");
        buttonPanel.add(formatLabel,
                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 5), 0, 0));

        JComboBox formatComboBox = new JComboBox();
        formatComboBox.setModel(new DefaultComboBoxModel(new String[] {
                "cml", "csmol", "csrdf", "csrxn", "cssdf",
                "mol", "mrv", "name", "rdf", "rxn", "smarts",
                "smiles", "sdf", "sybyl"
        }));
        formatComboBox.setSelectedItem(format);
        formatComboBox.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                format = (String)cb.getSelectedItem();
            }
        });
        buttonPanel.add(formatComboBox,
                new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 5), 0, 0));

        JButton exportButton = new JButton();
        exportButton.setText("Export");
        exportButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exportActionPerformed();
            }
        });
        buttonPanel.add(exportButton,
                new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

        JLabel title = new JLabel("Molecule source");
        JScrollPane scrollPane = new JScrollPane();
        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        textArea.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
            }

            public void insertUpdate(DocumentEvent e) {
                if(behavior == IMPORTER) {
                    importActionPerformed();
                }
            }

            public void removeUpdate(DocumentEvent e) {
                if(behavior == IMPORTER) {
                    importActionPerformed();
                }
            }
        });

        JPanel panel = new JPanel();
        panel.setBorder(Borders.TABBED_DIALOG_BORDER);
        GridBagLayout gbpLayout = new GridBagLayout();
        panel.setLayout(gbpLayout);
        gbpLayout.columnWidths = new int[] {0, 0};
        gbpLayout.rowHeights = new int[] {5, 0, 11, 0, 5, 0, 0};
        gbpLayout.columnWeights = new double[] {1.0, 1.0E-4};
        gbpLayout.rowWeights =
                new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};

        panel.add(buttonPanel,
                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        panel.add(title,
                new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        panel.add(scrollPane, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));


        return panel;
    }
}