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