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