1
2
3
4
5
6
7
8
9
10
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 import com.eviware.soapui.support.types.StringToStringMap;
27
28 /***
29 * Settings implementation for XmlBeans generated SettingsConfig
30 *
31 * @author Ole.Matzura
32 */
33
34 public class XmlBeansSettingsImpl implements Settings
35 {
36 private final Settings parent;
37 private final SettingsConfig config;
38 private final Map<String, SettingConfig> values = new HashMap<String, SettingConfig>();
39 private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
40 private final ModelItem item;
41 private final SettingsListener settingsListener = new WeakSettingsListener( new InternalSettingsListener() );
42
43 public XmlBeansSettingsImpl( ModelItem item, Settings parent, SettingsConfig config )
44 {
45 this.item = item;
46 this.config = config;
47 this.parent = parent;
48
49 List<SettingConfig> settingList = config.getSettingList();
50 for( SettingConfig setting : settingList )
51 {
52 values.put( setting.getId(), setting );
53 }
54
55 if( parent != null )
56 {
57 parent.addSettingsListener( settingsListener );
58 }
59 }
60
61 public boolean isSet( String id )
62 {
63 return values.containsKey( id );
64 }
65
66 public String getString( String id, String defaultValue )
67 {
68 if( values.containsKey( id ) )
69 return values.get( id ).getStringValue();
70 return parent == null ? defaultValue : parent.getString( id, defaultValue );
71 }
72
73 public void setString( String id, String value )
74 {
75 String oldValue = getString( id, null );
76
77 if( oldValue == null && value == null )
78 return;
79
80 if( value != null && value.equals( oldValue ) )
81 return;
82
83 if( value == null )
84 {
85 clearSetting( id );
86 }
87 else
88 {
89 if( !values.containsKey( id ) )
90 {
91 SettingConfig setting = config.addNewSetting();
92 setting.setId( id );
93 values.put( id, setting );
94 }
95
96 values.get( id ).setStringValue( value );
97 }
98
99 notifySettingChanged( id, value, oldValue );
100 }
101
102 private void notifySettingChanged( String id, String value, String oldValue )
103 {
104 SettingsListener[] l = listeners.toArray( new SettingsListener[listeners.size()] );
105 for( SettingsListener listener : l )
106 {
107 listener.settingChanged( id, value, oldValue );
108 }
109 }
110
111 public boolean getBoolean( String id )
112 {
113 if( values.containsKey( id ) )
114 return Boolean.parseBoolean( values.get( id ).getStringValue() );
115
116 return parent == null ? false : parent.getBoolean( id );
117 }
118
119 public long getLong( String id, long defaultValue )
120 {
121 if( values.containsKey( id ) )
122 {
123 try
124 {
125 return Long.parseLong( values.get( id ).getStringValue() );
126 }
127 catch( NumberFormatException e )
128 {
129 }
130 }
131
132 return parent == null ? defaultValue : parent.getLong( id, defaultValue );
133 }
134
135 public void setBoolean( String id, boolean value )
136 {
137 if( !value )
138 setString( id, "false" );
139 else
140 setString( id, "true" );
141 }
142
143 public void addSettingsListener( SettingsListener listener )
144 {
145 listeners.add( listener );
146 }
147
148 public void removeSettingsListener( SettingsListener listener )
149 {
150 listeners.remove( listener );
151 }
152
153 public void clearSetting( String id )
154 {
155 if( values.containsKey( id ) )
156 {
157 int ix = config.getSettingList().indexOf( values.get( id ) );
158 config.removeSetting( ix );
159 values.remove( id );
160 }
161 }
162
163 public ModelItem getModelItem()
164 {
165 return item;
166 }
167
168 public void release()
169 {
170 if( listeners != null )
171 listeners.clear();
172
173 if( parent != null )
174 parent.removeSettingsListener( settingsListener );
175 }
176
177 private final class InternalSettingsListener implements SettingsListener
178 {
179 public void settingChanged( String name, String newValue, String oldValue )
180 {
181 if( !values.containsKey( name ) )
182 {
183 notifySettingChanged( name, newValue, oldValue );
184 }
185 }
186 }
187
188 public void setLong( String id, long value )
189 {
190 setString( id, Long.toString( value ) );
191 }
192
193 public void setConfig( SettingsConfig soapuiSettings )
194 {
195 StringToStringMap changed = new StringToStringMap();
196
197 for( SettingConfig config : soapuiSettings.getSettingList() )
198 {
199 if( !config.getStringValue().equals( getString( config.getId(), null ) ) )
200 changed.put( config.getId(), getString( config.getId(), null ) );
201 }
202
203 values.clear();
204
205 config.set( soapuiSettings );
206 List<SettingConfig> settingList = config.getSettingList();
207 for( SettingConfig setting : settingList )
208 {
209 values.put( setting.getId(), setting );
210 }
211
212 for( String key : changed.keySet() )
213 {
214 notifySettingChanged( key, getString( key, null ), changed.get( key ) );
215 }
216 }
217 }