1
28 package demo.view.hierarchy;
29
30 import org.w3c.dom.Document;
31 import y.anim.AnimationObject;
32 import y.anim.AnimationPlayer;
33 import y.base.Node;
34 import y.base.NodeCursor;
35 import y.io.GraphMLIOHandler;
36 import y.view.GenericNodeRealizer;
37 import y.view.Graph2D;
38 import y.view.Graph2DTraversal;
39 import y.view.Graph2DViewRepaintManager;
40 import y.view.HitInfo;
41 import y.view.NodeRealizer;
42 import y.view.ProxyShapeNodeRealizer;
43 import y.view.ViewMode;
44 import y.view.hierarchy.DefaultHierarchyGraphFactory;
45 import y.view.hierarchy.GroupNodePainter;
46
47 import java.awt.EventQueue;
48 import java.beans.PropertyChangeEvent;
49 import java.beans.PropertyChangeListener;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.util.Locale;
53
54
61 public class FadingGroupStateIconDemo extends GroupingDemo {
62
63
66 protected void registerViewModes() {
67 super.registerViewModes();
68 view.addViewMode(new FadingIconViewMode());
69 }
70
71
76 protected GraphMLIOHandler createGraphMLIOHandler() {
77 return new GraphMLIOHandler(){
78 public void read(Graph2D graph, InputStream in) throws IOException {
79 super.read(graph, in);
80 setTransparentGroupState(graph);
81 }
82
83 public void read(Graph2D graph, Document documentElement) throws IOException {
84 super.read(graph, documentElement);
85 setTransparentGroupState(graph);
86 }
87
88 private void setTransparentGroupState(Graph2D graph) {
89 for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
90 setOpacity(graph.getRealizer(nc.node()), 0);
91 }
92 }
93 };
94 }
95
96
99 protected void configureDefaultGroupNodeRealizers() {
100 getHierarchyManager().setGraphFactory(new DefaultHierarchyGraphFactory() {
105 public NodeRealizer createNodeRealizer(Object hint) {
106 final NodeRealizer nr = super.createNodeRealizer(hint);
107 setOpacity(nr, 0);
108 return nr;
109 }
110 });
111 super.configureDefaultGroupNodeRealizers();
112 }
113
114
120 private static void setOpacity(NodeRealizer realizer, float opacity) {
121 if (realizer instanceof ProxyShapeNodeRealizer) {
122 final ProxyShapeNodeRealizer proxyRealizer = (ProxyShapeNodeRealizer) realizer;
124 for (int i = 0; i < proxyRealizer.realizerCount(); i++) {
125 setOpacity(proxyRealizer.getRealizer(i), opacity);
126 }
127 } else {
128 GroupNodePainter.GroupStateStyle style = (GroupNodePainter.GroupStateStyle) ((GenericNodeRealizer) realizer).getStyleProperty(
130 GroupNodePainter.GROUP_STATE_STYLE_ID);
131 if (style == null) {
132 style = new GroupNodePainter.GroupStateStyle();
133 ((GenericNodeRealizer) realizer).setStyleProperty(GroupNodePainter.GROUP_STATE_STYLE_ID, style);
134 }
135 style.setOpacity(opacity);
136 }
137 }
138
139
143 public static void main(String[] args) {
144 EventQueue.invokeLater(new Runnable() {
145 public void run() {
146 Locale.setDefault(Locale.ENGLISH);
147 initLnF();
148 (new FadingGroupStateIconDemo()).start();
149 }
150 });
151 }
152
153
157 public class FadingIconViewMode extends ViewMode {
158
159
160 private AnimationPlayer player;
161
162
163 private Node lastSeenNode;
164
165
166 private Graph2DViewRepaintManager repaintManager;
167
168
169 private FadingIconAnimation lastFadeInAnimation;
170
171
174 public FadingIconViewMode() {
175 player = new AnimationPlayer(false);
176
177 addPropertyChangeListener(new PropertyChangeListener() {
180 public void propertyChange(PropertyChangeEvent e) {
181 if (ACTIVE_VIEW_PROPERTY.equals(e.getPropertyName())) {
182 if (repaintManager != null) {
183 player.removeAnimationListener(repaintManager);
184 }
185 repaintManager = new Graph2DViewRepaintManager(view);
186 player.addAnimationListener(repaintManager);
187 }
188 }
189 });
190 }
191
192
196 public void mouseMoved(double x, double y) {
197 final Graph2D graph = view.getGraph2D();
198 if (graph.getHierarchyManager() != null) {
199 final HitInfo hitInfo =
200 view.getHitInfoFactory().createHitInfo(x, y, Graph2DTraversal.NODES, true);
201
202 Node n = null;
204 if (hitInfo.hasHitNodes()) {
205 n = hitInfo.getHitNode();
206 if (graph.getHierarchyManager().isNormalNode(n)) {
207 if (graph.getHierarchyManager().getParentNode(n) == lastSeenNode) {
208 n = lastSeenNode;
209 } else {
210 n = null;
211 }
212 }
213 }
214
215 if (lastSeenNode != n && lastSeenNode != null && lastSeenNode.getGraph() != null) {
217 if (lastFadeInAnimation != null) {
218 lastFadeInAnimation.setActive(false);
219 }
220
221 final NodeRealizer lastSeenNodeRealizer = ((Graph2D) lastSeenNode.getGraph()).getRealizer(lastSeenNode);
222 player.animate(new FadingIconAnimation(lastSeenNodeRealizer, false));
223
224 lastSeenNode = null;
225 }
226
227 if (lastSeenNode != n && n != null) {
229 final NodeRealizer hitNodeRealizer = ((Graph2D) n.getGraph()).getRealizer(n);
230
231 final FadingIconAnimation animation = new FadingIconAnimation(hitNodeRealizer, true);
232 player.animate(animation);
233
234 lastFadeInAnimation = animation;
235 lastSeenNode = n;
236 }
237 }
238 }
239
240
244 private final class FadingIconAnimation implements AnimationObject {
245
246
247 private static final int FADE_DURATION = 500;
248
249
250 private float startValue;
251
252
253 private NodeRealizer realizer;
254
255
256 private boolean fadeIn;
257
258
259 private boolean active;
260
261
266 public FadingIconAnimation(NodeRealizer realizer, boolean fadeIn) {
267 this.realizer = realizer;
268 this.fadeIn = fadeIn;
269 this.active = true;
270 }
271
272
275 public void initAnimation() {
276 if (repaintManager != null) {
277 repaintManager.add(realizer);
278 }
279
280 if (fadeIn) {
281 setOpacity(realizer, 0);
283 } else {
284 startValue = getOpacity(realizer);
286 }
287 }
288
289
293 public void calcFrame(double time) {
294 if (active) {
295 if (fadeIn) {
296 setOpacity(realizer, (float) time);
297 } else {
298 setOpacity(realizer, startValue * (1 - (float) time));
299 }
300 }
301 }
302
303 public void disposeAnimation() {
304 if (repaintManager != null) {
305 repaintManager.remove(realizer);
306 }
307 }
308
309
313 public long preferredDuration() {
314 if (fadeIn) {
315 return FADE_DURATION;
316 } else {
317 if (startValue > 0) {
318 return (long) (FADE_DURATION * startValue);
320 } else {
321 return 0;
322 }
323 }
324 }
325
326
331 public void setActive(boolean active) {
332 this.active = active;
333 }
334
335
340 private float getOpacity(NodeRealizer realizer) {
341 NodeRealizer r = getDelegateRealizer(realizer);
342 return ((GroupNodePainter.GroupStateStyle) ((GenericNodeRealizer) r).getStyleProperty(
343 GroupNodePainter.GROUP_STATE_STYLE_ID)).getOpacity();
344 }
345
346
352 private GenericNodeRealizer getDelegateRealizer(NodeRealizer realizer) {
353 if (realizer instanceof ProxyShapeNodeRealizer) {
354 return (GenericNodeRealizer) ((ProxyShapeNodeRealizer) realizer).getRealizerDelegate();
355 } else {
356 return (GenericNodeRealizer) realizer;
357 }
358 }
359 }
360 }
361 }