]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics/src/org/simantics/AutosaveVirtualGraphs.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / AutosaveVirtualGraphs.java
index 2bff1915a36e6e4e3e99d6793948af88c605b3bc..63611709c7cb4440b855a67bd5703b6dbe0353e6 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012 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;\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.core.runtime.preferences.DefaultScope;\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.osgi.service.prefs.Preferences;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.service.LifecycleSupport;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-import org.simantics.internal.Activator;\r
-\r
-/**\r
- * @author Tuukka Lehtonen <tuukka.lehtonen@semantum.fi>\r
- */\r
-public class AutosaveVirtualGraphs extends Job {\r
-\r
-       private static final boolean         TRACE        = false;\r
-\r
-       private boolean                      enabled      = true;\r
-\r
-       private Preferences                  defaultPrefs = DefaultScope.INSTANCE.getNode(AutosavePreferences.P_NODE);\r
-       private Preferences                  prefs        = InstanceScope.INSTANCE.getNode(AutosavePreferences.P_NODE);\r
-\r
-       private static AutosaveVirtualGraphs INSTANCE;\r
-\r
-       private boolean getBooleanPref(String preference, boolean def) {\r
-               return prefs.getBoolean(preference, defaultPrefs.getBoolean(preference, def));\r
-       }\r
-\r
-       private int getIntPref(String preference, int def) {\r
-               return prefs.getInt(preference, defaultPrefs.getInt(preference, def));\r
-       }\r
-\r
-       private AutosaveVirtualGraphs() {\r
-               super("Autosave Virtual Graphs");\r
-               setSystem(true);\r
-               setPriority(Job.LONG);\r
-       }\r
-\r
-       public static synchronized AutosaveVirtualGraphs getInstance() {\r
-               if(INSTANCE == null) {\r
-                       INSTANCE = new AutosaveVirtualGraphs();\r
-               }\r
-               return INSTANCE;\r
-       }\r
-\r
-       /**\r
-        * @param enabled\r
-        * @return\r
-        */\r
-       public boolean isEnabled() {\r
-               return enabled;\r
-       }\r
-\r
-       /**\r
-        * @param enabled new enabled state\r
-        * @return previous enabled state\r
-        */\r
-       public boolean setEnabled(boolean enabled) {\r
-               if (enabled == this.enabled)\r
-                       return enabled;\r
-               this.enabled = enabled;\r
-               if (enabled)\r
-                       scheduleAfterInterval();\r
-               else\r
-                       cancel();\r
-               return !enabled;\r
-       }\r
-\r
-       public AutosaveVirtualGraphs scheduleAfterInterval() {\r
-               // Check global disable first.\r
-               String enabled = System.getProperty(AutosavePreferences.SYSTEM_PROPERTY_AUTOSAVE);\r
-               if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {\r
-                       return this;\r
-               }\r
-\r
-               // Then check preference.\r
-               if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))\r
-                       return this;\r
-\r
-               wakeUp();\r
-               int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);\r
-               schedule(interval*1000L);\r
-               return this;\r
-       }\r
-\r
-       @Override\r
-       public boolean shouldSchedule() {\r
-               return enabled;\r
-       }\r
-\r
-       @Override\r
-       public boolean shouldRun() {\r
-               return enabled;\r
-       }\r
-\r
-       protected IStatus runHeadless(IProgressMonitor monitor) {\r
-               try {\r
-                       Session session = Simantics.peekSession();\r
-                       if (session == null)\r
-                               return Status.CANCEL_STATUS;\r
-                       LifecycleSupport lfs = session.peekService(LifecycleSupport.class);\r
-                       if (lfs == null || lfs.isClosed() || lfs.isClosing())\r
-                               return Status.CANCEL_STATUS;\r
-\r
-                       // Save\r
-                       monitor.beginTask("Autosaving virtual graphs...", IProgressMonitor.UNKNOWN);\r
-                       if (TRACE)\r
-                               System.out.println("Autosaving virtual graphs...");\r
-                       long startTime = System.nanoTime();\r
-\r
-                       session.syncRequest(new ReadRequest() {\r
-                               @Override\r
-                               public void run(ReadGraph graph) throws DatabaseException {\r
-                                       doSave(graph);\r
-                               }\r
-                       });\r
-\r
-                       if (TRACE) {\r
-                               long endTime = System.nanoTime();\r
-                               System.out.println("Autosave of virtual graphs completed in " + (endTime-startTime)*1e-6 + " ms");\r
-                       }\r
-\r
-                       return Status.OK_STATUS;\r
-               } catch (DatabaseException e) {\r
-                       return new Status(Status.ERROR, Activator.PLUGIN_ID, "Autosaving virtual graphs failed", e);\r
-               } finally {\r
-               }\r
-       }\r
-\r
-       @Override\r
-       protected IStatus run(IProgressMonitor monitor) {\r
-               if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))\r
-                       return Status.OK_STATUS;\r
-               int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);\r
-               try {\r
-                       // Never run while a heavy database job is in progress.\r
-                       if (DatabaseJob.inProgress()) {\r
-                               // Schedule again in at most 10 seconds instead of\r
-                               // waiting for the whole autosave period again.\r
-                               interval = Math.min(10, interval);\r
-                               return Status.OK_STATUS;\r
-                       }\r
-\r
-//                     if(PlatformUI.isWorkbenchRunning()) {\r
-//                             return runInWorkbench(monitor);\r
-//                     } else {\r
-                               return runHeadless(monitor);\r
-//                     }\r
-               } finally {\r
-                       schedule(interval*1000L);\r
-               }\r
-       }\r
-\r
-       public static void doSave(ReadGraph graph) throws DatabaseException {\r
-               VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
-               vgs.saveAll();\r
-       }\r
-\r
-       public static void saveVirtualGraphsPeriodically() {\r
-               getInstance().scheduleAfterInterval();\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 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:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics;
+
+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.core.runtime.preferences.DefaultScope;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.osgi.service.prefs.Preferences;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.internal.Activator;
+
+/**
+ * @author Tuukka Lehtonen <tuukka.lehtonen@semantum.fi>
+ */
+public class AutosaveVirtualGraphs extends Job {
+
+       private static final boolean         TRACE        = false;
+
+       private boolean                      enabled      = true;
+
+       private Preferences                  defaultPrefs = DefaultScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
+       private Preferences                  prefs        = InstanceScope.INSTANCE.getNode(AutosavePreferences.P_NODE);
+
+       private static AutosaveVirtualGraphs INSTANCE;
+
+       private boolean getBooleanPref(String preference, boolean def) {
+               return prefs.getBoolean(preference, defaultPrefs.getBoolean(preference, def));
+       }
+
+       private int getIntPref(String preference, int def) {
+               return prefs.getInt(preference, defaultPrefs.getInt(preference, def));
+       }
+
+       private AutosaveVirtualGraphs() {
+               super("Autosave Virtual Graphs");
+               setSystem(true);
+               setPriority(Job.LONG);
+       }
+
+       public static synchronized AutosaveVirtualGraphs getInstance() {
+               if(INSTANCE == null) {
+                       INSTANCE = new AutosaveVirtualGraphs();
+               }
+               return INSTANCE;
+       }
+
+       /**
+        * @param enabled
+        * @return
+        */
+       public boolean isEnabled() {
+               return enabled;
+       }
+
+       /**
+        * @param enabled new enabled state
+        * @return previous enabled state
+        */
+       public boolean setEnabled(boolean enabled) {
+               if (enabled == this.enabled)
+                       return enabled;
+               this.enabled = enabled;
+               if (enabled)
+                       scheduleAfterInterval();
+               else
+                       cancel();
+               return !enabled;
+       }
+
+       public AutosaveVirtualGraphs scheduleAfterInterval() {
+               // Check global disable first.
+               String enabled = System.getProperty(AutosavePreferences.SYSTEM_PROPERTY_AUTOSAVE);
+               if (enabled != null && enabled.equalsIgnoreCase(Boolean.FALSE.toString())) {
+                       return this;
+               }
+
+               // Then check preference.
+               if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))
+                       return this;
+
+               wakeUp();
+               int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);
+               schedule(interval*1000L);
+               return this;
+       }
+
+       @Override
+       public boolean shouldSchedule() {
+               return enabled;
+       }
+
+       @Override
+       public boolean shouldRun() {
+               return enabled;
+       }
+
+       protected IStatus runHeadless(IProgressMonitor monitor) {
+               try {
+                       Session session = Simantics.peekSession();
+                       if (session == null)
+                               return Status.CANCEL_STATUS;
+                       LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
+                       if (lfs == null || lfs.isClosed() || lfs.isClosing())
+                               return Status.CANCEL_STATUS;
+
+                       // Save
+                       monitor.beginTask("Autosaving virtual graphs...", IProgressMonitor.UNKNOWN);
+                       if (TRACE)
+                               System.out.println("Autosaving virtual graphs...");
+                       long startTime = System.nanoTime();
+
+                       session.syncRequest(new ReadRequest() {
+                               @Override
+                               public void run(ReadGraph graph) throws DatabaseException {
+                                       doSave(graph);
+                               }
+                       });
+
+                       if (TRACE) {
+                               long endTime = System.nanoTime();
+                               System.out.println("Autosave of virtual graphs completed in " + (endTime-startTime)*1e-6 + " ms");
+                       }
+
+                       return Status.OK_STATUS;
+               } catch (DatabaseException e) {
+                       return new Status(Status.ERROR, Activator.PLUGIN_ID, "Autosaving virtual graphs failed", e);
+               } finally {
+               }
+       }
+
+       @Override
+       protected IStatus run(IProgressMonitor monitor) {
+               if (!getBooleanPref(AutosavePreferences.P_VG_AUTOSAVE_ENABLED, AutosavePreferences.DEFAULT_VG_AUTOSAVE_ENABLED))
+                       return Status.OK_STATUS;
+               int interval = getIntPref(AutosavePreferences.P_VG_AUTOSAVE_INTERVAL, AutosavePreferences.DEFAULT_VG_AUTOSAVE_INTERVAL);
+               try {
+                       // Never run while a heavy database job is in progress.
+                       if (DatabaseJob.inProgress()) {
+                               // Schedule again in at most 10 seconds instead of
+                               // waiting for the whole autosave period again.
+                               interval = Math.min(10, interval);
+                               return Status.OK_STATUS;
+                       }
+
+//                     if(PlatformUI.isWorkbenchRunning()) {
+//                             return runInWorkbench(monitor);
+//                     } else {
+                               return runHeadless(monitor);
+//                     }
+               } finally {
+                       schedule(interval*1000L);
+               }
+       }
+
+       public static void doSave(ReadGraph graph) throws DatabaseException {
+               VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
+               vgs.saveAll();
+       }
+
+       public static void saveVirtualGraphsPeriodically() {
+               getInstance().scheduleAfterInterval();
+       }
+
+}