| 1 |
package org.guha.rcdk.view; |
| 2 |
|
| 3 |
import org.guha.rcdk.util.Misc; |
| 4 |
import org.openscience.cdk.DefaultChemObjectBuilder; |
| 5 |
import org.openscience.cdk.interfaces.IAtomContainer; |
| 6 |
import org.openscience.cdk.smiles.SmilesParser; |
| 7 |
|
| 8 |
import java.awt.*; |
| 9 |
import java.awt.datatransfer.DataFlavor; |
| 10 |
import java.awt.datatransfer.Transferable; |
| 11 |
import java.awt.datatransfer.UnsupportedFlavorException; |
| 12 |
|
| 13 |
/** |
| 14 |
* A one line summary. |
| 15 |
* |
| 16 |
* @author Rajarshi Guha |
| 17 |
*/ |
| 18 |
public 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 |
| 38 |
class 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 |
} |