Create a Java Application That Uses Flare HTML5 Help

This post describes the creation of a simple Java application and Flare project and HTML5 output to demonstrate opening a Flare HTML5 help topic with a button click or an F1 keypress from a Java application. This is a “hello world” level example.

Downloads:

Java Application and Flare HTML5 Help

MadCap provides conceptual information and instructions for programmers and authors about how to configure applications to use context sensitive help with Flare HTML5 outputs. This post covers some of that workflow. The example is less complicated but not as flexible as MadCap’s suggested workflows. However this post goes into greater detail about how to connect an application to HTML5 help in the context of a Java application.

MadCap Software: CSH Calls for HTML5 Output (WebHelp 2.0)

The example Java and Flare projects are configured such that the application opens a URL for the Flare HTML5 output using the CSHID number in the URL For simplicity, the CSHID number value used is 1 and the topic is Topic.htm (topic.htm in the sample output). Additional calls to open help can be added with the same technique. For more information about CSHID and CSH calls, see the preceding link.

With the arrangement established by the samples in this post, a Flare author would manage the mapping of ID numbers to topics. A programmer would call the same function for every help button click or F1 press and pass the desired ID number. No lookup is necessary on the Java application side. A programmer only needs to know which values to use for actions and UI locations in the application. There is no need to supply a properties file to a programmer with this setup. One possible workflow is the author informs a programmer which IDs to use and the programmer implements those. Another workflow is a programmer assigns IDs and the author updates the Flare project to map those IDs to the topics. Either way the mapping is managed through the Flare HTML5 system.

Other scenarios may be explored in subsequent posts. The Flare documentation describes a workflow in which a copy of a header or a properties file is used by the application so that the application is aware of the mapping between the ID name and number. That workflow adds some flexibility. But that workflow is not demonstrated in this post.

Create a Java Application with NetBeans

This example uses NetBeans to create the Java sample. The steps use the IDE options so that those who do not program regularly with Java can replicate the steps more easily. However designing forms in an IDE such as NetBeans involves a reliance on the mechanisms the IDE uses to generate code. For example a NetBeans project with a JFrame form may contain an additional XML file with a .form extension to facilitate form building. The Java class for the JFrame is sufficient to run the program. But the .form file is required for the form designer in NetBeans.

NetBeans wiki: What is the .form file?

  1. Install NetBeans. This example was created on a computer running Windows 8 with Java, the Java Development Kit, and NetBeans installed. https://netbeans.org/
    netbeans-ide
  2. From the menu, select File > New ProjectThe New Project screen appears.
    new-project-screen
  3. Select Java Application.
  4. Click Next.
  5. Enter a project name such as JavaAppFlareHtml5Help.
  6. Click Finish.

Add a JFrame Form to the Application

The window user interface for this example is implemented with a JFrame. The steps which follow describe adding a a JFrame to a Java project in NetBeans.

  1. Right-click the package and select New > JFrame form.
    new-jframe-form-menu-option
    The New JFrame Form screen appears.
  2. Enter a class name such as JFrameHtml5Example.
    new-jframe-form-screen
  3. Click Finish.
  4. The JFrame designer appears in NetBeans.
    new-jframe-in-designer

Add a JLabel and a Help JButton to the JFrame

The JLabel is just an embellishment for this example to place some text on the JFrame. The JButton provides an interface to click to show a help topic.

  1. Drag a JLabel from the palette to the designer. JLabel is located under Swing Controls on the palette and is indicated as Label. This and the next step are not necessary to demonstrate opening help.
  2. From the properties screen for the JLabel, change the text to Flare HTML5 Help Example.
    label-in-jframe-designer
  3. Drag a JButton from the palette to the designer. JButton is located under Swing Controls and is indicated as Button.
  4. From the properties screen for the JButton, change the text to Help.
    button-in-jframe-designer
  5. Double-click the button in the designer. The code for the action event appears in the code editor. Call a yet-to-be-defined function called showHelp and pass a string value of 1.
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            showHelp("1");
        }

     

Add F1 Events

To create F1 behavior, a handler must be added for every control on the form which should cause help to open when F1 is pressed. In this case the only control to worry about is the JButton since it is the only control which can be active. By default, JLabels cannot be activated. A handler is also added to the JFrame itself.

  1. To add a keyPressed event for the JFrame and the JButton, from the properties screen for each, click the ellipses next to keyPressed under Events. The Handlers for keyPressed screen appears. The screen shot shows the screen after the event has been added.
    handlers-for-keypressed-screen
  2. Click Add.
  3. The Add Handler screen appears.
    add-handler-screen
  4. Enter a name such as jFrameKeyPressed and click OK.
  5. The handler code appears in the code editor. Call the yet-to-be-defined showHelp method from the handler for the case where F1 is pressed. The constant value for the F1 keycode in Java is 112. Wrap the call to showHelp in a conditional that checks for that value.
        private void jFrameKeyPressed(java.awt.event.KeyEvent evt) {                                  
            if (evt.getKeyCode() == 112){
                showHelp("1");
            }
        }                                 
    
        private void jButtonKeyPressed(java.awt.event.KeyEvent evt) {                                   
            if (evt.getKeyCode() == 112){
                showHelp("1");
            }
        }

     

