X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.management%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fmanagement%2FSessionContextProvider.java;fp=bundles%2Forg.simantics.db.management%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fmanagement%2FSessionContextProvider.java;h=421c911da053a77e83624d9906aebac4750ee83d;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.db.management/src/org/simantics/db/management/SessionContextProvider.java b/bundles/org.simantics.db.management/src/org/simantics/db/management/SessionContextProvider.java new file mode 100644 index 000000000..421c911da --- /dev/null +++ b/bundles/org.simantics.db.management/src/org/simantics/db/management/SessionContextProvider.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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.db.management; + +import org.eclipse.core.runtime.ListenerList; + +/** + * @author Tuukka Lehtonen + */ +public class SessionContextProvider implements ISessionContextProvider { + + private ListenerList listeners = new ListenerList(); + + private Object handle; + + private ISessionContext context; + + public SessionContextProvider(Object handle) { + this.handle = handle; + } + + public Object getHandle() { + return handle; + } + + public void setHandle(Object handle) { + this.handle = handle; + } + + @Override + public void addContextChangedListener(ISessionContextChangedListener listener) { + listeners.add(listener); + } + + @Override + public ISessionContext getSessionContext() { + return context; + } + + @Override + public void removeContextChangedListener(ISessionContextChangedListener listener) { + listeners.remove(listener); + } + + private boolean safeEquals(Object o1, Object o2) { + if (o1 == null) + return o2 == null; + return o1.equals(o2); + } + + @Override + public ISessionContext setSessionContext(ISessionContext context) { + ISessionContext oldContext = this.context; + this.context = context; + + if (!safeEquals(oldContext, context)) + fireChanged(oldContext, context); + + return oldContext; + } + + protected void fireChanged(ISessionContext oldValue, ISessionContext newValue) { + SessionContextChangedEvent event = new SessionContextChangedEvent(this, handle, oldValue, newValue); + for (Object o : listeners.getListeners()) + ((ISessionContextChangedListener) o).sessionContextChanged(event); + } + + @Override + public String toString() { + return "SessionContextProvider [handle=" + handle + ", context=" + context + "]"; + } + +}