From: <sh...@us...> - 2008-11-26 12:21:53
|
Revision: 13296 http://cdk.svn.sourceforge.net/cdk/?rev=13296&view=rev Author: shk3 Date: 2008-11-26 12:21:38 +0000 (Wed, 26 Nov 2008) Log Message: ----------- added an enter element mode Modified Paths: -------------- jchempaint/trunk/src/main/org/openscience/jchempaint/action/JCPActionChangeMode.java jchempaint/trunk/src/main/org/openscience/jchempaint/action/SaveAsAction.java Added Paths: ----------- jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementOrGroupDialog.java jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementSwingModule.java Modified: jchempaint/trunk/src/main/org/openscience/jchempaint/action/JCPActionChangeMode.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/action/JCPActionChangeMode.java 2008-11-26 12:05:31 UTC (rev 13295) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/action/JCPActionChangeMode.java 2008-11-26 12:21:38 UTC (rev 13296) @@ -44,6 +44,7 @@ import org.openscience.cdk.controller.MoveModule; import org.openscience.cdk.controller.RemoveModule; import org.openscience.cdk.event.ICDKChangeListener; +import org.openscience.jchempaint.dialog.EnterElementSwingModule; import org.openscience.jchempaint.dialog.PTDialog; import org.openscience.jchempaint.dialog.PeriodicTablePanel; @@ -91,7 +92,7 @@ dialog.setVisible(true); } else if (type.equals("enterelement")) { - //TODO not yet a module + jcpPanel.get2DHub().setActiveDrawModule(new EnterElementSwingModule(jcpPanel.get2DHub())); } else if (type.equals("up_bond")) { //TODO not yet a module Modified: jchempaint/trunk/src/main/org/openscience/jchempaint/action/SaveAsAction.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/action/SaveAsAction.java 2008-11-26 12:05:31 UTC (rev 13295) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/action/SaveAsAction.java 2008-11-26 12:21:38 UTC (rev 13296) @@ -253,20 +253,6 @@ return outFile; } - public static void updateMoleculeCoordinates(IMoleculeSet som, Renderer2DModel r2dm){ - Iterable<IAtomContainer> atomCons = som.molecules(); - for(IAtomContainer atomCon : atomCons) { - for (int i = 0; i < atomCon.getAtomCount(); i++) - { - IAtom currentAtom = atomCon.getAtom(i); - if(r2dm.getRenderingCoordinate(currentAtom)!=null){ - currentAtom.setPoint2d(new Point2d((Point2d)r2dm.getRenderingCoordinate(currentAtom))); - currentAtom.setPoint2d(new Point2d(currentAtom.getPoint2d().x*0.0422,currentAtom.getPoint2d().y*0.0422)); - } - } - } - } - protected File saveAsCML2(IChemObject object, File outFile) throws Exception { if(Float.parseFloat(System.getProperty("java.specification.version"))<1.5){ Added: jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementOrGroupDialog.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementOrGroupDialog.java (rev 0) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementOrGroupDialog.java 2008-11-26 12:21:38 UTC (rev 13296) @@ -0,0 +1,141 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All we ask is that proper credit is given for our work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */package org.openscience.jchempaint.dialog; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +public class EnterElementOrGroupDialog extends JDialog implements ActionListener { + + private static EnterElementOrGroupDialog dialog; + private static String value = ""; + private JComboBox list; + + /** + * Set up and show the dialog. The first Component argument + * determines which frame the dialog depends on; it should be + * a component in the dialog's controlling frame. The second + * Component argument should be null if you want the dialog + * to come up with its left corner in the center of the screen; + * otherwise, it should be the component on top of which the + * dialog should appear. + */ + public static String showDialog(Component frameComp, + Component locationComp, + String labelText, + String title, + String[] possibleValues, + String initialValue, + String longValue) { + Frame frame = JOptionPane.getFrameForComponent(frameComp); + dialog = new EnterElementOrGroupDialog(frame, + locationComp, + labelText, + title, + possibleValues, + initialValue, + longValue); + dialog.setVisible(true); + return value; + } + + private EnterElementOrGroupDialog(Frame frame, + Component locationComp, + String labelText, + String title, + Object[] data, + String initialValue, + String longValue) { + super(frame, title, true); + + //Create and initialize the buttons. + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(this); + // + final JButton setButton = new JButton("Ok"); + setButton.setActionCommand("Set"); + setButton.addActionListener(this); + getRootPane().setDefaultButton(setButton); + + //main part of the dialog + list = new JComboBox(data); + list.setEditable(true); + JPanel listPane = new JPanel(); + listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS)); + JLabel label = new JLabel(labelText); + label.setLabelFor(list); + listPane.add(label); + listPane.add(Box.createRigidArea(new Dimension(0,5))); + listPane.add(list); + listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); + + //Lay out the buttons from left to right. + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); + buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); + buttonPane.add(Box.createHorizontalGlue()); + buttonPane.add(cancelButton); + buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); + buttonPane.add(setButton); + + //Put everything together, using the content pane's BorderLayout. + Container contentPane = getContentPane(); + contentPane.add(listPane, BorderLayout.CENTER); + contentPane.add(buttonPane, BorderLayout.PAGE_END); + + //Initialize values. + pack(); + setLocationRelativeTo(locationComp); + } + + //Handle clicks on the Set and Cancel buttons. + public void actionPerformed(ActionEvent e) { + if ("Set".equals(e.getActionCommand())) { + EnterElementOrGroupDialog.value = (String)(list.getSelectedItem()); + } + if(e.getActionCommand().equals("Cancel")){ + EnterElementOrGroupDialog.value=""; + } + EnterElementOrGroupDialog.dialog.setVisible(false); + } +} Added: jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementSwingModule.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementSwingModule.java (rev 0) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/dialog/EnterElementSwingModule.java 2008-11-26 12:21:38 UTC (rev 13296) @@ -0,0 +1,154 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All we ask is that proper credit is given for our work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package org.openscience.jchempaint.dialog; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import javax.vecmath.Point2d; + +import org.openscience.cdk.DefaultChemObjectBuilder; +import org.openscience.cdk.config.IsotopeFactory; +import org.openscience.cdk.controller.ControllerModuleAdapter; +import org.openscience.cdk.controller.IChemModelRelay; +import org.openscience.cdk.controller.IController2DModel; +import org.openscience.cdk.geometry.GeometryTools; +import org.openscience.cdk.interfaces.IAtom; +import org.openscience.cdk.interfaces.IAtomContainer; +import org.openscience.cdk.interfaces.IIsotope; +import org.openscience.cdk.interfaces.IMolecule; +import org.openscience.cdk.layout.AtomPlacer; +import org.openscience.cdk.smiles.SmilesParser; +import org.openscience.cdk.tools.manipulator.ChemModelManipulator; + +public class EnterElementSwingModule extends ControllerModuleAdapter { + + private HashMap<String,IMolecule> funcgroupsmap=new HashMap<String,IMolecule>(); + + public EnterElementSwingModule(IChemModelRelay chemModelRelay) { + super(chemModelRelay); + String filename = "org/openscience/jchempaint/resources/funcgroups.txt"; + InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); + SmilesParser sp=new SmilesParser(DefaultChemObjectBuilder.getInstance()); + StringBuffer sb=new StringBuffer(); + InputStreamReader isr = new InputStreamReader(ins); + try{ + while(true){ + int i=isr.read(); + if(i==-1){ + break; + }else if(((char)i)=='\n' || ((char)i)=='\r'){ + if(!sb.toString().equals("")){ + StringTokenizer st=new StringTokenizer(sb.toString()); + String key=(String)st.nextElement(); + String value=(String)st.nextElement(); + funcgroupsmap.put(key, sp.parseSmiles(value)); + sb=new StringBuffer(); + } + }else{ + sb.append((char)i); + } + } + if(!sb.toString().equals("")){ + StringTokenizer st=new StringTokenizer(sb.toString()); + String key=(String)st.nextElement(); + String value=(String)st.nextElement(); + funcgroupsmap.put(key, sp.parseSmiles(value)); + } + }catch(Exception ex){ + ex.printStackTrace(); + } + } + + public void mouseClickedDown(Point2d worldCoord) { + + IAtom closestAtom = chemModelRelay.getClosestAtom(worldCoord); + String[] funcGroupsKeys=new String[funcgroupsmap.keySet().size()+1]; + Iterator<String> it=funcgroupsmap.keySet().iterator(); + int h=1; + funcGroupsKeys[0]=""; + while(it.hasNext()){ + funcGroupsKeys[h]=(String)it.next(); + h++; + } + String x=EnterElementOrGroupDialog.showDialog(null,null, "Enter an element symbol or choose/enter a functional group abbrivation:", "Enter element", funcGroupsKeys, "",""); + try{ + IAtomContainer ac=(IAtomContainer)funcgroupsmap.get(x.toLowerCase()); + //this means a functional group was entered + if(ac!=null && !x.equals("")){ + ac=(IAtomContainer)((IAtomContainer)funcgroupsmap.get(x)).clone(); + IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModelRelay.getIChemModel(), closestAtom); + IAtom lastplaced=null; + int counter=0; + //this is the starting point for placing + ac.getAtom(0).setPoint2d(closestAtom.getPoint2d()); + lastplaced=ac.getAtom(0); + counter=1; + container.add(ac); + container.removeAtomAndConnectedElectronContainers(closestAtom); + AtomPlacer ap=new AtomPlacer(); + //TODO the bond length is wrong + while(lastplaced!=null){ + IAtomContainer placedNeighbours=container.getBuilder().newAtomContainer(); + IAtomContainer unplacedNeighbours=container.getBuilder().newAtomContainer(); + List<IAtom> l=container.getConnectedAtomsList(lastplaced); + for(int i=0;i<l.size();i++){ + if(l.get(i).getPoint2d()!=null) + placedNeighbours.addAtom((IAtom)l.get(i)); + else + unplacedNeighbours.addAtom((IAtom)l.get(i)); + } + ap.distributePartners(lastplaced, placedNeighbours, GeometryTools.get2DCenter(placedNeighbours), unplacedNeighbours, chemModelRelay.getIJava2DRenderer().getRenderer2DModel().getBondLength()); + lastplaced=ac.getAtom(counter); + counter++; + if(counter==ac.getAtomCount()) + lastplaced=null; + } + }else if(x!=null && x.length()>0){ + if(Character.isLowerCase(x.toCharArray()[0])) + x=Character.toUpperCase(x.charAt(0))+x.substring(1); + IsotopeFactory ifa=IsotopeFactory.getInstance(closestAtom.getBuilder()); + IIsotope iso=ifa.getMajorIsotope(x); + if(iso!=null) + closestAtom.setSymbol(x); + } + chemModelRelay.updateView(); + }catch(Exception ex){ + ex.printStackTrace(); + } + } + + public String getDrawModeString() { + return IController2DModel.DrawMode.ADDATOMORCHANGEELEMENT.getName(); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |