1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.teststeps;
14
15 import java.awt.BorderLayout;
16 import java.awt.Component;
17 import java.awt.Toolkit;
18 import java.awt.event.MouseAdapter;
19 import java.awt.event.MouseEvent;
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.swing.AbstractListModel;
26 import javax.swing.JLabel;
27 import javax.swing.JList;
28 import javax.swing.JPanel;
29 import javax.swing.JPopupMenu;
30 import javax.swing.JScrollPane;
31 import javax.swing.ListCellRenderer;
32 import javax.swing.event.PopupMenuEvent;
33 import javax.swing.event.PopupMenuListener;
34
35 import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable;
36 import com.eviware.soapui.impl.wsdl.panels.support.assertions.AssertionsListener;
37 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
38 import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
39 import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
40 import com.eviware.soapui.support.action.ActionSupport;
41
42 public class AssertionsPanel extends JPanel
43 {
44 private AssertionListModel assertionListModel;
45 private JList assertionList;
46 private JPopupMenu assertionListPopup;
47 private final Assertable assertable;
48
49 public AssertionsPanel( Assertable assertable )
50 {
51 super( new BorderLayout() );
52 this.assertable = assertable;
53
54 assertionListModel = new AssertionListModel();
55 assertionList = new JList( assertionListModel );
56 assertionList.setToolTipText( "Assertions for this request" );
57 assertionList.setCellRenderer( new AssertionCellRenderer() );
58
59 assertionListPopup = new JPopupMenu();
60 assertionListPopup.add( new AddAssertionAction( assertable ));
61
62 assertionListPopup.addPopupMenuListener( new PopupMenuListener(){
63
64 public void popupMenuWillBecomeVisible(PopupMenuEvent e)
65 {
66 while( assertionListPopup.getComponentCount() > 1 )
67 assertionListPopup.remove( 1 );
68
69 if( assertionListModel.getSize() == 0 ) return;
70 int ix = assertionList.getSelectedIndex();
71 if( ix == -1 ) return;
72
73 WsdlMessageAssertion assertion = assertionListModel.getAssertionAt( ix );
74 ActionSupport.addActions( assertion.getActions(), assertionListPopup );
75 }
76
77 public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
78 {
79 }
80
81 public void popupMenuCanceled(PopupMenuEvent e)
82 {
83 }});
84
85 assertionList.setComponentPopupMenu( assertionListPopup );
86
87 assertionList.addMouseListener( new MouseAdapter() {
88
89 public void mouseClicked(MouseEvent e)
90 {
91 if( e.getClickCount() < 2 ) return;
92
93 int ix = assertionList.getSelectedIndex();
94 if( ix == -1 ) return;
95
96 Object obj = assertionList.getModel().getElementAt( ix );
97 if( obj instanceof WsdlMessageAssertion )
98 {
99 WsdlMessageAssertion assertion = (WsdlMessageAssertion) obj;
100 if( assertion.isConfigurable() )
101 assertion.configure();
102
103 return;
104 }
105
106 if( obj instanceof AssertionError )
107 {
108 AssertionError error = (AssertionError) obj;
109 if( error.getLineNumber() >= 0 )
110 {
111 selectError(error);
112 }
113 else Toolkit.getDefaultToolkit().beep();
114 }
115 else Toolkit.getDefaultToolkit().beep();
116 }});
117
118 add( new JScrollPane( assertionList ), BorderLayout.CENTER );
119 }
120
121 public void setEnabled( boolean enabled )
122 {
123 assertionList.setEnabled( enabled );
124 }
125
126 protected void selectError(AssertionError error)
127 {
128 }
129
130 private static class AssertionCellRenderer extends JLabel implements ListCellRenderer
131 {
132 public Component getListCellRendererComponent(
133 JList list,
134 Object value,
135 int index,
136 boolean isSelected,
137 boolean cellHasFocus)
138 {
139 if( value instanceof WsdlMessageAssertion )
140 {
141 WsdlMessageAssertion assertion = (WsdlMessageAssertion) value;
142 setText( assertion.getName() + " - " + assertion.getStatus().toString() );
143 setIcon( assertion.getIcon() );
144 }
145 else if( value instanceof AssertionError )
146 {
147 AssertionError assertion = (AssertionError) value;
148 setText( " -> " + assertion.toString() );
149 setIcon( null );
150 }
151
152 if (isSelected)
153 {
154 setBackground(list.getSelectionBackground());
155 setForeground(list.getSelectionForeground());
156 }
157 else
158 {
159 setBackground(list.getBackground());
160 setForeground(list.getForeground());
161 }
162
163 setEnabled(list.isEnabled());
164 setFont(list.getFont());
165 setOpaque(true);
166
167 return this;
168 }
169 }
170
171 private class AssertionListModel extends AbstractListModel implements PropertyChangeListener,
172 AssertionsListener
173 {
174 private List<Object> items = new ArrayList<Object>();
175
176 public AssertionListModel()
177 {
178 init();
179 }
180
181 public int getSize()
182 {
183 return items.size();
184 }
185
186 public Object getElementAt(int index)
187 {
188 return index >= items.size() ? null : items.get( index );
189 }
190
191 public WsdlMessageAssertion getAssertionAt( int index )
192 {
193 Object object = items.get( index );
194 while( object instanceof AssertionError && index > 0 )
195 {
196 object = items.get( --index );
197 }
198
199 return (WsdlMessageAssertion) object;
200 }
201
202 public void refresh()
203 {
204 release();
205 init();
206 fireContentsChanged( this, 0, getSize() );
207 }
208
209 private void init()
210 {
211 assertable.addAssertionsListener( this );
212
213 for( int c = 0; c < assertable.getAssertionCount(); c++ )
214 {
215 WsdlMessageAssertion assertion = assertable.getAssertionAt( c );
216 addAssertion(assertion);
217 }
218 }
219
220 public void release()
221 {
222 items.clear();
223
224 for( int c = 0; c < assertable.getAssertionCount(); c++ )
225 {
226 WsdlMessageAssertion assertion = assertable.getAssertionAt( c );
227 assertion.removePropertyChangeListener( this );
228 }
229
230 assertable.removeAssertionsListener( this );
231 }
232
233 public void propertyChange(PropertyChangeEvent evt)
234 {
235 refresh();
236 }
237
238 public void assertionAdded(WsdlMessageAssertion assertion)
239 {
240 int sz = getSize();
241 addAssertion(assertion);
242
243 fireIntervalAdded( this, sz, items.size()-1 );
244 }
245
246 private void addAssertion(WsdlMessageAssertion assertion)
247 {
248 assertion.addPropertyChangeListener( this );
249 items.add( assertion );
250
251 AssertionError[] errors = assertion.getErrors();
252 if( errors != null)
253 {
254 for( int i = 0; i < errors.length; i++ )
255 items.add( errors[i] );
256 }
257 }
258
259 public void assertionRemoved(WsdlMessageAssertion assertion)
260 {
261 int ix = items.indexOf( assertion );
262 if( ix == -1 ) return;
263
264 assertion.removePropertyChangeListener( this );
265 items.remove( ix );
266 fireIntervalRemoved( this, ix, ix );
267
268
269 while( ix < items.size() && items.get( ix ) instanceof AssertionError )
270 {
271 items.remove( ix );
272 fireIntervalRemoved( this, ix, ix );
273 }
274 }
275 }
276
277 public void release()
278 {
279 assertionListModel.release();
280 }
281 }