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