+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 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.db.management;\r
+\r
+import org.eclipse.core.runtime.ListenerList;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SessionContextProvider implements ISessionContextProvider {\r
+\r
+ private ListenerList listeners = new ListenerList();\r
+ \r
+ private Object handle;\r
+\r
+ private ISessionContext context;\r
+ \r
+ public SessionContextProvider(Object handle) {\r
+ this.handle = handle;\r
+ }\r
+ \r
+ public Object getHandle() {\r
+ return handle;\r
+ }\r
+\r
+ public void setHandle(Object handle) {\r
+ this.handle = handle;\r
+ }\r
+\r
+ @Override\r
+ public void addContextChangedListener(ISessionContextChangedListener listener) {\r
+ listeners.add(listener);\r
+ }\r
+\r
+ @Override\r
+ public ISessionContext getSessionContext() {\r
+ return context;\r
+ }\r
+\r
+ @Override\r
+ public void removeContextChangedListener(ISessionContextChangedListener listener) {\r
+ listeners.remove(listener);\r
+ }\r
+\r
+ private boolean safeEquals(Object o1, Object o2) {\r
+ if (o1 == null)\r
+ return o2 == null;\r
+ return o1.equals(o2);\r
+ }\r
+ \r
+ @Override\r
+ public ISessionContext setSessionContext(ISessionContext context) {\r
+ ISessionContext oldContext = this.context;\r
+ this.context = context;\r
+\r
+ if (!safeEquals(oldContext, context))\r
+ fireChanged(oldContext, context);\r
+\r
+ return oldContext;\r
+ }\r
+\r
+ protected void fireChanged(ISessionContext oldValue, ISessionContext newValue) {\r
+ SessionContextChangedEvent event = new SessionContextChangedEvent(this, handle, oldValue, newValue);\r
+ for (Object o : listeners.getListeners())\r
+ ((ISessionContextChangedListener) o).sessionContextChanged(event);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return "SessionContextProvider [handle=" + handle + ", context=" + context + "]";\r
+ }\r
+\r
+}\r