View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.impl.actions;
14  
15  import java.io.File;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.WorkspaceImpl;
19  import com.eviware.soapui.support.MessageSupport;
20  import com.eviware.soapui.support.SoapUIException;
21  import com.eviware.soapui.support.UISupport;
22  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
23  
24  /***
25   * Action for creating a new Workspace
26   * 
27   * @author ole.matzura
28   */
29  
30  public class NewWorkspaceAction extends AbstractSoapUIAction<WorkspaceImpl>
31  {
32  	public static final String SOAPUI_ACTION_ID = "NewWorkspaceAction";
33  	public static final MessageSupport messages = MessageSupport.getMessages( NewWorkspaceAction.class );
34  
35  	public NewWorkspaceAction()
36  	{
37  		super( messages.get( "Title" ), messages.get( "Description" ) );
38  	}
39  
40  	public void perform( WorkspaceImpl workspace, Object param )
41  	{
42  		if( SoapUI.getTestMonitor().hasRunningTests() )
43  		{
44  			UISupport.showErrorMessage( messages.get( "FailBecauseOfRunningTests" ) );
45  			return;
46  		}
47  
48  		String name = UISupport.prompt( messages.get( "EnterName.Prompt" ), messages.get( "EnterName.Title" ), "" );
49  		if( name == null )
50  			return;
51  
52  		File newPath = UISupport.getFileDialogs().saveAs( this, messages.get( "SaveAs.Title" ), ".xml",
53  				"soapUI Workspace (*.xml)", new File( name + "-workspace.xml" ) );
54  		if( newPath == null )
55  			return;
56  
57  		if( SoapUI.getDesktop().closeAll() )
58  		{
59  			if( newPath.exists() )
60  			{
61  				if( !UISupport.confirm( messages.get( "Overwrite.Prompt" ), messages.get( "Overwrite.Title" ) ) )
62  				{
63  					return;
64  				}
65  
66  				if( !newPath.delete() )
67  				{
68  					UISupport.showErrorMessage( messages.get( "NewWorkspaceAction.FailedToDeleteExisting" ) );
69  					return;
70  				}
71  			}
72  
73  			Boolean val = Boolean.TRUE;
74  
75  			if( workspace.getOpenProjectList().size() > 0 )
76  			{
77  				val = UISupport.confirmOrCancel( messages.get( "SaveAllProjects.Prompt" ), messages
78  						.get( "SaveAllProjects.Title" ) );
79  				if( val == null )
80  					return;
81  			}
82  
83  			workspace.save( val.booleanValue() );
84  
85  			try
86  			{
87  				workspace.switchWorkspace( newPath );
88  				SoapUI.getSettings().setString( SoapUI.CURRENT_SOAPUI_WORKSPACE, newPath.getAbsolutePath() );
89  				workspace.setName( name );
90  			}
91  			catch( SoapUIException e )
92  			{
93  				UISupport.showErrorMessage( e );
94  			}
95  
96  		}
97  	}
98  }