Start of Content Area

Procedure documentation Initialising the Context Locate the document in its SAP Library structure

In 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 will merely load the content of the resource bundle to the context.

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.

Procedure

...

       1.      To open the TreeView, double-click TreeView in the project structure in the Web Dynpro Explorer.

       2.      Choose the Implementation tab.

       3.      At the end of the file, after //@@begin others, enter the following lines:

//@@begin others

 

// store resource handler for property resource file in private

// member variable

private IWDResourceHandler resourceHandlerForTree = null;

 

/**

 * Adds child elements to given parent element. The parent

 * element corresponds to a folder.  The folder content (folder

 * elements) is stored as a list of context elements of type

 * IFolderContentElement within the non-singleton node 'ChildNode'

 * under the parent node.  

 */

private void addChildren(

  IPrivateTreeView.IFolderContentElement parent) {

  IPrivateTreeView.IFolderContentNode folderContentNode =

    parent.nodeChildNode();

  IPrivateTreeView.IFolderContentElement folderContentElement;

 

  // read entries (folder content) for given key (folder)

  // in resource bundle    .

  String entries = resourceHandlerForTree.getString(parent.getText());

  StringTokenizer strTokenizer = new StringTokenizer(entries, ";");

  String anEntryToken;

 

  // if there is no entry in Filesystem.properties for given

  // key (parent.getText()) then the key is returned.  

  if (entries.equals(parent.getText())) {

    folderContentElement =

      folderContentNode.createFolderContentElement();

    folderContentElement.setText("Empty Folder");

    folderContentElement.setHasChildren(false);

    folderContentElement.setIgnoreAction(true);

    folderContentNode.addElement(folderContentElement);

  } else {

    // populate non-singleton child node with node elements

    // (folder content elements)

    while (strTokenizer.hasMoreTokens()) {

      anEntryToken = strTokenizer.nextToken();

      folderContentElement =

        folderContentNode.createFolderContentElement();

 

      if (anEntryToken.indexOf(".") != -1) {

        // entryToken is a file

        folderContentElement.setHasChildren(false);

        folderContentElement.setIgnoreAction(false);

        folderContentElement.setIconSource("~sapicons/s_b_crea.gif");

      } else {

        // entry token is a folder

        folderContentElement.setHasChildren(true);

        folderContentElement.setIgnoreAction(true);

        folderContentElement.setIconSource("~sapicons/s_clofol.gif");

      }

 

      folderContentElement.setText(anEntryToken);

      folderContentElement.setIsExpanded(false);

      folderContentNode.addElement(folderContentElement);

    }

  }

}

 

//@@end

This graphic is explained in the accompanying text 

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 the 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 node elements of the typ IFolderContentNode ==

  IPrivateTreeView.IFolderContentNode rootFolderContentNode =

    wdContext.nodeFolderContent();

  IPrivateTreeView.IFolderContentElement rootFolderContentElement;

 

  // begin populating context node 'FolderContent'

  String aDriveToken;

  while (strTokenizer.hasMoreTokens()) {

    aDriveToken = strTokenizer.nextToken();

 

    // instantiate the new context node element of type    

   // 'IFolderContentElement'

    rootFolderContentElement =

      rootFolderContentNode.createFolderContentElement();

 

    //set contained context value attributes

    rootFolderContentElement.setText(aDriveToken);

    rootFolderContentElement.setHasChildren(true);

    rootFolderContentElement.setIconSource(

      "~sapicons/s_clofol.gif");

    rootFolderContentElement.setIgnoreAction(true);

    // add root folder element to root folder node       

    rootFolderContentNode.addElement(rootFolderContentElement);

 

    // if last folder node element, fill its non-singleton

    // child node (storing its entries) with children (folder node

    // elements) and expand the node ('Drive D' in tree).

    if (!strTokenizer.hasMoreTokens()) {

      addChildren(rootFolderContentElement);

      rootFolderContentElement.setIsExpanded(true);

    } else {

      rootFolderContentElement.setIsExpanded(false);

    }

  }

  //@@end

}

This graphic is explained in the accompanying text 

To add the missing imports, position the mouse pointer in the source code area, click the secondary mouse button, and choose Source à Organize Imports.

Result

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:

Mapping the Event Parameters

 

End of Content Area