2. Working with Presentations

Exercise Objectives

Learn how to customize application code that is generated by the BI Java-Client Class wizard. The customizations will enable a user to change the type of view that is displayed and to access a saved report directly.

Exercise Description

In this exercise, you will examine the Java source code that was generated by the BI Java-Client Class wizard and learn to customize that code to add additional features to your application. In particular, you will add new menu items that perform the following actions: toggle the displayed presentation between a crosstab and a graph, and invoke a saved report.

Exercise Tasks

Adding menus to the application

You will add the following menu items (with submenus) to the Tools menu in the application:

To update the application menu:
  1. In the System - Navigator pane of JDeveloper, right-click BIApplication1.java and choose UI Editor from the popup menu. Ignore any "Unable to initialize live instance for ..." messages that may appear in the UI Editor Log.
  2. If the Structure pane is not open, then press Ctrl+Shift+S to open it. In the Structure pane, click the plus ('+') sign to the left of Menu to expand Menu.
  3. Select m_menubar. Notice that the UI Editor is refreshed to allow menu editing.
  4. In the UI Editor, choose Tools. Notice that the menu items appear.
  5. Create the Change View menu:

    1. In the box below the Insert Calculation menu item, enter Change View....
    2. Right-click Change View... and choose Insert Submenu to start building the submenu items.
    3. In the box to the right of the Change Views menu item, enter Graph.
    4. Select Graph in the UI Editor.
    5. In the Property Inspector pane, scroll down the list of properties, and enter m_mnuGraph as the value for the name property.
    6. In the UI Editor, enter Crosstab in the box below the Graph submenu item.
    7. Select Crosstab in the UI Editor.
    8. In the Property Inspector pane, enter m_mnuCrosstab as the value for the name property.

  6. Create the View Special Reports menu:

    1. In the UI Editor, click the box below the Change View menu item and enter View Special Reports... in the box.
    2. Right-click View Special Reports... and choose Insert Submenu to add the submenu item.
    3. In the box to the right of the View Special Reports menu item, enter Asian Sales Variance.

  7. In the System-Navigator pane, right-click BIApplication1.java and choose Make to compile the code. The debug window at the bottom of the JDeveloper screen should indicate "Successful compilation: 0 errors, 0 warnings."
  8. In the Sytem-Navigator pane, select BIApplication1.java and, from the File menu, choose Save All.
  9. To run the application, right-click BIApplication1.java and choose Run BIApplication1.java.
  10. After the application starts, notice the new menu items that have been added to the menu bar. From the Tools menu, if you choose Change View, a menu tree is displayed with the submenu items. None of the menu items do anything yet because you have not defined action listeners to trigger events. You will add this feature in the next section.
  11. From the File menu, choose Exit to close the running Java application and return to JDeveloper.

Adding custom code for changing views

In this section, you add the code that enables the application to change the view type to a crosstab or a graph.

  1. Right-click BIApplication1.java and choose Code Editor from the popup menu.
  2. Scroll down in the Code Editor and position the cursor before the last right bracket (}) in the code.
  3. Read the following code thoroughly, and then copy and paste this code into the Code Editor at the cursor location that was defined in Step 2:

    // Define stored Dataview for graphs
    Graph m_GraphView = null;

    // Define stored Dataview for crosstabs
    Crosstab m_CrosstabView = null;

    // Add static constants to represent selected view types
    static final int VIEW_TYPE_GRAPH = 1;
    static final int VIEW_TYPE_CROSSTAB = 2;
    static final int VIEW_TYPE_DUAL= 3; // to be used in Exercise 4

    // Add a constant that indicates whether a dual view is displayed, for use in Exercise 4
    public boolean m_DualView = false;

    // setViewType() sets the type of view for the display of the presentation
    public void setViewType(int vt) {

      //Based on the type of view that you are switching to
      switch (vt) {

        case VIEW_TYPE_GRAPH:

          // Add the Graph to the cache if the cache is empty
          if (m_GraphView == null ) {

            // Create the Graph and set paging control visible
            Graph graph = new Graph();
            graph.setPagingControlVisible(true);

            // Add to the Graph Cache
            m_GraphView = graph;
          }

          // Use the data source of the Active View
          m_GraphView.setDataSource(m_activeView.getDataSource());

          // Display the Graph
          displayView(null, m_GraphView);

          break;

        case VIEW_TYPE_CROSSTAB:

          // Add the Crosstab to the cache if the cache is empty
          if (m_CrosstabView == null ) {

            // Create the Crosstab and set paging control visible
            Crosstab crosstab = new Crosstab();
            crosstab.setPagingControlVisible(true);

            // Add to the Crosstab Cache
            m_CrosstabView = crosstab;
          }

          // Use the data source of the Active View
          m_CrosstabView.setDataSource(m_activeView.getDataSource());

          // Display the Crosstab
          displayView(null, m_CrosstabView);

          break;
      }
    }

  4. In the System-Navigator pane, right-click BIApplication1.java and choose Make to compile the code. The debug window at the base of the JDeveloper screen should indicate "Successful compilation: 0 errors, 0 warnings." If the debug window suggests that errors are present, click the lines that are indicated in the debug window and JDeveloper will move to the line where the error is encountered. When debugging, first ensure that the code has been copied and pasted correctly and inserted at the correct location in the Code Editor.
  5. From the File menu, choose Save All to save the current application.

