📄 jchempainteditorpanel.java
字号:
if (jchemPaintModel != null) { return jchemPaintModel.getRendererModel().getBackColor(); } else { return Color.WHITE; } } /** * Gets the toolBar attribute of the JChemPaintPanel object * *@return The toolBar value */ public JToolBar getToolBar() { return toolBar; } /** * Gets the menu attribute of the JChemPaintPanel object * *@return The menu value */ public JChemPaintMenuBar getMenu() { return menu; } /** * Creates a new JFrame that owns a new JChemPaintModel and returns it. * *@return The new JFrame containing the JChemPaintEditorPanel */ public static JFrame getEmptyFrameWithModel() { JChemPaintModel model = new JChemPaintModel(); model.setTitle(getNewFrameName()); model.setAuthor(JCPPropertyHandler.getInstance().getJCPProperties().getProperty("General.UserName")); Package self = Package.getPackage("org.openscience.cdk.applications.jchempaint"); String version = self.getImplementationVersion(); model.setSoftware("JChemPaint " + version); model.setGendate((Calendar.getInstance()).getTime().toString()); JFrame jcpf = getNewFrame(model); return jcpf; } /** * Creates a new JFrame that owns a new JChemPaintModel and returns it which contains * a model. * *@param mod The JChemPaintModel *@return The new JFrame containing the JChemPaintEditorPanel */ public static JFrame getFrameWithModel(IChemModel mod) { JChemPaintModel model = new JChemPaintModel(mod); model.setTitle(mod.getID()); model.setAuthor(JCPPropertyHandler.getInstance().getJCPProperties().getProperty("General.UserName")); Package self = Package.getPackage("org.openscience.cdk.applications.jchempaint"); String version = self.getImplementationVersion(); model.setSoftware("JChemPaint " + version); model.setGendate((Calendar.getInstance()).getTime().toString()); JFrame jcpf = getNewFrame(model); return jcpf; } /** * Creates a new JChemPaintEditorPanel and assigns a given Model to it. * *@param model The model to be assigned to the new frame. *@return The new JChemPaintFrame with its new JChemPaintModel */ public static JFrame getNewFrame(JChemPaintModel model) { JFrame frame = new JFrame(); frame.addWindowListener(new JChemPaintPanel.AppCloser()); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JChemPaintEditorPanel jcpep = new JChemPaintEditorPanel(); frame.getContentPane().add(jcpep); jcpep.registerModel(model); jcpep.setJChemPaintModel(model,null); frame.setTitle(model.getTitle()); //This ensures that the drawingpanel is never smaller than the application frame.addComponentListener(new ComponentAdapter(){ public void componentResized(ComponentEvent e) { if(((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().getBackgroundDimension().width<((JFrame)e.getSource()).getWidth()-30) ((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().setBackgroundDimension(new Dimension(((JFrame)e.getSource()).getWidth()-30,((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().getBackgroundDimension().height)); if(((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().getBackgroundDimension().height<((JFrame)e.getSource()).getHeight()-30) ((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().setBackgroundDimension(new Dimension(((JChemPaintEditorPanel)((JFrame)e.getSource()).getContentPane().getComponent(0)).getJChemPaintModel().getRendererModel().getBackgroundDimension().width,((JFrame)e.getSource()).getHeight()-30)); } }); model.getControllerModel().setAutoUpdateImplicitHydrogens(true); model.getRendererModel().setShowEndCarbons(true); return frame; } public void setupPopupMenus(PopupController2D inputAdapter) { if (inputAdapter.getPopupMenu(Atom.class) == null) { inputAdapter.setPopupMenu(Atom.class, new JChemPaintPopupMenu(this, "atom")); } if (inputAdapter.getPopupMenu(PseudoAtom.class) == null) { inputAdapter.setPopupMenu(PseudoAtom.class, new JChemPaintPopupMenu(this, "pseudo")); } if (inputAdapter.getPopupMenu(Bond.class) == null) { inputAdapter.setPopupMenu(Bond.class, new JChemPaintPopupMenu(this, "bond")); } if (inputAdapter.getPopupMenu(ChemModel.class) == null) { inputAdapter.setPopupMenu(ChemModel.class, new JChemPaintPopupMenu(this, "chemmodel")); } if (inputAdapter.getPopupMenu(Reaction.class) == null) { inputAdapter.setPopupMenu(Reaction.class, new JChemPaintPopupMenu(this, "reaction")); } } public Image takeSnapshot() { Image snapImage = null; try { logger.info("Making snapshot... "); Renderer2D r2d = new Renderer2D(jchemPaintModel.getRendererModel()); r2d.setRenderer2DModel(jchemPaintModel.getRendererModel()); IChemModel model = null; try { model = (IChemModel) jchemPaintModel.getChemModel().clone(); } catch (CloneNotSupportedException exception) { logger.error("Clone of IChemModel failed: ", exception.getMessage()); logger.debug(exception); return null; } IAtomContainer ac = model.getBuilder().newAtomContainer(); Iterator containers = MoleculeSetManipulator.getAllAtomContainers(model.getMoleculeSet()).iterator(); while (containers.hasNext()) ac.add((IAtomContainer)containers.next()); HashMap oldrenderingcoordinates=new HashMap(); Iterator it=jchemPaintModel.getRendererModel().getRenderingCoordinates().keySet().iterator(); while(it.hasNext()){ Object key=it.next(); oldrenderingcoordinates.put(key,new Point2d(((Point2d)jchemPaintModel.getRendererModel().getRenderingCoordinates().get(key)).x,((Point2d)jchemPaintModel.getRendererModel().getRenderingCoordinates().get(key)).y)); } Dimension dim = GeometryTools.get2DDimension(ac,jchemPaintModel.getRendererModel().getRenderingCoordinates()); GeometryTools.translateAllPositive(ac,jchemPaintModel.getRendererModel().getRenderingCoordinates()); GeometryTools.translate2D(ac, new Vector2d(40,40),jchemPaintModel.getRendererModel().getRenderingCoordinates()); it=jchemPaintModel.getRendererModel().getRenderingCoordinates().keySet().iterator(); while(it.hasNext()){ Point2d point =(Point2d)jchemPaintModel.getRendererModel().getRenderingCoordinates().get(it.next()); point.y=(int)dim.getHeight()+80-point.y; } snapImage = createImage((int)dim.getWidth()+80, (int)dim.getHeight()+80); Graphics2D snapGraphics = (Graphics2D) snapImage.getGraphics(); snapGraphics.setBackground(Color.WHITE); snapGraphics.clearRect(0,0,(int)dim.getWidth()+80, (int)dim.getHeight()+80); r2d.getRenderer2DModel().setRenderingCoordinates(jchemPaintModel.getRendererModel().getRenderingCoordinates()); r2d.useScreenSize=false; r2d.paintMolecule(ac, (Graphics2D) snapGraphics,false,true); r2d.useScreenSize=true; jchemPaintModel.getRendererModel().setRenderingCoordinates(oldrenderingcoordinates); logger.info("created..."); logger.debug("painting succeeded."); } catch (NullPointerException e) { snapImage = null; } return snapImage; } /** * Method to notify this CDKChangeListener if something has changed in another * object * *@param e The EventObject containing information on the nature and source of * the event */ public void stateChanged(EventObject e) { //FIXME was dependant on ReallyPaintPanel.drawingnow drawingPanel.repaint(); } /** * Enlarges the 'one molecule' dimension to fit a set of reactions or * molecules. * *@param baseDim Description of the Parameter *@param model Description of the Parameter *@return Description of the Return Value */ private Dimension makeChemModelFit(Dimension baseDim, IChemModel model) { Dimension newDim = new Dimension(baseDim); // a bit ugly, but assume moleculeSet *or* reactionSet IMoleculeSet moleculeSet = model.getMoleculeSet(); if (moleculeSet != null) { newDim.height = newDim.height * (moleculeSet.getMoleculeCount()); return newDim; } org.openscience.cdk.interfaces.IReactionSet reactionSet = model.getReactionSet(); if (reactionSet != null) { newDim.height = newDim.height * (reactionSet.getReactionCount()); } return newDim; } /** * Lays out the molecules in a MoleculeSet, or reaction in a ReactionSet * in a one column table. * *@param baseDim Description of the Parameter *@param model Description of the Parameter */ private void layoutInTable(Dimension baseDim, IChemModel model, Renderer2DModel rm) { // a bit ugly, but assume moleculeSet *or* reactionSet IMoleculeSet moleculeSet = model.getMoleculeSet(); if (moleculeSet != null) { for (int i = 0; i < moleculeSet.getAtomContainerCount(); i++) { GeometryTools.center(moleculeSet.getAtomContainer(i), baseDim,rm.getRenderingCoordinates()); GeometryTools.translate2D(moleculeSet.getAtomContainer(i), 0, baseDim.height * i,rm.getRenderingCoordinates()); } return; } org.openscience.cdk.interfaces.IReactionSet reactionSet = model.getReactionSet(); if (reactionSet != null) { for (int i = 1; i <= reactionSet.getReactionCount(); i++) { IAtomContainer ac = reactionSet.getBuilder().newAtomContainer(); Iterator containers = ReactionManipulator.getAllAtomContainers(reactionSet.getReaction(i - 1)).iterator(); while (containers.hasNext()) ac.add((IAtomContainer)containers.next()); GeometryTools.center(ac, baseDim,rm.getRenderingCoordinates()); GeometryTools.translate2D(ac, 0, baseDim.height * (reactionSet.getReactionCount() - i),rm.getRenderingCoordinates()); } } } /** * Gets the validatorEngine attribute of the JChemPaintEditorPanel class * *@return The validatorEngine value */ public static ValidatorEngine getValidatorEngine() { if (engine == null) { engine = new ValidatorEngine(); // default validators engine.addValidator(new BasicValidator()); engine.addValidator(new ValencyValidator()); engine.addValidator(new CDKValidator()); engine.addValidator(new DictionaryValidator(dictdb)); engine.addValidator(new PDBValidator()); } return engine; } /** * Gets the dictionaryDatabase attribute of the JChemPaint object * *@return The dictionaryDatabase value */ public static DictionaryDatabase getDictionaryDatabase() { if (dictdb == null) { dictdb = new DictionaryDatabase(); try { File dictdir = new File(JCPPropertyHandler.getInstance().getJChemPaintDir(), "dicts"); logger.info("User dict dir: ", dictdir); logger.debug(" exists: ", dictdir.exists()); logger.debug(" isDirectory: ", dictdir.isDirectory()); if (dictdir.exists() && dictdir.isDirectory()) { File[] dicts = dictdir.listFiles(); for (int i = 0; i < dicts.length; i++) { // loop over these files and load them try { FileReader reader = new FileReader(dicts[i]); String filename = dicts[i].getName(); dictdb.readDictionary(reader, filename.substring(0, filename.indexOf('.'))); } catch (IOException exception) { logger.error("Problem with reading macie dictionary..."); } } } logger.info("Read these dictionaries: "); Enumeration dicts = dictdb.listDictionaries(); while (dicts.hasMoreElements()) { logger.info(" - ", dicts.nextElement().toString()); } } catch (Exception exc) { logger.error("Could not handle dictionary initialization. Maybe I'm running in a sandbox."); } } return dictdb; } /* * Methods implementing the CDKEditBus interface */ /** * Gets the aPIVersion attribute of the JChemPaintEditorPanel object * *@return The aPIVersion value */ public String getAPIVersion() { return "1.8"; } public void runScript(String s1, String s2) { logger.info("runScript method currently not supported"); } /** * Mandatory because JChemPaint is a ChangeListener. Used by other classes to * update the information in one of the three statusbar fields. * *@param e ChangeEvent */ //As long there is nothing it it, it shouldn't overwrite the function of JChemPaintPanel public void stateChanged(ChangeEvent e) { if(e.getSource() instanceof JChemPaintPanel) { IChemModel editorModel = ((JChemPaintPanel)e.getSource()).getJChemPaintModel().getChemModel(); JViewport viewPort =((JScrollPane) ((Container) this.getComponent(0)).getComponent(0)).getViewport(); if (viewPort.getView() == null) { viewPort.add(this.getDrawingPanel()); } this.getJChemPaintModel().setChemModel(editorModel); if (editorModel != null) { this.scaleAndCenterMolecule(this.getJChemPaintModel().getChemModel()); } } super.stateChanged(e); if (jchemPaintModel != null) { for (int i = 0; i < 3; i++) { String status = jchemPaintModel.getStatus(i); statusBar.setStatus(i + 1, status); } } else { if (statusBar != null) { statusBar.setStatus(1, "no model"); } } } /* * Listener notification support methods START here */ /** * Adds a feature to the ChangeListener attribute of the SenecaDataset object * * @param x The feature to be added to the ChangeListener attribute */ public void addChangeListener(ChangeListener x) { if (changeListeners == null) changeListeners = new EventListenerList(); changeListeners.add(ChangeListener.class, x); // bring it up to date with current state x.stateChanged(new ChangeEvent(this)); } public void removeChangeListener(ChangeListener x) { changeListeners.remove(ChangeListener.class, x); } protected void fireChange(String reason) { lastEventReason = reason; // Create the event: ChangeEvent c = new ChangeEvent(this); // Get the listener list if (changeListeners != null) { Object[] listeners = changeListeners.getListenerList(); // Process the listeners last to first // List is in pairs, Class and instance for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == ChangeListener.class) { ChangeListener cl = (ChangeListener) listeners[i + 1]; cl.stateChanged(c); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -