| 1 |
package org.guha.rcdk.draw; |
| 2 |
|
| 3 |
import org.openscience.cdk.DefaultChemObjectBuilder; |
| 4 |
import org.openscience.cdk.interfaces.IAtomContainer; |
| 5 |
import org.openscience.cdk.interfaces.IAtomContainerSet; |
| 6 |
import org.openscience.cdk.interfaces.IChemModel; |
| 7 |
import org.openscience.cdk.layout.StructureDiagramGenerator; |
| 8 |
|
| 9 |
import javax.swing.*; |
| 10 |
import java.awt.event.WindowAdapter; |
| 11 |
import java.awt.event.WindowEvent; |
| 12 |
|
| 13 |
public class Get2DStructureFromJCP { |
| 14 |
private static IChemModel chemModel; |
| 15 |
|
| 16 |
public Get2DStructureFromJCP() { |
| 17 |
} |
| 18 |
|
| 19 |
public Get2DStructureFromJCP(IAtomContainer molecule) { |
| 20 |
IAtomContainer localMolecule = null; |
| 21 |
// we should make 2D coords |
| 22 |
try { |
| 23 |
StructureDiagramGenerator sdg = new StructureDiagramGenerator(); |
| 24 |
sdg.setMolecule(molecule); |
| 25 |
sdg.generateCoordinates(); |
| 26 |
localMolecule = sdg.getMolecule(); |
| 27 |
} catch (Exception exc) { |
| 28 |
exc.printStackTrace(); |
| 29 |
} |
| 30 |
|
| 31 |
IAtomContainerSet som = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainerSet.class); |
| 32 |
som.addAtomContainer(localMolecule); |
| 33 |
chemModel = DefaultChemObjectBuilder.getInstance().newInstance(IChemModel.class); |
| 34 |
chemModel.setMoleculeSet(som); |
| 35 |
} |
| 36 |
|
| 37 |
public void showWindow() { |
| 38 |
|
| 39 |
JFrame frame = new JFrame(); |
| 40 |
JDialog dlg = new JDialog(frame, true); |
| 41 |
|
| 42 |
dlg.setTitle("Molecule Editor"); |
| 43 |
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
| 44 |
dlg.addWindowListener(new MyAppCloser()); |
| 45 |
|
| 46 |
// JChemPaintEditorPanel editPanel = new JChemPaintEditorPanel(); |
| 47 |
// JChemPaintModel model = new JChemPaintModel(); |
| 48 |
// editPanel.registerModel(model); |
| 49 |
// editPanel.setJChemPaintModel(model); |
| 50 |
// |
| 51 |
// dlg.add(editPanel); |
| 52 |
// dlg.pack(); |
| 53 |
// dlg.setVisible(true); |
| 54 |
} |
| 55 |
|
| 56 |
private final static class MyAppCloser extends WindowAdapter { |
| 57 |
|
| 58 |
/** |
| 59 |
* closing Event. Shows a warning if this window has unsaved data and |
| 60 |
* terminates jvm, if last window. |
| 61 |
* |
| 62 |
* @param e Description of the Parameter |
| 63 |
*/ |
| 64 |
public void windowClosing(WindowEvent e) { |
| 65 |
// JDialog dlg = (JDialog) e.getSource(); |
| 66 |
// JChemPaintEditorPanel editPanel = (JChemPaintEditorPanel) dlg |
| 67 |
// .getContentPane().getComponent(0); |
| 68 |
// |
| 69 |
// chemModel = editPanel.getChemModel(); |
| 70 |
// ((JDialog) e.getSource()).setVisible(false); |
| 71 |
// ((JDialog) e.getSource()).dispose(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public IChemModel getChemModel() { |
| 76 |
return chemModel; |
| 77 |
} |
| 78 |
|
| 79 |
public IAtomContainer[] getMolecules() { |
| 80 |
IAtomContainerSet som = chemModel.getMoleculeSet(); |
| 81 |
if (som == null) return null; |
| 82 |
else { |
| 83 |
IAtomContainer[] ret = new IAtomContainer[som.getAtomContainerCount()]; |
| 84 |
int c = 0; |
| 85 |
for (IAtomContainer molecule : som.atomContainers()) { |
| 86 |
ret[c++] = molecule; |
| 87 |
} |
| 88 |
|
| 89 |
return ret; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public static void main(String[] args) { |
| 94 |
Get2DStructureFromJCP e = new Get2DStructureFromJCP(); |
| 95 |
e.showWindow(); |
| 96 |
System.exit(0); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// public class JCPReturn { |
| 101 |
// |
| 102 |
// public static void main(String[] args) { |
| 103 |
// Get2DStructureFromJCP jcp = new Get2DStructureFromJCP(); |
| 104 |
// ChemModel model = jcp.showWindow(); |
| 105 |
// System.out.println("From main: " + model); |
| 106 |
// |
| 107 |
// Molecule[] molecules = jcp.getMolecules(); |
| 108 |
// System.out.println(molecules.length + " structures drawn"); |
| 109 |
// |
| 110 |
// } |
| 111 |
// |
| 112 |
// } |