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.actions;
14  
15  import javax.swing.JButton;
16  import javax.swing.JCheckBox;
17  import javax.swing.JTextField;
18  import javax.swing.text.Document;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.impl.wsdl.support.http.ProxyUtils;
22  import com.eviware.soapui.model.settings.Settings;
23  import com.eviware.soapui.settings.ProxySettings;
24  import com.eviware.soapui.support.DocumentListenerAdapter;
25  import com.eviware.soapui.support.StringUtils;
26  import com.eviware.soapui.support.UISupport;
27  import com.eviware.soapui.support.components.SimpleForm;
28  import com.eviware.soapui.support.types.StringToStringMap;
29  
30  public class ProxyPrefs implements Prefs
31  {
32  
33  	public static final String HOST = "Host";
34  	public static final String PORT = "Port";
35  	public static final String USERNAME = "Username";
36  	public static final String PASSWORD = "Password";
37  	public static final String EXCLUDES = "Excludes";
38  	public static final String ENABLE_PROXY = "Enable Proxy";
39  
40  	private JTextField hostTextField;
41  	private JTextField portTextField;
42  	private JCheckBox enableProxyCheckbox;
43  	private SimpleForm proxyPrefForm;
44  
45  	private final String title;
46  
47  	public ProxyPrefs( String title )
48  	{
49  		this.title = title;
50  	}
51  
52  	public String getTitle()
53  	{
54  		return title;
55  	}
56  
57  	public SimpleForm getForm()
58  	{
59  		if( proxyPrefForm == null )
60  		{
61  			proxyPrefForm = new SimpleForm();
62  			proxyPrefForm.addSpace( 5 );
63  			hostTextField = proxyPrefForm.appendTextField( HOST, "proxy host to use" );
64  			hostTextField.getDocument().addDocumentListener( new ProxyDocumentListenerAdapter() );
65  			portTextField = proxyPrefForm.appendTextField( PORT, "proxy port to use" );
66  			portTextField.getDocument().addDocumentListener( new ProxyDocumentListenerAdapter() );
67  			proxyPrefForm.appendTextField( USERNAME, "proxy username to use" );
68  			proxyPrefForm.appendTextField( PASSWORD, "proxy password to use" );
69  			proxyPrefForm.appendTextField( EXCLUDES, "Comma-seperated list of hosts to exclude" );
70  			enableProxyCheckbox = proxyPrefForm.appendCheckBox( ENABLE_PROXY, "enable using proxy", true );
71  		}
72  		return proxyPrefForm;
73  	}
74  
75  	private class ProxyDocumentListenerAdapter extends DocumentListenerAdapter
76  	{
77  		@Override
78  		public void update( Document document )
79  		{
80  			enableProxyCheckbox.setSelected( !StringUtils.isNullOrEmpty( hostTextField.getText() )
81  					&& !StringUtils.isNullOrEmpty( portTextField.getText() ) );
82  		}
83  	}
84  
85  	public void getFormValues( Settings settings )
86  	{
87  		StringToStringMap values = new StringToStringMap();
88  		proxyPrefForm.getValues( values );
89  		storeValues( values, settings );
90  	}
91  
92  	public StringToStringMap getValues( Settings settings )
93  	{
94  		StringToStringMap values = new StringToStringMap();
95  		values.put( HOST, settings.getString( ProxySettings.HOST, "" ) );
96  		values.put( PORT, settings.getString( ProxySettings.PORT, "" ) );
97  		values.put( USERNAME, settings.getString( ProxySettings.USERNAME, "" ) );
98  		values.put( PASSWORD, settings.getString( ProxySettings.PASSWORD, "" ) );
99  		values.put( EXCLUDES, settings.getString( ProxySettings.EXCLUDES, "" ) );
100 		values.put( ENABLE_PROXY, settings.getBoolean( ProxySettings.ENABLE_PROXY ) );
101 		ProxyUtils.setProxyEnabled( settings.getBoolean( ProxySettings.ENABLE_PROXY ) );
102 
103 		return values;
104 	}
105 
106 	public void setFormValues( Settings settings )
107 	{
108 		getForm().setValues( getValues( settings ) );
109 	}
110 
111 	public void storeValues( StringToStringMap values, Settings settings )
112 	{
113 		settings.setString( ProxySettings.HOST, values.get( HOST ) );
114 		settings.setString( ProxySettings.PORT, values.get( PORT ) );
115 		settings.setString( ProxySettings.USERNAME, values.get( USERNAME ) );
116 		settings.setString( ProxySettings.PASSWORD, values.get( PASSWORD ) );
117 		settings.setString( ProxySettings.EXCLUDES, values.get( EXCLUDES ) );
118 		settings.setBoolean( ProxySettings.ENABLE_PROXY, values.getBoolean( ENABLE_PROXY ) );
119 		JButton applyProxyButton = ( JButton )SoapUI.getApplyProxyButton();
120 		if( values.getBoolean( ENABLE_PROXY ) )
121 		{
122 			if( applyProxyButton != null )
123 				applyProxyButton.setIcon( UISupport.createImageIcon( SoapUI.PROXY_ENABLED_ICON ) );
124 
125 			ProxyUtils.setProxyEnabled( true );
126 		}
127 		else
128 		{
129 			if( applyProxyButton != null )
130 				applyProxyButton.setIcon( UISupport.createImageIcon( SoapUI.PROXY_DISABLED_ICON ) );
131 
132 			ProxyUtils.setProxyEnabled( false );
133 		}
134 	}
135 
136 }