View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.HashMap;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import com.eviware.soapui.config.SettingConfig;
22  import com.eviware.soapui.config.SettingsConfig;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.model.settings.Settings;
25  import com.eviware.soapui.model.settings.SettingsListener;
26  
27  /***
28   * Settings implementation for XmlBeans generated SettingsConfig
29   * 
30   * @author Ole.Matzura
31   */
32  
33  public class XmlBeansSettingsImpl implements Settings
34  {
35  	private final Settings parent;
36  	private final SettingsConfig config;
37  	private final Map<String,SettingConfig> values = new HashMap<String,SettingConfig>();
38  	private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
39  	private final ModelItem item;
40  	private final SettingsListener settingsListener = new WeakSettingsListener( new InternalSettingsListener() );
41  
42  	public XmlBeansSettingsImpl( ModelItem item, Settings parent, SettingsConfig config )
43  	{
44  		this.item = item;
45  		this.config = config;
46  		this.parent = parent;
47  		
48  		List<SettingConfig> settingList = config.getSettingList();
49  		for( SettingConfig setting : settingList )
50  		{
51  			values.put( setting.getId(), setting );
52  		}
53  		
54  		if( parent != null )
55  		{
56  			parent.addSettingsListener( settingsListener );
57  		}
58  	}
59  	
60  	public boolean isSet( String id )
61  	{
62  		return values.containsKey( id );
63  	}
64  
65  	public String getString(String id,String defaultValue)
66  	{
67  		if( values.containsKey( id )) return values.get( id ).getStringValue() ;
68  		return parent == null ? defaultValue : parent.getString( id, defaultValue );
69  	}
70  
71  	public void setString(String id, String value)
72  	{
73  		String oldValue = getString( id, null );
74  		
75  		if( oldValue == null && value == null )
76  			return;
77  		
78  		if( value != null && value.equals( oldValue ))
79  			return;
80  		
81  		if( value == null )
82  		{
83  			clearSetting( id );
84  		}
85  		else
86  		{
87  			if( !values.containsKey( id )) 
88  			{
89  				SettingConfig setting = config.addNewSetting();
90  				setting.setId( id );
91  				values.put( id, setting );
92  			}
93  			
94  			values.get( id ).setStringValue( value );
95  		}
96  		
97  		notifySettingChanged(id, value, oldValue);
98  	}
99  
100 	private void notifySettingChanged(String id, String value, String oldValue)
101 	{
102 		SettingsListener [] l = listeners.toArray( new SettingsListener[ listeners.size() ]);
103 		for( SettingsListener listener : l )
104 		{
105 			listener.settingChanged( id, value, oldValue );
106 		}
107 	}
108 
109 	public boolean getBoolean(String id)
110 	{
111 		if( values.containsKey( id )) 
112 			return Boolean.parseBoolean( values.get( id ).getStringValue() );
113 		
114 		return parent == null ? false : parent.getBoolean( id );
115 	}
116 
117 	public long getLong(String id, long defaultValue)
118 	{
119 		if( values.containsKey( id ))
120 		{
121 			try
122 			{
123 				return Long.parseLong(values.get(id).getStringValue());
124 			}
125 			catch (NumberFormatException e)
126 			{
127 			}			
128 		}
129 		
130 		return parent == null ? defaultValue : parent.getLong( id, defaultValue );
131 	}
132 	
133 	public void setBoolean(String id, boolean value)
134 	{
135 		setString( id, Boolean.toString( value ));
136 	}
137 
138 	public void addSettingsListener(SettingsListener listener)
139 	{
140 		listeners.add( listener );
141 	}
142 
143 	public void removeSettingsListener(SettingsListener listener)
144 	{
145 		listeners.remove( listener );
146 	}
147 
148 	public void clearSetting(String id)
149 	{
150 		if( values.containsKey( id ))
151 		{
152 			int ix = config.getSettingList().indexOf( values.get( id ));
153 			config.removeSetting( ix );
154 			values.remove( id );
155 		}
156 	}
157 
158 	public ModelItem getModelItem()
159 	{
160 		return item;
161 	}
162 
163 	public void release()
164 	{
165 		if( parent != null )
166 			parent.removeSettingsListener( settingsListener );
167 	}
168 	
169 	private final class InternalSettingsListener implements SettingsListener
170 	{
171 		public void settingChanged(String name, String newValue, String oldValue)
172 		{
173 			if( !values.containsKey( name ))
174 			{
175 				notifySettingChanged(name, newValue, oldValue);
176 			}
177 		}
178 	}
179 }