1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.testcase;
14
15 import java.awt.BorderLayout;
16 import java.awt.Color;
17 import java.awt.Component;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.MouseAdapter;
20 import java.awt.event.MouseEvent;
21
22 import javax.swing.BorderFactory;
23 import javax.swing.JLabel;
24 import javax.swing.JList;
25 import javax.swing.JPanel;
26 import javax.swing.JPopupMenu;
27 import javax.swing.JScrollPane;
28 import javax.swing.ListCellRenderer;
29
30 import com.eviware.soapui.impl.wsdl.testcase.TestCaseLogItem;
31 import com.eviware.soapui.impl.wsdl.testcase.TestCaseLogModel;
32 import com.eviware.soapui.model.testsuite.TestStepResult;
33 import com.eviware.soapui.support.UISupport;
34 import com.eviware.soapui.support.action.ActionList;
35 import com.eviware.soapui.support.action.ActionSupport;
36
37 /***
38 * Panel for displaying TestStepResults
39 *
40 * @author Ole.Matzura
41 */
42
43 public class TestCaseLog extends JPanel
44 {
45 private TestCaseLogModel logListModel;
46 private JList testLogList;
47
48 public TestCaseLog()
49 {
50 super(new BorderLayout());
51
52 buildUI();
53 }
54
55 private void buildUI()
56 {
57 logListModel = new TestCaseLogModel();
58 testLogList = new JList(logListModel);
59 testLogList.setCellRenderer(new TestLogCellRenderer());
60 testLogList.setPrototypeCellValue( "Testing 123" );
61 testLogList.setFixedCellWidth( -1 );
62 testLogList.addMouseListener(new LogListMouseListener());
63 testLogList.setBorder( BorderFactory.createLineBorder( Color.GRAY ) );
64
65 JScrollPane scrollPane = new JScrollPane(testLogList);
66 scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Test Log"));
67 add(scrollPane, BorderLayout.CENTER);
68 }
69
70 private static final class TestLogCellRenderer extends JLabel implements ListCellRenderer
71 {
72 public TestLogCellRenderer()
73 {
74 setOpaque(true);
75 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
76 setIcon(null);
77 }
78
79 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
80 boolean cellHasFocus)
81 {
82 if (value instanceof String)
83 {
84 setText(value.toString());
85 }
86 else if (value instanceof TestCaseLogItem)
87 {
88 TestCaseLogItem logItem = (TestCaseLogItem) value;
89 String msg = logItem.getMsg();
90 setText(msg == null ? "" : msg);
91 }
92
93 if (isSelected)
94 {
95 setBackground(list.getSelectionBackground());
96 setForeground(list.getSelectionForeground());
97 }
98 else
99 {
100 setBackground(list.getBackground());
101 setForeground(list.getForeground());
102 }
103
104 setEnabled(list.isEnabled());
105
106
107 return this;
108 }
109 }
110
111 /***
112 * Mouse Listener for triggering default action and showing popup for log list items
113 *
114 * @author Ole.Matzura
115 */
116
117 private final class LogListMouseListener extends MouseAdapter
118 {
119 public void mouseClicked(MouseEvent e)
120 {
121 if (e.getClickCount() < 2)
122 return;
123 int selectedIndex = testLogList.getSelectedIndex();
124 if (selectedIndex == -1)
125 return;
126 TestStepResult result = logListModel.getResultAt(selectedIndex);
127 if (result != null && result.getActions() != null)
128 result.getActions().performDefaultAction(new ActionEvent(this, 0, null));
129 }
130
131 public void mousePressed(MouseEvent e)
132 {
133 if (e.isPopupTrigger())
134 showPopup(e);
135 }
136
137 public void mouseReleased(MouseEvent e)
138 {
139 if (e.isPopupTrigger())
140 showPopup(e);
141 }
142
143 public void showPopup(MouseEvent e)
144 {
145 int row = testLogList.locationToIndex(e.getPoint());
146 if (row == -1)
147 return;
148
149 if (testLogList.getSelectedIndex() != row)
150 {
151 testLogList.setSelectedIndex(row);
152 }
153
154 TestStepResult result = logListModel.getResultAt(row);
155 if (result == null)
156 return;
157
158 ActionList actions = result.getActions();
159
160 if (actions == null || actions.getActionCount() == 0)
161 return;
162
163 JPopupMenu popup = ActionSupport.buildPopup(actions);
164 UISupport.showPopup(popup, testLogList, e.getPoint());
165 }
166 }
167
168 public void clear()
169 {
170 logListModel.clear();
171 }
172
173 public void addText(String string)
174 {
175 logListModel.addText( string );
176 }
177
178 public void addTestStepResult(TestStepResult stepResult)
179 {
180 logListModel.addTestStepResult( stepResult );
181 }
182
183 public TestCaseLogModel getLogListModel()
184 {
185 return logListModel;
186 }
187
188 public void setLogListModel(TestCaseLogModel logListModel)
189 {
190 this.logListModel = logListModel;
191 testLogList.setModel( logListModel );
192 }
193 }