1 package util;
2 
3 import java.awt.Color;
4 import java.io.IOException;
5 
6 import javax.swing.JFrame;
7 
8 import chemaxon.formats.MolExporter;
9 import chemaxon.marvin.beans.MViewPane;
10import chemaxon.struc.Molecule;
11import chemaxon.util.HitColoringAndAlignmentOptions;
12
13/**
14 * Various utility functions used in the example codes for displaying molecules.
15 * 
16 * @author JChem Base team, ChemAxon Ltd.
17 */
18public final class DisplayUtil {
19
20    private static final int DEFAULT_FRAME_SIZE = 300;
21
22    /**
23     * Returns the SMILES representation of the given molecule.
24     * 
25     * @param molecule input molecule
26     * @return SMILES representation
27     * @throws IllegalArgumentException if SMILES export failed
28     */
29    public static String toSmiles(Molecule molecule) {
30        try {
31            return MolExporter.exportToFormat(molecule, "smiles");
32        } catch (IOException e) {
33            throw new IllegalArgumentException("SMILES export error", e);
34        }
35    }
36
37    /**
38     * Shows the given molecule on the screen in a JFrame window.
39     * 
40     * @param mol molecule to show
41     * @param pos position number of the JFrame window on the screen
42     */
43    public static void showMolecule(Molecule mol, int pos, String title) {
44        showMolecule(mol, pos, DEFAULT_FRAME_SIZE, title);
45    }
46
47    /**
48     * Shows the molecule on the screen in a JFrame window of the given size.
49     * 
50     * @param mol molecule to show
51     * @param pos position number of the JFrame window on the screen
52     * @param size size of the JFrame window
53     */
54    public static void showMolecule(Molecule mol, int pos, int size, String title) {
55
56        // Create an MViewPane
57        MViewPane mvpane = new MViewPane();
58        mvpane.setM(0, mol);
59
60        // Display the result in a JFrame window
61        JFrame frame = new JFrame();
62        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
63        frame.getContentPane().add(mvpane);
64        frame.pack();
65        frame.setBounds((pos % 4) * size, (pos / 4) * size, size, size);
66        frame.setTitle(title);
67        frame.setVisible(true);
68    }
69
70    /**
71     * Creates a {@link HitColoringAndAlignmentOptions} object with custom coloring options.
72     * 
73     * @return coloring options object
74     */
75    public static HitColoringAndAlignmentOptions createColoringOptions() {
76
77        // Create options object
78        HitColoringAndAlignmentOptions coloringOptions = new HitColoringAndAlignmentOptions();
79
80        // Hit should be colored in target
81        coloringOptions.setColoringEnabled(true);
82
83        // Use custom colors for hit and non-hit parts of the target
84        coloringOptions.setHitColor(Color.RED);
85        coloringOptions.setNonHitColor(Color.GREEN);
86
87        return coloringOptions;
88    }
89
90}
91