View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    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.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  {
    37  	private final JXEditTextArea textArea;
    38  	private String dialogTitle;
    39  	private static final Logger log = Logger.getLogger( SaveXmlTextAreaAction.class );
    40
    41  	public SaveXmlTextAreaAction( JXEditTextArea textArea, String dialogTitle )
    42  	{
    43  		super( "Save as.." );
    44  		this.textArea = textArea;
    45  		this.dialogTitle = dialogTitle;
    46  		putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu S" ));
    47  	}
    48
    49  	public void actionPerformed(ActionEvent e)
    50  	{
    51        File file = UISupport.getFileDialogs().saveAs(this, dialogTitle, ".xml", "XML Files (*.xml)", null);
    52        if( file == null )
    53           return;
    54
    55  		FileWriter writer = null;
    56
    57  		try
    58  		{
    59  			writer = new FileWriter(file);
    60  			writer.write( textArea.getText() );
    61
    62  			log.info( "XML written to [" + file.getAbsolutePath() + "]" );
    63  			writer.close();
    64  		}
    65  		catch (IOException e1)
    66  		{
    67  			UISupport.showErrorMessage( "Error saving xml to file: " + e1.getMessage());
    68  		}
    69  		finally
    70  		{
    71  			if( writer != null )
    72  			{
    73  				try
    74  				{
    75  					writer.close();
    76  				}
    77  				catch (IOException e1)
    78  				{
    79  					SoapUI.logError( e1 );
    80  				}
    81  			}
    82  		}
    83  	}
    84  }