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:
- Change View
- View Special Reports
To update the application menu:
- 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.
- 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.
- Select m_menubar. Notice that the UI Editor is refreshed
to allow menu editing.
- In the UI Editor, choose Tools. Notice that the menu items
appear.
- Create the Change View menu:
- In the box below the Insert Calculation menu item, enter
Change
View...
.
- Right-click Change View... and choose Insert
Submenu to start building the submenu items.
- In the box to the right of the Change Views menu item, enter
Graph
.
- Select Graph in the UI Editor.
- In the Property Inspector pane, scroll down the list of properties, and
enter
m_mnuGraph
as the value for the name
property.
- In the UI Editor, enter
Crosstab
in the box below the Graph
submenu item.
- Select Crosstab in the UI Editor.
- In the Property Inspector pane, enter
m_mnuCrosstab
as the
value for the name property.
- Create the View Special Reports menu:
- In the UI Editor, click the box below the Change View menu item and enter
View Special Reports...
in the box.
- Right-click View Special Reports... and choose Insert
Submenu to add the submenu item.
- In the box to the right of the View Special Reports menu item, enter
Asian
Sales Variance
.
- 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."
- In the Sytem-Navigator pane, select BIApplication1.java
and, from the File menu, choose Save All.
- To run the application, right-click BIApplication1.java
and choose Run BIApplication1.java.
- 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.
- 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.
- Right-click BIApplication1.java and choose Code Editor from the popup menu.
- Scroll down in the Code Editor and position the cursor before the last right
bracket (}) in the code.
- 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;
}
}
- 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.
- 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:
- Add an action listener to change the view type to a graph:
- Right-click BIApplication1.java and choose
UI Editor from the popup menu. In the Structure
pane, drill down Menu.
- Drill down m_menubar, then m_mnuTools
"Tools", and finally jMenu1 "Change
View ...".
- Select m_mnuGraph "Graph". In the
Property Inspector pane, select the Events tab that appears at the bottom.
- Choose the "..." button that appears at the right of the ActionPerformed
value.
- Enter
m_mnuGraph_ActionPerformed
as the name and choose OK.
- 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.
- Add an action listener to change the view type to a crosstab:
- Right-click BIApplication1.java and choose
UI Editor to return to the UI Editor window.
- In the Structure pane, select m_mnuCrosstab "Crosstab".
In the Property Inspector, select the Events tab that appears at the bottom
of the pane.
- Choose the "..." button that appears at the right of the ActionPerformed
value.
- Enter
m_mnuCrosstab_ActionPerformed
as the name and choose
OK.
- 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.
- Add an action listener to display the Asian Sales Variance report:
- Right-click BIApplication1.java and choose
UI Editor to return to the UI Editor window.
- In the Structure pane, expand jMenu2 "View Special
Reports..." and select jMenuItem1 "Asian
Sales Variance".
- In the Property Inspector, select the Events tab that appears at the bottom.
- Choose the "..." button that apppears at the right of the ActionPerformed
value.
- Accept the default name (jMenuItem1_actionPerformed) and choose OK.
- 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.
- From the File menu, choose Save to save your
work.
- To run the application, right-click BIApplication1.java
and choose Run BIApplication1.java from the menu.
- 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.
- From the Tools menu, choose View Special Reports
then Asian Sales Variance to open the saved graph.
- From the Tools menu, choose Change View then
Crosstab to change the graph display to a crosstab
display.
- 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.