/*
* Copyright (c) 1998-99 The Java Apache Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Java Apache
* Project (http://java.apache.org/)."
*
* 4. The names "Apache JMeter" and "Java Apache Project" must
* not be used to endorse or promote products derived from this software
* without prior written permission.
*
* 5. Products derived from this software may not be called "Apache JMeter"
* nor may "Java Apache Project" appear in their names without
* prior written permission of the Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Java Apache
* Project (http://java.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Project. For more information
* on the Java Apache Project please see <http://java.apache.org/>.
*
*/
package org.apache.jmeter.visualizers;
import org.apache.jmeter.visualizers.Visualizer;
import org.apache.jmeter.threads.*;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.*;
import org.apache.jmeter.util.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class VisualizerContainer
implements SampleListener, ActionListener, ThreadGroupListener
{
private Hashtable visualizers;
private Hashtable panels;
private Class[] visualizerClasses;
private Vector threadGroups;
private JPanel vPanel;
private JPanel subPanel;
private JComboBox visualizerList;
private CardLayout topCardLayout;
public VisualizerContainer(Vector visualizers) {
this.visualizers = new Hashtable();
this.panels = new Hashtable();
this.threadGroups = new Vector();
visualizerClasses = new Class[visualizers.size()];
for(int i=0;i<visualizerClasses.length;i++) {
visualizerClasses[i]=visualizers.elementAt(i).getClass();
}
}
public VisualizerContainer(Vector visualizers, String[] threadGroups) {
this(visualizers);
for(int i=0;i<threadGroups.length;i++)
addThreadGroup(threadGroups[i]);
}
public void addThreadGroup(String group) {
threadGroups.addElement(group);
Vector a = getVisualizers();
JComboBox visualizerList = new JComboBox(a);
visualizerList.setEditable(false);
visualizerList.setSelectedIndex(0);
visualizerList.addActionListener(this);
visualizerList.setActionCommand("visualizer."+group);
JPanel visualizerPanel = new JPanel();
//visualizerPanel.setBorder(new TitledBorder(new EtchedBorder(),
// "Actions"));
visualizerPanel.setLayout(new BorderLayout());
visualizerPanel.add(visualizerList, BorderLayout.NORTH);
JPanel subPanel = new JPanel();
subPanel.setLayout(new CardLayout());
visualizers.put(group, visualizerList);
panels.put(group, subPanel);
String startupVisualizer =
JMeterUtils.getPropDefault("startup.action",".");
int count = 0;
for (Enumeration e = a.elements(); e.hasMoreElements();) {
Visualizer visualizer = (Visualizer) e.nextElement();
subPanel.add(visualizer.getControlPanel(), visualizer.toString());
if ( startupVisualizer
.equalsIgnoreCase(visualizer.getClass().getName()) ) {
System.out.println("### Setting Visualizer="+startupVisualizer);
visualizerList.setSelectedIndex(count);
}
++count;
}
visualizerPanel.add(subPanel, BorderLayout.CENTER);
getVisualizerPanel().add(visualizerPanel, group);
}
public void removeThreadGroup(String group) {
visualizers.remove(group);
panels.remove(group);
}
public void setCurrentThreadGroup(String group) {
topCardLayout.show(vPanel, group);
}
public JPanel getVisualizerPanel() {
if (vPanel == null) {
vPanel = new JPanel();
topCardLayout = new CardLayout();
vPanel.setLayout(topCardLayout);
vPanel.setBorder(new TitledBorder(new EtchedBorder(), "Actions"));
}
return vPanel;
}
public void clear() {
for(int i=0;i<threadGroups.size();i++)
((Visualizer)((JComboBox)visualizers.get(threadGroups.elementAt(i))).getSelectedItem()).clear();
}
private Vector getVisualizers() {
Vector v = new Vector();
for (int i=0;i<visualizerClasses.length;i++)
try {
v.addElement(visualizerClasses[i].newInstance());
}
catch (IllegalAccessException e) {e.printStackTrace();}
catch (InstantiationException e) {e.printStackTrace();}
return v;
}
public void sampleStarted(SampleEvent e) {}
public void sampleStopped(SampleEvent e) {}
public void sampleOccurred(SampleEvent e) {
((Visualizer)((JComboBox)visualizers.get(
e.getThreadGroup())).getSelectedItem()).add(e.getResult());
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.startsWith("visualizer")) {
String group = cmd.substring(cmd.indexOf(".")+1);
Visualizer v = (Visualizer)
((JComboBox)visualizers.get(group)).getSelectedItem();
JPanel p = (JPanel)panels.get(group);
((CardLayout)p.getLayout()).show(p,v.toString());
}
}
public void threadGroupAdded(ThreadGroupEvent e) {
addThreadGroup(e.getThreadGroup());
}
public void threadGroupRemoved(ThreadGroupEvent e) {
removeThreadGroup(e.getThreadGroup());
}
public void threadGroupSelected(ThreadGroupEvent e) {
setCurrentThreadGroup(e.getThreadGroup());
}
}