com.actelion.research.gui.JEditableChemistryView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openchemlib Show documentation
Show all versions of openchemlib Show documentation
Open Source Chemistry Library
package com.actelion.research.gui;
import com.actelion.research.chem.DrawingObjectList;
import com.actelion.research.chem.ExtendedDepictor;
import com.actelion.research.chem.StereoMolecule;
import com.actelion.research.chem.reaction.Reaction;
import com.actelion.research.gui.editor.SwingEditorDialog;
import com.actelion.research.gui.hidpi.HiDPIHelper;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
public class JEditableChemistryView extends JChemistryView {
private static final String EDIT_MESSAGE = "";
private StereoMolecule[] mMolecules;
private Reaction mReaction;
/**
* Creates a new JEditableChemistryView for showing & editing a reaction or molecule(s).
* This default implementation will support copy/paste and drag&drop.
* @param chemistryType one of the ExtendedDepictor.TYPE_... options
*/
public JEditableChemistryView(int chemistryType) {
super(chemistryType);
if (chemistryType == ExtendedDepictor.TYPE_REACTION) {
mReaction = new Reaction();
super.setContent(mReaction);
}
else {
mMolecules = new StereoMolecule[1];
mMolecules[0] = new StereoMolecule();
super.setContent(mMolecules);
}
setEditable(true);
}
/**
* If chemistryType is
* @return all molecules
*/
public StereoMolecule[] getStructures() {
return mMolecules;
}
public Reaction getReaction() {
return mReaction;
}
@Override
public void setContent(StereoMolecule[] mol) {
setContent(mol, null);
}
@Override
public void setContent(Reaction rxn) {
setContent(rxn, null);
}
@Override
public void setContent(StereoMolecule[] mol, DrawingObjectList drawingObjectList) {
mMolecules = mol;
super.setContent(mol, drawingObjectList);
}
@Override
public void setContent(Reaction rxn, DrawingObjectList drawingObjectList) {
if (rxn == null)
mReaction.clear();
else
mReaction = rxn;
super.setContent(mReaction, drawingObjectList);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && isEnabled() && isEditable()) {
if (getChemistryType() == ExtendedDepictor.TYPE_REACTION)
editReaction();
else
editMolecules();
return;
}
super.mouseClicked(e);
}
private boolean isEmpty() {
if (getChemistryType() == ExtendedDepictor.TYPE_REACTION)
return mReaction == null || mReaction.isEmpty();
if (mMolecules != null)
for (StereoMolecule mol:mMolecules)
if (mol.getAllAtoms() != 0)
return false;
return true;
}
@Override
public void paintComponent(Graphics g) {
Dimension theSize = getSize();
Insets insets = getInsets();
theSize.width -= insets.left + insets.right;
theSize.height -= insets.top + insets.bottom;
super.paintComponent(g);
if (isEnabled() && isEditable() && isEmpty()) {
g.setFont(g.getFont().deriveFont(Font.PLAIN, HiDPIHelper.scale(10)));
FontMetrics metrics = g.getFontMetrics();
Rectangle2D bounds = metrics.getStringBounds(EDIT_MESSAGE, g);
g.drawString(EDIT_MESSAGE, insets.left+(int)(theSize.width-bounds.getWidth())/2,
insets.top+(theSize.height-metrics.getHeight())/2+metrics.getAscent());
}
}
private void editReaction() {
SwingEditorDialog dialog = createDrawDialog("Edit Reaction", new Reaction(mReaction));
dialog.setVisible(true);
if (!dialog.isCancelled()) {
Reaction newRxn = dialog.getReactionAndDrawings();
// rescue catalysts, because the editor doesn't handle them
if (newRxn != null) {
for (int i=0; i