View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  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.support.UISupport;
26  import com.eviware.soapui.support.xml.JXEditTextArea;
27  
28  /***
29   * Saves the XML of a JXmlTextArea
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class SaveXmlTextAreaAction extends AbstractAction
35  {
36  	private final JXEditTextArea textArea;
37  	private String dialogTitle;
38  	private static final Logger log = Logger.getLogger( SaveXmlTextAreaAction.class );
39  	
40  	public SaveXmlTextAreaAction( JXEditTextArea textArea, String dialogTitle )
41  	{
42  		super( "Save as.." );
43  		this.textArea = textArea;
44  		this.dialogTitle = dialogTitle;
45  		putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu S" ));
46  	}
47  	
48  	public void actionPerformed(ActionEvent e)
49  	{
50        File file = UISupport.getFileDialogs().saveAs(this, dialogTitle, ".xml", "XML Files (*.xml)", null);
51        if( file == null )
52           return;
53  		
54  		FileWriter writer = null;
55  		
56  		try
57  		{
58  			writer = new FileWriter(file);
59  			writer.write( textArea.getText() );
60  			
61  			log.info( "XML written to [" + file.getAbsolutePath() + "]" );
62  			writer.close();
63  		}
64  		catch (IOException e1)
65  		{
66  			UISupport.showErrorMessage( "Error saving xml to file: " + e1.getMessage());
67  		}
68  		finally
69  		{
70  			if( writer != null )
71  			{
72  				try
73  				{
74  					writer.close();
75  				}
76  				catch (IOException e1)
77  				{
78  					e1.printStackTrace();
79  				}
80  			}
81  		}
82  	}
83  }