View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.wsdl;
14  
15  import java.util.Collection;
16  import java.util.List;
17  
18  import javax.swing.ImageIcon;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.config.ModelItemConfig;
22  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.model.support.AbstractModelItem;
25  import com.eviware.soapui.model.support.ModelSupport;
26  import com.eviware.soapui.support.StringUtils;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.resolver.ResolveContext;
29  
30  /***
31   * Abstract base class for WSDL-implementation classes
32   * 
33   * @author Ole.Matzura
34   */
35  
36  public abstract class AbstractWsdlModelItem<T extends ModelItemConfig> extends AbstractModelItem
37  {
38  	private XmlBeansSettingsImpl settings;
39  	private T config;
40  	private ImageIcon icon;
41  	private final ModelItem parent;
42  
43  	protected AbstractWsdlModelItem( T config, ModelItem parent, String icon )
44  	{
45  		this.parent = parent;
46  		if( config != null )
47  			setConfig( config );
48  
49  		if( icon != null )
50  			this.icon = UISupport.createImageIcon( icon );
51  	}
52  
53  	public boolean dependsOn( ModelItem modelItem )
54  	{
55  		return ModelSupport.dependsOn( this, modelItem );
56  	}
57  
58  	public ModelItem getParent()
59  	{
60  		return parent;
61  	}
62  
63  	public ImageIcon getIcon()
64  	{
65  		return icon;
66  	}
67  
68  	public void setIcon( ImageIcon icon )
69  	{
70  		if( icon == this.icon )
71  			return;
72  
73  		ImageIcon oldIcon = this.icon;
74  		this.icon = icon;
75  		notifyPropertyChanged( ICON_PROPERTY, oldIcon, icon );
76  	}
77  
78  	public String getDescription()
79  	{
80  		String description = config.getDescription();
81  		return StringUtils.hasContent( description ) ? description : "";
82  	}
83  
84  	public void setDescription( String description )
85  	{
86  		String old = getDescription();
87  		config.setDescription( description );
88  		notifyPropertyChanged( DESCRIPTION_PROPERTY, old, description );
89  	}
90  
91  	public String getName()
92  	{
93  		return config.getName();
94  	}
95  
96  	public void setName( String name )
97  	{
98  		String old = getName();
99  		name = name.trim();
100 		config.setName( name );
101 		notifyPropertyChanged( NAME_PROPERTY, old, name );
102 	}
103 
104 	public XmlBeansSettingsImpl getSettings()
105 	{
106 		return settings;
107 	}
108 
109 	public T getConfig()
110 	{
111 		return config;
112 	}
113 
114 	public void setConfig( T config )
115 	{
116 		this.config = config;
117 		
118 		if( config != null && config.isSetName())
119 		{
120 			config.setName( config.getName().trim() );
121 		}
122 
123 		if( settings != null )
124 			settings.release();
125 
126 		if( !config.isSetSettings() )
127 			config.addNewSettings();
128 
129 		settings = new XmlBeansSettingsImpl( this, parent == null ? SoapUI.getSettings() : parent.getSettings(),
130 				this.config.getSettings() );
131 	}
132 
133 	public String getId()
134 	{
135 		if( !config.isSetId() )
136 			config.setId( ModelSupport.generateModelItemID() );
137 
138 		return config.getId();
139 	}
140 
141 	protected void setSettings( XmlBeansSettingsImpl settings )
142 	{
143 		if( this.settings != null )
144 			this.settings.release();
145 
146 		this.settings = settings;
147 	}
148 
149 	public AbstractWsdlModelItem<?> getWsdlModelItemByName( Collection<? extends AbstractWsdlModelItem<?>> items,
150 			String name )
151 	{
152 		for( AbstractWsdlModelItem<?> item : items )
153 		{
154 			if( item.getName() != null && item.getName().equals( name ) )
155 				return item;
156 		}
157 
158 		return null;
159 	}
160 
161 	public void release()
162 	{
163 		if( settings != null )
164 		{
165 			settings.release();
166 		}
167 	}
168 
169 	public void resolve( ResolveContext<?> context )
170 	{
171 		List<? extends ModelItem> children = getChildren();
172 		if( children == null )
173 			return;
174 
175 		for( ModelItem modelItem : children )
176 		{
177 			if( modelItem instanceof AbstractWsdlModelItem )
178 			{
179 				( ( AbstractWsdlModelItem<?> )modelItem ).resolve( context );
180 			}
181 		}
182 	}
183 
184 	public void beforeSave()
185 	{
186 		List<? extends ModelItem> children = getChildren();
187 		if( children == null )
188 			return;
189 
190 		for( ModelItem modelItem : children )
191 		{
192 			if( modelItem instanceof AbstractWsdlModelItem )
193 			{
194 				( ( AbstractWsdlModelItem<?> )modelItem ).beforeSave();
195 			}
196 		}
197 	}
198 
199 	public void afterLoad()
200 	{
201 		List<? extends ModelItem> children = getChildren();
202 		if( children == null )
203 			return;
204 
205 		for( ModelItem modelItem : children )
206 		{
207 			if( modelItem instanceof AbstractWsdlModelItem )
208 			{
209 				( ( AbstractWsdlModelItem<?> )modelItem ).afterLoad();
210 			}
211 		}
212 	}
213 }