~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/applications/jchempaint/action/SaveAsAction.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) 1997-2007  The JChemPaint project
 
8
 *
 
9
 *  Contact: jchempaint-devel@lists.sourceforge.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 java.awt.Component;
 
32
import java.awt.event.ActionEvent;
 
33
import java.io.File;
 
34
import java.io.FileWriter;
 
35
import java.io.Writer;
 
36
import java.lang.reflect.Constructor;
 
37
import java.util.Iterator;
 
38
 
 
39
import javax.swing.JFileChooser;
 
40
import javax.swing.JFrame;
 
41
import javax.swing.JOptionPane;
 
42
import javax.swing.filechooser.FileFilter;
 
43
 
 
44
import org.openscience.cdk.Reaction;
 
45
import org.openscience.cdk.applications.jchempaint.JCPPropertyHandler;
 
46
import org.openscience.cdk.applications.jchempaint.JChemPaintModel;
 
47
import org.openscience.cdk.applications.jchempaint.JChemPaintPanel;
 
48
import org.openscience.cdk.applications.jchempaint.io.IJCPFileFilter;
 
49
import org.openscience.cdk.applications.jchempaint.io.JCPFileView;
 
50
import org.openscience.cdk.applications.jchempaint.io.JCPSaveFileFilter;
 
51
import org.openscience.cdk.interfaces.IAtomContainer;
 
52
import org.openscience.cdk.interfaces.IChemModel;
 
53
import org.openscience.cdk.interfaces.IChemObject;
 
54
import org.openscience.cdk.io.CDKSourceCodeWriter;
 
55
import org.openscience.cdk.io.IChemObjectWriter;
 
56
import org.openscience.cdk.io.MDLWriter;
 
57
import org.openscience.cdk.io.SMILESWriter;
 
58
import org.openscience.cdk.io.SVGWriter;
 
59
import org.openscience.cdk.io.listener.SwingGUIListener;
 
60
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;
 
61
 
 
62
/**
 
63
 * Opens a "Save as" dialog
 
64
 *
 
65
 * @cdk.module jchempaint
 
66
 * @author     steinbeck
 
67
 */
 
68
public class SaveAsAction extends JCPAction
 
