com.actelion.research.gui.clipboard.ClipboardHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openchemlib Show documentation
Show all versions of openchemlib Show documentation
Open Source Chemistry Library
/*
* Copyright (c) 1997 - 2016
* Actelion Pharmaceuticals Ltd.
* Gewerbestrasse 16
* CH-4123 Allschwil, Switzerland
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package com.actelion.research.gui.clipboard;
import com.actelion.research.chem.*;
import com.actelion.research.chem.coords.CoordinateInventor;
import com.actelion.research.chem.io.RXNFileCreator;
import com.actelion.research.chem.io.RXNFileParser;
import com.actelion.research.chem.name.StructureNameResolver;
import com.actelion.research.chem.reaction.Reaction;
import com.actelion.research.gui.clipboard.external.ChemDrawCDX;
import com.actelion.research.gui.wmf.WMF;
import com.actelion.research.gui.wmf.WMFGraphics2D;
import com.actelion.research.util.Sketch;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.geom.Rectangle2D;
import java.io.*;
/**
* Title: Actelion Library
* Description: Actelion Java Library
* Copyright: Copyright (c) 2002-2003
* Company: Actelion Ltd
*
* @author Thomas Sander, Christian Rufener
* @version 1.0
*/
public class ClipboardHandler implements IClipboardHandler
{
private static final byte MDLSK[] = {(byte) 'M', (byte) 'D', (byte) 'L', (byte) 'S', (byte) 'K', 0, 0};
/**
* Get a Molecule from the Clipboard. The supported formats are: MDLSK,MDLCT,MDL_MOL,CF_ENHMETAFILE with embedded sketch
* If the clipboard molecule has 3D coordinates, then new 2D-coords are invented and used instead.
*
* @return Molecule found or null if no molecule present on the clipboard
*/
public StereoMolecule pasteMolecule()
{
return pasteMolecule(true);
}
/**
* Get a Molecule from the Clipboard. The supported formats are: MDLSK,MDLCT,MDL_MOL,CF_ENHMETAFILE with embedded sketch
*
* @param prefer2D if true and if the clipboard molecule has 3D coordinates, then new 2D-coords are invented
* @return Molecule found or null if no molecule present on the clipboard
*/
public StereoMolecule pasteMolecule(boolean prefer2D)
{
byte[] buffer = null;
StereoMolecule mol = null;
if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_SERIALIZEMOLECULE)) != null) {
try {
ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer));
Object o = is.readObject();
System.out.println("Object read from Bytearray input " + o);
if (o instanceof StereoMolecule) {
mol = (StereoMolecule) o;
}
is.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("NativeClipboardAccessor.pasteMolecule(): Exception " + e);
}
}
System.out.println("Mol is " + mol);
if (mol == null) {
if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_CTAB)) != null || (buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_MOLFILE)) != null) {
MolfileParser p = new MolfileParser();
mol = new StereoMolecule();
if (!p.parse(mol, new String(buffer))) {
mol = null;
System.err.println("Error Parsing CTAB during clipboard paste");
}
}
if (mol == null) {
if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_SKETCH)) != null || (buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_EMBEDDEDSKETCH)) != null) {
try {
mol = new StereoMolecule();
if (!Sketch.createMolFromSketchBuffer(mol, buffer)) {
mol = null;
}
} catch (IOException e) {
mol = null;
e.printStackTrace();
System.out.println("NativeClipboardAccessor.pasteMolecule(): Exception " + e);
}
}
}
}
String clipboardText = null;
if (mol == null) {
if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_IDCODE)) != null) {
clipboardText = new String(buffer);
try {
mol = new StereoMolecule();
IDCodeParser parser = new IDCodeParser(prefer2D);
System.out.printf("Pasted string '%s'\n",clipboardText);
parser.parse(mol,buffer);
if (mol.getAllAtoms() == 0)
mol = null;
} catch (Exception e) {
mol = null;
System.out.println("NativeClipboardAccessor.pasteMolecule(): Exception " + e);
}
}
}
if (mol == null) {
// get StringFlavor from clipboard and try parsing it as molfile, smiles, or (if NameResolver exists) as name
if (clipboardText == null) {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
Object o = t.getTransferData(DataFlavor.stringFlavor);
if (o != null)
clipboardText = o.toString();
}
catch (Exception ioe) {}
}
if (clipboardText != null) {
mol = new MolfileParser().getCompactMolecule(clipboardText);
if (mol == null) {
mol = new StereoMolecule();
try {
new SmilesParser().parse(mol, clipboardText);
}
catch (Exception e) {
mol = null;
}
}
if (mol == null)
mol = StructureNameResolver.resolve(clipboardText);
}
}
if (prefer2D && mol != null && is3DMolecule(mol)) {
mol.ensureHelperArrays(Molecule.cHelperParities); // to ensure stereo parities
new CoordinateInventor().invent(mol);
// mol.setStereoBondsFromParity(); not needed anymore
}
System.out.println("returned Mol is " + mol);
return mol;
}
/**
* Get a Reaction from the Clipboard
*
* @return Reaction or null if no reaction present
*/
public Reaction pasteReaction()
{
byte[] buffer = null;
Reaction rxn = null;
if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_SERIALIZEREACTION)) != null) {
try {
ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer));
Object o = is.readObject();
if (o instanceof Reaction) {
rxn = (Reaction) o;
}
is.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("NativeClipboardAccessor.pasteReaction(): Exception " + e);
}
} else if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_CTAB)) != null) {
RXNFileParser p = new RXNFileParser();
rxn = new Reaction();
try {
if (!p.parse(rxn, new String(buffer)))
rxn = null;
} catch (Exception e) {
System.err.println("Error parsing Reaction Buffer " + e);
rxn = null;
}
} else if ((buffer = NativeClipboardHandler.getClipboardData(NativeClipboardHandler.NC_SKETCH)) != null) {
try {
rxn = new Reaction();
if (!Sketch.createReactionFromSketchBuffer(rxn, buffer)) {
rxn = null;
}
} catch (IOException e) {
rxn = null;
}
}
return rxn;
}
public boolean copyMolecule(String molfile)
{
StereoMolecule m = new StereoMolecule();
MolfileParser p = new MolfileParser();
p.parse(m, molfile);
return copyMolecule(m);
}
/**
* Copies a molecule to the clipboard in various formats:
* ENHMETAFILE with an embedded sketch
* MDLSK Sketch
* MDLCT MDL molfile
*/
public boolean copyMolecule(StereoMolecule mol)
{
boolean ok = false;
try {
StereoMolecule m = mol.getCompactCopy();
for (int atom=0; atom
© 2015 - 2025 Weber Informatics LLC | Privacy Policy