1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.example;
14 import java.awt.Color;
15 import java.awt.geom.Rectangle2D;
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;
35 * Diagram with LOADING... text
37 * @author Toni Kalajainen
39 public class LoadingMessageDiagram {
41 public static final ElementClass LOADING_CLASS =
44 DefaultTransform.INSTANCE,
48 new LoadingAnimation()
51 public static final IDiagram LOADING_DIAGRAM = createLoadingDiagram();
54 * Create diagram where flashing "LOADING" text
57 public static IDiagram createLoadingDiagram()
59 String name = "Loading...";
60 IDiagram d = Diagram.spawnNew(DiagramClass.DEFAULT);
61 d.setHint(DiagramHints.KEY_TEXT, name);
63 IElement film = Element.spawnNew(FilmClass.FILM_CLASS);
64 ElementUtils.fitToRectangle(film, new Rectangle2D.Double(0, 4, 40, 30));
66 ElementUtils.setFillColor(film, new Color(0.3f, 0.3f, 0.8f, 0.5f));
68 IElement lbl = Element.spawnNew( LOADING_CLASS );
70 ElementUtils.setPos(lbl, 5,7);
71 ElementUtils.setText(lbl, "Loading");
72 ElementUtils.setTextColor(lbl, Color.WHITE);
79 private static class LoadingAnimation implements Heartbeat {
81 private static final long serialVersionUID = 2721735864087380222L;
84 public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {
85 int dotCount = (int) ((time % 1000L) / 200L);
87 for (int i=0; i<dotCount; i++)
89 ElementUtils.setText(e, "Loading"+dots);
91 ctx.getContentContext().setDirty();