69
{
 
70
 
 
71
    private static final long serialVersionUID = -5138502232232716970L;
 
72
    
 
73
    protected IChemObjectWriter cow;
 
74
    protected static String type = null;
 
75
    private FileFilter currentFilter = null;
 
76
 
 
77
 
 
78
        /**
 
79
         *  Constructor for the SaveAsAction object
 
80
         */
 
81
        public SaveAsAction()
 
82
        {
 
83
                super();
 
84
        }
 
85
 
 
86
        /**
 
87
         *  Constructor for the SaveAsAction object
 
88
         *
 
89
         *@param  jcpPanel       Description of the Parameter
 
90
         *@param  isPopupAction  Description of the Parameter
 
91
         */
 
92
  public SaveAsAction(JChemPaintPanel jcpPanel, boolean isPopupAction)
 
93
        {
 
94
                super(jcpPanel, "", isPopupAction);
 
95
        }
 
96
 
 
97
 
 
98
        /**
 
99
         *  Opens a dialog frame and manages the saving of a file.
 
100
         *
 
101
         *@param  event  Description of the Parameter
 
102
         */
 
103
        public void actionPerformed(ActionEvent event)
 
104
        {
 
105
 
 
106
                JChemPaintModel jcpm = jcpPanel.getJChemPaintModel();
 
107
                if (jcpm == null)
 
108
                {
 
109
                        String error = "Nothing to save.";
 
110
                        JOptionPane.showMessageDialog(jcpPanel, error);
 
111
                } else
 
112
                {
 
113
                        saveAs(event);
 
114
                }
 
115
        }
 
116
 
 
117
        protected void saveAs(ActionEvent event)
 
118
        {
 
119
                int ready=1;
 
120
                while(ready==1){
 
121
                        JChemPaintModel jcpm = jcpPanel.getJChemPaintModel();
 
122
                        JFileChooser chooser = new JFileChooser();
 
123
                        chooser.setCurrentDirectory(jcpPanel.getCurrentWorkDirectory());
 
124
                        JCPSaveFileFilter.addChoosableFileFilters(chooser);
 
125
                        if (jcpPanel.getCurrentSaveFileFilter() != null)
 
126
                        {
 
127
                                // XXX needs fixing
 
128
                                // chooser.setFileFilter(jcpPanel.getCurrentSaveFileFilter());
 
129
                        }
 
130
                        if (currentFilter != null)
 
131
                        {
 
132
                                chooser.setFileFilter(currentFilter);
 
133
                        }
 
134
                        chooser.setFileView(new JCPFileView());
 
135
        
 
136
                        int returnVal = chooser.showSaveDialog(jcpPanel);
 
137
                        
 
138
                        IChemObject object = getSource(event);
 
139
                        currentFilter = chooser.getFileFilter();
 
140
                        if(returnVal==JFileChooser.CANCEL_OPTION)
 
141
                                ready=0;
 
142
                        if (returnVal == JFileChooser.APPROVE_OPTION)
 
143
                        {
 
144
                                type = ((IJCPFileFilter) currentFilter).getType();
 
145
                                File outFile = chooser.getSelectedFile();
 
146
                                if(outFile.exists()){
 
147
                                        ready=JOptionPane.showConfirmDialog((Component)null,"File "+outFile.getName()+" already exists. Do you want to overwrite it?","File already exists",JOptionPane.YES_NO_OPTION);
 
148
                                }else{
 
149
                                        ready=0;
 
150
                                }
 
151
                                if(ready==0){
 
152
                                        
 
153
                                        IChemModel model = (IChemModel) jcpm.getChemModel();
 
154
                                        if (object == null)
 
155
                                        {
 
156
                                                // called from main menu
 
157
                                                try
 
158
                                                {
 
159
                                                        if (type.equals(JCPSaveFileFilter.mol))
 
160
                                                        {
 
161
                                                                saveAsMol(model, outFile);
 
162
                                                        } else if (type.equals(JCPSaveFileFilter.cml))
 
163
                                                        {
 
164
                                                                saveAsCML2(model, outFile);
 
165
                                                        } else if (type.equals(JCPSaveFileFilter.smiles))
 
166
                                                        {
 
167
                                                                saveAsSMILES(model, outFile);
 
168
                                                        } else if (type.equals(JCPSaveFileFilter.svg))
 
169
                                                        {
 
170
                                                                saveAsSVG(model, outFile);
 
171
                                                        } else if (type.equals(JCPSaveFileFilter.cdk))
 
172
                                                        {
 
173
                                                                saveAsCDKSourceCode(model, outFile);
 
174
                                                        } else
 
175
                                                        {
 
176
                                                                String error = "Cannot save file in this format: " + type;
 
177
                                                                logger.error(error);
 
178
                                                                JOptionPane.showMessageDialog(jcpPanel, error);
 
179
                                                                return;
 
180
                                                        }
 
181
                                                        jcpm.resetIsModified();
 
182
                                                } catch (Exception exc)
 
183
                                                {
 
184
                                                        String error = "Error while writing file: " + exc.getMessage();
 
185
                                                        logger.error(error);
 
186
                                                        logger.debug(exc);
 
187
                                                        JOptionPane.showMessageDialog(jcpPanel, error);
 
188
                                                }
 
189
                
 
190
                                        } else if (object instanceof Reaction)
 
191
                                        {
 
192
                                                try
 
193
                                                {
 
194
                                                        if (type.equals(JCPSaveFileFilter.cml))
 
195
                                                        {
 
196
                                                                saveAsCML2(object, outFile);
 
197
                                                        } else
 
198
                                                        {
 
199
                                                                String error = "Cannot save reaction in this format: " + type;
 
200
                                                                logger.error(error);
 
201
                                                                JOptionPane.showMessageDialog(jcpPanel, error);
 
202
                                                        }
 
203
                                                } catch (Exception exc)
 
204
                                                {
 
205
                                                        String error = "Error while writing file: " + exc.getMessage();
 
206
                                                        logger.error(error);
 
207
                                                        logger.debug(exc);
 
208
                                                        JOptionPane.showMessageDialog(jcpPanel, error);
 
209
                                                }
 
210
                                        }
 
211
                                        jcpPanel.setCurrentWorkDirectory(chooser.getCurrentDirectory());
 
212
                                        jcpPanel.setCurrentSaveFileFilter(chooser.getFileFilter());
 
213
                                        jcpPanel.setIsAlreadyAFile(outFile);
 
214
                                        jcpPanel.getJChemPaintModel().setTitle(outFile.getName());
 
215
                                        ((JFrame)jcpPanel.getParent().getParent().getParent().getParent()).setTitle(outFile.getName());
 
216
                                }
 
217
                        }
 
218
                }
 
219
        }
 
220
 
 
221
    private boolean askIOSettings() {
 
222
        return JCPPropertyHandler.getInstance().getJCPProperties()
 
223
            .getProperty("askForIOSettings", "true").equals("true");
 
224
    }
 
225
    
 
226
        protected void saveAsMol(IChemModel model, File outFile) throws Exception
 
227
        {
 
228
                logger.info("Saving the contents in a MDL molfile file...");
 
229
        String fileName = outFile.toString();
 
230
        if (!fileName.endsWith(".mol")) {
 
231
            fileName += ".mol";
 
232
            outFile = new File(fileName);
 
233
        }
 
234
        outFile=new File(fileName);
 
235
        cow = new MDLWriter(new FileWriter(outFile));
 
236
                if (cow != null && askIOSettings())
 
237
                {
 
238
                        cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
 
239
                }
 
240
                org.openscience.cdk.interfaces.IMoleculeSet som = model.getMoleculeSet();
 
241
                cow.write(som);
 
242
                cow.close();
 
243
        }
 
244
 
 
245
        protected void saveAsCML2(IChemObject object, File outFile) throws Exception
 
246
        {
 
247
                if(Float.parseFloat(System.getProperty("java.specification.version"))<1.5){
 
248
                        JOptionPane.showMessageDialog(null,"For saving as CML you need Java 1.5 or higher!");
 
249
                        return;
 
250
                }
 
251
                logger.info("Saving the contents in a CML 2.0 file...");
 
252
        String fileName = outFile.toString();
 
253
        if (!fileName.endsWith(".cml")) {
 
254
            fileName += ".cml";
 
255
            outFile = new File(fileName);
 
256
        }
 
257
        FileWriter sw = new FileWriter(outFile);
 
258
        Class cmlWriterClass = this.getClass().getClassLoader().loadClass("org.openscience.cdk.io.CMLWriter");
 
259
 
 
260
        if (cmlWriterClass != null) {
 
261
                cow = (IChemObjectWriter)cmlWriterClass.newInstance();
 
262
                Constructor constructor = cow.getClass().getConstructor(new Class[]{Writer.class});
 
263
                cow = (IChemObjectWriter)constructor.newInstance(new Object[]{sw});
 
264
        } else {
 
265
                // provide a fail save for JChemPaint builds for Java 1.4
 
266
                cow = new MDLWriter(sw);
 
267
        }
 
268
                if (cow != null && askIOSettings())
 
269
                {
 
270
                        cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
 
271
                }
 
272
                cow.write(object);
 
273
                cow.close();
 
274
                sw.close();
 
275
        }
 
276
 
 
277
        protected void saveAsSMILES(IChemModel model, File outFile) throws Exception
 
278
        {
 
279
                logger.info("Saving the contents in SMILES format...");
 
280
        String fileName = outFile.toString();
 
281
        if (!fileName.endsWith(".smi")) {
 
282
            fileName += ".smi";
 
283
        }
 
284
        cow = new SMILESWriter(new FileWriter(outFile));
 
285
                if (cow != null && askIOSettings())
 
286
                {
 
287
                        cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
 
288
                }
 
289
                org.openscience.cdk.interfaces.IMoleculeSet som = model.getMoleculeSet();
 
290
                cow.write(som);
 
291
                cow.close();
 
292
        }
 
293
 
 
294
        protected void saveAsCDKSourceCode(IChemModel model, File outFile) throws Exception
 
295
        {
 
296
                logger.info("Saving the contents as a CDK source code file...");
 
297
        String fileName = outFile.toString();
 
298
        if (!fileName.endsWith(".cdk")) {
 
299
            fileName += ".cdk";
 
300
            outFile = new File(fileName);
 
301
        }
 
302
                cow = new CDKSourceCodeWriter(new FileWriter(outFile));
 
303
                if (cow != null && askIOSettings())
 
304
                {
 
305
                        cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
 
306
                }
 
307
                Iterator containers = ChemModelManipulator.getAllAtomContainers(model).iterator();
 
308
                while (containers.hasNext()) {
 
309
                        IAtomContainer ac = (IAtomContainer)containers.next();
 
310
                        if (ac != null) {
 
311
                                cow.write(ac);
 
312
                        } else {
 
313
                                System.err.println("AC == null!");
 
314
                        }
 
315
                }
 
316
                cow.close();
 
317
        }
 
318
 
 
319
        protected void saveAsSVG(IChemModel model, File outFile) throws Exception
 
320
        {
 
321
                logger.info("Saving the contents as a SVG file...");
 
322
                cow = new SVGWriter(new FileWriter(outFile));
 
323
                if (cow != null && askIOSettings())
 
324
                {
 
325
                        cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
 
326
                }
 
327
                Iterator containers = ChemModelManipulator.getAllAtomContainers(model).iterator();
 
328
                while (containers.hasNext()) {
 
329
                        IAtomContainer ac = (IAtomContainer)containers.next();
 
330
                        if (ac != null)
 
331
                        {
 
332
                                cow.write((IAtomContainer) ac.clone());
 
333
                        } else
 
334
                        {
 
335
                                System.err.println("AC == null!");
 
336
                        }
 
337
                }
 
338
                cow.close();
 
339
        }
 
340
}
 
341