X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftypicals%2FTypicalSupport.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftypicals%2FTypicalSupport.java;h=3caae3712f72270897f1ca86ef685e390f4198b0;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..3caae3712 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalSupport.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.typicals; + +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.simantics.db.service.GraphChangeListenerSupport; +import org.simantics.project.IProject; + +/** + * @author Tuukka Lehtonen + * @deprecated not to be used anymore, will be removed + */ +public class TypicalSupport { + + protected IProject project; + protected GraphChangeListenerSupport changeListenerSupport; + + private IEclipsePreferences typicalPreferences; + private TypicalDiagramTemplateListener typicalDiagramTemplateListener; + private IPreferenceChangeListener typicalSynchronizationPreferenceListener = new TypicalSynchronizationPreferenceListener(); + + public TypicalSupport(IProject project) { + this.project = project; + this.changeListenerSupport = project.getSession().getService(GraphChangeListenerSupport.class); + + setTypicalSynchronization( isSyncEnabled() ); + typicalPreferences = InstanceScope.INSTANCE.getNode(TypicalPreferences.P_NODE); + typicalPreferences.addPreferenceChangeListener(typicalSynchronizationPreferenceListener); + } + + public void dispose() { + if (typicalPreferences != null) { + typicalPreferences.removePreferenceChangeListener(typicalSynchronizationPreferenceListener); + } + if (typicalDiagramTemplateListener != null) { + changeListenerSupport.removeMetadataListener(typicalDiagramTemplateListener); + typicalDiagramTemplateListener = null; + } + } + + public void setTypicalSynchronization(boolean enabled) { + if (enabled) { + if (typicalDiagramTemplateListener == null) { + typicalDiagramTemplateListener = new TypicalDiagramTemplateListener(); + changeListenerSupport.addMetadataListener( typicalDiagramTemplateListener ); + } + } else { + if (typicalDiagramTemplateListener != null) { + changeListenerSupport.removeMetadataListener( typicalDiagramTemplateListener ); + typicalDiagramTemplateListener = null; + } + } + } + + private boolean isSyncEnabled() { + return InstanceScope.INSTANCE + .getNode(TypicalPreferences.P_NODE) + .getBoolean( + TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED, + TypicalPreferences.DEFAULT_REALTIME_TYPICAL_SYNC_ENABLED); + } + + private class TypicalSynchronizationPreferenceListener implements IPreferenceChangeListener { + @Override + public void preferenceChange(PreferenceChangeEvent event) { + if (project == null || project.isDisposed()) + return; + + if (TypicalPreferences.P_REALTIME_TYPICAL_SYNC_ENABLED.equals(event.getKey())) { + boolean enabled = isSyncEnabled(); + setTypicalSynchronization(enabled); + } + } + } + +}