CustomNodeRealizer.java |
1 /**************************************************************************** 2 * This demo file is part of yFiles for Java 2.14. 3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28, 4 * 72070 Tuebingen, Germany. All rights reserved. 5 * 6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution 7 * of demo files in source code or binary form, with or without 8 * modification, is not permitted. 9 * 10 * Owners of a valid software license for a yFiles for Java version that this 11 * demo is shipped with are allowed to use the demo source code as basis 12 * for their own yFiles for Java powered applications. Use of such programs is 13 * governed by the rights and conditions as set out in the yFiles for Java 14 * license agreement. 15 * 16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 ***************************************************************************/ 28 package demo.io.graphml; 29 30 import y.view.NodeRealizer; 31 import y.view.ShapeNodeRealizer; 32 33 import java.awt.Color; 34 import java.awt.Graphics2D; 35 36 import demo.view.DemoDefaults; 37 38 /** 39 * A simple customization of {@link y.view.ShapeNodeRealizer} that holds additional 40 * fields. 41 * GraphML serialization of this realizer and its additional fields is handled by 42 * {@link CustomNodeRealizerSerializer}. 43 */ 44 public class CustomNodeRealizer extends ShapeNodeRealizer { 45 // Custom value 46 private int customValue; 47 // Custom attribute 48 private String customAttribute; 49 50 /** Creates a new instance of CustomNodeRealizer. */ 51 public CustomNodeRealizer() { 52 setSize(60, 40); 53 setCustomAttribute("v1.0"); 54 setCustomValue(333); 55 setFillColor(DemoDefaults.DEFAULT_NODE_COLOR); 56 } 57 58 /** Creates a new instance of CustomNodeRealizer. */ 59 public CustomNodeRealizer(NodeRealizer nr) { 60 super(nr); 61 // If the given node realizer is of this type, then apply copy semantics. 62 if (nr instanceof CustomNodeRealizer) { 63 CustomNodeRealizer fnr = (CustomNodeRealizer) nr; 64 // Copy the values of custom attributes. 65 setCustomValue(fnr.customValue); 66 setCustomAttribute(fnr.customAttribute); 67 } 68 } 69 70 public NodeRealizer createCopy(NodeRealizer nr) { 71 return new CustomNodeRealizer(nr); 72 } 73 74 public void paintText(Graphics2D gfx) { 75 super.paintText(gfx); 76 gfx.setColor(Color.blue); 77 gfx.drawString("value: " + getCustomValue(), (float) getX() + 4, (float) getY() + 12); 78 gfx.drawString("attr: " + getCustomAttribute(), (float) getX() + 4, (float) (getY() + getHeight() - 2)); 79 } 80 81 public int getCustomValue() { 82 return customValue; 83 } 84 85 public void setCustomValue(int customValue) { 86 this.customValue = customValue; 87 } 88 89 public String getCustomAttribute() { 90 return customAttribute; 91 } 92 93 public void setCustomAttribute(String customAttribute) { 94 this.customAttribute = customAttribute; 95 } 96 } 97