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.AbstractCustomLabelPainter;
32  import y.view.NodeLabel;
33  import y.view.YLabel;
34  
35  import java.awt.Color;
36  import java.awt.Graphics2D;
37  import java.awt.Paint;
38  import java.awt.Stroke;
39  import java.util.Map;
40  
41  /**
42   * This painter draws the name label of a detailed entity of an entity relationship diagram (ERD).
43   *
44   * The painter does not draw an opaque background as the <code>ErdNodePainter</code> already paints all
45   * background colors.
46   *
47   * @see ErdNodePainter
48   */
49  public class ErdNameLabelPainter implements YLabel.Painter {
50    private final YLabel.Painter painter;
51    private final YLabel.Painter boxPainter;
52  
53    /** Creates a new <code>ErdNameLabelPainter</code> */
54    public ErdNameLabelPainter() {
55      this(defaultPainter());
56    }
57  
58    /**
59     * Creates a new <code>ErdNameLabelPainter</code> that uses the given label painter.
60     * @param painter the label painter
61     */
62    public ErdNameLabelPainter( final YLabel.Painter painter ) {
63      this.painter = painter;
64      this.boxPainter = new BoxPainter();
65    }
66  
67    /**
68     * Paints the label.
69     * @param label the label context
70     * @param gfx the grapics object
71     */
72    public void paint( final YLabel label, final Graphics2D gfx ) {
73      if (painter != null) {
74        painter.paint(label, gfx);
75      }
76    }
77  
78    /**
79     * Paints the label content to the specified location. In this case it is a string with contains the name of the entity.
80     * @param label the label context
81     * @param gfx the graphics object
82     * @param x the x-coordinate of the label
83     * @param y the y-coordinate of the label
84     * @param width the width of the label
85     * @param height the height of the label
86     */
87    public void paintContent(
88            final YLabel label,
89            final Graphics2D gfx,
90            final double x,
91            final double y,
92            final double width,
93            final double height
94    ) {
95      if (painter != null) {
96        painter.paintContent(label, gfx, x, y, width, height);
97      }
98    }
99  
100   /**
101    * Paints the background of the label.
102    * @param label the label context
103    * @param gfx the graphics object
104    * @param x the x-coordinate of the label
105    * @param y the y-coordinate of the label
106    * @param width the width of the label
107    * @param height the height of the label
108    */
109   public void paintBox(
110           final YLabel label,
111           final Graphics2D gfx,
112           final double x,
113           final double y,
114           final double width,
115           final double height
116   ) {
117     // paint the box only if the *main* painter is not null
118     if (painter != null) {
119       // the <code>BoxPainter</code> will only draw a label background if
120       // the label is selected
121       boxPainter.paintBox(label, gfx, x, y, width, height);
122     }
123   }
124 
125   /**
126    * Returns the text box of a given label
127    * @param label the label context
128    * @return a rectangle with the size and position of the text box
129    */
130   public OrientedRectangle getTextBox( final YLabel label ) {
131     if (painter != null) {
132       return painter.getTextBox(label);
133     } else {
134       return null;
135     }
136   }
137 
138   /**
139    * Returns the icon box of a given label
140    * @param label the label context
141    * @return a rectangle with the size and position of the icon box
142    */
143   public OrientedRectangle getIconBox( final YLabel label ) {
144     if (painter != null) {
145       return painter.getIconBox(label);
146     } else {
147       return null;
148     }
149   }
150 
151 
152   /**
153    * Returns the default painter if it exists.
154    * @return the default painter or null if it does not exist
155    */
156   static YLabel.Painter defaultPainter() {
157     final YLabel.Factory factory = NodeLabel.getFactory();
158     final Map c = factory.createDefaultConfigurationMap();
159     final Object painter = c.get(YLabel.Painter.class);
160     if (painter instanceof YLabel.Painter) {
161       return (YLabel.Painter) painter;
162     } else {
163       return null;
164     }
165   }
166 
167 
168   /**
169    * This painter draws the label box only if the label is selected. No background color is drawn
170    * because the <code>ErdNodePainter</code> already draws all background colors.
171    * @see ErdNodePainter
172    */
173   private static final class BoxPainter extends AbstractCustomLabelPainter {
174 
175     /**
176      * Paints a selection box, if the label is selected by the user.
177      * @param label the label to paint.
178      * @param graphics the graphics context to paint upon.
179      * @param x the x-coordinate of the upper left corner of the label's
180      * background rectangle.
181      * @param y the y-coordinate of the upper left corner of the label's
182      * background rectangle.
183      * @param width the width of the label's background rectangle.
184      * @param height the height of the label's background rectangle.
185      */
186     public void paintBox(
187             final YLabel label,
188             final Graphics2D graphics,
189             final double x,
190             final double y,
191             final double width,
192             final double height
193     ) {
194       final Color oldColor = graphics.getColor();
195       final Paint oldPaint = graphics.getPaint();
196       final Stroke oldStroke = graphics.getStroke();
197 
198       paintSelectionBox(label, graphics, x, y, width, height);
199 
200       graphics.setStroke(oldStroke);
201       graphics.setPaint(oldPaint);
202       graphics.setColor(oldColor);
203     }
204 
205     /** Does nothing */
206     public void paintContent(
207             final YLabel label,
208             final Graphics2D gfx,
209             final double x,
210             final double y,
211             final double width,
212             final double height
213     ) {
214     }
215 
216     /** Does nothing */
217     public OrientedRectangle getTextBox( final YLabel label ) {
218       return null;
219     }
220 
221     /** Does nothing */
222     public OrientedRectangle getIconBox( final YLabel label ) {
223       return null;
224     }
225   }
226 
227 }
228