Add Methods to Open Help

The technique employed in this example relies on a call to a showHelp method within every event and handler that should cause help to open. The context for the help comes from the ID passed to showHelp. The showHelp method appends the ID to a base URL for the help system. The base URL is the path to the main help file followed by #cshid=. Once the URL is built, it is passed to a method to open a web page.

Stack Overflow: Open a Link in Browser with Java Button

  1. Add methods to open a web page from the Java application. You can paste these into the class for the JFrame.
  2.     public void showHelp(String key){
            URI uri;
            try {
                uri = new URI("http://tregner.com/example-projects/java-app-help/java-app-help.htm#cshid=" + key);
                openWebpage(uri);
            } catch (URISyntaxException ex) {
                Logger.getLogger(JFrameHtml5Example.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        //see: http://stackoverflow.com/questions/10967451/open-a-link-in-browser-with-java-button
        public static void openWebpage(URI uri) {
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                try {
                    desktop.browse(uri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void openWebpage(URL url) {
            try {
                openWebpage(url.toURI());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
  3. If the above code is pasted into the class file and there are not imports for the classes on which the methods depend, the Import Classes screen appears. These are the imports required for the example:
    import java.awt.Desktop;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger

    import-classes-screen

     

  4. Click OK.
  5. Save the project files.

Code the Main Method to Open the JFrame

Open the java file that contains the main method. For this example, the file is JavaAppFlareHtml5Help.java. The main method should create and show an instance of the JFrame. Change the main method to this:

    public static void main(String[] args) {
        JFrameHtml5Example frame = new JFrameHtml5Example();
        frame.setVisible(true);
    }

 

Create a Flare Project to Build the Help

Create a new Flare project with the Empty template. Use HTML5 as the default target.

  1. Select File > New Project. The Start New Project Wizard appears.
  2. For the name, enter java-example.
  3. Click Next.
  4. Under Source, select New from template and select Empty.
  5. Click Next.
  6. For the primary target, select HTML5.
  7. Click Finish.

Add an extra topic named extra-topic.htm and set that as the default topic on the HTML5 target. This ensures the default topic is not the topic opened by the application. That demonstrates the application opens a specific topic and not just the default.

  1. Right-click the Content folder in Content Explorer and select New > Topic. The Add File screen appears.
  2. In the File Name field, enter extra-topic.
  3. Click Add.
  4. Save the topic.
  5. In the Project Organizer within the Targets folder, double-click the HTML5 target to open it in the Target Editor.
  6. On the General tab in the Startup Topic field, select extra-topic.
  7. Save the target.

Set the topic alias. Open the alias file and assign Topic.htm to the identifier in the alias file. The header file will update when the alias file is saved.

  1. From the Project Organizer within the Advanced > CSH folder, double-click the AliasFile.flali file. The alias file appears in an editor.
  2. In the alias file editor, navigate to the Topic.htm file and right-click the file.
  3. From the menu, select Assign topic to selected identifier.
  4. Save the alias file.
  5. In the Target Editor for the HTML5 target on the General tab, change the name of the output file to java-app-help.
  6. In the Target Editor for the HTML5 target on the General tab, change the Startup Topic to extra-topic.htm.

MadCap Software: Creating and Assigning Identifiers

A handful of changes were made to the template for this example. Some of the changes are shown in the screen shot that follows. The order of items in the screen shot is:

  • Project Organizer
  • Content Explorer
  • Topic.htm in XML Editor
  • HeaderFile.h in Header Editor
  • AliasFile.flali in Alias Editor

everything-for-hello-world-csh

Test the Application and Help

Build the target an place the output somewhere. Adjust the path in the showHelp method to reflect the location of the help files.

    public void showHelp(String key){
        URI uri;
        try {
            uri = new URI("http://tregner.com/example-projects/java-app-help/java-app-help.htm#cshid=" + key);
            openWebpage(uri);
        } catch (URISyntaxException ex) {
            Logger.getLogger(JFrameHtml5Example.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

To run the program from the IDE, with the project open and active, select Run > Run Project.

Downloads:

Leave a comment

Your email address will not be published.

HTML tags are not allowed.

251,110 Spambots Blocked by Simple Comments