View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.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.addMouseListener(new LogListMouseListener());
61  		testLogList.setBorder( BorderFactory.createLineBorder(  Color.GRAY ) );
62  
63  		JScrollPane scrollPane = new JScrollPane(testLogList);
64  		scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Test Log"));
65  		add(scrollPane, BorderLayout.CENTER);
66  	}
67  
68  	private static final class TestLogCellRenderer extends JLabel implements ListCellRenderer
69  	{
70  		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
71  				boolean cellHasFocus)
72  		{
73  			setIcon(null);
74  
75  			if (value instanceof String)
76  			{
77  				setText(value.toString());
78  			}
79  			else if (value instanceof TestCaseLogItem)
80  			{
81  				TestCaseLogItem logItem = (TestCaseLogItem) value;
82  				setIcon(null);
83  				String msg = logItem.getMsg();
84  				setText(msg == null ? "" : msg);
85  			}
86  
87  			if (isSelected)
88  			{
89  				setBackground(list.getSelectionBackground());
90  				setForeground(list.getSelectionForeground());
91  			}
92  			else
93  			{
94  				setBackground(list.getBackground());
95  				setForeground(list.getForeground());
96  			}
97  
98  			setEnabled(list.isEnabled());
99  			setFont(list.getFont());
100 			setOpaque(true);
101 			setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
102 
103 			return this;
104 		}
105 	}
106 
107 	/***
108 	 * Mouse Listener for triggering default action and showing popup for log list items
109 	 * 
110 	 * @author Ole.Matzura
111 	 */
112 
113 	private final class LogListMouseListener extends MouseAdapter
114 	{
115 		public void mouseClicked(MouseEvent e)
116 		{
117 			if (e.getClickCount() < 2)
118 				return;
119 			int selectedIndex = testLogList.getSelectedIndex();
120 			if (selectedIndex == -1)
121 				return;
122 			TestStepResult result = logListModel.getResultAt(selectedIndex);
123 			if (result != null && result.getActions() != null)
124 				result.getActions().performDefaultAction(new ActionEvent(this, 0, null));
125 		}
126 
127 		public void mousePressed(MouseEvent e)
128 		{
129 			if (e.isPopupTrigger())
130 				showPopup(e);
131 		}
132 
133 		public void mouseReleased(MouseEvent e)
134 		{
135 			if (e.isPopupTrigger())
136 				showPopup(e);
137 		}
138 
139 		public void showPopup(MouseEvent e)
140 		{
141 			int row = testLogList.locationToIndex(e.getPoint());
142 			if (row == -1)
143 				return;
144 
145 			if (testLogList.getSelectedIndex() != row)
146 			{
147 				testLogList.setSelectedIndex(row);
148 			}
149 
150 			TestStepResult result = logListModel.getResultAt(row);
151 			if (result == null)
152 				return;
153 
154 			ActionList actions = result.getActions();
155 
156 			if (actions == null || actions.getActionCount() == 0)
157 				return;
158 
159 			JPopupMenu popup = ActionSupport.buildPopup(actions);
160 			UISupport.showPopup(popup, testLogList, e.getPoint());
161 		}
162 	}
163 
164 	public void clear()
165 	{
166 		logListModel.clear();
167 	}
168 
169 	public void addText(String string)
170 	{
171 		logListModel.addText( string );
172 	}
173 
174 	public void addTestStepResult(TestStepResult stepResult)
175 	{
176 		logListModel.addTestStepResult( stepResult );
177 	}
178 
179 	public TestCaseLogModel getLogListModel()
180 	{
181 		return logListModel;
182 	}
183 
184 	public void setLogListModel(TestCaseLogModel logListModel)
185 	{
186 		this.logListModel = logListModel;
187 		testLogList.setModel( logListModel );
188 	}
189 }