Skip to content
Permalink
6340f8bf78
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
131 lines (96 sloc) 3.02 KB
package edu.uconn.tripoint.ui;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class CommandRunner {
private JFrame _frame;
private Font _labelfont;
private Process _p;
private String _title;
public CommandRunner(JFrame frame, String title){
_frame = frame;
_title = title;
}
public void run() {
final JOptionPane pane = new JOptionPane();
final CommandOutputPanel op = new CommandOutputPanel(_labelfont);
final JProgressBar jpb = new JProgressBar();
jpb.setSize(500, 10);
jpb.setIndeterminate(true);
final JButton cancel = new JButton("Cancel");
final JButton finish = new JButton("Finish");
final JPanel cp = new JPanel(new BorderLayout());
cp.add(jpb, BorderLayout.CENTER);
cp.add(cancel, BorderLayout.EAST);
final JPanel combinedpanel = new JPanel(new BorderLayout());
combinedpanel.add(op, BorderLayout.NORTH);
combinedpanel.add(cp, BorderLayout.SOUTH);
pane.setMessage(combinedpanel);
pane.setOptions(new Object[]{});
final JDialog dialog = pane.createDialog(_frame, _title);
finish.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
cancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(_p != null){
_p.destroy();
}
dialog.dispose();
}
});
SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>(){
@Override
protected Void doInBackground() throws Exception {
try {
PipedOutputStream pout = new PipedOutputStream();
System.setOut(new PrintStream(pout));
PipedInputStream pin = new PipedInputStream();
BufferedReader stdin = new BufferedReader(new InputStreamReader(pin));
System.out.println("Test");
String s = null;
while((s = stdin.readLine()) != null){
op.addLine(s);
op.revalidate();
op.repaint();
}
stdin.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
@Override
protected void done(){
cp.remove(jpb);
cp.remove(cancel);
cp.add(finish, BorderLayout.EAST);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.revalidate();
dialog.repaint();
}
};
sw.execute();
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setVisible(true);
}
}