/*
 *   GUI application demonstration for exe4j
 */

import com.install4j.api.launcher.Controller;
import com.install4j.api.launcher.StartupNotification;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Properties;

public class HelloGui extends JFrame {

    private HelloGui() {

        setSize(600, 400);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width - 600) / 2, (screenSize.height - 400) / 2);
        setTitle("Hello World GUI");

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem menuItem = new JMenuItem("Exit");
        menuItem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    System.exit(0);
                }
            }
        );

        menu.add(menuItem);
        menuBar.add(menu);
        setJMenuBar(menuBar);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel helloLabel = new JLabel(getGreetingText());
        helloLabel.setFont(helloLabel.getFont().deriveFont(50f));
        helloLabel.setHorizontalAlignment(SwingConstants.CENTER);
        getContentPane().add(helloLabel, BorderLayout.CENTER);


        Box box = Box.createVerticalBox();
        box.add(new JLabel(" * On Windows: start hello_gui.exe again to see how startup notification works"));
        box.add(new JLabel(" * Quit and pass \"fail\" as an argument to hello_gui to see what happens for a startup failure"));
        getContentPane().add(box, BorderLayout.SOUTH);
    }

    private String getGreetingText() {

        String greetingName;
        try {
            File baseDirectory = new File(getClass().getResource(getClass().getName() + ".class").getFile()).getParentFile().getParentFile();
            File propertiesFile = new File(baseDirectory, "hello.properties");
            Properties properties = new Properties();
            FileInputStream inputStream = new FileInputStream(propertiesFile);
            properties.load(inputStream);
            inputStream.close();
            greetingName = properties.getProperty("greetingName");

        } catch (IOException ex) {
            greetingName = "world";
        }
        return "Hello " + greetingName + "!";
    }

    public static void main(String[] args)
        throws InterruptedException
    {

        try {
            Controller.writeMessage("Initializing giant application ...");
            Thread.sleep(1000);
            Controller.writeMessage("Opening complex main window ...");
            Thread.sleep(1000);
        } catch (Controller.ConnectionException ex) {
        }
        if (args.length == 1 && args[0].equals("fail")) {
			throw new RuntimeException("I was asked to fail");
        } else {
            final HelloGui helloGui = new HelloGui();

            // startup notification on Microsoft Windows
            StartupNotification.registerStartupListener(new StartupNotification.Listener() {
                public void startupPerformed(final String parameters) {
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            JOptionPane.showMessageDialog(helloGui, "I've been started again, with parameters \"" + parameters +  "\".", "Hello World", JOptionPane.INFORMATION_MESSAGE);
                        }
                    });
                }
            });

            helloGui.setVisible(true);
        }
    }
}