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.components;
14  
15  import com.jgoodies.looks.HeaderStyle;
16  import com.jgoodies.looks.Options;
17  
18  import javax.swing.*;
19  import java.awt.*;
20  import java.awt.event.ActionEvent;
21  import java.beans.PropertyChangeEvent;
22  import java.beans.PropertyChangeListener;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  public class JInspectorPanelImpl extends JPanel implements PropertyChangeListener, JInspectorPanel
29  {	
30  	private float defaultDividerLocation = 0.7F;
31  	
32  	private final JSplitPane mainSplit;
33  	private JPanel inspectorPanel;
34  	private int lastDividerLocation = 0;
35  	private JXToolBar inspectToolbar;
36  	private List<Inspector> inspectors = new ArrayList<Inspector>();
37  	private Map<Inspector,JToggleButton> inspectorButtons = new HashMap<Inspector,JToggleButton>();
38  	public Inspector currentInspector;
39  
40  	private final int orientation;
41  
42  	public JInspectorPanelImpl( JComponent contentComponent )
43  	{
44  		this( contentComponent, SwingConstants.BOTTOM );
45  	}
46  	
47  	public JInspectorPanelImpl(JComponent contentComponent, int orientation )
48  	{
49  		super( new BorderLayout() );
50  		this.orientation = orientation;
51  
52  		inspectorPanel = new JPanel(new CardLayout());
53  		inspectorPanel.setVisible( false );
54  		
55  		mainSplit = new JSplitPane( orientation == SwingConstants.LEFT || orientation == SwingConstants.RIGHT ?
56  					JSplitPane.HORIZONTAL_SPLIT : JSplitPane.VERTICAL_SPLIT);
57  		mainSplit.setDividerSize( 5 );
58  		mainSplit.setBorder( null );
59  		mainSplit.setOneTouchExpandable( false );
60  		
61  		JXToolBar toolbar = createInspectButtons();
62  		if( orientation == SwingConstants.BOTTOM )
63  		{
64  			mainSplit.setTopComponent( contentComponent );
65  			mainSplit.setBottomComponent( inspectorPanel );
66  			mainSplit.setResizeWeight( 0.8 );
67  			toolbar.setBorder( BorderFactory.createEmptyBorder( 1, 2, 3, 2 ));
68  			add( toolbar, BorderLayout.SOUTH  );
69  		}
70  		else if( orientation == SwingConstants.LEFT )
71  		{
72  			mainSplit.setRightComponent( contentComponent );
73  			
74  			JPanel p = new JPanel( new BorderLayout() );
75  			p.add( toolbar );
76  			toolbar.setBorder( BorderFactory.createEmptyBorder( 2, 3, 0, 4 ));
77  			mainSplit.setLeftComponent( inspectorPanel );
78  			mainSplit.setResizeWeight( 0.2 );
79  			
80  			toolbar.setOrientation( JToolBar.VERTICAL );
81  			
82  			
83  			add(p, BorderLayout.WEST  );
84  		}
85  		else if( orientation == SwingConstants.RIGHT )
86  		{
87  			mainSplit.setLeftComponent( contentComponent );
88  			
89  			JPanel p = new JPanel( new BorderLayout() );
90  			p.add( toolbar );
91  			toolbar.setBorder( BorderFactory.createEmptyBorder( 2, 1, 0, 3 ));
92  			mainSplit.setRightComponent( inspectorPanel );
93  			mainSplit.setResizeWeight( 0.8 );
94  			toolbar.setOrientation( JToolBar.VERTICAL );
95  			add( p, BorderLayout.EAST  );
96  		}
97  
98  		add(mainSplit, BorderLayout.CENTER);
99  	}
100 	
101 	private JXToolBar createInspectButtons()
102 	{
103 		inspectToolbar = new JXToolBar()
104 		{
105 			@Override
106 			public Dimension getMinimumSize()
107 			{
108 				return new Dimension( 10, 10 );
109 			}
110 		};
111 		
112 		inspectToolbar.setRollover( true );
113 		inspectToolbar.putClientProperty( Options.HEADER_STYLE_KEY, HeaderStyle.SINGLE );
114 		inspectToolbar.setBorder( BorderFactory.createEmptyBorder( 3, 0, 3, 0 ) );
115 
116 		if( orientation == SwingConstants.TOP || orientation == SwingConstants.BOTTOM )
117 			inspectToolbar.addSpace( 10 );
118 		inspectToolbar.setBackground( Color.WHITE );
119 		inspectToolbar.setOpaque( true );
120 		return inspectToolbar;
121 	}
122 	
123 	public float getDefaultDividerLocation()
124 	{
125 		return defaultDividerLocation;
126 	}
127 
128 	public void setDefaultDividerLocation( float defaultDividerLocation )
129 	{
130 		this.defaultDividerLocation = defaultDividerLocation;
131 		setResetDividerLocation();
132 	}
133 
134 	public <T extends Inspector> T addInspector( final T inspector )
135 	{
136 		if( inspectors.size() > 0 )
137 		{
138 			inspectToolbar.addSpace(5);
139 		}
140 		
141 		inspectors.add( inspector );
142 		inspector.addPropertyChangeListener( JInspectorPanelImpl.this );
143 		
144 		inspectorPanel.add( inspector.getComponent(), inspector.getInspectorId() );
145 		JToggleButton button = new JToggleButton( new SelectInspectorAction( inspector ));
146 		
147 		inspectorButtons.put( inspector, button );
148 		if( orientation == SwingConstants.LEFT  )
149 		{
150 			String text = button.getText();
151 			button.setText( null );
152 			button.setPreferredSize( new Dimension( 17, 10 ) );
153 			button.setIcon( new VTextIcon( inspectToolbar, text, VTextIcon.ROTATE_LEFT) );
154 			inspectToolbar.add( button );
155 		}
156 		else if( orientation == SwingConstants.RIGHT  )
157 		{
158 			String text = button.getText();
159 			button.setText( null );
160 			button.setPreferredSize( new Dimension( 17, 10 ) );
161 			button.setIcon( new VTextIcon( inspectToolbar, text, VTextIcon.ROTATE_RIGHT) );
162 			inspectToolbar.add( button );
163 		}
164 		else
165 			inspectToolbar.add( button );
166 		
167 		button.setMinimumSize( new Dimension( 10, 10 ) );
168 		
169 		inspectToolbar.invalidate();
170 		repaint();
171 
172 		return inspector;
173 	}
174 	
175 	public Inspector getInspector( String inspectorId  )
176 	{
177 		for( Inspector inspector : inspectors )
178 		{
179 			if( inspector.getInspectorId().equals( inspectorId ))
180 				return inspector;
181 		}
182 		
183 		return null;
184 	}
185 	
186 	public Inspector getInspectorByTitle( String title )
187 	{
188 		for( Inspector inspector : inspectors )
189 		{
190 			if( inspector.getTitle().equals( title ))
191 				return inspector;
192 		}
193 		
194 		return null;
195 	}
196 	
197 	public void propertyChange(PropertyChangeEvent evt)
198 	{
199 		if( evt.getPropertyName().equals( Inspector.ENABLED_PROPERTY ))
200 		{
201 			JToggleButton toggleButton = inspectorButtons.get( evt.getSource() );
202 			if( toggleButton != null )
203 				toggleButton.setEnabled( ( Boolean ) evt.getNewValue() );
204 		}
205 	}
206 
207    public JComponent getComponent() {
208       return this;
209    }
210 
211    public class SelectInspectorAction extends AbstractAction implements PropertyChangeListener
212 	{
213 		private final Inspector inspector;
214 
215 		public SelectInspectorAction( Inspector inspector )
216 		{
217 			super( inspector.getTitle());
218 			this.inspector = inspector;
219 			
220 			putValue( AbstractAction.SHORT_DESCRIPTION, inspector.getDescription() );
221 			putValue( AbstractAction.SMALL_ICON, inspector.getIcon());
222 			setEnabled( inspector.isEnabled() );
223 			
224 			inspector.addPropertyChangeListener( this );
225 		}
226 
227 		public void actionPerformed( ActionEvent arg0 )
228 		{
229 			JToggleButton button = inspectorButtons.get( inspector );
230 			if( !button.isSelected() )
231 			{
232 				deactivate();
233 //				currentInspector = null;
234 //				button.setBackground( inspectToolbar.getBackground() );
235 //				lastDividerLocation = mainSplit.getDividerLocation();
236 //				inspectorPanel.setVisible( false );
237 			}
238 			else
239 			{
240 				activate( inspector );
241 			}
242 		}
243 
244 		public void propertyChange( PropertyChangeEvent evt )
245 		{
246 			if( evt.getPropertyName().equals( Inspector.TITLE_PROPERTY ))
247 				putValue( AbstractAction.NAME, evt.getNewValue() );
248 			else if( evt.getPropertyName().equals( Inspector.ICON_PROPERTY ))
249 				putValue( AbstractAction.SMALL_ICON, evt.getNewValue() );
250 			else if( evt.getPropertyName().equals( Inspector.DESCRIPTION_PROPERTY ))
251 				putValue( AbstractAction.SHORT_DESCRIPTION, evt.getNewValue() );
252 			else if( evt.getPropertyName().equals( Inspector.ENABLED_PROPERTY ))
253 			{
254 				boolean enable = ((Boolean)evt.getNewValue()).booleanValue();
255 				setEnabled( enable );
256 				
257 				if( !enable && currentInspector == inspector )
258 				{
259 					inspectorButtons.get( currentInspector ).setSelected( false );
260 				}
261 			}
262 		}
263 	}
264 	
265 	public void release()
266 	{
267 		for( Inspector inspector : inspectors )
268 		{
269 			inspector.removePropertyChangeListener( this );
270 			inspector.release();
271 		}
272 		
273 		inspectors.clear();
274 		inspectorPanel.removeAll();
275 		mainSplit.removeAll();
276 	}
277 
278 	public List<Inspector> getInspectors()
279 	{
280 		return inspectors;
281 	}
282 
283 	public Inspector getCurrentInspector()
284 	{
285 		return currentInspector;
286 	}
287 
288 	public void setInspectorsVisible( boolean b )
289 	{
290 		inspectorPanel.setVisible( b );
291 	}
292 	
293 	public void setInspectorVisible( Inspector inspector, boolean b )
294 	{
295 		if( inspectorButtons.containsKey( inspector ))
296 		{
297 			if( !b && inspector == currentInspector )
298 			{
299 				activate( null );
300 			}
301 
302 			inspectorButtons.get( inspector ).setVisible( b );
303 		}
304 	}
305 
306 	public void setToolbarVisible( boolean b )
307 	{
308 		inspectToolbar.setVisible( b );
309 	}
310 
311 	public double getResizeWeight()
312 	{
313 		return mainSplit.getResizeWeight();
314 	}
315 
316 	public void setResizeWeight( double value )
317 	{
318 		mainSplit.setResizeWeight( value );
319 	}
320 
321 	public int getDividerLocation()
322 	{
323 		return mainSplit.getDividerLocation();
324 	}
325 
326 	public void setResetDividerLocation()
327 	{
328 		mainSplit.setDividerLocation( defaultDividerLocation );
329 	}
330 
331 	public void setDividerLocation( int dividerLocation )
332 	{
333 		mainSplit.setDividerLocation( dividerLocation );
334 	}
335 
336 	public void setCurrentInspector( String string )
337 	{
338 		for( Inspector inspector : inspectors )
339 		{
340 			if( inspector.getTitle().equals( string ))
341 			{
342 				activate( inspector );
343 				break;
344 			}
345 		}
346 	}
347 	
348 	public void deactivate()
349 	{
350 		activate( null );
351 	}
352 	
353 	public void activate( Inspector inspector )
354 	{
355 		if( inspector == currentInspector )
356 			return;
357 		
358 		if( currentInspector != null )
359 		{
360 			inspectorButtons.get( currentInspector ).setSelected( false );
361 			currentInspector.deactivate();
362 		}
363 		
364 		if( inspector == null )
365 		{
366 			currentInspector = null;
367 			inspectorPanel.setVisible( false );
368 		}
369 		else
370 		{
371 			JToggleButton button = inspectorButtons.get( inspector );
372 			currentInspector = inspector;
373 			
374 			button.setSelected( true );
375 			button.setBackground( Color.WHITE );
376 			
377 			if( !inspectorPanel.isVisible() )
378 			{
379 				inspectorPanel.setVisible( true );
380 				if( lastDividerLocation == 0 )
381 					mainSplit.setDividerLocation( defaultDividerLocation );
382 				else
383 					mainSplit.setDividerLocation( lastDividerLocation );
384 			}
385 			
386 			CardLayout cards = ( CardLayout ) inspectorPanel.getLayout();
387 			cards.show( inspectorPanel, inspector.getInspectorId() );
388 			
389 			currentInspector.activate();
390 		}
391 	}
392 
393 	public void setContentComponent( JComponent content )
394 	{
395 		mainSplit.setTopComponent( content );
396 	}
397 	
398 	public void removeInspector( Inspector inspector )
399 	{
400 		if( currentInspector == inspector )
401 		{
402 			deactivate();
403 		}
404 		
405 		inspector.release();
406 		inspectors.remove( inspector );
407 		JToggleButton toggleButton = inspectorButtons.get( inspector );
408 		
409 		int ix = inspectToolbar.getComponentIndex( toggleButton );
410 		if( ix > 1 )
411 			inspectToolbar.remove( ix-1 );
412 		
413 		inspectToolbar.remove( toggleButton);
414 		inspectorPanel.remove( inspector.getComponent() );
415 		inspectToolbar.repaint();
416 		inspectorButtons.remove( inspector );
417 	}
418 }