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.settings;
14  
15  import java.util.HashSet;
16  import java.util.Set;
17  
18  import com.eviware.soapui.model.settings.Settings;
19  import com.eviware.soapui.model.settings.SettingsListener;
20  import com.eviware.soapui.support.types.StringToStringMap;
21  
22  /***
23   * Default Settings implementation
24   * 
25   * @author Ole.Matzura
26   */
27  
28  public class SettingsImpl implements Settings
29  {
30  	private final Settings parent;
31  	private final StringToStringMap values = new StringToStringMap();
32  	private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
33  
34  	public SettingsImpl()
35  	{
36  		this( null );
37  	}
38  
39  	public SettingsImpl( Settings parent )
40  	{
41  		this.parent = parent;
42  	}
43  
44  	public boolean isSet( String id )
45  	{
46  		return values.containsKey( id );
47  	}
48  
49  	public String getString( String id, String defaultValue )
50  	{
51  		if( values.containsKey( id ) )
52  			return values.get( id );
53  		return parent == null ? defaultValue : parent.getString( id, defaultValue );
54  	}
55  
56  	public void setString( String id, String value )
57  	{
58  		String oldValue = getString( id, null );
59  		values.put( id, value );
60  
61  		for( SettingsListener listener : listeners )
62  		{
63  			listener.settingChanged( id, value, oldValue );
64  		}
65  	}
66  
67  	public void reloadSettings()
68  	{
69  		for( SettingsListener listener : listeners )
70  		{
71  			listener.settingsReloaded();
72  		}
73  	}
74  
75  	public boolean getBoolean( String id )
76  	{
77  		if( values.containsKey( id ) )
78  			return Boolean.parseBoolean( values.get( id ) );
79  		return parent == null ? false : parent.getBoolean( id );
80  	}
81  
82  	public void setBoolean( String id, boolean value )
83  	{
84  		String oldValue = getString( id, null );
85  		values.put( id, Boolean.toString( value ) );
86  
87  		for( SettingsListener listener : listeners )
88  		{
89  			listener.settingChanged( id, Boolean.toString( value ), oldValue );
90  		}
91  	}
92  
93  	public long getLong( String id, long defaultValue )
94  	{
95  		if( values.containsKey( id ) )
96  		{
97  			try
98  			{
99  				return Long.parseLong( values.get( id ) );
100 			}
101 			catch( NumberFormatException e )
102 			{
103 			}
104 		}
105 
106 		return defaultValue;
107 	}
108 
109 	public void addSettingsListener( SettingsListener listener )
110 	{
111 		listeners.add( listener );
112 	}
113 
114 	public void removeSettingsListener( SettingsListener listener )
115 	{
116 		listeners.remove( listener );
117 	}
118 
119 	public void clearSetting( String id )
120 	{
121 		values.remove( id );
122 	}
123 
124 	public void setLong( String id, long value )
125 	{
126 		values.put( id, Long.toString( value ) );
127 	}
128 }