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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
15 package org.simantics.utils.ui.gfx.rasterize;
\r
17 import java.util.LinkedList;
\r
19 import org.eclipse.core.runtime.IProgressMonitor;
\r
20 import org.eclipse.core.runtime.IStatus;
\r
21 import org.eclipse.core.runtime.Status;
\r
22 import org.eclipse.core.runtime.jobs.Job;
\r
23 import org.eclipse.swt.graphics.ImageData;
\r
28 * @author Toni Kalajainen
\r
30 public class LazyRasterizer {
\r
32 public static void addJob(RasterJob job) {
\r
33 getJob().addJob(job);
\r
36 public static void addAsFirstJob(RasterJob job) {
\r
37 getJob().addAsFirstJob(job);
\r
40 public static void removeJob(RasterJob job) {
\r
41 getJob().removeJob(job);
\r
44 static RasterizeJob job;
\r
46 synchronized static RasterizeJob getJob() {
\r
48 job = new RasterizeJob();
\r
52 static class RasterizeJob extends Job {
\r
53 LinkedList<RasterJob> queue = new LinkedList<RasterJob>();
\r
55 @SuppressWarnings("unused")
\r
56 private boolean isRunning;
\r
58 public RasterizeJob() {
\r
59 super("Rasterize Job");
\r
63 protected IStatus run(IProgressMonitor monitor) {
\r
64 //System.out.println(Thread.currentThread());
\r
65 setThread(Thread.currentThread());
\r
66 int taskLeft = 10000;
\r
68 monitor.beginTask("Rasterizing", taskLeft);
\r
71 RasterJob job = pullNextJob();
\r
73 // 2. Check if canceled
\r
74 if (monitor.isCanceled()) {
\r
76 return Status.CANCEL_STATUS;
\r
81 monitor.subTask(job.raster.toString());
\r
82 ImageData id = job.raster.rasterize(job.width, job.height);
\r
83 job.listener.rasterizationComplete(job, id);
\r
84 } catch (Exception e) {
\r
85 job.listener.rasterizationFailed(job, e);
\r
88 int jobsLeft = jobsLeft();
\r
90 if (jobsLeft == 0) {
\r
91 workLeft = taskLeft;
\r
93 workLeft = ((double) taskLeft) / ((double) jobsLeft());
\r
95 int worked = (int) workLeft;
\r
96 monitor.worked(worked);
\r
99 monitor.subTask("");
\r
100 // 4. Pick next job
\r
101 job = pullNextJob();
\r
102 } while (job != null);
\r
108 return Status.OK_STATUS;
\r
111 protected synchronized int jobsLeft() {
\r
112 return queue.size();
\r
115 protected synchronized RasterJob pullNextJob() {
\r
116 if (queue.isEmpty())
\r
118 return queue.removeFirst();
\r
121 public synchronized void clear() {
\r
122 RasterJob jobs[] = queue.toArray(new RasterJob[0]);
\r
123 for (RasterJob j : jobs)
\r
127 public synchronized void addJob(RasterJob job) {
\r
128 queue.addLast(job);
\r
129 job.status = RasterJobStatus.QUEUED;
\r
130 //if (!isRunning && queue.size()==1)
\r
134 public synchronized void addAsFirstJob(RasterJob job) {
\r
135 queue.addFirst(job);
\r
136 job.status = RasterJobStatus.QUEUED;
\r
137 //if (!isRunning && queue.size()==1)
\r
141 public synchronized boolean removeJob(RasterJob job) {
\r
142 if (queue.remove(job)) {
\r
143 job.status = RasterJobStatus.COMPLETE;
\r
144 job.listener.rasterizationCanceled(job);
\r