Created
July 3, 2012 16:44
-
-
Save rwst/3040923 to your computer and use it in GitHub Desktop.
patch by Wayne Volkmuth for cpy/paste via MDLCT, issue #85
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -C3 -r org.openscience.cdk.jchempaint30/src/main/org/openscience/jchempaint/action/CopyPasteAction.java org.openscience.cdk.jchempaint.patched/src/main/org/openscience/jchempaint/action/CopyPasteAction.java | |
*** org.openscience.cdk.jchempaint30/src/main/org/openscience/jchempaint/action/CopyPasteAction.java 2009-12-08 03:48:25.000000000 -0800 | |
--- org.openscience.cdk.jchempaint.patched/src/main/org/openscience/jchempaint/action/CopyPasteAction.java 2010-09-13 11:07:35.000000000 -0700 | |
*************** | |
*** 28,41 **** | |
*/ | |
package org.openscience.jchempaint.action; | |
- import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.ClipboardOwner; | |
import java.awt.datatransfer.DataFlavor; | |
import java.awt.datatransfer.Transferable; | |
import java.awt.datatransfer.UnsupportedFlavorException; | |
import java.awt.event.ActionEvent; | |
import java.io.ByteArrayInputStream; | |
import java.io.StringBufferInputStream; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
--- 28,43 ---- | |
*/ | |
package org.openscience.jchempaint.action; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.ClipboardOwner; | |
import java.awt.datatransfer.DataFlavor; | |
+ import java.awt.datatransfer.SystemFlavorMap; | |
import java.awt.datatransfer.Transferable; | |
import java.awt.datatransfer.UnsupportedFlavorException; | |
import java.awt.event.ActionEvent; | |
import java.io.ByteArrayInputStream; | |
+ import java.io.IOException; | |
+ import java.io.InputStream; | |
import java.io.StringBufferInputStream; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
*************** | |
*** 64,69 **** | |
--- 66,72 ---- | |
import org.openscience.cdk.io.IChemObjectWriter; | |
import org.openscience.cdk.io.INChIPlainTextReader; | |
import org.openscience.cdk.io.ISimpleChemObjectReader; | |
+ import org.openscience.cdk.io.MDLCTV2000Reader; | |
import org.openscience.cdk.io.MDLReader; | |
import org.openscience.cdk.io.MDLWriter; | |
import org.openscience.cdk.io.ReaderFactory; | |
*************** | |
*** 103,108 **** | |
--- 106,113 ---- | |
private DataFlavor molFlavor = new DataFlavor( | |
"chemical/x-mdl-molfile", "mdl mol file format"); | |
+ private DataFlavor ctFlavor = new DataFlavor( | |
+ "chemical/x-mdlct-molfile", "mdl CT mol file format, used on clipboards by ISIS/Draw and ChemDraw"); | |
private DataFlavor svgFlavor = new DataFlavor( | |
"image/svg+xml", "scalable vector graphics"); | |
private DataFlavor cmlFlavor = new DataFlavor( | |
*************** | |
*** 124,129 **** | |
--- 129,141 ---- | |
return transfer != null && transfer.isDataFlavorSupported(flavor); | |
} | |
+ public CopyPasteAction() { | |
+ super(); | |
+ SystemFlavorMap sfm = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap(); | |
+ sfm.setFlavorsForNative("MDLCT", new DataFlavor[]{ctFlavor}); | |
+ sfm.setNativesForFlavor(ctFlavor, new String[]{"MDLCT"}); | |
+ } | |
+ | |
public void actionPerformed(ActionEvent e) { | |
logger.info(" type ", type); | |
logger.debug(" source ", e.getSource()); | |
*************** | |
*** 242,257 **** | |
if (supported(transfer, molFlavor) ) { | |
try { | |
! StringBufferInputStream sbis = (StringBufferInputStream) transfer.getTransferData(molFlavor); | |
int x; | |
StringBuffer sb = new StringBuffer(); | |
while((x=sbis.read())!=-1){ | |
sb.append((char)x); | |
} | |
reader = new MDLReader(new StringReader(sb.toString())); | |
} catch (Exception e1) { | |
e1.printStackTrace(); | |
} | |
} else if (supported(transfer, DataFlavor.stringFlavor) ) { | |
try { | |
content = (String) transfer.getTransferData(DataFlavor.stringFlavor); | |
--- 254,279 ---- | |
if (supported(transfer, molFlavor) ) { | |
try { | |
! InputStream sbis = (InputStream) transfer.getTransferData(molFlavor); | |
int x; | |
StringBuffer sb = new StringBuffer(); | |
while((x=sbis.read())!=-1){ | |
sb.append((char)x); | |
} | |
reader = new MDLReader(new StringReader(sb.toString())); | |
+ flip=true; | |
} catch (Exception e1) { | |
e1.printStackTrace(); | |
} | |
+ } else if (supported(transfer, ctFlavor)) { | |
+ try { | |
+ InputStream sbis = (InputStream) transfer.getTransferData(ctFlavor); | |
+ StringBuffer sb = getMolFromCTStream(sbis); | |
+ reader = new MDLReader(new StringReader(sb.toString())); | |
+ flip=true; | |
+ } catch (Exception e1) { | |
+ e1.printStackTrace(); | |
+ } | |
} else if (supported(transfer, DataFlavor.stringFlavor) ) { | |
try { | |
content = (String) transfer.getTransferData(DataFlavor.stringFlavor); | |
*************** | |
*** 265,270 **** | |
--- 287,293 ---- | |
// if looks like CML - InputStream required. Reader throws error. | |
if(content!=null && content.indexOf("cml")>-1) { | |
reader = new CMLReader(new ByteArrayInputStream(content.getBytes())); | |
+ flip=true; | |
} | |
IMolecule toPaste = null; | |
*************** | |
*** 482,487 **** | |
--- 505,555 ---- | |
/** | |
+ * Convert the data from a stream holding MDLCT format data into MDL | |
+ * Mol format. MDLCT format is Mol format where each line is converted | |
+ * into a string prepended with its length, and newline removed. | |
+ * @throws IOException | |
+ */ | |
+ private StringBuffer getMolFromCTStream(InputStream sbis) throws IOException { | |
+ StringBuffer sb = new StringBuffer(); | |
+ // In MDLCT format lines are prepended with their length. There are | |
+ // no newline characters (i.e. a line is just like a Pascal string) | |
+ // So, loop over lines and read them in. | |
+ int len; | |
+ while ((len = sbis.read()) != -1) { | |
+ if (len > 0) { | |
+ byte[] cbuf = new byte[len]; | |
+ // attempt to read all characters in the "line" | |
+ int rd = sbis.read(cbuf, 0, len); | |
+ // unless we read that number of characters, something is wrong | |
+ if (rd == -1) { | |
+ throw new IOException("MDLCT line specified length of " + len + " characters for line, found EOF"); | |
+ } else if (rd != len) { | |
+ throw new IOException("MDLCT input specified length of " + len + " characters for line, found " + rd); | |
+ } | |
+ sb.append(new String(cbuf)); | |
+ } | |
+ sb.append(System.getProperty("line.separator")); | |
+ } | |
+ return sb; | |
+ } | |
+ | |
+ /** | |
+ * Convert the data from a MDL Mol data into MDLCT format. MDLCT | |
+ * format is Mol format where each line is converted into a string | |
+ * prepended with its length, and newline removed. | |
+ */ | |
+ private String getCTStringFromMol(String mol) { | |
+ StringBuffer sb = new StringBuffer(); | |
+ for (String s : mol.toString().split(System.getProperty("line.separator"))) { | |
+ int len = s.length(); | |
+ sb.append((char) len); | |
+ sb.append(s); | |
+ } | |
+ return sb.toString(); | |
+ } | |
+ | |
+ /** | |
* A workaround for reading structure from file, these tend to get | |
* in upside down | |
* TODO remove when obsolete. | |
*************** | |
*** 572,591 **** | |
class JcpSelection implements Transferable, ClipboardOwner { | |
private DataFlavor [] supportedFlavors = { | |
! molFlavor, DataFlavor.stringFlavor, svgFlavor, cmlFlavor, smilesFlavor | |
}; | |
String mol; | |
String smiles; | |
String svg; | |
String cml; | |
@SuppressWarnings("unchecked") | |
public JcpSelection(IAtomContainer tocopy1) throws Exception { | |
IMolecule tocopy= tocopy1.getBuilder().newMolecule(tocopy1); | |
// MDL mol output | |
StringWriter sw = new StringWriter(); | |
new MDLWriter(sw).writeMolecule(tocopy); | |
this.mol=sw.toString(); | |
SmilesGenerator sg=new SmilesGenerator(); | |
smiles = sg.createSMILES(tocopy); | |
// SVG output | |
--- 640,662 ---- | |
class JcpSelection implements Transferable, ClipboardOwner { | |
private DataFlavor [] supportedFlavors = { | |
! molFlavor, ctFlavor, DataFlavor.stringFlavor, svgFlavor, cmlFlavor, smilesFlavor | |
}; | |
String mol; | |
String smiles; | |
+ String mdlct; | |
String svg; | |
String cml; | |
@SuppressWarnings("unchecked") | |
public JcpSelection(IAtomContainer tocopy1) throws Exception { | |
IMolecule tocopy= tocopy1.getBuilder().newMolecule(tocopy1); | |
+ flipStructure(tocopy); | |
// MDL mol output | |
StringWriter sw = new StringWriter(); | |
new MDLWriter(sw).writeMolecule(tocopy); | |
this.mol=sw.toString(); | |
+ this.mdlct = getCTStringFromMol(sw.toString()); | |
SmilesGenerator sg=new SmilesGenerator(); | |
smiles = sg.createSMILES(tocopy); | |
// SVG output | |
*************** | |
*** 629,634 **** | |
--- 700,707 ---- | |
return new StringBufferInputStream(smiles); | |
} else if(parFlavor.equals(DataFlavor.stringFlavor)) { | |
return mol; | |
+ } else if(parFlavor.equals(ctFlavor)) { | |
+ return new StringBufferInputStream(mdlct); | |
} else if(parFlavor.equals(cmlFlavor)) { | |
return new StringBufferInputStream(cml); | |
} else if(parFlavor.equals(svgFlavor)) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment