/*
 * Copyright (c) 1997-2024 IDRsolutions (https://www.idrsolutions.com)
 */
package org.jpedal.examples.handlers;

import org.jpedal.examples.viewer.Commands;
import org.jpedal.examples.viewer.Viewer;
import org.jpedal.external.JPedalActionHandler;
import org.jpedal.external.Options;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.HashMap;
import java.util.Map;

public class ExampleActionHandler extends JFrame {
    @SuppressWarnings("unused")
    public static void main(final String[] args) {
        new ExampleActionHandler();
    }

    public ExampleActionHandler() {
        /* add the Viewer component */
        final Viewer viewer = new Viewer("default.xml")//can also be no value
        viewer.setRootContainer(getContentPane());

        viewer.setupViewer();

        /* create a new JPedalActionHandler implementation */
        final JPedalActionHandler printAction = (currentGUI, commands-> currentGUI.showMessageDialog("Custom print dialog""Custom Title", JOptionPane.INFORMATION_MESSAGE);

        /* add the implementation to a Map, with its corresponding command, in this case Commands.PRINT */
        final Map<Integer, Object> actions = new HashMap<>();
        actions.put(Commands.PRINT, printAction);

        /* pass the map into the external handler */
        viewer.addExternalHandler(actions, Options.JPedalActionHandler);

        /* display the Viewer */
        displayViewer();
    }

    private void displayViewer() {
        final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int width = d.width / 2;
        final int height = d.height / 2;
        if (width < 700) {
            width = 700;
        }

        setSize(width, height);
        setLocationRelativeTo(null)//centre on screen
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
}