Navigating through the relational network

The previous recipe enabled us to show one level of a data tree; this recipe is going to allow us to navigate from one level to another, thus enabling us to explore the entire tree.

Getting ready

Open the files downloaded from the Packt Publishing website for Chapter 8 | Recipe 4 to follow along.

How to do it...

The following are the required steps to allow the user to navigate through the relational network:

  1. Create the PlusButton class to allow the user to navigate down the tree.
  2. Create the MinusButton class to allow the user to navigate up the tree.
  3. Add RelationalNetworkEvent to indicate to the relational network that the user wants to navigate.
  4. In NodeVisual, add the PlusButton class if the node is a child.
  5. In NodeVisual, add the MinusButton class if the node is the center node.
  6. In RelationalNetwork.as, add a listener to the center node for the RelationalNetworkEvent.NAVIGATE_TO_PARENT event.
  7. Still in RelationalNetwork.as, add a listener to each child for the RelationalNetworkEvent.NAVIGATE_TO_CHILDREN event.
  8. In RelationalNetwork.as, create the listener function for the previous events. In that function remove everything from the stage and recreate the network using the node that was clicked on as the center node. If the center node was clicked use its parent.

How it works...

What we need to do here is first create the buttons (steps 1 and 2) so that the user can click on them to indicate that he wants to navigate up or down the relational network. We drew simple buttons using the Flash drawing API: a plus for the children and a minus for the center node. We located those buttons just below the label in the center of the node. The placement might not be the optimal one, but in this context, where there are not too many options, it works well (if you wanted to add more interaction possibilities, it might get crowded).

When one of these buttons get clicked, the NodeVisual class catches it and dispatches either a NAVIGATE_TO_PARENT or NAVIGATE_TO_CHILDREN event. The RelationalNetwork class then catches it. If the node that sent the event is the center node, then it recreates the network from its parent. If the node that sent the event is a child, then it recreates the network from the child node.

The code for recreating the network is found in the _onNavigateToChildren and _onNavigateToParent functions and is basically the same that can be found in the constructor of the RelationalNetwork class.

There's more...

By tiding up the code a bit, you could make this recipe better.

Refactoring

If you look at the code from the constructor and the functions _onNavigateToChildren and _onNavigateToParent, you will see a lot of the same code being repeated. This is not a sign of a good code (it was done like this so that we could upgrade the code for later recipes) and could be consolidated in just one function. Doing this would make the code easier to maintain.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.131.238