1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.entityrelationship.painters;
15  
16  import y.geom.OrientedRectangle;
17  import y.view.AbstractCustomLabelPainter;
18  import y.view.NodeLabel;
19  import y.view.YLabel;
20  
21  import javax.swing.text.html.StyleSheet;
22  import java.awt.Color;
23  import java.awt.Graphics2D;
24  import java.awt.Paint;
25  import java.awt.Stroke;
26  import java.util.Map;
27  
28  /**
29   * This painter draws the name label of a detailed entity of an entity relationship diagram (ERD).
30   *
31   * The painter does not draw an opaque background as the <code>ErdNodePainter</code> already paints all
32   * background colors.
33   *
34   * @see ErdNodePainter
35   */
36  public class ErdNameLabelPainter implements YLabel.Painter {
37    private final YLabel.Painter painter;
38    private final YLabel.Painter boxPainter;
39  
40    /** Creates a new <code>ErdNameLabelPainter</code> */
41    public ErdNameLabelPainter() {
42      this(defaultPainter());
43    }
44  
45    /**
46     * Creates a new <code>ErdNameLabelPainter</code> that uses the given label painter.
47     * @param painter the label painter
48     */
49    public ErdNameLabelPainter( final YLabel.Painter painter ) {
50      this.painter = painter;
51      this.boxPainter = new BoxPainter();
52    }
53  
54    /**
55     * Paints the label.
56     * @param label the label context
57     * @param gfx the grapics object
58     */
59    public void paint( final YLabel label, final Graphics2D gfx ) {
60      if (painter != null) {
61        painter.paint(label, gfx);
62      }
63    }
64  
65    /**
66     * Paints the label content to the specified location. In this case it is a string with contains the name of the entity.
67     * @param label the label context
68     * @param gfx the graphics object
69     * @param x the x-coordinate of the label
70     * @param y the y-coordinate of the label
71     * @param width the width of the label
72     * @param height the height of the label
73     */
74    public void paintContent(
75            final YLabel label,
76            final Graphics2D gfx,
77            final double x,
78            final double y,
79            final double width,
80            final double height
81    ) {
82      if (painter != null) {
83        painter.paintContent(label, gfx, x, y, width, height);
84      }
85    }
86  
87    /**
88     * Paints the background of the label.
89     * @param label the label context
90     * @param gfx the graphics object
91     * @param x the x-coordinate of the label
92     * @param y the y-coordinate of the label
93     * @param width the width of the label
94     * @param height the height of the label
95     */
96    public void paintBox(
97            final YLabel label,
98            final Graphics2D gfx,
99            final double x,
100           final double y,
101           final double width,
102           final double height
103   ) {
104     // paint the box only if the *main* painter is not null
105     if (painter != null) {
106       // the <code>BoxPainter</code> will only draw a label background if
107       // the label is selected
108       boxPainter.paintBox(label, gfx, x, y, width, height);
109     }
110   }
111 
112   /**
113    * Returns the text box of a given label
114    * @param label the label context
115    * @return a rectangle with the size and position of the text box
116    */
117   public OrientedRectangle getTextBox( final YLabel label ) {
118     if (painter != null) {
119       return painter.getTextBox(label);
120     } else {
121       return null;
122     }
123   }
124 
125   /**
126    * Returns the icon box of a given label
127    * @param label the label context
128    * @return a rectangle with the size and position of the icon box
129    */
130   public OrientedRectangle getIconBox( final YLabel label ) {
131     if (painter != null) {
132       return painter.getIconBox(label);
133     } else {
134       return null;
135     }
136   }
137 
138 
139   /**
140    * Returns the default painter if it exists.
141    * @return the default painter or null if it does not exist
142    */
143   static YLabel.Painter defaultPainter() {
144     final YLabel.Factory factory = NodeLabel.getFactory();
145     final Map c = factory.createDefaultConfigurationMap();
146     final Object painter = c.get(YLabel.Painter.class);
147     if (painter instanceof YLabel.Painter) {
148       return (YLabel.Painter) painter;
149     } else {
150       return null;
151     }
152   }
153 
154 
155   /**
156    * This painter draws the label box only if the label is selected. No background color is drawn
157    * because the <code>ErdNodePainter</code> already draws all background colors.
158    * @see ErdNodePainter
159    */
160   private static final class BoxPainter extends AbstractCustomLabelPainter {
161 
162     /**
163      * Paints a selection box, if the label is selected by the user.
164      * @param label the label to paint.
165      * @param graphics the graphics context to paint upon.
166      * @param x the x-coordinate of the upper left corner of the label's
167      * background rectangle.
168      * @param y the y-coordinate of the upper left corner of the label's
169      * background rectangle.
170      * @param width the width of the label's background rectangle.
171      * @param height the height of the label's background rectangle.
172      */
173     public void paintBox(
174             final YLabel label,
175             final Graphics2D graphics,
176             final double x,
177             final double y,
178             final double width,
179             final double height
180     ) {
181       final Color oldColor = graphics.getColor();
182       final Paint oldPaint = graphics.getPaint();
183       final Stroke oldStroke = graphics.getStroke();
184 
185       paintSelectionBox(label, graphics, x, y, width, height);
186 
187       graphics.setStroke(oldStroke);
188       graphics.setPaint(oldPaint);
189       graphics.setColor(oldColor);
190     }
191 
192     /** Does nothing */
193     public void paintContent(
194             final YLabel label,
195             final Graphics2D gfx,
196             final double x,
197             final double y,
198             final double width,
199             final double height
200     ) {
201     }
202 
203     /** Does nothing */
204     public OrientedRectangle getTextBox( final YLabel label ) {
205       return null;
206     }
207 
208     /** Does nothing */
209     public OrientedRectangle getIconBox( final YLabel label ) {
210       return null;
211     }
212   }
213 
214 }
215