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.support.editor.views;
14  
15  import com.eviware.soapui.support.editor.EditorLocation;
16  import com.eviware.soapui.support.editor.EditorLocationListener;
17  import com.eviware.soapui.support.editor.xml.XmlDocument;
18  import com.eviware.soapui.support.editor.xml.XmlEditor;
19  import com.eviware.soapui.support.editor.xml.XmlEditorView;
20  
21  import java.beans.PropertyChangeEvent;
22  import java.beans.PropertyChangeListener;
23  import java.beans.PropertyChangeSupport;
24  import java.util.HashSet;
25  import java.util.Set;
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( isActive() )
166          setXml( (String) evt.getNewValue() );
167       else
168          xmlChanged = true;
169    }
170 
171    public abstract void setXml( String xml );
172 
173    public void release()
174    {
175       if( this.xmlDocument != null )
176       {
177          this.xmlDocument.removePropertyChangeListener( XmlDocument.XML_PROPERTY, this );
178          this.xmlDocument = null;
179       }
180    }
181 
182    public void addLocationListener( EditorLocationListener<T> listener )
183    {
184       listeners.add( listener );
185    }
186 
187    public void removeLocationListener( EditorLocationListener<T> listener )
188    {
189       listeners.remove( listener );
190    }
191 
192    public void fireLocationChanged( EditorLocation<T> location )
193    {
194       for( EditorLocationListener<T> listener : listeners )
195          listener.locationChanged( location );
196    }
197 
198    public EditorLocation<T> getEditorLocation()
199    {
200       return null;
201    }
202 
203    public String getXml()
204    {
205       return xmlDocument == null ? null : xmlDocument.getXml();
206    }
207 
208    public void setLocation( EditorLocation<T> location )
209    {
210    }
211 
212    public void locationChanged( EditorLocation<T> location )
213    {
214    }
215 
216    public void syncUpdates()
217    {
218       if( !isActive() && xmlChanged )
219       {
220          setXml( xmlDocument == null ? null : xmlDocument.getXml() );
221          xmlChanged = false;
222       }
223    }
224 
225    public XmlEditor<T> getEditor()
226    {
227       return editor;
228    }
229 
230    public void requestFocus()
231    {
232    }
233 }