--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.example;\r
+\r
+import java.awt.Color;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.DiagramClass;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.impl.Diagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.Heartbeat;\r
+import org.simantics.g2d.element.handler.Text;\r
+import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
+import org.simantics.g2d.element.handler.impl.FixedSize;\r
+import org.simantics.g2d.element.handler.impl.TextColorImpl;\r
+import org.simantics.g2d.element.handler.impl.TextPainter;\r
+import org.simantics.g2d.element.impl.Element;\r
+import org.simantics.g2d.elementclass.FilmClass;\r
+\r
+/**\r
+ * Diagram with LOADING... text\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class LoadingMessageDiagram {\r
+\r
+ public static final ElementClass LOADING_CLASS =\r
+ ElementClass.compile(\r
+ TextColorImpl.WHITE,\r
+ DefaultTransform.INSTANCE,\r
+ FixedSize.of(30, 20),\r
+ Text.INSTANCE,\r
+ new TextPainter(),\r
+ new LoadingAnimation()\r
+ );\r
+\r
+ public static final IDiagram LOADING_DIAGRAM = createLoadingDiagram();\r
+\r
+ /**\r
+ * Create diagram where flashing "LOADING" text\r
+ * @return\r
+ */\r
+ public static IDiagram createLoadingDiagram()\r
+ {\r
+ String name = "Loading...";\r
+ IDiagram d = Diagram.spawnNew(DiagramClass.DEFAULT);\r
+ d.setHint(DiagramHints.KEY_TEXT, name);\r
+\r
+ IElement film = Element.spawnNew(FilmClass.FILM_CLASS);\r
+ ElementUtils.fitToRectangle(film, new Rectangle2D.Double(0, 4, 40, 30));\r
+ d.addElement(film);\r
+ ElementUtils.setFillColor(film, new Color(0.3f, 0.3f, 0.8f, 0.5f));\r
+\r
+ IElement lbl = Element.spawnNew( LOADING_CLASS );\r
+ d.addElement(lbl);\r
+ ElementUtils.setPos(lbl, 5,7);\r
+ ElementUtils.setText(lbl, "Loading");\r
+ ElementUtils.setTextColor(lbl, Color.WHITE);\r
+\r
+ return d;\r
+ }\r
+\r
+\r
+\r
+ private static class LoadingAnimation implements Heartbeat {\r
+\r
+ private static final long serialVersionUID = 2721735864087380222L;\r
+\r
+ @Override\r
+ public void heartbeat(IElement e, long time, long deltaTime, ICanvasContext ctx) {\r
+ int dotCount = (int) ((time % 1000L) / 200L);\r
+ String dots = "";\r
+ for (int i=0; i<dotCount; i++)\r
+ dots += ".";\r
+ ElementUtils.setText(e, "Loading"+dots);\r
+\r
+ ctx.getContentContext().setDirty();\r
+ }\r
+\r
+ }\r
+\r
+}\r