PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/rcdkjar/src/org/guha/rcdk/view/MoleculeImageToClipboard.java

http://github.com/rajarshi/cdkr
Java | 67 lines | 45 code | 15 blank | 7 comment | 2 complexity | a13243c7b2384de91faac1d2956b8928 MD5 | raw file
 1package org.guha.rcdk.view;
 2
 3import org.guha.rcdk.util.Misc;
 4import org.openscience.cdk.DefaultChemObjectBuilder;
 5import org.openscience.cdk.interfaces.IAtomContainer;
 6import org.openscience.cdk.smiles.SmilesParser;
 7
 8import java.awt.*;
 9import java.awt.datatransfer.DataFlavor;
10import java.awt.datatransfer.Transferable;
11import java.awt.datatransfer.UnsupportedFlavorException;
12
13/**
14 * A one line summary.
15 *
16 * @author Rajarshi Guha
17 */
18public class MoleculeImageToClipboard {
19
20    public static void copyImageToClipboard(IAtomContainer molecule, RcdkDepictor depictor) throws Exception {
21        Image image = depictor.getImage(molecule);
22        // now copy to clipboard
23        ImageSelection.copyImageToClipboard(image);
24    }
25
26    public static void main(String[] args) throws Exception {
27        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
28        IAtomContainer mol = sp.parseSmiles("c1ccccc1CC(=O)C1COCNC1");
29
30        MoleculeImageToClipboard.copyImageToClipboard(mol, Misc.getDefaultDepictor());
31    }
32
33
34}
35
36
37// http://elliotth.blogspot.com/2005/09/copying-images-to-clipboard-with-java.html
38class ImageSelection implements Transferable {
39    private Image image;
40
41    public static void copyImageToClipboard(Image image) {
42        ImageSelection imageSelection = new ImageSelection(image);
43        Toolkit toolkit = Toolkit.getDefaultToolkit();
44        toolkit.getSystemClipboard().setContents(imageSelection, null);
45    }
46
47    public ImageSelection(Image image) {
48        this.image = image;
49    }
50
51    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
52        if (flavor.equals(DataFlavor.imageFlavor) == false) {
53            throw new UnsupportedFlavorException(flavor);
54        }
55        return image;
56    }
57
58    public boolean isDataFlavorSupported(DataFlavor flavor) {
59        return flavor.equals(DataFlavor.imageFlavor);
60    }
61
62    public DataFlavor[] getTransferDataFlavors() {
63        return new DataFlavor[]{
64                DataFlavor.imageFlavor
65        };
66    }
67}