001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    /*
006     * Splash.java
007     *
008     * Created on September 19, 2005, 8:09 PM
009     */
010     package graphlab.platform;
011    
012    import javax.swing.*;
013    import java.awt.*;
014    import java.io.*;
015    import java.net.URL;
016    
017    /**
018     * an splash screen for showing when the program is loading,
019     * it simply redirects the System.out, so every thing you write
020     * on System.out will be written on the splash!
021     *
022     * @author azin
023     */
024    public class GSplash extends javax.swing.JFrame {
025    
026        /**
027         *
028         */
029        private static final long serialVersionUID = 7023272493672748876L;
030        public PrintWriter pp;
031        BufferedReader br;
032        private boolean show;
033        PrintStream defaultOut;
034        private PrintStream out;
035    
036        /**
037         * Creates new form Splash
038         */
039        public GSplash() {
040            initComponents();
041    //        pack();
042            PipedOutputStream src = new PipedOutputStream();
043            PipedInputStream p = null;
044            try {
045                p = new PipedInputStream(src);
046            } catch (IOException e) {
047                e.printStackTrace();
048            }
049            pp = new PrintWriter(src);
050            BufferedInputStream bbr = new BufferedInputStream(p);
051            br = new BufferedReader(new InputStreamReader(bbr));
052            out = new PrintStream(src);
053            defaultOut = System.err;
054        }
055    
056        public PrintWriter getOut() {
057            return pp;
058        }
059    
060        public void showMessages() {
061            show = true;
062            setVisible(true);
063            System.setErr(out);
064            new Thread() {
065                public void run() {
066                    String s = "";
067                    boolean firstTime = true;
068                    long time = System.currentTimeMillis();
069                    while (show) {
070                        try {
071    //                        Thread.sleep(100);
072    //                        if (br.ready()) {
073                            String l = br.readLine();
074                            s = l + "\n" + s;
075    //                        defaultOut.println(l);
076                            text.setText(s);
077                            defaultOut.println(l);
078                            if (time + 4000 > System.currentTimeMillis())
079                                toFront();
080                            firstTime = false;
081    //                        }
082                        } catch (Exception e) {
083    //                        e.printStackTrace();
084                            System.err.println("err?");
085                            show = false;
086                        }
087                    }
088                }
089            }.start();
090        }
091    
092        public void stopShowing() {
093            show = false;
094            System.setErr(defaultOut);
095        }
096    
097        /**
098         * This method is called from within the constructor to
099         * initialize the form.
100         */
101        private void initComponents() {
102            text = new javax.swing.JTextArea();
103            bg = new javax.swing.JLabel();
104    
105            getContentPane().setLayout(null);
106    
107    //        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
108    //        setAlwaysOnTop(true);
109            toFront();
110            setResizable(false);
111            setUndecorated(true);
112            text.setEditable(false);
113            text.setFont(new java.awt.Font("dialog", 20, 9));
114            text.setOpaque(false);
115            add(text);
116            text.setBounds(40, 200, 400, 150);
117    
118            bg.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
119            Class<? extends GSplash> clazz = getClass();
120            URL resource = clazz.getResource("splash.jpg");
121            if (resource == null)
122                resource = clazz.getResource("splash.gif");
123            if (resource == null)
124                resource = clazz.getResource("splash.png");
125            if (resource == null)
126                resource = clazz.getResource("splash.bmp");
127            if (resource != null) {
128                ImageIcon icon = new ImageIcon(resource);
129                bg.setIcon(icon);
130                bg.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
131            }
132            bg.setVerticalAlignment(javax.swing.SwingConstants.TOP);
133            add(bg);
134            super.setPreferredSize(bg.getSize());
135            super.setSize(bg.getSize());
136            Dimension sz = Toolkit.getDefaultToolkit().getScreenSize();
137            Dimension s = getSize();
138            setLocation((sz.width - s.width) / 2, (sz.height - s.height) / 2);
139        }
140    
141        // Variables declaration - do not modify
142        private javax.swing.JLabel bg;
143        private javax.swing.JTextArea text;
144        // End of variables declaration
145    
146    }