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;
http://www.chemaxon.com/marvin/examples/beans/sketch-simple/index.html
@author
@version
@since
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) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
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() {
sketchPane = new MSketchPane();
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);
}
private void importActionPerformed() {
String molS = textArea.getText();
sketchPane.setMol(molS);
}
private void exportActionPerformed() {
String molS = sketchPane.getMol(format);
textArea.setText( molS );
}
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;
}
}