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