View Javadoc

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