/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.engine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import com.jstatcom.component.CompSettings;
/**
* Default component for displaying status information about procedure calls. It
* shows time, name and engine of the running thread object. Instances of this
* class can be created or they can be shared by using the
* getInstance method. This makes sense, because usually all
* PCall objects in an application share the status display. This
* display component assumes that only one PCall is running at a
* time.
*
* @author Markus Kraetzig
* @see com.jstatcom.engine.PCall
*/
public class DefaultPCallControl extends JPanel implements PCallListener {
private static final Logger log = Logger
.getLogger(DefaultPCallControl.class);
private static DefaultPCallControl instance = null;
private javax.swing.JLabel jLabel = null;
private StatusDisplayThread displayThread = null;
private javax.swing.JButton jButton = null;
private PCall runningPCall = null;
private ActionListener stopAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
if (runningPCall != null)
runningPCall.cancel();
}
};
private class StatusDisplayThread extends Thread {
private final String pCallName;
public StatusDisplayThread(String name) {
pCallName = name;
}
public void run() {
Date start = new Date();
try {
while (true) {
final Date now = new Date();
now.setTime(now.getTime() - start.getTime() - 3600000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
getJLabel().setText(
" Running \""
+ pCallName
+ "\" (elapsed time "
+ CompSettings
.getSimpleDateFormat()
.format(now) + ")");
}
});
// interrupt lets it finish loop
Thread.sleep(1000);
}
} catch (Throwable ex) {
// do nothing, might have interrupted sleep
}
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
getJLabel().setText("");
}
});
} catch (Throwable ex) {
log.error("Error calling invokeLater", ex);
}
}
}
/**
*
*/
public DefaultPCallControl() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(new java.awt.BorderLayout());
this.add(getJLabel(), java.awt.BorderLayout.CENTER);
this.add(getJButton(), java.awt.BorderLayout.EAST);
getJButton().setVisible(true);
this.setSize(200, 25);
this.setBorder(new javax.swing.border.EtchedBorder());
}
/*
* @see com.jstatcom.engine.PCallListener#finished(com.jstatcom.engine.PCall)
*/
public void finished(PCall pc) {
if (displayThread != null)
displayThread.interrupt();
displayThread = null;
runningPCall = null;
getJButton().setEnabled(false);
}
/*
* @see com.jstatcom.engine.PCallListener#queued(com.jstatcom.engine.PCall)
*/
public void queued(PCall pc) {
// do nothing
}
/*
* @see com.jstatcom.engine.PCallListener#started(com.jstatcom.engine.PCall)
*/
public void started(PCall pc) {
if (displayThread != null && displayThread.isAlive())
throw new RuntimeException(
"Another thread is running, display out of sync.");
displayThread = new StatusDisplayThread(pc.getName());
displayThread.start();
runningPCall = pc;
getJButton().setEnabled(true);
}
/*
* @see com.jstatcom.engine.PCallListener#success()
*/
public void success() {
// do nothing
}
/**
* Gets an instance of this class that can be shared. It will be created on
* first invocation of this method.
*
* @return instance of this class
*/
public static DefaultPCallControl getInstance() {
if (instance == null)
instance = new DefaultPCallControl();
return instance;
}
/**
* This method initializes jLabel
*
* @return javax.swing.JLabel
*/
private javax.swing.JLabel getJLabel() {
if (jLabel == null) {
jLabel = new javax.swing.JLabel();
jLabel.setText("");
}
return jLabel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private javax.swing.JButton getJButton() {
if (jButton == null) {
jButton = new javax.swing.JButton();
jButton.setPreferredSize(new java.awt.Dimension(100, 25));
jButton.setText("STOP");
jButton.setEnabled(false);
jButton.addActionListener(stopAction);
}
return jButton;
}
/**
* Shows or hides the stop button.
*
* @param isVisible
*/
public void setStopButtonVisible(boolean isVisible) {
getJButton().setVisible(isVisible);
}
} // @jve:visual-info decl-index=0 visual-constraint="10,10"
Ads help maintain this website.