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