import com.install4j.api.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;

// This is a custom screen which is outside the default screen flow. It is registered behind
// the "Finished" system screen and is never shown when using the "Next" and "Back"
// buttons. Only the "Show example" button in the HelloScreen activates this screen.
// This screen only offers the back button for navigation which takes the user back to
// the HelloScreen. Also, this screen shows how to catch InstallerEvents in order to react
// to changes in other screens.
public class ExampleScreen extends CustomScreen {

    // label with the example that is updated by the ChangeListener
    private JLabel labelExample;

    // A custom screen is a panel and you can set any layout manager and add your components to it.
    // No interaction with other screens is possible at this point. Since this screen wants to
    // add a ChangeListener to the HelloScreen object, it has to install an InstallerEventListener
    // to the InstallerContext (as soon as it is delivered to this screen via setInstallerContext)
    // and catch the InstallerEvent of type EVENT_START. Then, the registered HelloScreen object
    // is available from the InstallerContext.
    public ExampleScreen() {
        labelExample = new JLabel();
        Border insetBorder = new EmptyBorder(10, 50, 10, 0);
        labelExample.setBorder(insetBorder);
        labelExample.setFont(labelExample.getFont().deriveFont(Font.BOLD));

        setLayout(new BorderLayout());
        Box box = Box.createVerticalBox();
        box.add(new JLabel("With your current selection, Hello World Suite will greet like:"));
        box.add(labelExample);
        box.add(new JLabel("Both command line and GUI will greet this way."));
        box.add(Box.createVerticalStrut(5));
        box.add(new JLabel("Should you require further assistance, please call"));
        JLabel labelNumber = new JLabel("1-800-HELLOWORLD");
        labelNumber.setBorder(insetBorder);
        box.add(labelNumber);

        add(box, BorderLayout.CENTER);

    }

    // Demonstrates how to use the InstallerEventListener. Here, the EVENT_START
    // event is used to add a ChangeListener to the HelloScreen. This cannot
    // be done in the constructor, since the screens are only registered after they have been
    // constructed and InstallerContext.getCustomScreenOfClass(HelloScreen.class)
    // would return null. Every CustomScreen is an InstallerEventListener and is registered
    // automatically with the InstallerWizardContext, so you just have to override this method.
    public void installerEvent(InstallerEvent event) {
        if (event.getType() == InstallerEvent.EVENT_START) {
            HelloScreen helloScreen = (HelloScreen)getInstallerContext().getCustomScreenOfClass(HelloScreen.class);
            if (helloScreen != null) {
                helloScreen.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent event) {
                        updateExample((HelloScreen)event.getSource());
                    }
                });
                updateExample(helloScreen);
            }
        }
    }

    // Implementation of abstract method. Determines the title of this screen.
    public String getTitle() {
        return "Greeting example";
    }

    // Implementation of abstract method. Determines the subtitle of this screen.
    public String getSubTitle() {
        return "This is how Hello World Suite will greet its users.";
    }

    // Implementation of abstract method. The components in this screen cannot grow to fill
    // the vertical axis. They want to be at their preferred size at the top of the screen.
    public boolean isFillVertical() {
        return false;
    }

    // Implementation of abstract method. The components in this screen cannot grow to fill
    // the horizontal axis. The want to be at their preferred size at the left of the screen.
    public boolean isFillHorizontal() {
        return false;
    }

    // The "Next" button will not be visible for this screen since this screen is outside the usual
    // screen flow and only the "Back" button should be visible.
    public boolean isNextVisible() {
        return false;
    }

    // The "Cancel" button will not be visible for this screen.
    public boolean isCancelVisible() {
        return false;
    }

    // If you want to influence the screen flow, you have to override the previous() and next() methods.
    // You can use the goto* methods in the installer context to switch to a screen of
    // your choosing. Make sure to return "false" in that case to prevent the default mechanism from
    // functioning.
    public boolean previous() {
        InstallerWizardContext context = getInstallerContext();
        CustomScreen helloScreen = context.getCustomScreenOfClass(HelloScreen.class);
        if (helloScreen == null) {
            JOptionPane.showMessageDialog(getFrame(), "HelloScreen is not registered", "Installer", JOptionPane.ERROR_MESSAGE);
        } else {
            context.gotoCustomScreen(helloScreen);
        }
        return false;
    }

    // helper method to update the example with the current settings in HelloScreen
    private void updateExample(HelloScreen helloScreen) {
        labelExample.setText("Hello " + helloScreen.getGreetingName());
    }

}
