1
28 package demo.view.viewmode;
29
30 import demo.view.DemoBase;
31 import demo.view.DemoDefaults;
32
33 import y.anim.AnimationFactory;
34 import y.anim.AnimationObject;
35 import y.anim.AnimationPlayer;
36 import y.base.Node;
37 import y.base.NodeMap;
38 import y.view.EditMode;
39 import y.view.Graph2DViewActions;
40 import y.view.Graph2DViewRepaintManager;
41 import y.view.HitInfo;
42 import y.view.NodeRealizer;
43 import y.view.ShapeNodeRealizer;
44 import y.view.ViewAnimationFactory;
45 import y.view.ViewMode;
46 import y.view.AutoDragViewMode;
47 import y.view.DefaultGraph2DRenderer;
48 import y.util.DefaultMutableValue2D;
49 import y.util.Value2D;
50 import y.view.YRenderingHints;
51
52 import javax.swing.ActionMap;
53 import javax.swing.InputMap;
54 import javax.swing.JComponent;
55 import java.awt.Dimension;
56 import java.awt.EventQueue;
57 import java.util.Locale;
58
59
60
66 public class RollOverEffectDemo extends DemoBase {
67
68 public RollOverEffectDemo() {
69 final DefaultGraph2DRenderer g2dr = new DefaultGraph2DRenderer();
70 g2dr.setDrawEdgesFirst(true);
71 view.setGraph2DRenderer(g2dr);
72 view.setPreferredSize(new Dimension(800, 600));
73 view.getRenderingHints().put(ShapeNodeRealizer.KEY_SLOPPY_RECT_PAINTING,
74 ShapeNodeRealizer.VALUE_SLOPPY_RECT_PAINTING_OFF);
75 view.getRenderingHints().put(YRenderingHints.KEY_SLOPPY_POLYLINE_PAINTING,
76 YRenderingHints.VALUE_SLOPPY_POLYLINE_PAINTING_OFF);
77 loadInitialGraph();
78 }
79
80 protected void configureDefaultRealizers() {
81 DemoDefaults.registerDefaultNodeConfiguration(false);
83 DemoDefaults.configureDefaultRealizers(view);
84 }
85
86
89 protected void registerViewModes() {
90 final EditMode editMode = createEditMode();
91 if (editMode != null) {
92 view.addViewMode(editMode);
93 }
94 view.addViewMode(new AutoDragViewMode());
95 view.addViewMode(new RollOverViewMode());
96
97 final Graph2DViewActions actions = new Graph2DViewActions(view);
99 ActionMap amap = view.getCanvasComponent().getActionMap();
100 amap.remove(Graph2DViewActions.EDIT_LABEL);
101 InputMap imap = actions.createDefaultInputMap(amap);
102 view.getCanvasComponent().setInputMap(JComponent.WHEN_FOCUSED, imap);
103 }
104
105
108 protected void loadInitialGraph() {
109 loadGraph("resource/rollover.graphml");
110 }
111
112
113 public static void main(String[] args) {
114 EventQueue.invokeLater(new Runnable() {
115 public void run() {
116 Locale.setDefault(Locale.ENGLISH);
117 initLnF();
118 (new RollOverEffectDemo()).start();
119 }
120 });
121 }
122
123
127 private static final class RollOverViewMode extends ViewMode {
128
129 private static final int NONE = 0;
130
131 private static final int MARKED = 1;
132
133 private static final int UNMARK = 2;
134
135
136
137 private static final int PREFERRED_DURATION = 350;
138
139
140 private static final Value2D SCALE_FACTOR =
141 DefaultMutableValue2D.create(3, 3);
142
143
144
145 private Node lastHitNode;
146
147 private NodeMap size;
148
149 private NodeMap state;
150
151 private ViewAnimationFactory factory;
152 private AnimationPlayer player;
153
154
157 public void mouseMoved( final double x, final double y ) {
158 final HitInfo hi = getHitInfo(x, y);
159 if (hi.hasHitNodes()) {
160 final Node node = (Node) hi.hitNodes().current();
161 if (node != lastHitNode) {
162 unmark(lastHitNode);
163 }
164 if (state.getInt(node) == NONE) {
165 mark(node);
166 lastHitNode = node;
167 }
168 } else {
169 unmark(lastHitNode);
170 lastHitNode = null;
171 }
172 }
173
174
178 public void activate( final boolean b ) {
179 if (b) {
180 factory = new ViewAnimationFactory(new Graph2DViewRepaintManager(view));
181 player = factory.createConfiguredPlayer();
182 size = view.getGraph2D().createNodeMap();
183 state = view.getGraph2D().createNodeMap();
184 } else {
185 view.getGraph2D().disposeNodeMap(state);
186 view.getGraph2D().disposeNodeMap(size);
187 state = null;
188 size = null;
189 player = null;
190 factory = null;
191 }
192 super.activate(b);
193 }
194
195
198 protected HitInfo getHitInfo( final double x, final double y ) {
199 final HitInfo hi = DemoBase.checkNodeHit(view, x, y);
200 setLastHitInfo(hi);
201 return hi;
202 }
203
204
208 protected void mark( final Node node ) {
209 if (state.getInt(node) == NONE) {
212 state.setInt(node, MARKED);
213
214 final NodeRealizer nr = getGraph2D().getRealizer(node);
215 size.set(node, DefaultMutableValue2D.create(nr.getWidth(), nr.getHeight()));
216 final AnimationObject ao = factory.scale(
217 nr,
218 SCALE_FACTOR,
219 ViewAnimationFactory.APPLY_EFFECT,
220 PREFERRED_DURATION);
221 player.animate(AnimationFactory.createEasedAnimation(ao));
222 }
223 }
224
225
229 protected void unmark( final Node node ) {
230 if (node == null) {
231 return;
232 }
233
234 if (state.getInt(node) == MARKED) {
237 state.setInt(node, UNMARK);
238
239 final Value2D oldSize = (Value2D) size.get(node);
240 final NodeRealizer nr = getGraph2D().getRealizer(node);
241 final AnimationObject ao = factory.resize(
242 nr,
243 oldSize,
244 ViewAnimationFactory.APPLY_EFFECT,
245 PREFERRED_DURATION);
246 final AnimationObject eao = AnimationFactory.createEasedAnimation(ao);
247 player.animate(new Reset(eao, node, nr, oldSize));
248 }
249 }
250
251
254 private final class Reset implements AnimationObject {
255 private AnimationObject ao;
256 private final Node node;
257 private final NodeRealizer nr;
258 private final Value2D oldSize;
259
260 Reset(
261 final AnimationObject ao,
262 final Node node,
263 final NodeRealizer nr,
264 final Value2D size
265 ) {
266 this.ao = ao;
267 this.node = node;
268 this.nr = nr;
269 this.oldSize = size;
270 }
271
272 public void initAnimation() {
273 ao.initAnimation();
274 }
275
276 public void calcFrame( final double time ) {
277 ao.calcFrame(time);
278 }
279
280
284 public void disposeAnimation() {
285 ao.disposeAnimation();
286 nr.setSize(oldSize.getX(), oldSize.getY());
287 size.set(node, null);
288 state.setInt(node, NONE);
289 }
290
291 public long preferredDuration() {
292 return ao.preferredDuration();
293 }
294 }
295 }
296 }
297