Lesson: Customizing the Copying of Graph Elements

During Cut, Copy, or Paste actions, Graph2DClipboard copies graph elements to/from the clipboard (or, more precisely, to/from the clipboard graph). By default, it uses the GraphCopier.CopyFactory implementation that is registered with the graph to do so. If we need special support when graph elements are copied, most notably, because we need additional data that is associated with graph elements to be copied, too, we simply provide our own CopyFactory implementation.

The graph class that is used in the Clipboard2 application, class CustomGraph2D, is our custom Graph2D subclass from the Handling Additional Data for Graph Elements trail. It holds additional data for the nodes of a graph. In order for these data to be copied properly, we create the CopyFactory implementation shown in Example 1.3, “Custom CopyFactory implementation for class CustomGraph2D”.

Example 1.3. Custom CopyFactory implementation for class CustomGraph2D

public class CustomGraph2DCopyFactory extends 
             Graph2DCopyFactory.HierarchicGraph2DCopyFactory {
  /** Creates a new CustomGraph2D. */
  public Graph createGraph() {
    return new CustomGraph2D();
  }

  /** Copies a node of a CustomGraph2D together with its additional data. */
  public Node copyNode(Graph targetGraph, Node originalNode) {
    Node newNode = super.copyNode(targetGraph, originalNode);
    if (originalNode.getGraph() instanceof CustomGraph2D && 
        targetGraph instanceof CustomGraph2D) {
      CustomGraph2D sourceGraph = (CustomGraph2D)originalNode.getGraph();
      CustomGraph2D g = (CustomGraph2D)targetGraph;
      g.setCustomText(newNode, sourceGraph.getCustomText(originalNode));
    }
    return newNode;
  }
}

As the base for our CustomGraph2DCopyFactory class we use the Graph2DCopyFactory.HierarchicGraph2DCopyFactory class which is used for copying graph elements in conjunction with the standard Graph2D. We only need to appropriately override the createGraph and the copyNode methods defined in the CopyFactory interface. Note that the former method will be called to create the proper graph type in the clipboard necessary to hold graph elements during the clipboard actions.

What we need to do now, is that we make the CustomGraph2D class return our CopyFactory implementation whenever requested. To this end, we override the protected method in CustomGraph2D as shown in Example 1.4, “Creating the CopyFactory in the Graph2D subclass”.

Example 1.4. Creating the CopyFactory in the Graph2D subclass

/** Creates a new CustomGraph2DCopyFactory. */
protected GraphCopier.CopyFactory createGraphCopyFactory() {
  return new CustomGraph2DCopyFactory();
}

With our CustomGraph2DCopyFactory in place, Graph2DClipboard will find and use it and any additional data will be copied together with its node.

As a side note, if we only needed special graph element copying logic with clipboard actions, then registering our CopyFactory implementation directly with the Graph2DClipboard using the setCopyFactory method would have also sufficed.