]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/AsyncImage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / AsyncImage.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.image.impl;\r
13 \r
14 import java.util.EnumSet;\r
15 import java.util.concurrent.Executor;\r
16 import java.util.concurrent.LinkedBlockingQueue;\r
17 import java.util.concurrent.ThreadFactory;\r
18 import java.util.concurrent.ThreadPoolExecutor;\r
19 import java.util.concurrent.TimeUnit;\r
20 \r
21 import org.simantics.g2d.image.DefaultImages;\r
22 import org.simantics.g2d.image.Image;\r
23 import org.simantics.utils.datastructures.cache.IProvider;\r
24 \r
25 /**\r
26  * AsyncImage acquires Image from IProvider asynchronously.\r
27  * Wait symbol is shown while symbol is acquired.\r
28  *\r
29  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
30  */\r
31 public class AsyncImage extends ImageProxy {\r
32 \r
33     static EnumSet<Feature> caps = EnumSet.of(Feature.Volatile, Feature.Vector);\r
34 \r
35     private static ThreadFactory tf = new ThreadFactory() {\r
36         @Override\r
37         public Thread newThread(Runnable r) {\r
38             Thread t = new Thread(new ThreadGroup("Renderer"), r);\r
39             t.setDaemon(false);\r
40             return t;\r
41         }\r
42     };\r
43     private static Executor EXECUTOR =\r
44         new ThreadPoolExecutor(\r
45                 0,\r
46                 1,\r
47                 3L, TimeUnit.SECONDS,\r
48                 new LinkedBlockingQueue<Runnable>(),\r
49                 tf);\r
50 \r
51     public AsyncImage(final IProvider<Image> imgProvider) {\r
52         super(DefaultImages.UNKNOWN.get());\r
53         EXECUTOR.execute(new Runnable() {\r
54             @Override\r
55             public void run() {\r
56                 setSource(DefaultImages.GRAB.get());\r
57                 setSource(imgProvider.get());\r
58             }});\r
59     }\r
60 \r
61     @Override\r
62     public EnumSet<Feature> getFeatures() {\r
63         return EnumSet.of(Feature.Volatile);\r
64     }\r
65 \r
66 }\r
67 \r