1
28 package demo.view.entityrelationship.painters;
29
30 import y.geom.YRectangle;
31 import y.layout.NodeLabelModel;
32 import y.view.GenericNodeRealizer;
33 import y.view.NodeLabel;
34 import y.view.NodeRealizer;
35 import y.view.ShapeNodePainter;
36 import y.view.YRenderingHints;
37
38 import java.awt.Color;
39 import java.awt.GradientPaint;
40 import java.awt.Graphics2D;
41 import java.awt.Paint;
42 import java.awt.Shape;
43 import java.awt.Stroke;
44 import java.awt.geom.Line2D;
45 import java.awt.geom.Rectangle2D;
46
47
55 public class ErdNodePainter implements GenericNodeRealizer.ContainsTest, GenericNodeRealizer.Painter {
56
57 public static final byte RECT = 0;
58
59
60 public static final byte ROUND_RECT = 1;
61
62
63 private final ErdNodePainterImpl painter;
64
65
66 public ErdNodePainter() {
67 this(ROUND_RECT);
68 }
69
70
77 public ErdNodePainter(final byte type) {
78 switch (type) {
79 case RECT:
80 case ROUND_RECT:
81 break;
82 default:
83 throw new IllegalArgumentException("Illegal type: " + type);
84 }
85
86 painter = new ErdNodePainterImpl(type);
87 }
88
89
94 public void paint( final NodeRealizer context, final Graphics2D graphics ) {
95 painter.paint(context, graphics);
96 }
97
98
103 public void paintSloppy( final NodeRealizer context, final Graphics2D graphics ) {
104 painter.paintSloppy(context, graphics);
105 }
106
107
108
115 public boolean contains( final NodeRealizer context, final double x, final double y ) {
116 return painter.contains(context, x, y);
117 }
118
119
124 static double separator( final NodeLabel label ) {
125 final YRectangle lr = label.getBox();
126 return lr.getY() + lr.getHeight() + Math.max(0, label.getDistance());
127 }
128
129
136 static boolean useErdStyle( final NodeRealizer context ) {
137 if (context.labelCount() > 0) {
138 final NodeLabel nl = context.getLabel();
139 if (NodeLabel.INTERNAL == nl.getModel()) {
140 final byte p = nl.getPosition();
141 return NodeLabel.TOP == p ||
142 NodeLabel.TOP_LEFT == p ||
143 NodeLabel.TOP_RIGHT == p;
144 }
145 }
146
147 return false;
148 }
149
150
151
154 private static final class ErdNodePainterImpl extends ShapeNodePainter {
155
156
162 ErdNodePainterImpl( final byte type ) {
163 super(type);
164 }
165
166
171 public void paint( final NodeRealizer context, final Graphics2D graphics ) {
172
173 if (context.labelCount() > 0) {
176 for (int i = 0, n = context.labelCount(); i < n; ++i) {
177 final NodeLabel nl = context.getLabel(i);
178 final NodeLabelModel model = nl.getLabelModel();
179 if (model instanceof ErdAttributesNodeLabelModel) {
180 nl.setOffsetDirty();
181 }
182 }
183 }
184
185 super.paint(context, graphics);
186 }
187
188
194 protected void paintFilledShape(
195 final NodeRealizer context,
196 final Graphics2D graphics,
197 final Shape shape
198 ) {
199 if (useErdStyle(context)) {
200 final NodeLabel label = context.getLabel();
202 final double y = separator(label);
203
204 final Shape oldClip = graphics.getClip();
205
206 final Rectangle2D cb;
207 if (oldClip != null) {
208 cb = oldClip.getBounds2D();
209 } else {
210 cb = new Rectangle2D.Double(context.getX() - 10, context.getY() - 10,
211 context.getWidth() + 20, context.getHeight() + 20);
212 }
213
214 final Rectangle2D.Double rectangle = new Rectangle2D.Double();
216 rectangle.setFrame(
217 cb.getX(),
218 y,
219 cb.getWidth(),
220 cb.getY() + cb.getHeight() - y);
221 graphics.clip(rectangle);
222 super.paintFilledShape(context, graphics, shape);
223 graphics.setClip(oldClip);
224
225 final Color oldColor = graphics.getColor();
226
227 rectangle.setFrame(
229 cb.getX(),
230 cb.getY(),
231 cb.getWidth(),
232 y - cb.getY());
233 graphics.clip(rectangle);
234 graphics.setColor(label.getBackgroundColor());
235 graphics.fill(shape);
236
237 graphics.setColor(oldColor);
238 graphics.setClip(oldClip);
239 } else {
240 super.paintFilledShape(context, graphics, shape);
242 }
243 }
244
245
251 protected void paintShapeBorder(
252 final NodeRealizer context,
253 final Graphics2D graphics,
254 final Shape shape
255 ) {
256 super.paintShapeBorder(context, graphics, shape);
258
259 if (useErdStyle(context)) {
261 final double y = separator(context.getLabel());
262
263 final double min = context.getY();
265 if (min < y && y < min + context.getHeight()) {
266 final double x = context.getX();
267
268 Color lc = getLineColor(context, useSelectionStyle(context, graphics));
270 if (lc != null) {
271 Stroke oldStroke = graphics.getStroke();
272 graphics.setStroke(context.getLineType());
273 graphics.setColor(lc);
274
275 final Line2D.Double line = new Line2D.Double();
276 line.setLine(x, y, x + context.getWidth(), y);
277 graphics.draw(line);
278
279 graphics.setStroke(oldStroke);
280 }
281 }
282 }
283 }
284
285
291 protected Paint getFillPaint(
292 final NodeRealizer context,
293 final boolean selected
294 ) {
295 final Color fc1 = getFillColor(context, selected);
297 if (fc1 != null) {
298 final Color fc2 = getFillColor2(context, selected);
299 if (fc2 != null) {
300 final double x = context.getX();
301 final double y = context.getY();
302
303 final double _y =
304 useErdStyle(context)
305 ? separator(context.getLabel())
306 : y;
307
308 return new GradientPaint(
309 (float) x,
310 (float) _y,
311 fc1,
312 (float) (x + context.getWidth()),
313 (float) (y + context.getHeight()),
314 fc2,
315 true);
316 } else {
317 return fc1;
318 }
319 } else {
320 return null;
321 }
322 }
323
324
330 static boolean useSelectionStyle(
331 final NodeRealizer context,
332 final Graphics2D gfx
333 ) {
334 return context.isSelected() && YRenderingHints.isSelectionPaintingEnabled(gfx);
335 }
336 }
337 }
338