How to Hide and Unhide Nodes Correctly

Tips & Tricks

Summary

In this article the differences between using y.base.Graph's own methods (Graph.hide(y.base.Node), Graph.unhide(y.base.Node)) and using GraphHider's methods (GraphHider.hide(y.base.Node), GraphHider.unhide(y.base.Node)) are being discussed.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

Basically there are two possibilities to hide a Node. You can call Graph.hide(y.base.Node) or use GraphHider.hide(y.base.Node). Using Graph.hide(y.base.Node) hides the given node from it's graph. This means the node is (temporarily) removed. Furthermore the node's adjacent edges are being removed, too, since the graph can't hold edges whose endpoints do not belong to the graph anymore. The graph itself does not store any information about hidden elements. That's why using Graph.unhide(y.base.Node) will reinsert the Node itself into the Graph, but not it's associated edges. The edges are lost.

Graph.hide(y.base.Node) removes a node and it's edges from the Graph. Graph.unhide(y.base.Node) reinserts only the given node to the graph.

Of course there is a possibility to hide and unhide nodes with their adjacent edges. All you have to do is use y.util.GraphHider to handle your hides and unhides.

GraphHider provides several alternatives to hide Nodes:


To unhide nodes two methods are available:

The following samplecode uses GraphHider.hide(y.base.Node) to hide nodes and GraphHider.unhideAll() to unhide the nodes as well as the edges:

package demo.view;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;

import javax.swing.JToolBar;
import y.base.Node;

import y.util.GraphHider;
import y.view.PopupMode;


/** 
 *  Demonstrates how to hide/unhide nodes using y.util.GraphHider.
 *  To hide a node use the popup menu by right clicking a node.
 *  To unhide all nodes use the toolbar button.
 */
public class GraphHiderDemo extends ViewActionDemo {
  GraphHider hider = new GraphHider(view.getGraph2D());

  public GraphHiderDemo() {
    //add a popup child mode to editMode (one that listens to the right mouse click
    //and pops up context sensitive menues)
    editMode.setPopupMode(new DemoPopupMode());
  }

  /**
   * Creates a toolbar for this demo.
   */
  protected JToolBar createToolBar() {
    JToolBar bar = new JToolBar();
    bar.add(new UnhideNodes());
    return bar;
  }

  class DemoPopupMode extends PopupMode {
    /** Popup menu for a hit node */
    public JPopupMenu getNodePopup(Node v) {
      JPopupMenu pm = new JPopupMenu();
      pm.add(new HideNode(v));
      return pm;
    }
  }

  /**
   * Action that hides a node
   */
  class HideNode extends AbstractAction {
    Node v;

    HideNode(Node v) {
      super("Hide Node");
      this.v = v;
    }

    public void actionPerformed(ActionEvent e) {
      hider.hide(v);
      view.updateView();
    }
  }

  /**
   * Action that unhides all nodes
   */
  class UnhideNodes extends AbstractAction {
    UnhideNodes(){
      super("Unhide Nodes");
    }
    public void actionPerformed(ActionEvent e) {
      hider.unhideAll();
      view.updateView();
    }
  }

  public static void main(String args[]) {
    GraphHiderDemo demo = new GraphHiderDemo();
    demo.start("GraphHider Demo");
  }
}

Resources

Categories this article belongs to:
yFiles for Java > yFiles Basic > Working With the Graph Structure
yFiles for Java > yFiles Layout
yFiles for Java > yFiles Viewer
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
yFiles.NET: 2.2, 2.3, 2.4
Keywords:
Graph - hide - Node - GraphHider