View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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( 
53              this, messages.get("SaveAs.Title"), ".xml", "soapUI Workspace (*.xml)",  
54  					new File(name + "-workspace.xml") ); 
55  		if( newPath == null )
56  			return;
57  
58  		if( SoapUI.getDesktop().closeAll())
59  		{
60  			if( newPath.exists() )
61  			{
62  				if( !UISupport.confirm( messages.get("Overwrite.Prompt"), messages.get("Overwrite.Title") )) 
63  				{
64  					return;
65  				}
66  				
67  				if( !newPath.delete())
68  				{
69  					UISupport.showErrorMessage( messages.get("NewWorkspaceAction.FailedToDeleteExisting") ); 
70  					return;
71  				}
72  			}
73  			
74  			Boolean val = Boolean.TRUE;
75  			
76  			if( workspace.getOpenProjectList().size() > 0 )
77  			{
78  				val = UISupport.confirmOrCancel( messages.get("SaveAllProjects.Prompt"), messages.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  }