~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/action/ValidateAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  $RCSfile$
 
3
 *  $Author: egonw $
 
4
 *  $Date: 2007-05-01 21:15:34 +0200 (Tue, 01 May 2007) $
 
5
 *  $Revision: 8292 $
 
6
 *
 
7
 *  Copyright (C) 2003-2007  The JChemPaint project
 
8
 *
 
9
 *  Contact: jchempaint-devel@lists.sf.net
 
10
 *
 
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.
 
19
 *
 
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.
 
24
 *
 
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.
 
28
 */
 
29
package org.openscience.cdk.applications.jchempaint.action;
 
30
 
 
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;
 
40
 
 
41
import javax.swing.*;
 
42
import java.awt.event.ActionEvent;
 
43
import java.util.Iterator;
 
44
 
 
45
 
 
46
/**
 
47
 * An action opening a validation frame
 
48
 * 
 
49
 * @cdk.module jchempaint
 
50
 * @author     E.L. Willighagen <elw38@cam.ac.uk>
 
51
 */
 
52
public class ValidateAction extends JCPAction
 
53
{
 
54
 
 
55
    private static final long serialVersionUID = -3776589605934024224L;
 
56
    
 
57
    ValidateFrame frame = null;
 
58
 
 
59
        public void actionPerformed(ActionEvent event)
 
60
        {
 
61
                logger.debug("detected validate action: ", type);
 
62
                if (type.equals("run"))
 
63
                {
 
64
                        IChemObject object = getSource(event);
 
65
                        if (object == null)
 
66
                        {
 
67
                                // called from main menu
 
68
                                JChemPaintModel jcpmodel = jcpPanel.getJChemPaintModel();
 
69
 
 
70
                                org.openscience.cdk.interfaces.IChemModel model = jcpmodel.getChemModel();
 
71
                                if (model != null)
 
72
                                {
 
73
                                        runValidate(model);
 
74
                                } else
 
75
                                {
 
76
                                        System.out.println("Empty model");
 
77
                                }
 
78
                        } else
 
79
                        {
 
80
                                // calleb from popup menu
 
81
                                logger.debug("Validate called from popup menu!");
 
82
                                runValidate(object);
 
83
                        }
 
84
                } else if (type.equals("clear"))
 
85
                {
 
86
                        clearValidate();
 
87
                } else if (type.startsWith("toggle") && type.length() > 6)
 
88
                {
 
89
                        String toggle = type.substring(6);
 
90
                        try
 
91
                        {
 
92
                                JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) event.getSource();
 
93
                                boolean newChecked = !menuItem.isSelected();
 
94
                                menuItem.setSelected(newChecked);
 
95
                                if (toggle.equals("Basic"))
 
96
                                {
 
97
                                        if (newChecked)
 
98
                                        {
 
99
                                                logger.info("Turned on " + toggle);
 
100
                                                JChemPaintEditorPanel.getValidatorEngine().addValidator(new BasicValidator());
 
101
                                        } else
 
102
                                        {
 
103
                                                logger.info("Turned off " + toggle);
 
104
                                                JChemPaintEditorPanel.getValidatorEngine().removeValidator(new BasicValidator());
 
105
                                        }
 
106
                                } else if (toggle.equals("CDK"))
 
107
                                {
 
108
                                        if (newChecked)
 
109
                                        {
 
110
                                                logger.info("Turned on " + toggle);
 
111
                                                JChemPaintEditorPanel.getValidatorEngine().addValidator(new CDKValidator());
 
112
                                        } else
 
113
                                        {
 
114
                                                logger.info("Turned off " + toggle);
 
115
                                                JChemPaintEditorPanel.getValidatorEngine().removeValidator(new CDKValidator());
 
116
                                        }
 
117
                                } else
 
118
                                {
 
119
                                        logger.error("Don't know what to toggle: " + toggle);
 
120
                                }
 
121
                        } catch (ClassCastException exception)
 
122
                        {
 
123
                                logger.error("Cannot toggle a non JCheckBoxMenuItem!");
 
124
                        }
 
125
                } else
 
126
                {
 
127
                        logger.error("Unknown command: " + type);
 
128
                }
 
129
        }
 
130
 
 
131
        private void clearValidate()
 
132
        {
 
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++)
 
140
                        {
 
141
                                ProblemMarker.unmark(atoms.getAtom(i));
 
142
                        }
 
143
                }
 
144
                jcpmodel.fireChange();
 
145
        }
 
146
 
 
147
        private void runValidate(IChemObject object)
 
148
        {
 
149
                logger.info("Running validation");
 
150
                clearValidate();
 
151
                if (jcpPanel.getJChemPaintModel() != null)
 
152
                {
 
153
                        frame = new ValidateFrame(jcpPanel);
 
154
                        frame.validate(object);
 
155
                        frame.pack();
 
156
                        frame.setVisible(true);
 
157
                }
 
158
        }
 
159
 
 
160
}
 
161