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.panels.request.components.editor.views;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  import java.beans.PropertyChangeSupport;
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlDocument;
22  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditor;
23  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditorView;
24  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlLocation;
25  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlLocationListener;
26  
27  /***
28   * Abstract base-class to be extended by XmlViews
29   * 
30   * @author ole.matzura
31   */
32  
33  public abstract class AbstractEditorView implements XmlEditorView, PropertyChangeListener
34  {
35  	private String title;
36  	private boolean isActive;
37  	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( this );
38  	private XmlDocument xmlDocument;
39  	private boolean xmlChanged;
40  	private Set<XmlLocationListener> listeners = new HashSet<XmlLocationListener>();
41  	private XmlEditor editor;
42  	
43  	public AbstractEditorView(String title, XmlEditor xmlEditor)
44  	{
45  		super();
46  		this.title = title;
47  		editor = xmlEditor;
48  		xmlChanged = false;
49  	}
50  
51  	protected PropertyChangeSupport getPropertyChangeSupport()
52  	{
53  		return propertyChangeSupport;
54  	}
55  	
56  	public boolean activate(XmlLocation location)
57  	{
58  		isActive = true;
59  		update();
60  		
61  		return true;
62  	}
63  
64  	public void update()
65  	{
66  		if( xmlChanged  )
67  		{
68  			setXml( xmlDocument == null ? null : xmlDocument.getXml() );
69  			xmlChanged = false;
70  		}
71  	}
72  
73  	public boolean isXmlChanged()
74  	{
75  		return xmlChanged;
76  	}
77  
78  	public boolean deactivate()
79  	{
80  		isActive = false;
81  		xmlChanged = false;
82  		
83  		return true;
84  	}
85  	
86  	public boolean isActive()
87  	{
88  		return isActive;
89  	}
90  
91  	public String getTitle()
92  	{
93  		return title;
94  	}
95  
96  	public void setTitle( String title )
97  	{
98  		String oldTitle = this.title;
99  		this.title = title;
100 		
101 		propertyChangeSupport.firePropertyChange( TITLE_PROPERTY, oldTitle, title );
102 	}
103 	
104 	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
105 	{
106 	   propertyChangeSupport.addPropertyChangeListener(propertyName, listener);	
107 	}
108 
109 	public void addPropertyChangeListener(PropertyChangeListener listener)
110 	{
111 		propertyChangeSupport.addPropertyChangeListener( listener );
112 	}
113 
114 	public void removePropertyChangeListener(PropertyChangeListener listener)
115 	{
116 		propertyChangeSupport.removePropertyChangeListener( listener );
117 	}
118 
119 	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
120 	{
121 		propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
122 	}
123 	
124 	public XmlDocument getXmlDocument()
125 	{
126 		return xmlDocument;
127 	}
128 
129 	public void setXmlDocument(XmlDocument xmlDocument)
130 	{
131 		if( this.xmlDocument != null )
132 		{
133 			this.xmlDocument.removePropertyChangeListener( XmlDocument.XML_PROPERTY, this );
134 		}
135 		
136 		this.xmlDocument = xmlDocument;
137 		
138 		if( xmlDocument != null )
139 		{
140 			this.xmlDocument.addPropertyChangeListener( XmlDocument.XML_PROPERTY, this );
141 			setXml( xmlDocument.getXml() );
142 		}
143 		else
144 		{
145 			setXml( null );
146 		}
147 		
148 		xmlChanged = false;
149 	}
150 
151 	public void propertyChange(PropertyChangeEvent evt)
152 	{
153 		if( isActive() )
154 			setXml( (String) evt.getNewValue() );
155 		else
156 			xmlChanged = true;
157 	}
158 	
159 	public abstract void setXml( String xml );
160 
161 	public void release()
162 	{
163 		if( this.xmlDocument != null )
164 		{
165 			this.xmlDocument.removePropertyChangeListener( XmlDocument.XML_PROPERTY, this );
166 			this.xmlDocument = null;
167 		}		
168 	}
169 
170 	public void addLocationListener(XmlLocationListener listener)
171 	{
172 		listeners.add( listener );
173 	}
174 
175 	public void removeLocationListener(XmlLocationListener listener)
176 	{
177 		listeners.remove( listener );
178 	}
179 	
180 	public void fireLocationChanged( XmlLocation location )
181 	{
182 		for( XmlLocationListener listener : listeners )
183 			listener.locationChanged( location );
184 	}
185 	
186 	public XmlLocation getLocation()
187 	{
188 		return null;
189 	}
190 	
191 	public String getXml()
192 	{
193 		return xmlDocument == null ? null : xmlDocument.getXml();
194 	}
195 
196 	public void setLocation(XmlLocation location)
197 	{
198 	}
199 	
200 	public void locationChanged(XmlLocation location)
201 	{
202 	}
203 
204 	public void syncUpdates()
205 	{
206 		if( !isActive() && xmlChanged )
207 		{
208 			setXml( xmlDocument == null ? null : xmlDocument.getXml() );
209 			xmlChanged = false;
210 		}
211 	}
212 	
213 	public XmlEditor getEditor()
214 	{
215 		return editor;
216 	}
217 
218 	public void requestFocus()
219 	{
220 	}
221 }