1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.request.components.editor;
14
15 import java.awt.BorderLayout;
16 import java.awt.Color;
17 import java.beans.PropertyChangeEvent;
18 import java.beans.PropertyChangeListener;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.swing.JPanel;
23 import javax.swing.JTabbedPane;
24 import javax.swing.SwingUtilities;
25 import javax.swing.event.ChangeEvent;
26 import javax.swing.event.ChangeListener;
27
28 import com.eviware.soapui.support.components.Inspector;
29 import com.eviware.soapui.support.components.JInspectorPanel;
30 import com.eviware.soapui.support.components.VTextIcon;
31 import com.eviware.soapui.support.components.VerticalTabbedPaneUI;
32
33 /***
34 * Editor-framework for Xml Documents
35 *
36 * @author ole.matzura
37 */
38
39 @SuppressWarnings("serial")
40 public class XmlEditor extends JPanel implements PropertyChangeListener, XmlLocationListener
41 {
42 private JTabbedPane inputTabs;
43 private List<XmlEditorView> views = new ArrayList<XmlEditorView>();
44 private XmlEditorView currentView;
45 private XmlDocument xmlDocument;
46 private JInspectorPanel inspectorPanel;
47 private InputTabsChangeListener inputTabsChangeListener;
48
49 public XmlEditor( XmlDocument xmlDocument )
50 {
51 super( new BorderLayout() );
52 this.xmlDocument = xmlDocument;
53
54 setBackground(Color.LIGHT_GRAY);
55 inputTabs = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
56 inputTabs.setUI(new VerticalTabbedPaneUI());
57
58 inputTabs.setFont(inputTabs.getFont().deriveFont(8));
59 inputTabsChangeListener = new InputTabsChangeListener();
60 inputTabs.addChangeListener( inputTabsChangeListener );
61
62 inspectorPanel = new JInspectorPanel( inputTabs );
63 add( inspectorPanel, BorderLayout.CENTER );
64 }
65
66 public void addEditorView( XmlEditorView editorView )
67 {
68 views.add( editorView );
69
70 inputTabs.addTab( null, new VTextIcon(inputTabs, editorView.getTitle(), VTextIcon.ROTATE_LEFT ),
71 editorView.getComponent() );
72
73 editorView.addPropertyChangeListener( this );
74 editorView.addLocationListener( this );
75
76 editorView.setXmlDocument( xmlDocument );
77 }
78
79 public void propertyChange(PropertyChangeEvent evt)
80 {
81 if( evt.getPropertyName().equals( XmlEditorView.TITLE_PROPERTY ))
82 {
83 int ix = views.indexOf( evt.getSource() );
84 if( ix == -1 )
85 return;
86
87 inputTabs.setTitleAt( ix, (String) evt.getNewValue() );
88 }
89 }
90
91 public void selectView( int viewIndex )
92 {
93 inputTabs.setSelectedIndex( viewIndex );
94 }
95
96 public void selectView( String viewId )
97 {
98 for( int c = 0; c < views.size(); c++ )
99 {
100 if( views.get( c ).getViewId().equals( viewId ))
101 {
102 inputTabs.setSelectedIndex( c );
103 return;
104 }
105 }
106 }
107
108
109 public void requestFocus()
110 {
111 if( currentView != null )
112 currentView.getComponent().requestFocus();
113 }
114
115 public final XmlDocument getXmlDocument()
116 {
117 return xmlDocument;
118 }
119
120 public boolean saveDocument( boolean validate )
121 {
122 return currentView == null ? true : currentView.saveDocument( validate );
123 }
124
125 public boolean hasFocus()
126 {
127 return currentView == null ? false : currentView.getComponent().hasFocus();
128 }
129
130 public final void setXmlDocument(XmlDocument xmlDocument)
131 {
132 if( this.xmlDocument != null )
133 this.xmlDocument.release();
134
135 this.xmlDocument = xmlDocument;
136
137 for( XmlEditorView view : views )
138 {
139 view.setXmlDocument( xmlDocument );
140 }
141 }
142
143 public final XmlEditorView getCurrentView()
144 {
145 return currentView;
146 }
147
148 public final JTabbedPane getInputTabs()
149 {
150 return inputTabs;
151 }
152
153 public final List<XmlEditorView> getViews()
154 {
155 return views;
156 }
157
158 public XmlEditorView getView( String viewId )
159 {
160 for( XmlEditorView view : views )
161 {
162 if( view.getViewId().equals( viewId ))
163 return view;
164 }
165
166 return null;
167 }
168
169 public Inspector getInspector( String inspectorId )
170 {
171 return inspectorPanel.getInspector( inspectorId );
172 }
173
174 public void locationChanged(XmlLocation location)
175 {
176 if( location != null )
177 {
178 for( Inspector inspector : inspectorPanel.getInspectors() )
179 {
180 ((XmlInspector)inspector).locationChanged( location );
181 }
182 }
183 }
184
185 public void setEditable( boolean enabled )
186 {
187 for( XmlEditorView view : views )
188 {
189 view.setEditable( enabled );
190 }
191 }
192
193 public void addInspector( XmlInspector inspector )
194 {
195 inspectorPanel.addInspector( inspector );
196 }
197
198 private final class InputTabsChangeListener implements ChangeListener
199 {
200 private int lastDividerLocation;
201
202 public void stateChanged( ChangeEvent e )
203 {
204 int currentViewIndex = views.indexOf( currentView );
205
206 if( currentView != null )
207 {
208 if( inputTabs.getSelectedIndex() == currentViewIndex)
209 return;
210
211 if( !currentView.deactivate() )
212 {
213 inputTabs.setSelectedIndex( currentViewIndex );
214 return;
215 }
216 }
217
218 XmlEditorView previousView = currentView;
219 int selectedIndex = inputTabs.getSelectedIndex();
220 if( selectedIndex == -1 )
221 {
222 currentView = null;
223 return;
224 }
225
226 currentView = views.get( selectedIndex );
227
228 if( currentView != null && !currentView.activate( previousView == null ? null : previousView.getLocation() ))
229 {
230 inputTabs.setSelectedIndex( currentViewIndex );
231 if( currentViewIndex == -1 )
232 return;
233 }
234
235 XmlInspector currentInspector = ( XmlInspector ) inspectorPanel.getCurrentInspector();
236
237 if( !currentView.isInspectable() && currentInspector != null )
238 lastDividerLocation = inspectorPanel.getDividerLocation();
239
240 inspectorPanel.setInspectorVisible( currentView.isInspectable() && currentInspector != null );
241 inspectorPanel.setToolbarVisible( currentView.isInspectable() );
242
243 if( currentView.isInspectable() && currentInspector != null )
244 {
245 if( lastDividerLocation == 0 )
246 inspectorPanel.setResetDividerLocation();
247 else
248 inspectorPanel.setDividerLocation( lastDividerLocation );
249 }
250
251 SwingUtilities.invokeLater( new Runnable() {
252
253 public void run()
254 {
255 if( currentView != null )
256 currentView.getComponent().requestFocus();
257 }} );
258 }
259 }
260
261 public void release()
262 {
263 for( XmlEditorView view : views )
264 {
265 view.release();
266 view.removeLocationListener( this );
267 view.removePropertyChangeListener( this );
268 }
269
270 views.clear();
271
272 inputTabs.removeChangeListener( inputTabsChangeListener );
273 inputTabs.removeAll();
274
275 inspectorPanel.release();
276 xmlDocument.release();
277 }
278 }