Use Context Menus in A Web Dynpro For Java Application
Use Context Menus in A Web Dynpro For Java Application
Applies to:
Web Dynpro for Java 7.11. For more information, visit the Web Dynpro Java homepage.
Summary
This tutorial explains the Web Dynpro context menu programming model and demonstrates how to
implement context menus in a file explorer application.
Author(s): Web Dynpro Java Team
Company: SAP AG
Created on: 29 June 2010
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 1
How to Use Context Menus in a Web Dynpro for Java Application
Table of Contents
Introduction ................................................................................................................................................... 3
Prerequisites ................................................................................................................................................. 4
Systems, Installed Applications, and Authorizations ................................................................................... 4
Objectives ..................................................................................................................................................... 4
The Tutorial Scenario: A File Browser ........................................................................................................... 4
Column Display Context Menu ................................................................................................................... 5
Navigation and Sorting Context Menu ........................................................................................................ 6
File-Specific Context Menu ........................................................................................................................ 6
Assigning Context Menus to Screen Areas .................................................................................................... 7
Creating Context Menus and Assigning Actions........................................................................................... 10
Modifying Context Menus at Runtime .......................................................................................................... 15
Tutorial Result ............................................................................................................................................. 17
Text Symbols........................................................................................................................................... 18
Copyright .................................................................................................................................................... 19
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 2
How to Use Context Menus in a Web Dynpro for Java Application
Introduction
In this tutorial you will learn how to implement context menus in a Web Dynpro application. This wil include
learning how to create context menus in the View Designer and how to assign them to areas of your screen.
You will also learn how to use the view controller method wdOnContextMenu to create and modify context
menu entries programmatically.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 3
How to Use Context Menus in a Web Dynpro for Java Application
Prerequisites
Objectives
After working through this tutorial you should be able to:
Understand the programming model for context menus in Web Dynpro
Use the NWDS View Designer to create context menus and assign them to view areas
Add code to the view controller hook method wdOnContextMenu() to create and modify context menu
entries.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 4
How to Use Context Menus in a Web Dynpro for Java Application
Initially, the root directory of the server’s file system is displayed. Let’s have a look at the context menus
available in this application.
Check/uncheck the entries in the context menu and note the changes in the table.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 5
How to Use Context Menus in a Web Dynpro for Java Application
The Up entry is disabled if the folder currently displayed has no parent folder.
When you select the menu entry, the files in the target directory are displayed in the table.
Navigate to any directory containing text files, for example, log files with the extension “.log” (for the exact list
of file types, see the standard expression PATTERN_TEXT_FILE in the view controller implementation).
Do a right-click on such a file. An additional context menu entry for opening a preview of the file appears:
When you select the Preview <filename> entry, a popup window is opened and the content of the file is
displayed.
Note that the context menu entry appears disabled for large files (more than 512 Kbytes):
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 6
How to Use Context Menus in a Web Dynpro for Java Application
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 7
How to Use Context Menus in a Web Dynpr o for Java Application
ColumnDisplayMenu
DirectoryEntryMenu
DirectoryMenu
To define the context menu assignments correctly, it is important to understand the hierarchy of the Web
Dynpro layout for this screen:
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 8
How to Use Context Menus in a Web Dynpro for Java Application
First, the “DirectoryMenu” context menu is assigned to the “DirectoryTable” UI element. The effect is that this
context menu appears everywhere inside the table (a consequence of the default value INHERIT for the
context menu behavior property).
We assign:
DirectoryTable => (PROVIDE, “DirectoryMenu”)
Next, we want the “ColumnDisplayMenu” context menu to appear above every column header. We define:
*Column => (PROVIDE, “ColumnDisplayMenu”)
As a result, this context menu appears inside the complete table header (not only on the column header
text).
The other consequence is that the “ColumnDisplayMenu” now also appears on the cell editors of the table
columns (but not in the free area inside the table body). To display the “DirectoryMenu” on the cell editors
instead, we define:
*Editor => (PROVIDE, “DirectoryMenu”)
For the editor of the Name column, we want the DirectoryEntryMenu to appear. Therefore, we define:
FileNameEditor => (PROVIDE, “DirectoryEntryMenu”)
This should give you the idea behind the properties contextMenuId and contextMenuBehaviour.
These assignments can also be done at runtime using the UI element API. We will not describe the creation
and assignment of context menus through the API in this tutorial. We will, however, describe how to modify
context menus defined and assigned at design-time using the runtime API.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 9
How to Use Context Menus in a Web Dynpro for Java Application
It should now be clear how the definitions above are created using the View Designer. Note that you can
select multiple UI elements in the Outline view and set common property values for all of them in one go:
A context menu created in this way is available in the drop-down list for setting the contextMenuId
property:
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 10
How to Use Context Menus in a Web Dynpro for Java Application
You can also use the “Create” button shown above. In this case, a new context menu with a generic ID such
as “Context Menu1” is created (you should change the ID to something more appropriate afterwards.)
The creation of context menu items works exactly as for menus created in the view layout. The same set of
menu items (MenuActionItem, MenuCheckBox. MenuRadioButton, MenuSeparator, Menu) is
available for context menus as well:
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 11
How to Use Context Menus in a Web Dynpro for Java Application
Let’s have a look at the action for opening a directory. This action has to “know” from which table row it was
triggered (through the context menu above that row). Therefore, we add a parameter filesElement of type
IFilesElement to the action (IFilesElement is the runtime type of the elements of the table’s data
source node Files):
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 12
How to Use Context Menus in a Web Dynpro for Java Application
If you have mapped the context node from the component controller, make sure that you select the
IFilesElement type from the view controller here.
For the OpenDirectoryItem menu item we define an event parameter mapping from the (implicit)
nodeElement event parameter to the filesElement action parameter:
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 13
How to Use Context Menus in a Web Dynpro for Java Application
This parameter mapping has the effect that the action parameter “filesElement” will always contain the
context element that represents the table row above which the context menu has been opened. (You cannot
use the lead selection of the table because opening a context menu does not change the lead selection.) Comment [D2]: It is important. May bei t
makes sense to stress this point?
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 14
How to Use Context Menus in a Web Dynpro for Java Application
You achieve this by adding the following code to the wdOnContextMenu() view controller method that is
called just before the context menu is opened:
The code first gets a runtime reference to the context menu “DirectoryEntryMenu” that has been defined at
design time. It then checks whether the current context menu request would open this context menu. If this is
the case, a method for modifying the context menu is called:
item.mappingOfOnAction().addSourceMapping(IWDMenuActionItem.IWDOnAction.NODE_ELEMENT,
"row");
contextMenu.addItem(item, 0);
}
item.setVisible(true);
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 15
How to Use Context Menus in a Web Dynpro for Java Application
This method first checks whether the “Preview” menu entry has already been created and added to the
context menu. If the file that belongs to the table entry where the context menu has been opened can be
previewed, the menu entry is made visible (and created if it does not yet exist). If the file is too large to be
previewed, the menu entry is disabled and its text is changed.
Of course, it would have been simpler to just define the “Preview” entry already at design time and change its
properties using data binding, but we want to illustrate that you still have full control over the context menu
before it is opened.
You can also create or delete context menus from within this hook method using the
IWDContextMenuManager API.. This API also allows you to change the assignment of context menus
dynamically (see method setCurrentContextMenu()). This could be used to switch between a set of
context menus defined at design time (depending on some condition known only at runtime) or to suppress a
context menu at runtime (by setting the current context menu to null).
Finally let us have a look at the parameters of the predefined hook method wdOnContextMenu():
public final void wdOnContextMenu
(
final com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDContextMenuManager
contextMenuManager,
final com.sap.tc.webdynpro.progmodel.api.IWDContextMenuEvent event
)
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 16
How to Use Context Menus in a Web Dynpro for Java Application
The context element that represents the item above which the context menu has been opened. This is
useful for all data-driven UI elements like tables and trees where the rows and nodes are defined by the
context elements of their data source node.
Tutorial Result
We have demonstrated how to define and assign context menus at design time and how to access a context
menu at runtime in the wdOnContextMenu()view controller method that is called just before the context
menu is opened. You have learned how to modify a context menu at runtime depending on certain conditions
about the object for which it has been opened.
The Web Dynpro programming model provides a declarative method for defining context menus and
assigning them to screen areas together with an API for creating, modifying, and assigning context menus at
runtime.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 17
How to Use Context Menus in a Web Dynpro for Java Application
Text Symbols
Symbol Usage
Note
Recommendation
Warning
See also
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 18
How to Use Context Menus in a Web Dynpro for Java Application
Copyright
© Copyright 2010 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG.
The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vend ors.
Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9 ,
iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Serv er,
PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes,
BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX,
Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registe red trademarks of Adobe Systems
Incorporated in the United States and/or other countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of
Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts
Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by
Netscape.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned
herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.
Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and
other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered
trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company.
All other product and service names mentioned are the trademarks of their respective companies. Data contained in this docume nt
serves informational purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP
Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable fo r errors or
omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the
express warranty statements accompanying such products and services, if any. Nothing herein should be construed as const ituting an
additional warranty.
SAP COMMUNITY NETW ORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 19