4
* $Date: 2007-05-01 21:15:34 +0200 (Tue, 01 May 2007) $
7
* Copyright (C) 1997-2007 The JChemPaint project
9
* Contact: jchempaint-devel@lists.sourceforge.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 java.awt.Component;
32
import java.awt.event.ActionEvent;
34
import java.io.FileWriter;
35
import java.io.Writer;
36
import java.lang.reflect.Constructor;
37
import java.util.Iterator;
39
import javax.swing.JFileChooser;
40
import javax.swing.JFrame;
41
import javax.swing.JOptionPane;
42
import javax.swing.filechooser.FileFilter;
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;
63
* Opens a "Save as" dialog
65
* @cdk.module jchempaint
68
public class SaveAsAction extends JCPAction
71
private static final long serialVersionUID = -5138502232232716970L;
73
protected IChemObjectWriter cow;
74
protected static String type = null;
75
private FileFilter currentFilter = null;
79
* Constructor for the SaveAsAction object
87
* Constructor for the SaveAsAction object
89
*@param jcpPanel Description of the Parameter
90
*@param isPopupAction Description of the Parameter
92
public SaveAsAction(JChemPaintPanel jcpPanel, boolean isPopupAction)
94
super(jcpPanel, "", isPopupAction);
99
* Opens a dialog frame and manages the saving of a file.
101
*@param event Description of the Parameter
103
public void actionPerformed(ActionEvent event)
106
JChemPaintModel jcpm = jcpPanel.getJChemPaintModel();
109
String error = "Nothing to save.";
110
JOptionPane.showMessageDialog(jcpPanel, error);
117
protected void saveAs(ActionEvent event)
121
JChemPaintModel jcpm = jcpPanel.getJChemPaintModel();
122
JFileChooser chooser = new JFileChooser();
123
chooser.setCurrentDirectory(jcpPanel.getCurrentWorkDirectory());
124
JCPSaveFileFilter.addChoosableFileFilters(chooser);
125
if (jcpPanel.getCurrentSaveFileFilter() != null)
128
// chooser.setFileFilter(jcpPanel.getCurrentSaveFileFilter());
130
if (currentFilter != null)
132
chooser.setFileFilter(currentFilter);
134
chooser.setFileView(new JCPFileView());
136
int returnVal = chooser.showSaveDialog(jcpPanel);
138
IChemObject object = getSource(event);
139
currentFilter = chooser.getFileFilter();
140
if(returnVal==JFileChooser.CANCEL_OPTION)
142
if (returnVal == JFileChooser.APPROVE_OPTION)
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);
153
IChemModel model = (IChemModel) jcpm.getChemModel();
156
// called from main menu
159
if (type.equals(JCPSaveFileFilter.mol))
161
saveAsMol(model, outFile);
162
} else if (type.equals(JCPSaveFileFilter.cml))
164
saveAsCML2(model, outFile);
165
} else if (type.equals(JCPSaveFileFilter.smiles))
167
saveAsSMILES(model, outFile);
168
} else if (type.equals(JCPSaveFileFilter.svg))
170
saveAsSVG(model, outFile);
171
} else if (type.equals(JCPSaveFileFilter.cdk))
173
saveAsCDKSourceCode(model, outFile);
176
String error = "Cannot save file in this format: " + type;
178
JOptionPane.showMessageDialog(jcpPanel, error);
181
jcpm.resetIsModified();
182
} catch (Exception exc)
184
String error = "Error while writing file: " + exc.getMessage();
187
JOptionPane.showMessageDialog(jcpPanel, error);
190
} else if (object instanceof Reaction)
194
if (type.equals(JCPSaveFileFilter.cml))
196
saveAsCML2(object, outFile);
199
String error = "Cannot save reaction in this format: " + type;
201
JOptionPane.showMessageDialog(jcpPanel, error);
203
} catch (Exception exc)
205
String error = "Error while writing file: " + exc.getMessage();
208
JOptionPane.showMessageDialog(jcpPanel, error);
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());
221
private boolean askIOSettings() {
222
return JCPPropertyHandler.getInstance().getJCPProperties()
223
.getProperty("askForIOSettings", "true").equals("true");
226
protected void saveAsMol(IChemModel model, File outFile) throws Exception
228
logger.info("Saving the contents in a MDL molfile file...");
229
String fileName = outFile.toString();
230
if (!fileName.endsWith(".mol")) {
232
outFile = new File(fileName);
234
outFile=new File(fileName);
235
cow = new MDLWriter(new FileWriter(outFile));
236
if (cow != null && askIOSettings())
238
cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
240
org.openscience.cdk.interfaces.IMoleculeSet som = model.getMoleculeSet();
245
protected void saveAsCML2(IChemObject object, File outFile) throws Exception
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!");
251
logger.info("Saving the contents in a CML 2.0 file...");
252
String fileName = outFile.toString();
253
if (!fileName.endsWith(".cml")) {
255
outFile = new File(fileName);
257
FileWriter sw = new FileWriter(outFile);
258
Class cmlWriterClass = this.getClass().getClassLoader().loadClass("org.openscience.cdk.io.CMLWriter");
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});
265
// provide a fail save for JChemPaint builds for Java 1.4
266
cow = new MDLWriter(sw);
268
if (cow != null && askIOSettings())
270
cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
277
protected void saveAsSMILES(IChemModel model, File outFile) throws Exception
279
logger.info("Saving the contents in SMILES format...");
280
String fileName = outFile.toString();
281
if (!fileName.endsWith(".smi")) {
284
cow = new SMILESWriter(new FileWriter(outFile));
285
if (cow != null && askIOSettings())
287
cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
289
org.openscience.cdk.interfaces.IMoleculeSet som = model.getMoleculeSet();
294
protected void saveAsCDKSourceCode(IChemModel model, File outFile) throws Exception
296
logger.info("Saving the contents as a CDK source code file...");
297
String fileName = outFile.toString();
298
if (!fileName.endsWith(".cdk")) {
300
outFile = new File(fileName);
302
cow = new CDKSourceCodeWriter(new FileWriter(outFile));
303
if (cow != null && askIOSettings())
305
cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
307
Iterator containers = ChemModelManipulator.getAllAtomContainers(model).iterator();
308
while (containers.hasNext()) {
309
IAtomContainer ac = (IAtomContainer)containers.next();
313
System.err.println("AC == null!");
319
protected void saveAsSVG(IChemModel model, File outFile) throws Exception
321
logger.info("Saving the contents as a SVG file...");
322
cow = new SVGWriter(new FileWriter(outFile));
323
if (cow != null && askIOSettings())
325
cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, 4));
327
Iterator containers = ChemModelManipulator.getAllAtomContainers(model).iterator();
328
while (containers.hasNext()) {
329
IAtomContainer ac = (IAtomContainer)containers.next();
332
cow.write((IAtomContainer) ac.clone());
335
System.err.println("AC == null!");