Update of /cvsroot/javaprofiler/test/module
In directory usw-pr-cvs1:/tmp/cvs-serv11628
Added Files:
MainThreads.java
Log Message:
Test of a graphical component
--- NEW FILE: MainThreads.java ---
package net.sourceforge.javaprofiler.module.views;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import net.sourceforge.javaprofiler.jpiimpl.commun.*;
import net.sourceforge.javaprofiler.jpiimpl.realtime.*;
/** Example of using TimeSeriesGraph to show number of running threads.
* Please supply the name of profiled computer as a command-line parameter.
* Default: localhost
* @author Lukas Petru
*/
public class MainThreads
{
static TimeSeriesGraph component;
public static void main (String[] args) {
connectAndRun(args);
}
private static void launchGui (final ImageR image, final IProf iprof) {
JFrame frame=new JFrame("Main");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
iprof.stop();
System.exit(0);
}
});
component=new TimeSeriesGraph();
component.setBorder(BorderFactory.createLineBorder(Color.yellow));
component.setBackground(Color.white);
frame.getContentPane().add(component, BorderLayout.NORTH);
frame.pack();
frame.show();
// Using java.util.Timer instead that of swing, because we don't
// want the task to run in the event-dispatching thread.
Timer timer=new Timer();
TimerTask task=new TimerTask() {
public void run() {
final TimeSeriesModel m=component.getModel();
final int[] data=m.copyData();
// roll data
System.arraycopy(data,1,data,0,data.length-1);
image.refreshThreads();
// get last value
int val=image.getThreads().size();
data[data.length-1]=val>0 ? val : 0;
// set data in event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
m.setData(data);
}
});
}
};
timer.schedule(task, 1000, 500);
}
private static ImageR getImage(IProf iprof) {
ImageR image=new ImageR(iprof, false);
return image;
}
private static IProf connect (String hostName) {
IProf iprof = new IProf(new CommunSetupSocket(CommunSetupSocket.
CLIENT_MODE, hostName, 25595));
try {
iprof.run();
return iprof;
} catch (IProfException e) {
System.out.println(e);
return null;
}
}
private static void connectAndRun(String[] arg) {
String hostName = "localhost";
if (arg.length >= 1)
hostName = arg[0];
System.out.println("host: " + hostName);
IProf iprof = connect(hostName);
if (iprof != null) {
ImageR image=getImage(iprof);
launchGui(image, iprof);
}
else System.out.println("Error connecting.");
}
}
|