Link

Replacing Viewer functionality with customized implementations

The Viewers’ ability to be customized extends to replacing the existing built-in functions with your own using the JPedalActionHandler interface.

Below we will cover how to implement the JPedalActionHandler and include it in the Viewer to replace existing functionality.

Creating Your Own JPedalActionHandler Implementation

JPedalActionHandler contains a single method which is invoked when the command it is linked to gets triggered.

public void actionPerformed(SwingGUI currentGUI, Commands commands);

This method receives a copy of the SwingGUI which holds all the GUI components and controls, and Commands that can trigger the commands in the Viewer API.

It is in this method that you will write the custom handing for the given command.

For example, below we have an implementation that will open a custom URL in the systems default browser.

JPedalActionHandler helpAction = new JPedalActionHandler() {
    public void actionPerformed(SwingGUI currentGUI, Commands commands) {
        try {
            java.awt.Desktop.getDesktop().browse(new URI(("https://myWebsite.com/support/")));
        } catch (final Exception e) {
            LogWriter.writeLog("Exception attempting launch browser: " + e);
        }
    }
};

Overriding an existing functionality

Once you have created your JPedalActionHandler implementations you will need to pass them to the Viewer using Viewer.addExternalHandler(Object newHandler, int type).

The Viewer can allow two types of External Handler to be used. FormsActionHandler and JPedalActionHandler which can be specified using the int values provided in the class org.jpedal.external.Options.

The Map input should be a map of all JPedalActionHandlers you want to implement with the key being the Command integer you wish to replace.

Continuing the example from above, we can have our JPedalActionHandler implementation replace the Commands.HELP action as follows.

Map actions = new HashMap();
actions.put(new Integer(Commands.HELP), helpAction);
        viewer.addExternalHandler(actions, Options.JPedalActionHandler);