1 /*******************************************************************************
2 * Copyright (c) 2012 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.maps.pojo;
14 import java.awt.Image;
15 import java.util.LinkedList;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.ScheduledThreadPoolExecutor;
19 import org.simantics.maps.ProvisionException;
20 import org.simantics.maps.query.Query;
21 import org.simantics.maps.tile.IFilter;
22 import org.simantics.maps.tile.ITileProvider;
23 import org.simantics.maps.tile.TileKey;
26 * @author Tuukka Lehtonen
29 public class TileJob implements Runnable {
31 LinkedList<Query<TileKey, Image>> queue = new LinkedList<Query<TileKey, Image>>();
32 Executor executor = new ScheduledThreadPoolExecutor(1);
33 ITileProvider provider;
38 public void setTileProvider(ITileProvider provider) {
39 this.provider = provider;
42 protected Image doQuery(TileKey key) throws ProvisionException {
43 return provider.get(key);
47 Query<TileKey, Image> job = pullNextJob();
51 Image result = doQuery(job.source);
52 job.listener.queryComplete(job, result);
53 } catch (Exception e) {
54 job.listener.queryFailed(job, e);
59 } while (job != null);
62 protected synchronized int jobsLeft() {
66 protected synchronized Query<TileKey, Image> pullNextJob() {
69 return queue.removeFirst();
72 public synchronized void clear() {
73 @SuppressWarnings("unchecked")
74 Query<TileKey, Image> jobs[] = queue.toArray(new Query[queue.size()]);
75 for (Query<TileKey, Image> j : jobs)
79 public synchronized void addJob(Query<TileKey, Image> job) {
81 if (queue.size() == 1) {
82 executor.execute(this);
86 public synchronized void addAsFirstJob(Query<TileKey, Image> job) {
88 if (queue.size() == 1) {
89 executor.execute(this);
93 public synchronized boolean removeJob(Query<TileKey, Image> job) {
94 if (queue.remove(job)) {
95 job.listener.queryCanceled(job);
101 public synchronized void filterQueries(IFilter<TileKey> filter) {
105 LinkedList<Query<TileKey, Image>> result = new LinkedList<Query<TileKey, Image>>();
106 for (Query<TileKey, Image> query : queue) {
107 if (filter.select(query.source))
110 query.listener.queryCanceled(query);