package install; import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.io.PrintWriter; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.JFrame; /** * The output console window to display all of the debug information regarding * the GetBIRCH installation process. The console itself consists of a text * area to display output information, and a progress bar to visually indicate * how far the installation process has progressed. ** * @author Graham Alvare * @author Dale Hamel * @author Brian Fristensky */ public class OutputConsole extends JFrame { /** * The non-editable text area object to display all of the console and * debug information regarding the GetBIRCH installation process. */ private JTextArea textConsole = new JTextArea() {{ setEditable(false); }}; /** * The progress bar for displaying the progress * of the GetBIRCH installation. */ private JProgressBar progressBar = new JProgressBar(0, 100); /** * The scroll pane to allow the textConsole object to scroll */ private JScrollPane scrollpane = new JScrollPane(textConsole, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); /** * Make the output console a serializable object */ private static final long serialVersionUID = 1L; /** * The current task being undertaken by the install process */ private String task = ""; /** * Determines whether the installation is canceled when the output console * is closed * * true = just exit GetBIRCH when the output window is closed * false = cancel the installation (clean up) when the output window is * closed */ private boolean finished = false; /** * A window listener that to finish the installation if the output console * is closed. */ private final WindowListener CLOSE_WINDOW = new WindowAdapter() { @Override public synchronized void windowClosing(WindowEvent arg0) { if (!finished) { Main.cancel(); } else { System.exit(0); } } }; /** * The log file's writer object - to simplify writing to the log file ** * The output console is attached to a log stream, such that information * sent to the console is also relayed to the GetBIRCH log */ private PrintWriter logwriter; /** * The log file's output stream ** * The output console is attached to a log stream, such that information * sent to the console is also relayed to the GetBIRCH log */ private OutputStream logstream; /** * This is the default constructor */ public OutputConsole(final OutputStream logstream) { super("Getbirch BIRCH Installer Console"); this.logwriter = new PrintWriter(logstream); this.logstream = logstream; System.out.println("Starting output console"); JPanel mainPane = new JPanel(); // configure the progress bar for the output console progressBar.setValue(0); progressBar.setStringPainted(true); progressBar.setIndeterminate(true); // configure the main pane for the output console mainPane.setLayout(new BorderLayout()); mainPane.add(scrollpane, BorderLayout.CENTER); mainPane.add(progressBar, BorderLayout.SOUTH); // configure the output console's display settings setSize(475, 257); add(mainPane); // be sure to display a message and shutdown appropriately if the // output console window is closed addWindowListener(CLOSE_WINDOW); doLayout(); validate(); repaint(50L); setVisible(true); // add a shutdown hook to close the logfile once the program is closed Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { logwriter.flush(); try { logstream.flush(); logstream.close(); } catch (IOException ioe) { ioe.printStackTrace(System.err); } } })); } /** * Writes a line to the output console ** * @param line the line to write to the console. */ public synchronized void println(String line) { //JScrollBar vbar = scrollpane.getVerticalScrollBar(); textConsole.append(line); textConsole.append("\n"); logwriter.println(line); logwriter.flush(); System.out.println(line); //vbar.setValue( vbar.getMaximum() ); } /** * Prints information from a thrown exception to the log * and System.out ** * @param th the exception to print */ public void exception(Throwable th) { th.printStackTrace(logwriter); th.printStackTrace(System.err); } /** * Returns an output stream for writing information to the logger */ public PrintStream getLogStream() throws IOException { /** * The piped output stream used for obtaining input from I/O commands */ final PipedOutputStream out = new PipedOutputStream(); /** * The piped input stream used for obtaining input from I/O commands */ final BufferedReader in = new BufferedReader(new InputStreamReader(new PipedInputStream(out))); // add all of the information sent by the piped output stream // to the output console new Thread(new Runnable() { public void run() { // algorithm adapted from: http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html String line; try { // loop until the stream is closed. while ((line = in.readLine()) != null) { println(line); } } catch (IOException ex) { } } }).start(); return new PrintStream(out); } /** * Changes the progress status for the progress bar ** * @param progress the new level of progress for the progress bar */ public synchronized void setProgress(int progress) { progressBar.setValue(progress); progressBar.setString(task + " - " + progress + "%"); } /** * Changes whether the progress bar is indeterminate. ** * @param isIndeterminate whether the progress bar is indeterminate */ public synchronized void setIndeterminate(boolean isIndeterminate) { progressBar.setIndeterminate(isIndeterminate); } /** * Changes the message displayed when the progress bar is moused-over ** * @param message the message to display */ public synchronized void setProgressTask(String task) { this.task = task; progressBar.setString(task); } /** * Communicates to the output console that the installation process * has completed successfully. */ public synchronized void finished() { //removeWindowListener(CLOSE_WINDOW); finished = true; progressBar.setVisible(false); } } // @jve:decl-index=0:visual-constraint="10,10"