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