]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ImageLoaderJob.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ImageLoaderJob.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ImageLoaderJob.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ImageLoaderJob.java
new file mode 100644 (file)
index 0000000..669f3c1
--- /dev/null
@@ -0,0 +1,79 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import java.util.concurrent.atomic.AtomicBoolean;\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.ui.progress.IProgressConstants;\r
+import org.simantics.DatabaseJob;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+class ImageLoaderJob extends DatabaseJob {\r
+\r
+       private GraphExplorerImplBase ge;\r
+       private AtomicBoolean isScheduled = new AtomicBoolean();\r
+\r
+       public ImageLoaderJob(GraphExplorerImplBase ge) {\r
+               super("Image Loader");\r
+               this.ge = ge;\r
+               setSystem(true);\r
+               setUser(false);\r
+               setPriority(Job.DECORATE);\r
+               setProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, Boolean.TRUE);\r
+       }\r
+\r
+       /**\r
+        * Schedules the job with {@link Job#schedule()} if necessary, i.e. if\r
+        * not already scheduled. This method avoids some unnecessary overhead\r
+        * caused by repeatedly invoking {@link Job#schedule()}.\r
+        * \r
+        * @return <code>true</code> if scheduled, <code>false</code> otherwise\r
+        */\r
+       public boolean scheduleIfNecessary(long delay) {\r
+               if (isScheduled.compareAndSet(false, true)) {\r
+                       schedule(delay);\r
+                       return true;\r
+               }\r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean shouldRun() {\r
+               return ge != null;\r
+       }\r
+\r
+       @Override\r
+       public boolean shouldSchedule() {\r
+               return ge != null;\r
+       }\r
+\r
+       @Override\r
+       protected IStatus run(IProgressMonitor monitor) {\r
+               try {\r
+                       isScheduled.set(false);\r
+                       GraphExplorerImplBase ge = this.ge;\r
+                       return (ge != null) ? ge.setPendingImages(monitor) : Status.CANCEL_STATUS;\r
+               } finally {\r
+                       monitor.done();\r
+               }\r
+       }\r
+\r
+       public void dispose() {\r
+               ge = null;\r
+       }\r
+}
\ No newline at end of file