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.support;
    14
    15  import java.awt.BorderLayout;
    16  import java.beans.PropertyChangeEvent;
    17  import java.beans.PropertyChangeListener;
    18  import java.beans.PropertyChangeSupport;
    19
    20  import javax.swing.Action;
    21  import javax.swing.Icon;
    22  import javax.swing.JButton;
    23  import javax.swing.JComponent;
    24  import javax.swing.JPanel;
    25
    26  import com.eviware.soapui.model.ModelItem;
    27  import com.eviware.soapui.support.UISupport;
    28  import com.eviware.soapui.ui.desktop.DesktopPanel;
    29
    30  /***
    31   * Base class for DesktopPanels..
    32   */
    33
    34  public abstract class ModelItemDesktopPanel<T extends ModelItem> extends JPanel implements DesktopPanel
    35  {
    36  	private PropertyChangeSupport propertyChangeSupport;
    37  	private final T modelItem;
    38  	private InternalPropertyChangeListener propertyChangeListener;
    39
    40  	public ModelItemDesktopPanel( T modelItem )
    41  	{
    42  		super( new BorderLayout() );
    43  		this.modelItem = modelItem;
    44  		propertyChangeSupport = new PropertyChangeSupport( this );
    45
    46  		propertyChangeListener = new InternalPropertyChangeListener();
    47  		((ModelItem)modelItem).addPropertyChangeListener( propertyChangeListener );
    48  	}
    49
    50  	protected boolean release()
    51  	{
    52  		modelItem.removePropertyChangeListener( propertyChangeListener );
    53  		return true;
    54  	}
    55
    56  	public JComponent getComponent()
    57  	{
    58  		return this;
    59  	}
    60
    61  	final public T getModelItem()
    62  	{
    63  		return modelItem;
    64  	}
    65
    66  	public Icon getIcon()
    67  	{
    68  		return modelItem.getIcon();
    69  	}
    70
    71  	abstract public boolean dependsOn(ModelItem modelItem);
    72
    73  	public String getTitle()
    74  	{
    75  		return modelItem.getName();
    76  	}
    77
    78  	public String getDescription()
    79  	{
    80  		return modelItem.getDescription();
    81  	}
    82
    83  	protected JButton createActionButton(Action action, boolean enabled)
    84  	{
    85  		JButton button = UISupport.createToolbarButton(action, enabled);
    86  		action.putValue(Action.NAME, null);
    87  		return button;
    88  	}
    89
    90  	public void addPropertyChangeListener(String propertyName,
    91  			PropertyChangeListener listener)
    92  	{
    93  		propertyChangeSupport.addPropertyChangeListener( propertyName, listener );
    94  	}
    95
    96  	public void addPropertyChangeListener(PropertyChangeListener listener)
    97  	{
    98  		propertyChangeSupport.addPropertyChangeListener( listener );
    99  	}
    100
    101 	public void removePropertyChangeListener(PropertyChangeListener listener)
    102 	{
    103 		propertyChangeSupport.removePropertyChangeListener( listener );
    104 	}
    105
    106 	public void removePropertyChangeListener(String propertyName,
    107 			PropertyChangeListener listener)
    108 	{
    109 		propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
    110 	}
    111
    112 	public void notifyPropertyChange( String propertyName, Object oldValue, Object newValue )
    113 	{
    114 		propertyChangeSupport.firePropertyChange( propertyName, oldValue, newValue );
    115 	}
    116
    117 	private class InternalPropertyChangeListener implements PropertyChangeListener
    118 	{
    119 		public void propertyChange(PropertyChangeEvent evt)
    120 		{
    121 			if( evt.getPropertyName().equals( ModelItem.NAME_PROPERTY ))
    122 				notifyPropertyChange( DesktopPanel.TITLE_PROPERTY, null, getTitle() );
    123
    124 			if( evt.getPropertyName().equals( ModelItem.ICON_PROPERTY ))
    125 				notifyPropertyChange( DesktopPanel.ICON_PROPERTY, null, getIcon() );
    126 		}
    127 	}
    128 }