001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui;
021    
022    import java.awt.BorderLayout;
023    import java.awt.Color;
024    import java.awt.Cursor;
025    import java.awt.Frame;
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    import java.awt.event.MouseAdapter;
029    import java.awt.event.MouseEvent;
030    import java.net.URL;
031    
032    import javax.swing.BorderFactory;
033    import javax.swing.JButton;
034    import javax.swing.JDialog;
035    import javax.swing.JLabel;
036    import javax.swing.JPanel;
037    import javax.swing.SwingUtilities;
038    
039    public class VersionDlg extends JDialog {
040            JLabel status = new JLabel();
041            JLabel link = new JLabel("http://mactor.sourceforge.net");
042            JButton b = new JButton("Ok");
043            public VersionDlg(Frame parent) {
044                    super(parent);
045                    setModal(true);
046                    setLayout(new BorderLayout());
047                    setTitle("MActor - Check for new version");
048                    link.setForeground(Color.blue);
049                    status.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
050                    link.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
051                    add(status, BorderLayout.WEST);
052                    add(link, BorderLayout.EAST);
053                    // link.setFont(link.getFont().)
054                    JPanel bp = new JPanel();
055                    bp.add(b);
056                    add(bp, BorderLayout.SOUTH);
057                    b.addActionListener(new ActionListener() {
058                            public void actionPerformed(ActionEvent arg0) {
059                                    VersionDlg.this.setVisible(false);
060                            }
061                    });
062                    link.addMouseListener(new MouseAdapter() {
063                            @Override
064                            public void mouseClicked(MouseEvent arg0) {
065                                    try {
066                                            BrowserUtil.openURL(new URL(link.getText()));
067                                    } catch (Exception e) {
068                                            e.printStackTrace();// ignore
069                                    }
070                            }
071                    });
072                    pack();
073                    setResizable(false);
074            }
075            public void performCheck() {
076                    link.setVisible(false);
077                    status.setText("Checking for new version..");
078                    b.setEnabled(false);
079                    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
080                    pack();
081                    SwingUtilities.invokeLater(new Runnable() {
082                            public void run() {
083                                    String version = VersionUtil.getVersion();
084                                    String newVersion = VersionUtil.getLatestVersion();
085                                    if (newVersion == null) {
086                                            status.setText("Unable to check if a new version is available");
087                                    } else if (!version.equals(newVersion)) {
088                                            status.setText("Version " + newVersion + " is available for download at: ");
089                                            link.setVisible(true);
090                                    } else {
091                                            status.setText("Your version is up to date!");
092                                    }
093                                    b.setEnabled(true);
094                                    setCursor(Cursor.getDefaultCursor());
095                                    validate();
096                                    pack();
097                            }
098                    });
099            }
100    }