]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/rasterize/LazyRasterizer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / rasterize / LazyRasterizer.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 /*\r
13  * 25.1.2007\r
14  */\r
15 package org.simantics.utils.ui.gfx.rasterize;\r
16 \r
17 import java.util.LinkedList;\r
18 \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
24 \r
25 /**\r
26  * LazyRasterizer\r
27  * \r
28  * @author Toni Kalajainen\r
29  */\r
30 public class LazyRasterizer {\r
31 \r
32     public static void addJob(RasterJob job) {\r
33         getJob().addJob(job);\r
34     }\r
35 \r
36     public static void addAsFirstJob(RasterJob job) {\r
37         getJob().addAsFirstJob(job);\r
38     }\r
39 \r
40     public static void removeJob(RasterJob job) {\r
41         getJob().removeJob(job);\r
42     }\r
43 \r
44     static RasterizeJob job;\r
45 \r
46     synchronized static RasterizeJob getJob() {\r
47         if (job == null)\r
48             job = new RasterizeJob();\r
49         return job;\r
50     }\r
51 \r
52     static class RasterizeJob extends Job {\r
53         LinkedList<RasterJob> queue = new LinkedList<RasterJob>();\r
54 \r
55         @SuppressWarnings("unused")\r
56         private boolean isRunning;\r
57 \r
58         public RasterizeJob() {\r
59             super("Rasterize Job");\r
60         }\r
61 \r
62         @Override\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
67             isRunning = true;\r
68             monitor.beginTask("Rasterizing", taskLeft);\r
69             try {\r
70                 // 1. Pick a job\r
71                 RasterJob job = pullNextJob();\r
72                 do {\r
73                     // 2. Check if canceled\r
74                     if (monitor.isCanceled()) {\r
75                         cancel();\r
76                         return Status.CANCEL_STATUS;\r
77                     }\r
78                     // 3. rasterize\r
79                     if (job != null) {\r
80                         try {\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
86                         }\r
87 \r
88                         int jobsLeft = jobsLeft();\r
89                         double workLeft;\r
90                         if (jobsLeft == 0) {\r
91                             workLeft = taskLeft;\r
92                         } else {\r
93                             workLeft = ((double) taskLeft) / ((double) jobsLeft());\r
94                         }\r
95                         int worked = (int) workLeft;\r
96                         monitor.worked(worked);\r
97                         taskLeft -= worked;\r
98                     }\r
99                     monitor.subTask("");\r
100                     // 4. Pick next job\r
101                     job = pullNextJob();\r
102                 } while (job != null);\r
103                 isRunning = false;\r
104 \r
105             } finally {\r
106                 monitor.done();\r
107             }\r
108             return Status.OK_STATUS;\r
109         }\r
110 \r
111         protected synchronized int jobsLeft() {\r
112             return queue.size();\r
113         }\r
114 \r
115         protected synchronized RasterJob pullNextJob() {\r
116             if (queue.isEmpty())\r
117                 return null;\r
118             return queue.removeFirst();\r
119         }\r
120 \r
121         public synchronized void clear() {\r
122             RasterJob jobs[] = queue.toArray(new RasterJob[0]);\r
123             for (RasterJob j : jobs)\r
124                 removeJob(j);\r
125         }\r
126 \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
131                 schedule();\r
132         }\r
133 \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
138                 schedule();\r
139         }\r
140         \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
145                 return true;\r
146             }\r
147             return false;\r
148         }\r
149 \r
150     }\r
151 \r
152 }\r