You are on page 1of 16

How to Use Advanced Features of

the Web Dynpro Context

Applies to:
Web Dynpro for Java 7.11. For more information, visit the Web Dynpro Java homepage.

Summary
In this tutorial you will learn about two new features of the Web Dynpro Java Context:
 Supplying data to UI element properties from context attributes declared using Java Native Types
 The IWDMultiSelection API belonging to a context node.
Author: Web Dynpro Java Team
Company: SAP AG
Created on: 29 June 2010

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 1

......................................................................................... How to Use Advanced Features of the Web Dynpro Context Table of Contents Introduction ...............................................................................sdn..................................................................... 11 Tutorial Summary...com © 2010 SAP AG 2 .......................................uac.. 15 Copyright ............................................................................................................................................................................. 5 Java Native Types Demo .........................................................com | BOC .............................................................................................................................................sap.....sap....................................................... 4 The Tutorial Scenario: A Simple Demo ...............................................com | UAC .......bpx...................................................................................... 4 Prerequisites ..................................................................sap.............................sap.. 6 Using a Context Node’s IWDMultiSelection API ........................................com | BPX ................................ 16 SAP COMMUNITY NETWORK SDN ....................boc...... 4 Objectives ..............

sap.com | BOC .com | BPX .com © 2010 SAP AG 3 .boc. Therefore.sdn. its name will be built into various generated Java class names. the declared names seen in method signatures have been substituted for Unix shell script style variable names as follows: ${nc} Name of the component controller ${nv} Name of the view controller ${cn} Context node name ${ca} Context attribute name SAP COMMUNITY NETWORK SDN . How to Use Advanced Features of the Web Dynpro Context Abbreviations Used Any time a software entity is declared at design time. to ensure that the coding examples seen in this document remain generic.uac.sap.sap.sap.com | UAC .bpx.

com | BOC .sap.bpx. Required Units of Software Software Component: HM-WDUIDMKTCNT Development Component: tc/wd/tut/prog/cntxt The exact steps required to install the Software Component are described in a separate document. SAP COMMUNITY NETWORK SDN . Prerequisites The NetWeaver Developer Studio (Version 7.sdn.11 or later) should be installed and connected to an SAP Java Application Server of the same version or higher. How to Use Advanced Features of the Web Dynpro Context Introduction In this tutorial you will learn about two new features of the Web Dynpro Java Context:  Supplying data to UI element properties from context attributes declared using Java Native Types  The IWDMultiSelection API belonging to a context node.boc.sap.com | BPX . Objectives After working through this tutorial you will be able to:  Bind context attribute defined using Java native types to UI element properties . This will allow you to compile and deploy the tutorial application.sap.  Use the new IWDMultiSelection API to process context nodes containing multiple element selections.uac.com © 2010 SAP AG 4 .sap.com | UAC .

bpx.sap.com | UAC . a screen similar to the one seen below will appear: The application demonstrates two different features simultaneously. At the top of the screen is the Java Native Types demo.com | BOC .sap. SAP COMMUNITY NETWORK SDN . How to Use Advanced Features of the Web Dynpro Context The Tutorial Scenario: A Simple Demo When you start the tutorial application.com © 2010 SAP AG 5 . and below it is the demo of the IWDMultiSelection API.com | BPX .sap.sdn.sap.boc.uac.

sap.sap. is entirely responsible for ensuring that the value so returned is both suitable for consumption by the UI element property.sap. Caveat Confector!1 The NetWeaver Developer Studio cannot possibly predict whether the Java native type’s toString() method will return a value suitable for binding to a UI element property! The developer therefore.com © 2010 SAP AG 6 . then the UI element to which this context attribute is bound will always be set to “read only”. How to Use Advanced Features of the Web Dynpro Context Java Native Types Demo This demo illustrates the fact that it is now possible to bind context attributes defined using Java native types to string-type properties of UI elements. The two Group UI elements seen on the screen shot above illustrate what can be done with the toString() method of various Java native types.uac. any Java native type that implements a toString() method can be used to supply a value to a string type UI element property. Stated simply. This cannot be overridden declaratively. and also how a composite Java native type can be unloaded and displayed directly.sap. On the left side of the screen.com | UAC .sdn. and useful for display to the end user! 1 Latin for “Developer Beware” SAP COMMUNITY NETWORK SDN . If such a write action is not possible. The Web Dynpro Framework determines whether it is possible to write a value received from the UI back into the Java native type. all the InputField UI elements are set to read only.com | BOC .com | BPX .bpx.boc. This happens automatically in spite of the fact that the readOnly property of the InputField UI element is set to false. notice that apart from the “Java String” field.

