ErdAttributesLabelConfiguration.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.view.entityrelationship.painters; 29 30 import y.geom.OrientedRectangle; 31 import y.view.NodeLabel; 32 import y.view.YLabel; 33 34 import java.awt.Graphics2D; 35 import java.awt.font.FontRenderContext; 36 import java.util.Map; 37 38 /** 39 * This configuration paints ERD attribute labels for detailed entities. 40 * 41 * The label uses the whole space of the lower compartment. So the size is retrieved from 42 * the oriented box instead of the content box. Also the the contains test is using this size. 43 */ 44 public class ErdAttributesLabelConfiguration implements YLabel.Layout, YLabel.Painter { 45 private final YLabel.Layout layout; 46 private final YLabel.Painter painter; 47 48 public ErdAttributesLabelConfiguration() { 49 this(defaultLayout(), ErdNameLabelPainter.defaultPainter()); 50 } 51 52 /** 53 * Creates a new <code>ErdAttributesLabelConfiguration</code>. 54 * @param layout the layout of the label 55 * @param painter the painter of the label 56 */ 57 public ErdAttributesLabelConfiguration( 58 final YLabel.Layout layout, 59 final YLabel.Painter painter 60 ) { 61 this.layout = layout; 62 this.painter = painter; 63 } 64 65 /** 66 * Paints the ERD attributes label. 67 * @param label the label context 68 * @param gfx the graphics object 69 */ 70 public void paint( final YLabel label, final Graphics2D gfx ) { 71 if (painter != null) { 72 final OrientedRectangle cb = label.getContentBox(); 73 if (cb.getUpY() == -1) { 74 // Use the oriented box size to paint the box, 75 // so the label is as wide as the node 76 final OrientedRectangle ob = label.getOrientedBox(); 77 final double h = ob.getHeight(); 78 final double x = ob.getAnchorX(); 79 final double y = ob.getAnchorY() - h; 80 paintBox(label, gfx, x, y, ob.getWidth(), h); 81 paintContent(label, gfx, x, y, cb.getWidth(), cb.getHeight()); 82 } else { 83 painter.paint(label, gfx); 84 } 85 } 86 } 87 88 /** 89 * Draws the content of the attributes label. In this case it is a string containing the attributes. 90 * @param label the label context 91 * @param gfx the graphics object 92 * @param x the x-coordinate of the label 93 * @param y the y-coordinate of the label 94 * @param width the width of the label 95 * @param height the height of the label 96 */ 97 public void paintContent( 98 final YLabel label, 99 final Graphics2D gfx, 100 final double x, 101 final double y, 102 final double width, 103 final double height 104 ) { 105 if (painter != null) { 106 painter.paintContent(label, gfx, x, y, width, height); 107 } 108 } 109 110 /** 111 * Paints the background of the attributes label. 112 * @param label the label context 113 * @param gfx the graphics object 114 * @param x the x-coordinate of the label 115 * @param y the y-coordinate of the label 116 * @param width the width of the label 117 * @param height the height of the label 118 */ 119 public void paintBox( 120 final YLabel label, 121 final Graphics2D gfx, 122 final double x, 123 final double y, 124 final double width, 125 final double height 126 ) { 127 if (painter != null) { 128 painter.paintBox(label, gfx, x, y, width, height); 129 } 130 } 131 132 /** 133 * Returns the text box of the current painter. 134 * @param label the label context 135 * @return a rectangle with the size and position of the text box 136 */ 137 public OrientedRectangle getTextBox( final YLabel label ) { 138 if (painter != null) { 139 return painter.getTextBox(label); 140 } else { 141 return null; 142 } 143 } 144 145 /** 146 * Returns the icon box of the current painter. 147 * @param label the label context 148 * @return a rectangle with the size and position of the icon box 149 */ 150 public OrientedRectangle getIconBox( final YLabel label ) { 151 if (painter != null) { 152 return painter.getIconBox(label); 153 } else { 154 return null; 155 } 156 } 157 158 /** 159 * Calculates the size of the label content. 160 * @param label the label context 161 * @param frc the font render context. 162 */ 163 public void calculateContentSize( 164 final YLabel label, 165 final FontRenderContext frc 166 ) { 167 if (layout != null) { 168 layout.calculateContentSize(label, frc); 169 } 170 } 171 172 /** 173 * Determines if the coordinates <code>(x,y)</code> lie within the label box. 174 * @param label the label context. 175 * @param x x-coordinate 176 * @param y y-coordinate 177 * @return <code>true</code> if the label box contains the coordinates (x,y), <code>false</code> otherwise 178 */ 179 public boolean contains( 180 final YLabel label, 181 final double x, 182 final double y 183 ) { 184 // Use the oriented box instead of the content box, so one can 185 // click on the whole compartment to select the label 186 return label.getOrientedBox().contains(x, y, true); 187 } 188 189 190 /** 191 * Retrieves a suitable default layout for this configuration. 192 * @return the default layout or <code>null</code> if no default layout is set 193 */ 194 static YLabel.Layout defaultLayout() { 195 final YLabel.Factory factory = NodeLabel.getFactory(); 196 final Map c = factory.createDefaultConfigurationMap(); 197 final Object layout = c.get(YLabel.Layout.class); 198 if (layout instanceof YLabel.Layout) { 199 return (YLabel.Layout) layout; 200 } else { 201 return null; 202 } 203 } 204 } 205 206