1
28 package demo.view.mindmap;
29
30 import demo.view.mindmap.StateIconProvider.StateIcon;
31
32 import y.view.GenericNodeRealizer;
33 import y.view.LineType;
34 import y.view.NodeRealizer;
35 import y.view.YRenderingHints;
36
37 import java.awt.Color;
38 import java.awt.Graphics2D;
39 import java.awt.RenderingHints;
40 import java.awt.geom.Line2D;
41
42
46 class MindMapNodePainter implements GenericNodeRealizer.Painter{
47 private static final String KEY_STATE_ICON = "MindMapNodePainter.StateIcon";
48
49
54 public void paint(final NodeRealizer context, Graphics2D graphics) {
55 paintImpl(context, graphics, false);
56 }
57
58
63 public void paintSloppy(final NodeRealizer context, final Graphics2D graphics) {
64 paintImpl(context, graphics, true);
65 }
66
67 private void paintImpl(final NodeRealizer context, Graphics2D graphics, final boolean sloppy) {
68 graphics = (Graphics2D) graphics.create();
69
70 final Color fc = context.getFillColor();
71 if (fc != null) {
72 final double x = context.getX();
73 final double y = context.getY() + context.getHeight();
74 graphics.setColor(fc);
75 graphics.setStroke(context.getLineType());
76 graphics.draw(new Line2D.Double(x, y, x + context.getWidth(), y));
77 }
78
79 final Color lc = context.getLineColor();
80 if (lc != null && context.isSelected() && YRenderingHints.isSelectionPaintingEnabled(graphics)) {
81 graphics.setColor(lc);
82 graphics.setStroke(LineType.LINE_1);
83 graphics.draw(context.getBoundingBox());
84 }
85
86 if (!sloppy) {
87 final StateIcon icon = getStateIcon(context);
88 if (icon != null) {
89 int xoffset = 0;
90 if (ViewModel.instance.isLeft(context.getNode())) {
91 xoffset = (int) context.getWidth() - 16;
92 }
93 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
94 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
95 graphics.translate(context.getX() + xoffset, context.getY());
96 icon.paintIcon(null, graphics, 0, 0);
97 }
98 }
99 graphics.dispose();
100 }
101
102
108 static StateIcon getStateIcon( final NodeRealizer nr ) {
109 if (nr instanceof GenericNodeRealizer) {
110 final Object name = ((GenericNodeRealizer) nr).getStyleProperty(KEY_STATE_ICON);
111 if (name instanceof String) {
112 return StateIconProvider.instance.getIcon((String) name);
113 }
114 }
115 return null;
116 }
117
118
125 static void setStateIcon( final NodeRealizer nr, final StateIcon icon ) {
126 if (nr instanceof GenericNodeRealizer) {
127 if (icon == null || icon.getName() == null) {
128 ((GenericNodeRealizer) nr).removeStyleProperty(KEY_STATE_ICON);
129 } else {
130 ((GenericNodeRealizer) nr).setStyleProperty(KEY_STATE_ICON, icon.getName());
131 }
132 }
133 }
134 }
135