!--a11y-->
Initializing the ContextIn the previous steps, you have created the context structure that is to represent the file system. You have also created a resource bundle containing the file structure.
In this step, you load the contents that are required to display the user interface template from the resource bundle to the context. When the application is started, you want it to display the drives and the opened folder Games.
To do this, you load the resource bundle in the method wdDoInit() and add the required folder to the context. You also set the individual context attributes, to which the individual tree nodes are bound. This includes the icons (so that a folder gets a folder icon and a file gets a file icon), the folders (so that they cannot be selected), the files that are leaves and not nodes, and the name of the folder or file to be displayed.
...
1. To open the TreeView, double-click TreeView in the project structure in the Web Dynpro Explorer.
2. Choose the Implementation tab page.
3. At the end of the file, after //@@begin others, enter the following lines:
//@@begin others
private IWDResourceHandler resourceHandlerForTree = null;
private void addChildren(IPrivateTreeView .ITreeNodeElement parent) {
String resourceText = resourceHandlerForTree.getString(parent.getText()); IPrivateTreeView.ITreeNodeElement child = null;
// if there is no entry in Filesystem.properties for key // parent.getText() then the key is returned if (resourceText.equals(parent.getText())) { child = parent.nodeChildNode(). createTreeNodeElement(); child.setText("Empty Folder"); child.setHasChildren(false); child.setIgnoreAction(true); parent.nodeChildNode().addElement(child); } else { // populate all children StringTokenizer strTokenizer = new StringTokenizer(resourceText, ";"); String token = null; while (strTokenizer.hasMoreTokens()) { token = strTokenizer.nextToken(); child = parent.nodeChildNode(). createTreeNodeElement();
if (token.indexOf(".") != -1) { // token is a file child.setHasChildren(false); child.setIgnoreAction(false); child.setIconSource("~sapicons/s_b_crea.gif"); } else { // token is a folder child.setHasChildren(true); child.setIgnoreAction(true); child.setIconSource("~sapicons/s_clofol.gif"); }
child.setText(token); child.setIsExpanded(false); parent.nodeChildNode().addElement(child); } } }
//@@end
|
To add the missing imports, position the mouse pointer in the source code area, click the secondary mouse button, and choose Source à Organize Imports.
Since the resource bundle is accessed in the methods wdDoInit() and addChildren(…), define a global variable called resourceHandlerForTree in the view controller. This saves you entering source code, because the connection to the file must only be established once.
The method addChildren(…) is created to save code lines, because it can be used in the method wdDoInit() and also later, when opening a folder, to add the child nodes to the current node.
4. Enter the following lines in the method wdDoInit() to obtain the initial tree structure:
public void wdDoInit() { //@@begin wdDoInit()
//===== STEP 1: Load all initial Data from //Filesystem.properties ========== resourceHandlerForTree = WDResourceHandler .createResourceHandlerForCurrentSession();
// Load the resource bundle "Filesystem.properties" // located in package "com.sap.tut.wd.tree.resources" // for locale set in the resource handler. resourceHandlerForTree.loadResourceBundle( "com.sap.tut.wd.tree.resources.Filesystem", this.getClass().getClassLoader());
// get all Drives String drives = resourceHandlerForTree .getString("Drives"); StringTokenizer strTokenizer = new StringTokenizer(drives,";");
//===== STEP 2: Create context node elements of the type //treeNode ========= IPrivateTreeView.ITreeNodeNode rootNode = wdContext.nodeTreeNode(); IPrivateTreeView.ITreeNodeElement level1Node = null;
// begin populating context node treeNode String token = null; while (strTokenizer.hasMoreTokens()) { token = strTokenizer.nextToken();
// instantiate the new context node element of type // treeNode level1Node = rootNode.createTreeNodeElement();
//set contained context value attributes level1Node.setText(token); level1Node.setHasChildren(true); level1Node.setIconSource("~sapicons/s_clofol.gif"); level1Node.setIgnoreAction(true); // add to rootNode rootNode.addElement(level1Node);
// if last Node, fill node with children and expand // the node (Drive D) if (!strTokenizer.hasMoreTokens()) { addChildren(level1Node); level1Node.setIsExpanded(true); } else { level1Node.setIsExpanded(false); } }
//@@end } |
To add the missing imports, position the mouse pointer in the source code area, click the secondary mouse button, and choose Source à Organize Imports.
You have defined a method that enables children to be added to a node. You have also saved data in the context of the method wdDoInit() that creates the appearance of the view as required in the user interface template at runtime.
Next step:
