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  
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  {
39  	private final T modelItem;
40  	private InternalPropertyChangeListener propertyChangeListener;
41  
42  	public ModelItemDesktopPanel( T modelItem )
43  	{
44  		super( new BorderLayout() );
45  		this.modelItem = modelItem;
46  		
47  		propertyChangeListener = new InternalPropertyChangeListener();
48  		((ModelItem)modelItem).addPropertyChangeListener( propertyChangeListener );
49  	}
50  
51  	protected boolean release()
52  	{
53  		modelItem.removePropertyChangeListener( propertyChangeListener );
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 final Icon getIcon()
68  	{
69  		return modelItem.getIcon();
70  	}
71  
72  	abstract public boolean dependsOn(ModelItem modelItem);
73  	
74  	public final String getTitle()
75  	{
76  		return modelItem.getName();
77  	}
78  	
79  	public final String getDescription()
80  	{
81  		TreePath treePath = SoapUI.getNavigator().getTreePath( modelItem );
82  		
83  		if( treePath == null )
84  		{
85  			return modelItem.getDescription();
86  		}
87  		else
88  		{
89  			String str = modelItem.getName() + " [";
90  			
91  			for( int c = 1; c < treePath.getPathCount(); c++ )
92  			{
93  				SoapUITreeNode comp = ( SoapUITreeNode ) treePath.getPathComponent( c );
94  				if( comp.getModelItem() instanceof EmptyModelItem )
95  					continue;
96  				
97  				if( c > 1 )
98  					str += "/";
99  				
100 				str += comp.toString();
101 			}
102 			
103 			str += "]";
104 			
105 			return str;
106 		}
107 	}
108 
109 	public static JButton createActionButton(Action action, boolean enabled)
110 	{
111 		JButton button = UISupport.createToolbarButton(action, enabled);
112 		action.putValue(Action.NAME, null);
113 		return button;
114 	}
115 
116 	public void notifyPropertyChange( String propertyName, Object oldValue, Object newValue )
117 	{
118 		firePropertyChange( propertyName, oldValue, newValue );
119 	}
120 	
121 	private class InternalPropertyChangeListener implements PropertyChangeListener
122 	{
123 		public void propertyChange(PropertyChangeEvent evt)
124 		{
125 			if( evt.getPropertyName().equals( ModelItem.NAME_PROPERTY ))
126 				notifyPropertyChange( DesktopPanel.TITLE_PROPERTY, null, getTitle() );
127 			
128 			if( evt.getPropertyName().equals( ModelItem.ICON_PROPERTY ))
129 				notifyPropertyChange( DesktopPanel.ICON_PROPERTY, null, getIcon() );
130 		}
131 	}
132 }