01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.support.xml.actions;
14:
15: import java.awt.event.ActionEvent;
16: import java.io.File;
17: import java.io.FileWriter;
18: import java.io.IOException;
19:
20: import javax.swing.AbstractAction;
21: import javax.swing.Action;
22:
23: import org.apache.log4j.Logger;
24:
25: import com.eviware.soapui.SoapUI;
26: import com.eviware.soapui.support.UISupport;
27: import com.eviware.soapui.support.xml.JXEditTextArea;
28:
29: /**
30: * Saves the XML of a JXmlTextArea to a file
31: *
32: * @author Ole.Matzura
33: */
34:
35: public class SaveXmlTextAreaAction extends AbstractAction {
36: private final JXEditTextArea textArea;
37: private String dialogTitle;
38: private static final Logger log = Logger
39: .getLogger(SaveXmlTextAreaAction.class);
40:
41: public SaveXmlTextAreaAction(JXEditTextArea textArea,
42: String dialogTitle) {
43: super ("Save as..");
44: this .textArea = textArea;
45: this .dialogTitle = dialogTitle;
46: putValue(Action.ACCELERATOR_KEY, UISupport
47: .getKeyStroke("menu S"));
48: }
49:
50: public void actionPerformed(ActionEvent e) {
51: File file = UISupport.getFileDialogs().saveAs(this ,
52: dialogTitle, ".xml", "XML Files (*.xml)", null);
53: if (file == null)
54: return;
55:
56: FileWriter writer = null;
57:
58: try {
59: writer = new FileWriter(file);
60: writer.write(textArea.getText());
61:
62: log.info("XML written to [" + file.getAbsolutePath() + "]");
63: writer.close();
64: } catch (IOException e1) {
65: UISupport.showErrorMessage("Error saving xml to file: "
66: + e1.getMessage());
67: } finally {
68: if (writer != null) {
69: try {
70: writer.close();
71: } catch (IOException e1) {
72: SoapUI.logError(e1);
73: }
74: }
75: }
76: }
77: }
|