-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- * 25.1.2007\r
- */\r
-package org.simantics.utils.ui.gfx.rasterize;\r
-\r
-import java.util.LinkedList;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.eclipse.swt.graphics.ImageData;\r
-\r
-/**\r
- * LazyRasterizer\r
- * \r
- * @author Toni Kalajainen\r
- */\r
-public class LazyRasterizer {\r
-\r
- public static void addJob(RasterJob job) {\r
- getJob().addJob(job);\r
- }\r
-\r
- public static void addAsFirstJob(RasterJob job) {\r
- getJob().addAsFirstJob(job);\r
- }\r
-\r
- public static void removeJob(RasterJob job) {\r
- getJob().removeJob(job);\r
- }\r
-\r
- static RasterizeJob job;\r
-\r
- synchronized static RasterizeJob getJob() {\r
- if (job == null)\r
- job = new RasterizeJob();\r
- return job;\r
- }\r
-\r
- static class RasterizeJob extends Job {\r
- LinkedList<RasterJob> queue = new LinkedList<RasterJob>();\r
-\r
- @SuppressWarnings("unused")\r
- private boolean isRunning;\r
-\r
- public RasterizeJob() {\r
- super("Rasterize Job");\r
- }\r
-\r
- @Override\r
- protected IStatus run(IProgressMonitor monitor) {\r
- //System.out.println(Thread.currentThread());\r
- setThread(Thread.currentThread());\r
- int taskLeft = 10000;\r
- isRunning = true;\r
- monitor.beginTask("Rasterizing", taskLeft);\r
- try {\r
- // 1. Pick a job\r
- RasterJob job = pullNextJob();\r
- do {\r
- // 2. Check if canceled\r
- if (monitor.isCanceled()) {\r
- cancel();\r
- return Status.CANCEL_STATUS;\r
- }\r
- // 3. rasterize\r
- if (job != null) {\r
- try {\r
- monitor.subTask(job.raster.toString());\r
- ImageData id = job.raster.rasterize(job.width, job.height);\r
- job.listener.rasterizationComplete(job, id);\r
- } catch (Exception e) {\r
- job.listener.rasterizationFailed(job, e);\r
- }\r
-\r
- int jobsLeft = jobsLeft();\r
- double workLeft;\r
- if (jobsLeft == 0) {\r
- workLeft = taskLeft;\r
- } else {\r
- workLeft = ((double) taskLeft) / ((double) jobsLeft());\r
- }\r
- int worked = (int) workLeft;\r
- monitor.worked(worked);\r
- taskLeft -= worked;\r
- }\r
- monitor.subTask("");\r
- // 4. Pick next job\r
- job = pullNextJob();\r
- } while (job != null);\r
- isRunning = false;\r
-\r
- } finally {\r
- monitor.done();\r
- }\r
- return Status.OK_STATUS;\r
- }\r
-\r
- protected synchronized int jobsLeft() {\r
- return queue.size();\r
- }\r
-\r
- protected synchronized RasterJob pullNextJob() {\r
- if (queue.isEmpty())\r
- return null;\r
- return queue.removeFirst();\r
- }\r
-\r
- public synchronized void clear() {\r
- RasterJob jobs[] = queue.toArray(new RasterJob[0]);\r
- for (RasterJob j : jobs)\r
- removeJob(j);\r
- }\r
-\r
- public synchronized void addJob(RasterJob job) { \r
- queue.addLast(job);\r
- job.status = RasterJobStatus.QUEUED;\r
- //if (!isRunning && queue.size()==1)\r
- schedule();\r
- }\r
-\r
- public synchronized void addAsFirstJob(RasterJob job) { \r
- queue.addFirst(job);\r
- job.status = RasterJobStatus.QUEUED;\r
- //if (!isRunning && queue.size()==1)\r
- schedule();\r
- }\r
- \r
- public synchronized boolean removeJob(RasterJob job) {\r
- if (queue.remove(job)) {\r
- job.status = RasterJobStatus.COMPLETE;\r
- job.listener.rasterizationCanceled(job);\r
- return true;\r
- }\r
- return false;\r
- }\r
-\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ * 25.1.2007
+ */
+package org.simantics.utils.ui.gfx.rasterize;
+
+import java.util.LinkedList;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.swt.graphics.ImageData;
+
+/**
+ * LazyRasterizer
+ *
+ * @author Toni Kalajainen
+ */
+public class LazyRasterizer {
+
+ public static void addJob(RasterJob job) {
+ getJob().addJob(job);
+ }
+
+ public static void addAsFirstJob(RasterJob job) {
+ getJob().addAsFirstJob(job);
+ }
+
+ public static void removeJob(RasterJob job) {
+ getJob().removeJob(job);
+ }
+
+ static RasterizeJob job;
+
+ synchronized static RasterizeJob getJob() {
+ if (job == null)
+ job = new RasterizeJob();
+ return job;
+ }
+
+ static class RasterizeJob extends Job {
+ LinkedList<RasterJob> queue = new LinkedList<RasterJob>();
+
+ @SuppressWarnings("unused")
+ private boolean isRunning;
+
+ public RasterizeJob() {
+ super("Rasterize Job");
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ //System.out.println(Thread.currentThread());
+ setThread(Thread.currentThread());
+ int taskLeft = 10000;
+ isRunning = true;
+ monitor.beginTask("Rasterizing", taskLeft);
+ try {
+ // 1. Pick a job
+ RasterJob job = pullNextJob();
+ do {
+ // 2. Check if canceled
+ if (monitor.isCanceled()) {
+ cancel();
+ return Status.CANCEL_STATUS;
+ }
+ // 3. rasterize
+ if (job != null) {
+ try {
+ monitor.subTask(job.raster.toString());
+ ImageData id = job.raster.rasterize(job.width, job.height);
+ job.listener.rasterizationComplete(job, id);
+ } catch (Exception e) {
+ job.listener.rasterizationFailed(job, e);
+ }
+
+ int jobsLeft = jobsLeft();
+ double workLeft;
+ if (jobsLeft == 0) {
+ workLeft = taskLeft;
+ } else {
+ workLeft = ((double) taskLeft) / ((double) jobsLeft());
+ }
+ int worked = (int) workLeft;
+ monitor.worked(worked);
+ taskLeft -= worked;
+ }
+ monitor.subTask("");
+ // 4. Pick next job
+ job = pullNextJob();
+ } while (job != null);
+ isRunning = false;
+
+ } finally {
+ monitor.done();
+ }
+ return Status.OK_STATUS;
+ }
+
+ protected synchronized int jobsLeft() {
+ return queue.size();
+ }
+
+ protected synchronized RasterJob pullNextJob() {
+ if (queue.isEmpty())
+ return null;
+ return queue.removeFirst();
+ }
+
+ public synchronized void clear() {
+ RasterJob jobs[] = queue.toArray(new RasterJob[0]);
+ for (RasterJob j : jobs)
+ removeJob(j);
+ }
+
+ public synchronized void addJob(RasterJob job) {
+ queue.addLast(job);
+ job.status = RasterJobStatus.QUEUED;
+ //if (!isRunning && queue.size()==1)
+ schedule();
+ }
+
+ public synchronized void addAsFirstJob(RasterJob job) {
+ queue.addFirst(job);
+ job.status = RasterJobStatus.QUEUED;
+ //if (!isRunning && queue.size()==1)
+ schedule();
+ }
+
+ public synchronized boolean removeJob(RasterJob job) {
+ if (queue.remove(job)) {
+ job.status = RasterJobStatus.COMPLETE;
+ job.listener.rasterizationCanceled(job);
+ return true;
+ }
+ return false;
+ }
+
+ }
+
+}