|
| 1 | +/* |
| 2 | +* Copyright (c) 1997 - 2016 |
| 3 | +* Actelion Pharmaceuticals Ltd. |
| 4 | +* Gewerbestrasse 16 |
| 5 | +* CH-4123 Allschwil, Switzerland |
| 6 | +* |
| 7 | +* All rights reserved. |
| 8 | +* |
| 9 | +* Redistribution and use in source and binary forms, with or without |
| 10 | +* modification, are permitted provided that the following conditions are met: |
| 11 | +* |
| 12 | +* 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | +* list of conditions and the following disclaimer. |
| 14 | +* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +* this list of conditions and the following disclaimer in the documentation |
| 16 | +* and/or other materials provided with the distribution. |
| 17 | +* 3. Neither the name of the the copyright holder nor the |
| 18 | +* names of its contributors may be used to endorse or promote products |
| 19 | +* derived from this software without specific prior written permission. |
| 20 | +* |
| 21 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | +* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +* |
| 32 | +*/ |
| 33 | + |
| 34 | +package com.actelion.research.jfx.gui.misc; |
| 35 | + |
| 36 | +import com.actelion.research.chem.*; |
| 37 | +import com.actelion.research.jfx.dataformat.MoleculeDataFormats; |
| 38 | +import javafx.scene.input.Clipboard; |
| 39 | +import javafx.scene.input.ClipboardContent; |
| 40 | +import javafx.scene.input.DataFormat; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +/** |
| 47 | + * Created by baerr on 7/28/14. |
| 48 | + */ |
| 49 | +public class ClipboardHelper { |
| 50 | + private ClipboardHelper() { |
| 51 | + } |
| 52 | + |
| 53 | + public static ClipboardContent writeContent(StereoMolecule mol) { |
| 54 | + return writeContent(mol, null); |
| 55 | + } |
| 56 | + |
| 57 | + public static ClipboardContent writeContent(StereoMolecule mol, ClipboardContent content) { |
| 58 | + if(content==null) |
| 59 | + content = new ClipboardContent(); |
| 60 | + |
| 61 | + // JavaFx/awt clipboard |
| 62 | + content.put(MoleculeDataFormats.DF_SERIALIZEDMOLECULE, mol); |
| 63 | + |
| 64 | + MolfileV3Creator molfileV3Creator = new MolfileV3Creator(mol); |
| 65 | + content.put(MoleculeDataFormats.DF_MDLMOLFILEV3, molfileV3Creator.getMolfile()); |
| 66 | + |
| 67 | + MolfileCreator molfileCreator = new MolfileCreator(mol); |
| 68 | + content.put(MoleculeDataFormats.DF_MDLMOLFILE, molfileCreator.getMolfile()); |
| 69 | + |
| 70 | + Canonizer can = new Canonizer(mol); |
| 71 | + String idcode = String.format("%s %s", can.getIDCode(), can.getEncodedCoordinates(true)); |
| 72 | + content.putString(idcode); |
| 73 | + |
| 74 | + IsomericSmilesCreator smilesCreator = new IsomericSmilesCreator(mol); |
| 75 | + content.put(MoleculeDataFormats.DF_SMILES, smilesCreator.getSmiles()); |
| 76 | + |
| 77 | + return content; |
| 78 | + } |
| 79 | + |
| 80 | + public static void copy(StereoMolecule mol) { |
| 81 | + // JavaFx clipboard |
| 82 | +// final Clipboard clipboard = Clipboard.getSystemClipboard(); |
| 83 | +// clipboard.setContent(writeContent(mol)); |
| 84 | + |
| 85 | + // Native clipboard |
| 86 | +// ClipboardHandler handler = new ClipboardHandler(); |
| 87 | +// handler.copyMolecule(mol); |
| 88 | + } |
| 89 | + |
| 90 | + public static StereoMolecule readContent(Clipboard clipboard) { |
| 91 | + StereoMolecule mol = null; |
| 92 | + List<DataFormat> formats = getAcceptedFormats(clipboard); |
| 93 | + |
| 94 | + int i = -1; |
| 95 | + while (mol == null && ++i < formats.size()) { |
| 96 | + DataFormat format = formats.get(i); |
| 97 | + |
| 98 | + if (format.equals(MoleculeDataFormats.DF_SERIALIZEDMOLECULE)) { |
| 99 | + System.out.println("Put molecule using " + format); |
| 100 | + try { |
| 101 | + mol = (StereoMolecule) clipboard.getContent(format); |
| 102 | + } catch (Exception e) { |
| 103 | + System.err.println("Cannot parse serialized data for molecule"); |
| 104 | + } |
| 105 | + } else if (format.equals(MoleculeDataFormats.DF_MDLMOLFILEV3) || format.equals(MoleculeDataFormats.DF_MDLMOLFILE)) { |
| 106 | + System.out.println("Put molecule using " + format); |
| 107 | + try { |
| 108 | + MolfileParser p = new MolfileParser(); |
| 109 | + p.parse(mol, clipboard.getContent(format).toString()); |
| 110 | + } catch (Exception e) { |
| 111 | + System.err.println("Cannot parse molfile/molfilev3 data for molecule"); |
| 112 | + } |
| 113 | + } else if (format.equals(DataFormat.PLAIN_TEXT)) { |
| 114 | + System.out.println("Put molecule using " + format); |
| 115 | + try { |
| 116 | + IDCodeParser p = new IDCodeParser(true); |
| 117 | + p.parse(mol, clipboard.getString()); |
| 118 | + } catch (Exception e) { |
| 119 | + System.err.println("Cannot parse idcode data for molecule"); |
| 120 | + } |
| 121 | + } else if (format.equals(MoleculeDataFormats.DF_SMILES)) { |
| 122 | + System.out.println("Put molecule using " + format); |
| 123 | + try { |
| 124 | + SmilesParser p = new SmilesParser(); |
| 125 | + p.parse(mol, clipboard.getContent(format).toString()); |
| 126 | + } catch (Exception e) { |
| 127 | + System.err.println("Cannot parse smiles data for molecule"); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return mol; |
| 132 | + } |
| 133 | + |
| 134 | + public static StereoMolecule paste() { |
| 135 | + StereoMolecule mol = null; |
| 136 | + |
| 137 | + // Native clipboard |
| 138 | +// ClipboardHandler handler = new ClipboardHandler(); |
| 139 | +// mol = handler.pasteMolecule(); |
| 140 | + if(mol!=null) { |
| 141 | + System.out.println("Got molecule from native clipboard"); |
| 142 | + } else { |
| 143 | + // JavaFx clipboard |
| 144 | + final Clipboard clipboard = Clipboard.getSystemClipboard(); |
| 145 | + mol = readContent(clipboard); |
| 146 | + if(mol!=null) { |
| 147 | + System.out.println("Got molecule from standard clipboard"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return mol; |
| 152 | + } |
| 153 | + |
| 154 | + public static List<DataFormat> getAcceptedFormats(Clipboard clipboard) |
| 155 | + { |
| 156 | + Set<DataFormat> formats = clipboard.getContentTypes(); |
| 157 | + List<DataFormat> res = new ArrayList<DataFormat>(); |
| 158 | + for (DataFormat dataFormat : MoleculeDataFormats.DATA_FORMATS) { |
| 159 | + for (DataFormat f : formats) { |
| 160 | + if (f.equals(dataFormat)) { |
| 161 | + res.add(f); |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return res; |
| 167 | + } |
| 168 | +} |
0 commit comments