]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalSupport.java
Merge "InputStream returns -1 on EOF instead of throwing IOException"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / TypicalSupport.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.typicals;\r
13 \r
14 import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
15 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;\r
16 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;\r
17 import org.eclipse.core.runtime.preferences.InstanceScope;\r
18 import org.simantics.db.service.GraphChangeListenerSupport;\r
19 import org.simantics.project.IProject;\r
20 \r
21 /**\r
22  * @author Tuukka Lehtonen\r
23  * @deprecated not to be used anymore, will be removed\r
24  */\r
25 public class TypicalSupport {\r
26 \r
27     protected IProject project;\r
28     protected GraphChangeListenerSupport changeListenerSupport;\r
29 \r
30     private IEclipsePreferences typicalPreferences;\r
31     private TypicalDiagramTemplateListener typicalDiagramTemplateListener;\r
32     private IPreferenceChangeListener typicalSynchronizationPreferenceListener = new TypicalSynchronizationPreferenceListener();\r
33 \r
34     public TypicalSupport(IProject project) {\r
35         this.project = project;\r
36         this.changeListenerSupport = project.getSession().getService(GraphChangeListenerSupport.class);\r
37 \r
38         setTypicalSynchronization( isSyncEnabled() );\r
39         typicalPreferences = InstanceScope.INSTANCE.getNode(TypicalPreferences.P_NODE);\r
40         typicalPreferences.addPreferenceChangeListener(typicalSynchronizationPreferenceListener);\r
41     }\r
42 \r
43     public void dispose() {\r
44         if (typicalPreferences != null) {\r
45             typicalPreferences.removePreferenceChangeListener(typicalSynchronizationPreferenceListener);\r
46         }\r
47         if (typicalDiagramTemplateListener != null) {\r
48             changeListenerSupport.removeMetadataListener(typicalDiagramTemplateListener);\r
49             typicalDiagramTemplateListener = null;\r
50         }\r
51     }\r
52 \r
53     public void setTypicalSynchronization(boolean enabled) {\r
54         if (enabled) {\r
55             if (typicalDiagramTemplateListener == null) {\r
56                 typicalDiagramTemplateListener = new TypicalDiagramTemplateListener();\r
57                 changeListenerSupport.addMetadataListener( typicalDiagramTemplateListener );\r
58             }\r
59         } else {\r
60             if (typicalDiagramTemplateListener != null) {\r
61                 changeListenerSupport.removeMetadataListener( typicalDiagramTemplateListener );\r
62                 typicalDiagramTemplateListener = null;\r
63             }\r
64         }\r
65     }\r
66 \r
67     private boolean isSyncEnabled() {\r
68         return InstanceScope.INSTANCE\r
69                 .getNode(TypicalPreferences.P_NODE)\r
70                 .getBoolean(\r
71                         TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED,\r
72                         TypicalPreferences.DEFAULT_REALTIME_TYPICAL_SYNC_ENABLED);\r
73     }\r
74 \r
75     private class TypicalSynchronizationPreferenceListener implements IPreferenceChangeListener {\r
76         @Override\r
77         public void preferenceChange(PreferenceChangeEvent event) {\r
78             if (project == null || project.isDisposed())\r
79                 return;\r
80 \r
81             if (TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED.equals(event.getKey())) {\r
82                 boolean enabled = isSyncEnabled();\r
83                 setTypicalSynchronization(enabled);\r
84             }\r
85         }\r
86     }\r
87 \r
88 }\r