]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalSupport.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / TypicalSupport.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalSupport.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalSupport.java
new file mode 100644 (file)
index 0000000..3caae37
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************\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
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.typicals;\r
+\r
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
+import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;\r
+import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.simantics.db.service.GraphChangeListenerSupport;\r
+import org.simantics.project.IProject;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @deprecated not to be used anymore, will be removed\r
+ */\r
+public class TypicalSupport {\r
+\r
+    protected IProject project;\r
+    protected GraphChangeListenerSupport changeListenerSupport;\r
+\r
+    private IEclipsePreferences typicalPreferences;\r
+    private TypicalDiagramTemplateListener typicalDiagramTemplateListener;\r
+    private IPreferenceChangeListener typicalSynchronizationPreferenceListener = new TypicalSynchronizationPreferenceListener();\r
+\r
+    public TypicalSupport(IProject project) {\r
+        this.project = project;\r
+        this.changeListenerSupport = project.getSession().getService(GraphChangeListenerSupport.class);\r
+\r
+        setTypicalSynchronization( isSyncEnabled() );\r
+        typicalPreferences = InstanceScope.INSTANCE.getNode(TypicalPreferences.P_NODE);\r
+        typicalPreferences.addPreferenceChangeListener(typicalSynchronizationPreferenceListener);\r
+    }\r
+\r
+    public void dispose() {\r
+        if (typicalPreferences != null) {\r
+            typicalPreferences.removePreferenceChangeListener(typicalSynchronizationPreferenceListener);\r
+        }\r
+        if (typicalDiagramTemplateListener != null) {\r
+            changeListenerSupport.removeMetadataListener(typicalDiagramTemplateListener);\r
+            typicalDiagramTemplateListener = null;\r
+        }\r
+    }\r
+\r
+    public void setTypicalSynchronization(boolean enabled) {\r
+        if (enabled) {\r
+            if (typicalDiagramTemplateListener == null) {\r
+                typicalDiagramTemplateListener = new TypicalDiagramTemplateListener();\r
+                changeListenerSupport.addMetadataListener( typicalDiagramTemplateListener );\r
+            }\r
+        } else {\r
+            if (typicalDiagramTemplateListener != null) {\r
+                changeListenerSupport.removeMetadataListener( typicalDiagramTemplateListener );\r
+                typicalDiagramTemplateListener = null;\r
+            }\r
+        }\r
+    }\r
+\r
+    private boolean isSyncEnabled() {\r
+        return InstanceScope.INSTANCE\r
+                .getNode(TypicalPreferences.P_NODE)\r
+                .getBoolean(\r
+                        TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED,\r
+                        TypicalPreferences.DEFAULT_REALTIME_TYPICAL_SYNC_ENABLED);\r
+    }\r
+\r
+    private class TypicalSynchronizationPreferenceListener implements IPreferenceChangeListener {\r
+        @Override\r
+        public void preferenceChange(PreferenceChangeEvent event) {\r
+            if (project == null || project.isDisposed())\r
+                return;\r
+\r
+            if (TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED.equals(event.getKey())) {\r
+                boolean enabled = isSyncEnabled();\r
+                setTypicalSynchronization(enabled);\r
+            }\r
+        }\r
+    }\r
+\r
+}\r