View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  
19  import javax.swing.Action;
20  import javax.swing.Icon;
21  import javax.swing.JButton;
22  import javax.swing.JComponent;
23  import javax.swing.JPanel;
24  import javax.swing.tree.TreePath;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.model.ModelItem;
28  import com.eviware.soapui.model.tree.SoapUITreeNode;
29  import com.eviware.soapui.model.tree.nodes.support.EmptyModelItem;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.ui.desktop.DesktopPanel;
32  
33  /***
34   * Base class for DesktopPanels..
35   */
36  
37  public abstract class ModelItemDesktopPanel<T extends ModelItem> extends JPanel implements DesktopPanel,
38  		PropertyChangeListener
39  {
40  	private final T modelItem;
41  
42  	public ModelItemDesktopPanel( T modelItem )
43  	{
44  		super( new BorderLayout() );
45  		this.modelItem = modelItem;
46  
47  		modelItem.addPropertyChangeListener( this );
48  	}
49  
50  	protected boolean release()
51  	{
52  		modelItem.removePropertyChangeListener( this );
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 final String getDescription()
79  	{
80  		TreePath treePath = SoapUI.getNavigator().getTreePath( modelItem );
81  
82  		if( treePath == null )
83  		{
84  			return modelItem.getDescription();
85  		}
86  		else
87  		{
88  			String str = modelItem.getName() + " [";
89  
90  			for( int c = 1; c < treePath.getPathCount(); c++ )
91  			{
92  				SoapUITreeNode comp = ( SoapUITreeNode )treePath.getPathComponent( c );
93  				if( comp.getModelItem() instanceof EmptyModelItem )
94  					continue;
95  
96  				if( c > 1 )
97  					str += "/";
98  
99  				str += comp.toString();
100 			}
101 
102 			str += "]";
103 
104 			return str;
105 		}
106 	}
107 
108 	public static JButton createActionButton( Action action, boolean enabled )
109 	{
110 		JButton button = UISupport.createToolbarButton( action, enabled );
111 		action.putValue( Action.NAME, null );
112 		return button;
113 	}
114 
115 	public void notifyPropertyChange( String propertyName, Object oldValue, Object newValue )
116 	{
117 		firePropertyChange( propertyName, oldValue, newValue );
118 	}
119 
120 	public void propertyChange( PropertyChangeEvent evt )
121 	{
122 		if( evt.getPropertyName().equals( ModelItem.NAME_PROPERTY ) )
123 			notifyPropertyChange( DesktopPanel.TITLE_PROPERTY, null, getTitle() );
124 
125 		if( evt.getPropertyName().equals( ModelItem.ICON_PROPERTY ) )
126 			notifyPropertyChange( DesktopPanel.ICON_PROPERTY, null, getIcon() );
127 	}
128 }