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    package graphlab.plugins.automaticupdator.net.interdirected.autoupdate;
006    
007    import javax.swing.*;
008    import java.awt.*;
009    import java.awt.event.ActionEvent;
010    import java.awt.event.ActionListener;
011    
012    /**
013     * @author Michael Quattlebaum
014     */
015    public class GuiStatusScreen extends JPanel {
016        static ScrollingJList jl;
017        static JButton button;
018        private static final long serialVersionUID = -6251102856738091493L;
019    
020        GuiStatusScreen(String title, String detailtext, int width, int height, String buttontext, boolean showbutton, String imageurl, int imagew, int imageh) {
021            Font screenfont = new Font("sanserif", Font.PLAIN, 12);
022            Font buttonfont = new Font("sanserif", Font.BOLD, 12);
023            final JFrame frame = new JFrame(title);
024            button = new JButton(buttontext);
025            button.setEnabled(false);
026            button.setFont(buttonfont);
027            JLabel label = new JLabel(detailtext);
028            label.setFont(screenfont);
029            JLabel space = new JLabel();
030            label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
031    
032            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
033            frame.getContentPane().setLayout(new FlowLayout());
034            if (imageurl != null) {
035                JLabel image = new JLabel();
036                if (imageurl.indexOf("http:") > -1) {
037                    image.setText("<HTML><IMG SRC='" + imageurl + "'></HTML>");
038                } else {
039                    ImageIcon icon = new ImageIcon(imageurl);
040                    image.setIcon(icon);
041                }
042                frame.getContentPane().add(image, BorderLayout.NORTH);
043            }
044            GuiStatusScreen.jl = new ScrollingJList(width, height, imageh, imagew);
045            GuiStatusScreen.jl.list.setModel(new DefaultListModel());
046            frame.getContentPane().add(label, BorderLayout.NORTH);
047            frame.getContentPane().add(space, BorderLayout.NORTH);
048            frame.getContentPane().add(GuiStatusScreen.jl, BorderLayout.CENTER);
049            if (showbutton) frame.getContentPane().add(button, BorderLayout.SOUTH);
050            frame.setMinimumSize(new Dimension(width, height));
051            frame.setPreferredSize(new Dimension(width, height));
052            frame.setSize(new Dimension(width, height));
053            //May want to set to true with a more complex Flow Layout
054    //          frame.setResizable(false);
055    
056            button.addActionListener(
057                    new ActionListener() {
058                        public void actionPerformed(ActionEvent ae) {
059                            // Button Actions go here.
060                            frame.setVisible(false);
061                        }
062                    }
063            );
064    
065            frame.setVisible(true);
066    
067        }
068    
069        /**
070         *
071         */
072        public void enableButton() {
073            button.setEnabled(true);
074        }
075    
076        /**
077         * @param text Text to append to the end of the status window.
078         */
079        public void appendText(String text) {
080            DefaultListModel dlm = (DefaultListModel) GuiStatusScreen.jl.list.getModel();
081            dlm.addElement((Object) text);
082            GuiStatusScreen.jl.list.ensureIndexIsVisible
083                    (GuiStatusScreen.jl.list.getModel().getSize() - 1);
084        }
085    
086    }
087    
088    /**
089     * @author Michael Quattlebaum
090     */
091    class ScrollingJList extends JPanel {
092        private static final long serialVersionUID = -6251102856738091493L;
093        JList list;
094        int width;
095        int height;
096        int iw;
097        int ih;
098    
099        /**
100         * @param w Width of the application window
101         * @param h Height of the application window
102         */
103        public ScrollingJList(int w, int h, int imagew, int imageh) {
104            setLayout(new BorderLayout());
105            Font listfont = new Font("monospaced", Font.PLAIN, 11);
106            list = new JList();
107            list.setFont(listfont);
108            add(new JScrollPane(list));
109            width = w;
110            height = h;
111            iw = imagew;
112            ih = imageh;
113        }
114    
115        /* (non-Javadoc)
116          * @see javax.swing.JComponent#getPreferredSize()
117          */
118        public Dimension getPreferredSize() {
119            return new Dimension(width - 50, height - ih - 80);
120        }
121    
122    }