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.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.AbstractAnimatableModelItem;
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 AbstractAnimatableModelItem<ModelItemConfig>
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  	@Override
69  	public void setIcon( ImageIcon icon )
70  	{
71  		if( icon == this.icon )
72  			return;
73  
74  		ImageIcon oldIcon = this.icon;
75  		this.icon = icon;
76  		notifyPropertyChanged( ICON_PROPERTY, oldIcon, icon );
77  	}
78  
79  	public String getDescription()
80  	{
81  		String description = config.getDescription();
82  		return StringUtils.hasContent( description ) ? description : "";
83  	}
84  
85  	public void setDescription( String description )
86  	{
87  		String old = getDescription();
88  		config.setDescription( description );
89  		notifyPropertyChanged( DESCRIPTION_PROPERTY, old, description );
90  	}
91  
92  	public String getName()
93  	{
94  		return config.getName();
95  	}
96  
97  	public void setName( String name )
98  	{
99  		String old = getName();
100 		name = name.trim();
101 		config.setName( name );
102 		notifyPropertyChanged( NAME_PROPERTY, old, name );
103 	}
104 
105 	public XmlBeansSettingsImpl getSettings()
106 	{
107 		return settings;
108 	}
109 
110 	public T getConfig()
111 	{
112 		return config;
113 	}
114 
115 	public void setConfig( T config )
116 	{
117 		this.config = config;
118 		
119 		if( config != null && config.isSetName())
120 		{
121 			config.setName( config.getName().trim() );
122 		}
123 
124 		if( settings != null )
125 			settings.release();
126 
127 		if( !config.isSetSettings() )
128 			config.addNewSettings();
129 
130 		settings = new XmlBeansSettingsImpl( this, parent == null ? SoapUI.getSettings() : parent.getSettings(),
131 				this.config.getSettings() );
132 	}
133 
134 	public String getId()
135 	{
136 		if( !config.isSetId() )
137 			config.setId( ModelSupport.generateModelItemID() );
138 
139 		return config.getId();
140 	}
141 
142 	protected void setSettings( XmlBeansSettingsImpl settings )
143 	{
144 		if( this.settings != null )
145 			this.settings.release();
146 
147 		this.settings = settings;
148 	}
149 
150 	public AbstractWsdlModelItem<?> getWsdlModelItemByName( Collection<? extends AbstractWsdlModelItem<?>> items,
151 			String name )
152 	{
153 		for( AbstractWsdlModelItem<?> item : items )
154 		{
155 			if( item.getName() != null && item.getName().equals( name ) )
156 				return item;
157 		}
158 
159 		return null;
160 	}
161 
162 	public void release()
163 	{
164 		if( settings != null )
165 		{
166 			settings.release();
167 		}
168 	}
169 
170 	public void resolve( ResolveContext<?> context )
171 	{
172 		List<? extends ModelItem> children = getChildren();
173 		if( children == null )
174 			return;
175 
176 		for( ModelItem modelItem : children )
177 		{
178 			if( modelItem instanceof AbstractWsdlModelItem<?> )
179 			{
180 				( ( AbstractWsdlModelItem<?> )modelItem ).resolve( context );
181 			}
182 		}
183 	}
184 
185 	public void beforeSave()
186 	{
187 		List<? extends ModelItem> children = getChildren();
188 		if( children == null )
189 			return;
190 
191 		for( ModelItem modelItem : children )
192 		{
193 			if( modelItem instanceof AbstractWsdlModelItem<?> )
194 			{
195 				( ( AbstractWsdlModelItem<?> )modelItem ).beforeSave();
196 			}
197 		}
198 	}
199 
200 	public void afterLoad()
201 	{
202 		List<? extends ModelItem> children = getChildren();
203 		if( children == null )
204 			return;
205 
206 		for( ModelItem modelItem : children )
207 		{
208 			if( modelItem instanceof AbstractWsdlModelItem<?> )
209 			{
210 				( ( AbstractWsdlModelItem<?> )modelItem ).afterLoad();
211 			}
212 		}
213 	}
214 }