View Javadoc

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