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.support.ModelSupport;
29  import com.eviware.soapui.model.tree.SoapUITreeNode;
30  import com.eviware.soapui.model.tree.nodes.support.EmptyModelItem;
31  import com.eviware.soapui.support.UISupport;
32  import com.eviware.soapui.ui.desktop.DesktopPanel;
33  
34  /***
35   * Base class for DesktopPanels..
36   */
37  
38  public abstract class ModelItemDesktopPanel<T extends ModelItem> extends JPanel implements DesktopPanel,
39  		PropertyChangeListener
40  {
41  	private final T modelItem;
42  
43  	public ModelItemDesktopPanel( T modelItem )
44  	{
45  		super( new BorderLayout() );
46  		this.modelItem = modelItem;
47  
48  		modelItem.addPropertyChangeListener( this );
49  	}
50  
51  	protected boolean release()
52  	{
53  		modelItem.removePropertyChangeListener( this );
54  		return true;
55  	}
56  
57  	public JComponent getComponent()
58  	{
59  		return this;
60  	}
61  
62  	final public T getModelItem()
63  	{
64  		return modelItem;
65  	}
66  
67  	public Icon getIcon()
68  	{
69  		return modelItem.getIcon();
70  	}
71  
72  	public boolean dependsOn( ModelItem modelItem )
73  	{
74  		return ModelSupport.dependsOn( getModelItem(), modelItem );
75  	}
76  
77  	public String getTitle()
78  	{
79  		return modelItem.getName();
80  	}
81  
82  	public final String getDescription()
83  	{
84  		TreePath treePath = SoapUI.getNavigator().getTreePath( modelItem );
85  
86  		if( treePath == null )
87  		{
88  			return modelItem.getDescription();
89  		}
90  		else
91  		{
92  			String str = modelItem.getName() + " [";
93  
94  			for( int c = 1; c < treePath.getPathCount(); c++ )
95  			{
96  				SoapUITreeNode comp = ( SoapUITreeNode )treePath.getPathComponent( c );
97  				if( comp.getModelItem() instanceof EmptyModelItem )
98  					continue;
99  
100 				if( c > 1 )
101 					str += "/";
102 
103 				str += comp.toString();
104 			}
105 
106 			str += "]";
107 
108 			return str;
109 		}
110 	}
111 
112 	public static JButton createActionButton( Action action, boolean enabled )
113 	{
114 		JButton button = UISupport.createToolbarButton( action, enabled );
115 		action.putValue( Action.NAME, null );
116 		return button;
117 	}
118 
119 	public void notifyPropertyChange( String propertyName, Object oldValue, Object newValue )
120 	{
121 		firePropertyChange( propertyName, oldValue, newValue );
122 	}
123 
124 	public void propertyChange( PropertyChangeEvent evt )
125 	{
126 		if( evt.getPropertyName().equals( ModelItem.NAME_PROPERTY ) )
127 			notifyPropertyChange( DesktopPanel.TITLE_PROPERTY, null, getTitle() );
128 
129 		if( evt.getPropertyName().equals( ModelItem.ICON_PROPERTY ) )
130 			notifyPropertyChange( DesktopPanel.ICON_PROPERTY, null, getIcon() );
131 	}
132 
133 	@Override
134 	public boolean onClose( boolean canCancel )
135 	{
136 		return release();
137 	}
138 }