View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.ui;
    14
    15  import java.awt.BorderLayout;
    16  import java.awt.Component;
    17  import java.awt.event.MouseAdapter;
    18  import java.awt.event.MouseEvent;
    19  import java.beans.PropertyChangeEvent;
    20  import java.beans.PropertyChangeListener;
    21  import java.util.ArrayList;
    22  import java.util.List;
    23
    24  import javax.swing.BorderFactory;
    25  import javax.swing.DefaultListCellRenderer;
    26  import javax.swing.DefaultListModel;
    27  import javax.swing.JList;
    28  import javax.swing.JPanel;
    29  import javax.swing.JScrollPane;
    30
    31  import com.eviware.soapui.ui.desktop.DesktopListener;
    32  import com.eviware.soapui.ui.desktop.DesktopPanel;
    33  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
    34
    35  /***
    36   * List for displaying current DesktopPanels
    37   *
    38   * @author Ole.Matzura
    39   */
    40
    41  public class JDesktopPanelsList extends JPanel
    42  {
    43  	private DefaultListModel desktopPanels;
    44  	private JList desktopPanelsList;
    45  	private SoapUIDesktop desktop;
    46  	private DesktopPanelPropertyChangeListener desktopPanelPropertyListener = new DesktopPanelPropertyChangeListener();
    47  	private InternalDesktopListener desktopListener = new InternalDesktopListener();
    48
    49  	public JDesktopPanelsList(SoapUIDesktop desktop)
    50  	{
    51  		super(new BorderLayout());
    52  		setDesktop( desktop );
    53
    54  		desktopPanels = new DefaultListModel();
    55  		desktopPanelsList = new JList(desktopPanels);
    56  		desktopPanelsList.setCellRenderer(new DesktopItemsCellRenderer());
    57  		desktopPanelsList.setToolTipText("Open windows");
    58  		desktopPanelsList.addMouseListener(new MouseAdapter()
    59  		{
    60  			public void mouseClicked(MouseEvent e)
    61  			{
    62  				if (e.getClickCount() < 2)
    63  					return;
    64
    65  				JDesktopPanelsList.this.desktop.showDesktopPanel((DesktopPanel) desktopPanelsList.getSelectedValue());
    66  			}
    67  		});
    68
    69  		add( new JScrollPane( desktopPanelsList ), BorderLayout.CENTER);
    70  	}
    71
    72  	private class DesktopPanelPropertyChangeListener implements PropertyChangeListener
    73  	{
    74  		public void propertyChange(PropertyChangeEvent evt)
    75  		{
    76  			DesktopPanel desktopPanel = (DesktopPanel) evt.getSource();
    77  			int ix = desktopPanels.indexOf(desktopPanel);
    78  			if (ix >= 0)
    79  				desktopPanels.set(ix, desktopPanel);
    80  		}
    81  	}
    82
    83  	private static class DesktopItemsCellRenderer extends DefaultListCellRenderer
    84  	{
    85  		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
    86  				boolean cellHasFocus)
    87  		{
    88  			super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
    89
    90  			DesktopPanel desktopPanel = (DesktopPanel) value;
    91  			String title = desktopPanel.getTitle();
    92  			setText(title);
    93  			setToolTipText(desktopPanel.getDescription());
    94  			setIcon( desktopPanel.getIcon() );
    95
    96  			setBorder( BorderFactory.createCompoundBorder(
    97  						BorderFactory.createEmptyBorder( 2, 2, 2, 2 ),
    98  						getBorder() ));
    99
    100 			return this;
    101 		}
    102 	}
    103
    104 	public List<DesktopPanel> getDesktopPanels()
    105 	{
    106 		List<DesktopPanel> result = new ArrayList<DesktopPanel>();
    107
    108 		for( int c = 0; c < desktopPanels.getSize(); c++ )
    109 			result.add( ( DesktopPanel ) desktopPanels.get( c ));
    110
    111 		return result;
    112 	}
    113
    114 	private class InternalDesktopListener implements DesktopListener
    115 	{
    116 		public void desktopPanelSelected(DesktopPanel desktopPanel)
    117 		{
    118 			desktopPanelsList.setSelectedValue( desktopPanel, false );
    119 		}
    120
    121 		public void desktopPanelCreated(DesktopPanel desktopPanel)
    122 		{
    123 			desktopPanels.addElement(desktopPanel);
    124 			desktopPanelsList.setSelectedValue(desktopPanel, false);
    125
    126 			desktopPanel.addPropertyChangeListener( desktopPanelPropertyListener );
    127 		}
    128
    129 		public void desktopPanelClosed(DesktopPanel desktopPanel)
    130 		{
    131 			desktopPanels.removeElement( desktopPanel );
    132 			desktopPanel.removePropertyChangeListener( desktopPanelPropertyListener );
    133 		}
    134 	}
    135
    136 	public void setDesktop( SoapUIDesktop newDesktop )
    137 	{
    138 		if( desktop != null )
    139 		{
    140 			desktop.removeDesktopListener( desktopListener );
    141
    142 			while( desktopPanels.size() > 0 )
    143 			{
    144 				DesktopPanel desktopPanel = ( DesktopPanel ) desktopPanels.getElementAt( 0 );
    145 				desktopPanel.removePropertyChangeListener( desktopPanelPropertyListener );
    146 				desktopPanels.remove( 0 );
    147 			}
    148
    149 		}
    150
    151 		desktop = newDesktop;
    152
    153 		desktop.addDesktopListener( desktopListener );
    154
    155 		for( DesktopPanel desktopPanel : desktop.getDesktopPanels() )
    156 		{
    157 			desktopPanel.addPropertyChangeListener( desktopPanelPropertyListener );
    158 			desktopPanels.addElement( desktopPanel );
    159 		}
    160 	}
    161
    162 	public JList getDesktopPanelsList()
    163 	{
    164 		return desktopPanelsList;
    165 	}
    166
    167 	public int getItemsCount()
    168 	{
    169 		return desktopPanels.size();
    170 	}
    171 }