]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/example/LoadingMessageDiagram.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / example / LoadingMessageDiagram.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.example;
13
14 import java.awt.Color;
15 import java.awt.geom.Rectangle2D;
16
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.diagram.DiagramClass;
19 import org.simantics.g2d.diagram.DiagramHints;
20 import org.simantics.g2d.diagram.IDiagram;
21 import org.simantics.g2d.diagram.impl.Diagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.ElementUtils;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.handler.Heartbeat;
26 import org.simantics.g2d.element.handler.Text;
27 import org.simantics.g2d.element.handler.impl.DefaultTransform;
28 import org.simantics.g2d.element.handler.impl.FixedSize;
29 import org.simantics.g2d.element.handler.impl.TextColorImpl;
30 import org.simantics.g2d.element.handler.impl.TextPainter;
31 import org.simantics.g2d.element.impl.Element;
32 import org.simantics.g2d.elementclass.FilmClass;
33
34 /**
35  * Diagram with LOADING... text
36  * 
37  * @author Toni Kalajainen
38  */
39 public class LoadingMessageDiagram {
40
41     public static final ElementClass LOADING_CLASS =
42         ElementClass.compile(
43                 TextColorImpl.WHITE,
44                 DefaultTransform.INSTANCE,
45                 FixedSize.of(30, 20),
46                 Text.INSTANCE,
47                 new TextPainter(),
48                 new LoadingAnimation()
49         );
50
51     public static final IDiagram LOADING_DIAGRAM = createLoadingDiagram();
52
53     /**
54      * Create diagram where flashing "LOADING" text
55      * @return
56      */
57     public static IDiagram createLoadingDiagram()
58     {
59         String name = "Loading...";
60         IDiagram d = Diagram.spawnNew(DiagramClass.DEFAULT);
61         d.setHint(DiagramHints.KEY_TEXT, name);
62
63         IElement film = Element.spawnNew(FilmClass.FILM_CLASS);
64         ElementUtils.fitToRectangle(film, new Rectangle2D.Double(0, 4, 40, 30));
65         d.addElement(film);
66         ElementUtils.setFillColor(film, new Color(0.3f, 0.3f, 0.8f, 0.5f));
67
68         IElement lbl = Element.spawnNew( LOADING_CLASS );
69         d.addElement(lbl);
70         ElementUtils.setPos(lbl, 5,7);
71         ElementUtils.setText(lbl, "Loading");
72         ElementUtils.setTextColor(lbl, Color.WHITE);
73
74         return d;
75     }
76
77
78
79     private static class LoadingAnimation implements Heartbeat {
80
81         private static final long serialVersionUID = 2721735864087380222L;
82
83         @Override
84         public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
85             int dotCount = (int) ((time % 1000L) / 200L);
86             String dots = "";
87             for (int i=0; i<dotCount; i++)
88                 dots += ".";
89             ElementUtils.setText(e, "Loading"+dots);
90
91             ctx.getContentContext().setDirty();
92         }
93
94     }
95
96 }