View Javadoc

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