]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/rasterize/LazyRasterizer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / rasterize / LazyRasterizer.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 /*
13  * 25.1.2007
14  */
15 package org.simantics.utils.ui.gfx.rasterize;
16
17 import java.util.LinkedList;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.swt.graphics.ImageData;
24
25 /**
26  * LazyRasterizer
27  * 
28  * @author Toni Kalajainen
29  */
30 public class LazyRasterizer {
31
32     public static void addJob(RasterJob job) {
33         getJob().addJob(job);
34     }
35
36     public static void addAsFirstJob(RasterJob job) {
37         getJob().addAsFirstJob(job);
38     }
39
40     public static void removeJob(RasterJob job) {
41         getJob().removeJob(job);
42     }
43
44     static RasterizeJob job;
45
46     synchronized static RasterizeJob getJob() {
47         if (job == null)
48             job = new RasterizeJob();
49         return job;
50     }
51
52     static class RasterizeJob extends Job {
53         LinkedList<RasterJob> queue = new LinkedList<RasterJob>();
54
55         @SuppressWarnings("unused")
56         private boolean isRunning;
57
58         public RasterizeJob() {
59             super("Rasterize Job");
60         }
61
62         @Override
63         protected IStatus run(IProgressMonitor monitor) {
64             //System.out.println(Thread.currentThread());
65             setThread(Thread.currentThread());
66             int taskLeft = 10000;
67             isRunning = true;
68             monitor.beginTask("Rasterizing", taskLeft);
69             try {
70                 // 1. Pick a job
71                 RasterJob job = pullNextJob();
72                 do {
73                     // 2. Check if canceled
74                     if (monitor.isCanceled()) {
75                         cancel();
76                         return Status.CANCEL_STATUS;
77                     }
78                     // 3. rasterize
79                     if (job != null) {
80                         try {
81                             monitor.subTask(job.raster.toString());
82                             ImageData id = job.raster.rasterize(job.width, job.height);
83                             job.listener.rasterizationComplete(job, id);
84                         } catch (Exception e) {
85                             job.listener.rasterizationFailed(job, e);
86                         }
87
88                         int jobsLeft = jobsLeft();
89                         double workLeft;
90                         if (jobsLeft == 0) {
91                             workLeft = taskLeft;
92                         } else {
93                             workLeft = ((double) taskLeft) / ((double) jobsLeft());
94                         }
95                         int worked = (int) workLeft;
96                         monitor.worked(worked);
97                         taskLeft -= worked;
98                     }
99                     monitor.subTask("");
100                     // 4. Pick next job
101                     job = pullNextJob();
102                 } while (job != null);
103                 isRunning = false;
104
105             } finally {
106                 monitor.done();
107             }
108             return Status.OK_STATUS;
109         }
110
111         protected synchronized int jobsLeft() {
112             return queue.size();
113         }
114
115         protected synchronized RasterJob pullNextJob() {
116             if (queue.isEmpty())
117                 return null;
118             return queue.removeFirst();
119         }
120
121         public synchronized void clear() {
122             RasterJob jobs[] = queue.toArray(new RasterJob[0]);
123             for (RasterJob j : jobs)
124                 removeJob(j);
125         }
126
127         public synchronized void addJob(RasterJob job) {            
128             queue.addLast(job);
129             job.status = RasterJobStatus.QUEUED;
130             //if (!isRunning && queue.size()==1)
131                 schedule();
132         }
133
134         public synchronized void addAsFirstJob(RasterJob job) {            
135             queue.addFirst(job);
136             job.status = RasterJobStatus.QUEUED;
137             //if (!isRunning && queue.size()==1)
138                 schedule();
139         }
140         
141         public synchronized boolean removeJob(RasterJob job) {
142             if (queue.remove(job)) {
143                 job.status = RasterJobStatus.COMPLETE;
144                 job.listener.rasterizationCanceled(job);
145                 return true;
146             }
147             return false;
148         }
149
150     }
151
152 }