As you can see.bpx.sap. SAP COMMUNITY NETWORK SDN .lang.uac.Runtime.lang.boc. the context attribute Runtime is of type java.Runtime object. you can see that in spite of this limitation.sap.sap.lang.sap.com | BPX . Therefore. However. writing such a value back to this particular Java native type is both simple and type safe. the value returned by the Class Loader’s toString() method has been included to illustrate that not all values returned by a toString() method are useful! The memory consumption values come from the java.sdn. In the screen shot of the component controller’s context below. its data type will always be String.com | BOC .String context attribute is open for input because whatever the user enters from the browser. calling this object’s toString() method directly would not produce a particularly useful value. However.com | UAC .com © 2010 SAP AG 7 . The InputField UI element bound to the java. How to Use Advanced Features of the Web Dynpro Context The values displayed here are simply the result returned by calling the Java native class’ toString() method. most of the values are meaningful to the end user.

public void wdDoInit() { //@@begin wdDoInit() Thread t = Thread.sap..sql. thisEl.setClassLoader(Thread.setThreadInfo(ManagementFactory.com | BPX .setJavaSqlDate(new java.sap.sap.getContextClassLoader()).getDefault().currentJavaNativeTypesElement(). IJavaNativeTypesElement thisEl = wdContext. thisEl.set(0x7). How to Use Advanced Features of the Web Dynpro Context The JavaNativeTypes context node has a cardinality of 1.set(0x1). getThreadMXBean(). thisEl. BitSet bs = new BitSet(8).setLocale(l).String"). bs.com | UAC .set(0x9).setRuntime(Runtime.setJavaString("java.setCurrency(Currency.set(0x5).currentThread().com © 2010 SAP AG 8 .MAX_VALUE)). 0)).set(0x3).currentTimeMillis())).sdn. thisEl.getInstance(l)).setBigDecimal(BigDecimal. thisEl. Locale l = Locale.currentTimeMillis())).getThreadInfo(t.bpx. //@@end } SAP COMMUNITY NETWORK SDN . thisEl. thisEl. bs.uac. thisEl. bs. thisEl. thisEl. bs.setBitSet(bs). thisEl.currentThread(). bs.getId(). bs.StringBuilder")).1 and the initial values of its attributes are defined in the wdDoInit() method of the component controller.lang.com | BOC .set(0xF).boc.lang.getRuntime()).Date(System.valueOf(Integer.setJavaStringBuilder(new StringBuilder("java. bs.setJavaUtilDate(new Date(System. thisEl.setBoolean(new Boolean(true)).set(0xB).sap.set(0xD). bs.

a reference to the empty node ${cn} that needs to be populated.com © 2010 SAP AG 9 . Instead. ${cn} = JavaNativeTypes and ${ca} = FreeMemory.boc.sap. public java. but cannot be seen simply by calling the class’ toString() method. SAP COMMUNITY NETWORK SDN .com | BPX .1. one for each memory value.2 In our case this is a reference to the one and only element in node JavaNativeTypes and a reference to the empty UnloadedThreadInfo node. 2 A word of caution about coding supply functions! When the node parameter is received by the supply function. its element collection will be null.ThreadInfo object contains multiple values that are individually meaningful for display. In this case. A java. It is local to the view controller. there is no particular need to duplicate their values as individual attributes in the context of the component controller. it makes more sense to use a supply function to unload this object into a context node of cardinality 1.management. This is why in the screen shot of the view controller’s context you can see a child context node called UnloadedThreadInfo.lang.lang. The coding for the getter method is consequently very simple.freeMemory() / 1024. The getter methods for these calculated attributes then fetch the required value from the java. Secondly. The expanded version of this node is shown to the left.Long get${cn}${ca}(IPrivate${nv}. notice that none of the attributes are calculated. this particular node is not mapped. that although this is the context of the view controller.com | UAC . How to Use Advanced Features of the Web Dynpro Context Since the desired memory consumption values are only needed for display purposes. Since there are many values of potential interest here.sap.Runtime object.I${cn}Element element) { //@@begin get${cn}${ca} return element.lang.com | BOC . You are entirely responsible for ensuring that when the supply function terminates (for whatever reason) the node collection contains sufficient elements that its cardinality constraint is not violated.sdn.sap.bpx.. The first is a reference to ${cn}’s parent element and the second. when a supply function is defined for node ${cn}. //@@end } A slightly more complex implementation can be seen with the ThreadInfo context attribute. Notice firstly. the context of the view controller contains three calculated attributes. This may or may not violate the node’s cardinality constraint.sap.uac. This is because we will populate the node by defining a supply function. In general. the proposed method name will be supply${cn}() and it will always receive two parameters. Failure to check this will cause an exception to be thrown in Web Dynpro Framework after the supply function has completed.getRuntime().

setWaitedTime(parentElement.getThreadState().getThreadInfo().boc.sdn.getThreadInfo().com © 2010 SAP AG 10 . //@@end } It is important to notice here that all these InputFields are open for input.com | BPX .setSuspended(parentElement. Then using the normal process of data binding. el. el. el.IUnloadedThreadInfoNode node.getThreadInfo().sap.IWDNodeElement) IUnloadedThreadInfoElement el = node.IJavaNativeTypesElement parentElement) { //@@begin supplyUnloadedThreadInfo(IWDNode.getThreadId()). el. How to Use Advanced Features of the Web Dynpro Context The coding in the supply function then simply unloads the required values from the ThreadInfo attribute found in parameter parentElement and populates the relevant context attributes.getBlockedTime()).setLockName(parentElement.getThreadInfo().setLockOwnerId(parentElement.getThreadInfo().sap.getThreadInfo(). el.getThreadInfo().getWaitedTime()).setThreadId(parentElement.getThreadInfo().com | UAC .toString()).isSuspended()).uac. el.getThreadInfo().getLockName()). el.setBlockedCount(parentElement.setBlockedTime(parentElement. el. el.setLockOwnerName(parentElement. el. the Java native types holding the unloaded ThreadInfo data are used to supply data to InputField UI elements. el.createAndAddUnloadedThreadInfoElement().getLockOwnerName()). This is because the Java native types bound to these UI elements are either strings or class representations of Java primitives.getThreadInfo().setInNative(parentElement.setWaitedCount(parentElement.getLockOwnerId()). IPrivate${nv}. SAP COMMUNITY NETWORK SDN .setThreadState(parentElement.getWaitedCount()). public void supplyUnloadedThreadInfo( IPrivate${nv}. it is possible (though probably quite difficult) to parse the value received from the client and write it back to the Java native type.getThreadInfo().getThreadName()).getBlockedCount()).isInNative()).sap.sap.setThreadName(parentElement.bpx.com | BOC .getThreadInfo(). el. Therefore.

isMultiSelected(i). That coding effort is now no longer necessary with the new IWDMultiSelection API returned by IWDNode.n  Selection Cardinality 0.sap.. In the screen shot below. it was necessary to iterate over the entire node collection testing the flag returned by IWDNode. The structure is very simple.n or 1.com © 2010 SAP AG 11 . How to Use Advanced Features of the Web Dynpro Context Using a Context Node’s IWDMultiSelection API When multiple elements within a context node are selected (i.sap. you can see two Group UI elements. The context node has the following properties:  Collection Cardinality 0.com | BOC ..sap.sdn. SAP COMMUNITY NETWORK SDN . Component Controller Configuration and Coding In the component controller.. the context node has a selection cardinality of 0. then formerly.bpx.n). The group on the left is a simple table containing 1000 rows and 2 columns.n  Initialise Lead Selection false  Supply Function supplyMultiSelect The ArabicNumber attribute is simply an integer.com | BPX .e. The group on the right shows some properties of IWDMultiSelection and the Table UI element holds the contents of IWDMultiSelection. and the RomanNumber attribute is a calculated attribute of type string.boc..sap.uac. Column 1 holds the first 1000 integers and column 2 shows the Roman representation of that integer.com | UAC . the context node that supplies the number data is called MultiSelect.getMultiSelection().iterateIndexes().

"D"). this. new RomanValue(50.append(equiv.createAndAddMultiSelectElement().int2roman(element. //@@end } The Roman class simply provides a static method for converting an Arabic number to a Roman number: public static class Roman { final static RomanValue[] ROMAN_VALUE_TABLE = { new RomanValue(1000. new RomanValue(9. "IX"). i<=1000. if (n >= 4000 || n < 1) result. String rom) { this. } return result. public void supply${cn} (IPrivate${nc}. The getter method for the RomanNumber attribute is shown here where ${cn} = MultiSelect and ${ca} = RomanNumber.toString(). How to Use Advanced Features of the Web Dynpro Context The easiest way to populate this context node is to use a supply function that populates nothing more than the ArabicNumber attribute. "V").com | BPX . "X"). new RomanValue(90.append("Numbers must be in the range 1 to 3999").getArabicNumber()). "XL").bpx.uac. public java. } } } SAP COMMUNITY NETWORK SDN .lang. new RomanValue(4.setArabicNumber(i). new RomanValue(5. //@@end } The RomanNumber attribute will be calculated on demand (typically when the value is required for the table UI element at screen rendering time).IContextElement parentElement) { //@@begin supply${cn}(IWDNode.com | BOC . RomanValue(int dec. } private static class RomanValue { int intVal. String romVal. new RomanValue(40.sdn. "C").sap. new RomanValue(500. "M").IWDNodeElement) for (int i=1.I${cn} node.boc.com © 2010 SAP AG 12 .romVal). i++) node. "IV"). "CM").com | UAC .sap. new RomanValue(1.sap. new RomanValue(100.intVal. "I") }. "L"). public static String int2roman(int n) { StringBuffer result = new StringBuffer().intVal = dec.romVal = rom. This coding is very simple.I${cn}Element element) { //@@begin get${cn}${ca} return Roman. new RomanValue(400. new RomanValue(10. IPrivate${nv}. "XC"). new RomanValue(900. result.sap. "CD").String get${cn}${ca}(IPrivate${nc}. else for (RomanValue equiv : ROMAN_VALUE_TABLE) while (n >= equiv.intVal) { n -= equiv.

unless the data in this node is flagged as dirty. The value of the Roman number is then built up by concatenating the Roman numeral representation of the subtracted number.sap. //@@end } //@@begin others IWDMessageManager msgMgr. //@@end This supply function is very similar to the one in the component controller for node MultiSelect. this data exists only in the view controller.IContextElement parentElement) { //@@begin supply${cn}(IWDNode. When the Arabic number has been reduced to zero.createAndAddMultiSelectedElement().getMessageManager(). the getter method will typically only be called by the Web Dynpro Framework when the Table UI element needs to be rendered. if no elements are selected then a message is produced to this effect. How to Use Advanced Features of the Web Dynpro Context The conversion uses iterative subtraction to remove the next highest value (as represented by the values in the ROMAN_VALUE_TABLE array) from the Arabic number. Therefore. The only problem here is that once the supply function for node MultiSelected has run once.com | BOC . This can be done by creating a context node called MultiSelected that has a structure identical to MultiSelect. Therefore. SAP COMMUNITY NETWORK SDN . a Table UI element is used to display the Arabic and Roman numbers. Since the context attribute holding the Roman number is a calculated attribute.IWDNodeElement) // This supply function will only be called if node MultiSelected is flagged as // "dirty". then the Web Dynpro Framework will never rerun the supply function.uac. this is a local context node. The MultiSelect context node is then consumed by the view controller as a mapped node. they should be replicated in node MultiSelected. View Controller Configuration and Coding In the view controller.nodeMultiSelect().com | UAC . Also.sap. however. getMultiSelection().sap. This would make the data in node MultiSelected invalid.iterateIndexes().sap.com © 2010 SAP AG 13 . the conversion process is complete. we need to show how many elements have been selected within the context node MultiSelect.next().boc.hasNext()) msgMgr.reportSuccess("No rows selected yet").hasNext()) node. if (!it. IPrivate${nv}. //@@end } public void supply${cn}(IPrivate${nv}. However. setArabicNumber(it. else while (it. or it has been explicitly invalidated Iterator<Integer> it = wdContext. not a mapped not. The key difference here however is that we are populating the node using the iterator supplied by the IWDMultiSelection API.bpx. but this time we are only concerned about the selected elements within node MultiSelect.I${cn}Node node. If any elements have been selected. In the view controller’s layout.com | BPX . This local context node also has a supply function defined for it. This happens either when the node is accessed for the very first // time. the number of selected rows in node MultiSelect could easily change. simply because it thinks the data is still clean.intValue()+1).sdn. the coding for supply function supplyMultiSelected is as follows (where ${cn} = MultiSelected): public void wdDoInit() { //@@begin wdDoInit() msgMgr = wdComponentAPI.

SAP COMMUNITY NETWORK SDN . the round trip to the server will cause the action event handler method to be called. but is known as an “empty” round trip.1. the round trip will still take place.com | UAC .invalidate(). //@@end } This tells the Web Dynpro Framework that the data in this node is now dirty and that it is necessary to rerun the node’s supply function. In the case that no action has been associated with a Table event.sap.getMultiSelection().sap.I${cn}Element element) { //@@begin get${cn}${ca} return wdContext.com © 2010 SAP AG 14 .boc.bpx.isEmpty().size(). //@@end } public java.nodeMultiSelect().getMultiSelection().uac.E.getMultiSelection(). force the supply function for node // MultiSelected to run wdContext.nodeMultiSelect().com | BOC .. Since the size of the selection could change frequently. In our case this action is called DoRowSelect and has an event handler method called onActionDoRowSelect(). The coding in the calculated attributes simply interrogates the IWDMultiSelection object belonging to node MultiSelect. calculated context attributes are used.toString(). public void onActionDoRowSelect(IWDCustomEvent wdEvent ) { //@@begin onActionDoRowSelect(ServerEvent) // Since a table row has been selected. How to Use Advanced Features of the Web Dynpro Context Therefore. size and SelectionRange).lang.I${cn}Element element) { //@@begin get${cn}${ca} return wdContext. No action event handler will be invokes and all that happens is that the context node’s lead selection property is updated to reflect the newly selected row. isEmpty. This is most easily done by creating an action in the view controller and then associating it with the onSelect event belonging to the Table UI element displaying the full table of Arabic/Roman numbers.com | BPX .sap. //@@end } public int get${cn}${ca}(IPrivate${nv}.sap. some of the properties of the IWDMultiSelection API are defined in a context node called MultiSelectProperties and of cardinality 1. ${cn} = MultiSelect and ${ca} = the context attribute in node MultiSelect whose value is being calculated (I. public boolean get${cn}${ca}(IPrivate${nv}.nodeMultiSelect(). 3 This means that every time a user selects a row.String get${cn}${ca}(IPrivate${nv}. Then here in the event handler method.I${cn}Element element) { //@@begin get${cn}${ca} return wdContext. In the code sample below. we invalidate the MultiSelected context node.nodeMultiSelected(). //@@end } 3 In the case of row selection from a table.sdn. we have to force the Web Dynpro Framework to rerun the supply function for node MultiSelected. Lastly. the default behaviour is that a round trip to the server will always take place – irrespective of whether an action has been associated with one of the Table’s events or not.

those fields will be open for input. you can quickly determine how many. then use Shift-Click to select the last row.com | BPX . elements have been selected within a context node’s element collection. Therefore.uac. you can see that selection range is displayed as a list of zero based indices. The value must also be meaningful to the end user! If the Java native type is the class representation of a Java primitive.bpx. then when such context attributes are bound to UI element properties. 4 To multi-select rows from a table you can either use Ctrl-Click to select individual rows. SAP COMMUNITY NETWORK SDN . Using the IWDMultiSelection context node API You should now also understand how to use the IWDMultiSelection context node API. or to select a block of rows.com | UAC . there is no possibility to populate the class attributes by parsing the string value generated by its toString() method.lang.boc. for complex Java native types other than java. However. The screen shot below shows an example: Here. and which.sdn. How to Use Advanced Features of the Web Dynpro Context When the user then makes a multiple selection from the Table. You are entirely responsible for ensuring that the data supplied by the Java native type’s toString() method is suitable for consumption by the UI element property to which you are binding it.sap.com | BOC . Tutorial Summary Binding context attributes defined using Java native types to UI element properties You should now understand how Java native types can be used to defined context attributes that supply data to UI element properties. 4 the MultiSelected context node is updated automatically. data from such Java native types can only be displayed in a “read only” manner. select the first row.sap.sap. Using the API.sap.com © 2010 SAP AG 15 .String.

boc. System p5. R/3. Acrobat. RETAIN. ICA. i5/OS. and PowerPoint are registered trademarks of Microsoft Corporation. MetaFrame. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. z/VM. These materials are subject to change without notice. Data contained in this document serves informational purposes only. MVS/ESA. National product specifications may vary. and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S. Windows. PartnerEdge. All other product and service names mentioned are the trademarks of their respective companies. RACF. Business Objects is an SAP company. Web Intelligence. BusinessObjects. HTML. POWER5+. DB2 Universal Database. SAP NetWeaver. OS/400. Massachusetts Institute of Technology. AS/400.. PostScript. System Storage. UNIX. Outlook. in the United States and in other countries. Duet. The information contained herein may be changed without prior notice. System i.A. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. Xcelsi us.sap. System z9. and Motif are registered trademarks of the Open Group. 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. JavaScript is a registered trademark of Sun Microsystems. Microsoft. System z. AIX. z9. System p. System i5. pSeries. SAP COMMUNITY NETWORK SDN . OpenPower. iSeries. XHTML and W3C are trademarks or registered trademarks of W3C®.com | BPX . ByDesign.sap. How to Use Advanced Features of the Web Dynpro Context Copyright © Copyright 2010 SAP AG.sap. DB2 Co nnect. System z10. XML.com | UAC . PowerVM.uac. DB2. Inc. S/390 Parallel Enterprise Server. GPFS. Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Power Architecture. POWER5.bpx. without representation or warranty of any kind. Crystal Decisions. X/Open. and other countries.sap. IBM. System x.sdn. Program Neighborhood. and MultiWin are trademarks or registered trademarks of Citrix Systems. and SAP Group shall not be liable for errors or omissions with respect to the materials. OS/390. OS/2. Intelligent Miner. All rights reserved. if any. Nothing herein should be construed as constituting an additional warranty. These materials are provided by SAP AG and its affiliated companies ("S AP Group") for informational purposes o nly. Oracle is a registered trademark of Oracle Corporation. Java is a registered trademark of Sun Microsystems. Crystal Reports. z10. S/390. z/OS. WebSphere. eServer. Inc. HACMP. PowerPC. BladeCenter. SAP. BatchPipes. POWER. Linux is the registered trademark of Linus Torvalds in the U. Redbooks. World Wide Web Consortium. SAP Business ByDesign. Parallel Sysplex. POWER6+. the Adobe logo. Citrix. Adobe.S. VideoFrame. 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. Netfinity. used under license for technology invented and implemented by Netscape. Inc.com | BOC . POWER6. WinFrame. OSF/1. and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Business Objects and the Business Objects logo.com © 2010 SAP AG 16 . Excel. xSeries. zSeries.