4
* $Date: 2007-05-01 21:15:34 +0200 (Tue, 01 May 2007) $
7
* Copyright (C) 2003-2007 The JChemPaint project
9
* Contact: jchempaint-devel@lists.sf.net
11
* This program is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public License
13
* as published by the Free Software Foundation; either version 2.1
14
* of the License, or (at your option) any later version.
15
* All we ask is that proper credit is given for our work, which includes
16
* - but is not limited to - adding the above copyright notice to the beginning
17
* of your source code files, and to any copyright notice that you may distribute
18
* with programs based on this work.
20
* This program is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU Lesser General Public License for more details.
25
* You should have received a copy of the GNU Lesser General Public License
26
* along with this program; if not, write to the Free Software
27
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29
package org.openscience.cdk.applications.jchempaint.action;
31
import org.openscience.cdk.applications.jchempaint.JChemPaintEditorPanel;
32
import org.openscience.cdk.applications.jchempaint.JChemPaintModel;
33
import org.openscience.cdk.applications.jchempaint.dialogs.ValidateFrame;
34
import org.openscience.cdk.interfaces.IChemObject;
35
import org.openscience.cdk.interfaces.IAtomContainer;
36
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;
37
import org.openscience.cdk.validate.BasicValidator;
38
import org.openscience.cdk.validate.CDKValidator;
39
import org.openscience.cdk.validate.ProblemMarker;
42
import java.awt.event.ActionEvent;
43
import java.util.Iterator;
47
* An action opening a validation frame
49
* @cdk.module jchempaint
50
* @author E.L. Willighagen <elw38@cam.ac.uk>
52
public class ValidateAction extends JCPAction
55
private static final long serialVersionUID = -3776589605934024224L;
57
ValidateFrame frame = null;
59
public void actionPerformed(ActionEvent event)
61
logger.debug("detected validate action: ", type);
62
if (type.equals("run"))
64
IChemObject object = getSource(event);
67
// called from main menu
68
JChemPaintModel jcpmodel = jcpPanel.getJChemPaintModel();
70
org.openscience.cdk.interfaces.IChemModel model = jcpmodel.getChemModel();
76
System.out.println("Empty model");
80
// calleb from popup menu
81
logger.debug("Validate called from popup menu!");
84
} else if (type.equals("clear"))
87
} else if (type.startsWith("toggle") && type.length() > 6)
89
String toggle = type.substring(6);
92
JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) event.getSource();
93
boolean newChecked = !menuItem.isSelected();
94
menuItem.setSelected(newChecked);
95
if (toggle.equals("Basic"))
99
logger.info("Turned on " + toggle);
100
JChemPaintEditorPanel.getValidatorEngine().addValidator(new BasicValidator());
103
logger.info("Turned off " + toggle);
104
JChemPaintEditorPanel.getValidatorEngine().removeValidator(new BasicValidator());
106
} else if (toggle.equals("CDK"))
110
logger.info("Turned on " + toggle);
111
JChemPaintEditorPanel.getValidatorEngine().addValidator(new CDKValidator());
114
logger.info("Turned off " + toggle);
115
JChemPaintEditorPanel.getValidatorEngine().removeValidator(new CDKValidator());
119
logger.error("Don't know what to toggle: " + toggle);
121
} catch (ClassCastException exception)
123
logger.error("Cannot toggle a non JCheckBoxMenuItem!");
127
logger.error("Unknown command: " + type);
131
private void clearValidate()
133
JChemPaintModel jcpmodel = jcpPanel.getJChemPaintModel();
134
org.openscience.cdk.interfaces.IChemModel model = jcpmodel.getChemModel();
135
Iterator containers = ChemModelManipulator.getAllAtomContainers(model).iterator();
136
while (containers.hasNext()) {
137
IAtomContainer atoms = (IAtomContainer)containers.next();
138
logger.info("Clearing errors on atoms: " + atoms.getAtomCount());
139
for (int i = 0; i < atoms.getAtomCount(); i++)
141
ProblemMarker.unmark(atoms.getAtom(i));
144
jcpmodel.fireChange();
147
private void runValidate(IChemObject object)
149
logger.info("Running validation");
151
if (jcpPanel.getJChemPaintModel() != null)
153
frame = new ValidateFrame(jcpPanel);
154
frame.validate(object);
156
frame.setVisible(true);