How to Hide and Unhide Nodes Correctly
Tips & TricksSummary
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.
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:
- GraphHider.hide(y.base.Node) e.g. hides the given node and all it's adjacent edges from the graph. The hidden elements will be stored so that they can be unhidden again at a later time.
- If you want to hide more than one Node use GraphHider.hide(y.base.NodeCursor) or alternatively
- GraphHider.hide(y.base.NodeList)
To unhide nodes two methods are available:
- GraphHider.unhideNodes()unhides only the hidden Nodes
- GraphHider.unhideAll() will unhide the nodes as well as the adjacent edges
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");
}
}