]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/AsyncImage.java
Merge "ShapeNode with separate stroke and fill paints"
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / AsyncImage.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.image.impl;
13
14 import java.util.EnumSet;
15 import java.util.concurrent.Executor;
16 import java.util.concurrent.LinkedBlockingQueue;
17 import java.util.concurrent.ThreadFactory;
18 import java.util.concurrent.ThreadPoolExecutor;
19 import java.util.concurrent.TimeUnit;
20
21 import org.simantics.g2d.image.DefaultImages;
22 import org.simantics.g2d.image.Image;
23 import org.simantics.utils.datastructures.cache.IProvider;
24
25 /**
26  * AsyncImage acquires Image from IProvider asynchronously.
27  * Wait symbol is shown while symbol is acquired.
28  *
29  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
30  */
31 public class AsyncImage extends ImageProxy {
32
33     static EnumSet<Feature> caps = EnumSet.of(Feature.Volatile, Feature.Vector);
34
35     private static ThreadFactory tf = new ThreadFactory() {
36         @Override
37         public Thread newThread(Runnable r) {
38             Thread t = new Thread(new ThreadGroup("Renderer"), r);
39             t.setDaemon(false);
40             return t;
41         }
42     };
43     private static Executor EXECUTOR =
44         new ThreadPoolExecutor(
45                 0,
46                 1,
47                 3L, TimeUnit.SECONDS,
48                 new LinkedBlockingQueue<Runnable>(),
49                 tf);
50
51     public AsyncImage(final IProvider<Image> imgProvider) {
52         super(DefaultImages.UNKNOWN.get());
53         EXECUTOR.execute(new Runnable() {
54             @Override
55             public void run() {
56                 setSource(DefaultImages.GRAB.get());
57                 setSource(imgProvider.get());
58             }});
59     }
60
61     @Override
62     public EnumSet<Feature> getFeatures() {
63         return EnumSet.of(Feature.Volatile);
64     }
65
66 }
67