Adding event listeners to the menu options

After adding the action code that changes view type, you are ready to add event listeners to the menu items. These event listeners are executed when a user selects a menu item.

To add Action Listeners for menu items:

  1. Add an action listener to change the view type to a graph:

    1. Right-click BIApplication1.java and choose UI Editor from the popup menu. In the Structure pane, drill down Menu.
    2. Drill down m_menubar, then m_mnuTools "Tools", and finally jMenu1 "Change View ...".
    3. Select m_mnuGraph "Graph". In the Property Inspector pane, select the Events tab that appears at the bottom.
    4. Choose the "..." button that appears at the right of the ActionPerformed value.
    5. Enter m_mnuGraph_ActionPerformed as the name and choose OK.
    6. In the open Code Editor, enter the following line as the body of the m_mnuGraph_ActionPerformed method:

      setViewType(VIEW_TYPE_GRAPH);


      This code invokes the setViewType method with parameter VIEW_TYPE_GRAPH, which changes the view to a graph.

  2. Add an action listener to change the view type to a crosstab:

    1. Right-click BIApplication1.java and choose UI Editor to return to the UI Editor window.
    2. In the Structure pane, select m_mnuCrosstab "Crosstab". In the Property Inspector, select the Events tab that appears at the bottom of the pane.
    3. Choose the "..." button that appears at the right of the ActionPerformed value.
    4. Enter m_mnuCrosstab_ActionPerformed as the name and choose OK.
    5. In the open Code Editor, enter the following line as the body of the m_mnuCrosstab_ActionPerformed method:

      setViewType(VIEW_TYPE_CROSSTAB);

      This code invokes the setViewType method with parameter VIEW_TYPE_CROSSTAB, which changes the view to a crosstab.

  3. Add an action listener to display the Asian Sales Variance report:

    1. Right-click BIApplication1.java and choose UI Editor to return to the UI Editor window.
    2. In the Structure pane, expand jMenu2 "View Special Reports..." and select jMenuItem1 "Asian Sales Variance".
    3. In the Property Inspector, select the Events tab that appears at the bottom.
    4. Choose the "..." button that apppears at the right of the ActionPerformed value.
    5. Accept the default name (jMenuItem1_actionPerformed) and choose OK.
    6. In the open Code Editor, enter the following line as the body of the jMenuItem1_actionPerformed method:

      // Define the presentation that is to be displayed.
      //Notice that this is the name of the presentation that you saved earlier.
      String strPath = "Sales Variance";

      // Open the presentation as a Dataview.
      Dataview dv = openView(strPath);

      // Display the view if it exists
      if ( dv != null ){
        displayView(strPath, dv);
      }
      else {
        String strMsg = "Unable to open view " + strPath; showErrorMessage(strMsg, null);
      }

      When the menu item is selected, this code opens the Sales Variance graph that you saved earlier.

  4. From the File menu, choose Save to save your work.
  5. To run the application, right-click BIApplication1.java and choose Run BIApplication1.java from the menu.
  6. Now, with the application running and the stoplight report displayed, from the Tools menu, choose Change View then Graph. The crosstab changes to a graph.
  7. From the Tools menu, choose View Special Reports then Asian Sales Variance to open the saved graph.
  8. From the Tools menu, choose Change View then Crosstab to change the graph display to a crosstab display.
  9. From the File menu, choose Exit to close the running Java application and to return to JDeveloper.

Exercise Summary

In this exercise, you learned how to add custom application logic to enable an end user to change the view type for a presentation.

1. Creating a BI Java-Client Application | Overview | 3. Customizing BI Objects