View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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  
17  import javax.swing.ImageIcon;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.config.ModelItemConfig;
21  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
22  import com.eviware.soapui.model.ModelItem;
23  import com.eviware.soapui.model.settings.Settings;
24  import com.eviware.soapui.model.support.AbstractModelItem;
25  import com.eviware.soapui.model.support.ModelSupport;
26  import com.eviware.soapui.support.UISupport;
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 ModelItem getParent()
52  	{
53  		return parent;
54  	}
55  
56  	public ImageIcon getIcon()
57  	{
58  		return icon;
59  	}
60     
61  	public void setIcon(ImageIcon icon)
62  	{
63  		if( icon == this.icon )
64  			return;
65  		
66  		ImageIcon oldIcon = this.icon;
67  		this.icon = icon;
68  		notifyPropertyChanged( ICON_PROPERTY, oldIcon, icon );
69  	}
70  
71  	public String getDescription()
72     {
73        String description = config.getDescription();
74  		return description ==  null || description.trim().length() == 0 ? null : description;
75     }
76  
77     public void setDescription(String description)
78     {
79        String old = getDescription();
80        config.setDescription( description );
81        notifyPropertyChanged( DESCRIPTION_PROPERTY, old, description );
82     }
83  	
84     public String getName()
85     {
86        return config.getName();
87     }
88  
89     public void setName(String name)
90     {
91        String old = getName();
92        config.setName( name );
93        notifyPropertyChanged( NAME_PROPERTY, old, name );
94     }
95  	
96  	public Settings getSettings()
97  	{
98  		return settings;
99  	}
100 	
101 	final public T getConfig()
102 	{
103 		return config;
104 	}
105 	
106 	public void setConfig( T config )
107 	{
108 		this.config = config;
109 
110 		if( settings != null )
111 			settings.release();
112 		
113 		if( !config.isSetSettings() )
114 			config.addNewSettings();
115 		
116 		settings = new XmlBeansSettingsImpl( this, parent == null ? SoapUI.getSettings() : parent.getSettings(), this.config.getSettings() );
117 	}
118 	
119 	public String getId()
120 	{
121 		if( !config.isSetId())
122 			config.setId( ModelSupport.generateModelItemID() );
123 		
124 		return config.getId();
125 	}
126 
127 	protected void setSettings( XmlBeansSettingsImpl settings )
128 	{
129 		if( this.settings != null )
130 			this.settings.release();
131 		
132 		this.settings = settings;
133 	}
134 
135 	public AbstractWsdlModelItem getWsdlModelItemByName( Collection<? extends AbstractWsdlModelItem> items, String name )
136 	{
137 		for( AbstractWsdlModelItem item : items )
138 		{
139 			if( item.getName().equals( name ))
140 				return item;
141 		}
142 		
143 		return null;
144 	}
145 
146 	public void release()
147 	{
148 		if( settings != null )
149 		{
150 			settings.release();
151 		}
152 	}
153 	
154 	public void beforeSave()
155 	{}
156 	
157 	protected final void afterLoad( AbstractWsdlModelItem root )
158 	{
159 		try
160 		{
161 			root.afterLoad();
162 		}
163 		catch( Exception e )
164 		{
165 			SoapUI.logError( e );
166 		}
167 		
168 		for( ModelItem modelItem : root.getChildren() )
169 		{
170 			if( modelItem instanceof AbstractWsdlModelItem )
171 			{
172 				afterLoad( ( AbstractWsdlModelItem ) modelItem );
173 			}
174 		}
175 	}
176 	
177 	public void afterLoad() throws Exception
178 	{}
179 }