]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics/src/org/simantics/DatabaseJob.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / DatabaseJob.java
diff --git a/bundles/org.simantics/src/org/simantics/DatabaseJob.java b/bundles/org.simantics/src/org/simantics/DatabaseJob.java
new file mode 100644 (file)
index 0000000..20960a4
--- /dev/null
@@ -0,0 +1,56 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * 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
+package org.simantics;\r
+\r
+import org.eclipse.core.runtime.jobs.IJobManager;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+\r
+/**\r
+ * An Eclipse job that inherently belongs to the\r
+ * {@link Simantics#DATABASE_JOB_FAMILY} job family.\r
+ * \r
+ * <p>\r
+ * Use {@link #inProgress()} to check whether there are currently any\r
+ * {@link DatabaseJob}s running in the platform's job manager. Checks should be\r
+ * performed if any synchronous database requests must be initiated in the UI\r
+ * thread but it is not 100% necessary to return an accurate result right at\r
+ * that moment. Performing too much synchronous requests in the UI thread will\r
+ * currently inevitably get the UI completely stuck and therefore offloading\r
+ * database work to database job worker threads is highly recommended.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class DatabaseJob extends Job {\r
+\r
+    public static final String DATABASE_JOB_FAMILY = "org.simantics.db.inDatabaseJob";\r
+\r
+    public DatabaseJob(String name) {\r
+        super(name);\r
+    }\r
+\r
+    @Override\r
+    public boolean belongsTo(Object family) {\r
+        if (DATABASE_JOB_FAMILY.equals(family))\r
+            return true;\r
+        return super.belongsTo(family);\r
+    }\r
+\r
+    public static boolean inProgress() {\r
+        IJobManager manager = Job.getJobManager();\r
+        Job[] jobs = manager.find(DATABASE_JOB_FAMILY);\r
+        for (Job job : jobs)\r
+            if (job.getState() == Job.RUNNING)\r
+                return true;\r
+        return false;\r
+    }\r
+\r
+}\r