001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.testcase;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.MouseAdapter;
020: import java.awt.event.MouseEvent;
021:
022: import javax.swing.BorderFactory;
023: import javax.swing.JLabel;
024: import javax.swing.JList;
025: import javax.swing.JPanel;
026: import javax.swing.JPopupMenu;
027: import javax.swing.JScrollPane;
028: import javax.swing.ListCellRenderer;
029:
030: import com.eviware.soapui.impl.wsdl.testcase.TestCaseLogItem;
031: import com.eviware.soapui.impl.wsdl.testcase.TestCaseLogModel;
032: import com.eviware.soapui.model.testsuite.TestStepResult;
033: import com.eviware.soapui.support.UISupport;
034: import com.eviware.soapui.support.action.swing.ActionList;
035: import com.eviware.soapui.support.action.swing.ActionSupport;
036:
037: /**
038: * Panel for displaying TestStepResults
039: *
040: * @author Ole.Matzura
041: */
042:
043: public class TestCaseLog extends JPanel {
044: private TestCaseLogModel logListModel;
045: private JList testLogList;
046:
047: public TestCaseLog() {
048: super (new BorderLayout());
049:
050: buildUI();
051: }
052:
053: private void buildUI() {
054: logListModel = new TestCaseLogModel();
055: testLogList = new JList(logListModel);
056: testLogList.setCellRenderer(new TestLogCellRenderer());
057: testLogList.setPrototypeCellValue("Testing 123");
058: testLogList.setFixedCellWidth(-1);
059: testLogList.addMouseListener(new LogListMouseListener());
060: testLogList.setBorder(BorderFactory
061: .createLineBorder(Color.GRAY));
062:
063: JScrollPane scrollPane = new JScrollPane(testLogList);
064: scrollPane.setBorder(BorderFactory.createTitledBorder(
065: BorderFactory.createEmptyBorder(), "Test Log"));
066: add(scrollPane, BorderLayout.CENTER);
067: }
068:
069: private static final class TestLogCellRenderer extends JLabel
070: implements ListCellRenderer {
071: public TestLogCellRenderer() {
072: setOpaque(true);
073: setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
074: setIcon(null);
075: }
076:
077: public Component getListCellRendererComponent(JList list,
078: Object value, int index, boolean isSelected,
079: boolean cellHasFocus) {
080: if (value instanceof String) {
081: setText(value.toString());
082: } else if (value instanceof TestCaseLogItem) {
083: TestCaseLogItem logItem = (TestCaseLogItem) value;
084: String msg = logItem.getMsg();
085: setText(msg == null ? "" : msg);
086: }
087:
088: if (isSelected) {
089: setBackground(list.getSelectionBackground());
090: setForeground(list.getSelectionForeground());
091: } else {
092: setBackground(list.getBackground());
093: setForeground(list.getForeground());
094: }
095:
096: setEnabled(list.isEnabled());
097: //setFont(list.getFont());
098:
099: return this ;
100: }
101: }
102:
103: /**
104: * Mouse Listener for triggering default action and showing popup for log list items
105: *
106: * @author Ole.Matzura
107: */
108:
109: private final class LogListMouseListener extends MouseAdapter {
110: public void mouseClicked(MouseEvent e) {
111: if (e.getClickCount() < 2)
112: return;
113: int selectedIndex = testLogList.getSelectedIndex();
114: if (selectedIndex == -1)
115: return;
116: TestStepResult result = logListModel
117: .getResultAt(selectedIndex);
118: if (result != null && result.getActions() != null)
119: result.getActions().performDefaultAction(
120: new ActionEvent(this , 0, null));
121: }
122:
123: public void mousePressed(MouseEvent e) {
124: if (e.isPopupTrigger())
125: showPopup(e);
126: }
127:
128: public void mouseReleased(MouseEvent e) {
129: if (e.isPopupTrigger())
130: showPopup(e);
131: }
132:
133: public void showPopup(MouseEvent e) {
134: int row = testLogList.locationToIndex(e.getPoint());
135: if (row == -1)
136: return;
137:
138: if (testLogList.getSelectedIndex() != row) {
139: testLogList.setSelectedIndex(row);
140: }
141:
142: TestStepResult result = logListModel.getResultAt(row);
143: if (result == null)
144: return;
145:
146: ActionList actions = result.getActions();
147:
148: if (actions == null || actions.getActionCount() == 0)
149: return;
150:
151: JPopupMenu popup = ActionSupport.buildPopup(actions);
152: UISupport.showPopup(popup, testLogList, e.getPoint());
153: }
154: }
155:
156: public void clear() {
157: logListModel.clear();
158: }
159:
160: public void addText(String string) {
161: logListModel.addText(string);
162: }
163:
164: public void addTestStepResult(TestStepResult stepResult) {
165: logListModel.addTestStepResult(stepResult);
166: }
167:
168: public TestCaseLogModel getLogListModel() {
169: return logListModel;
170: }
171:
172: public void setLogListModel(TestCaseLogModel logListModel) {
173: this.logListModel = logListModel;
174: testLogList.setModel(logListModel);
175: }
176: }
|