]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.management/src/org/simantics/db/management/SessionContextProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.management / src / org / simantics / db / management / SessionContextProvider.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.db.management;\r
13 \r
14 import org.eclipse.core.runtime.ListenerList;\r
15 \r
16 /**\r
17  * @author Tuukka Lehtonen\r
18  */\r
19 public class SessionContextProvider implements ISessionContextProvider {\r
20 \r
21     private ListenerList    listeners = new ListenerList();\r
22     \r
23     private Object          handle;\r
24 \r
25     private ISessionContext context;\r
26     \r
27     public SessionContextProvider(Object handle) {\r
28         this.handle = handle;\r
29     }\r
30     \r
31     public Object getHandle() {\r
32         return handle;\r
33     }\r
34 \r
35     public void setHandle(Object handle) {\r
36         this.handle = handle;\r
37     }\r
38 \r
39     @Override\r
40     public void addContextChangedListener(ISessionContextChangedListener listener) {\r
41         listeners.add(listener);\r
42     }\r
43 \r
44     @Override\r
45     public ISessionContext getSessionContext() {\r
46         return context;\r
47     }\r
48 \r
49     @Override\r
50     public void removeContextChangedListener(ISessionContextChangedListener listener) {\r
51         listeners.remove(listener);\r
52     }\r
53 \r
54     private boolean safeEquals(Object o1, Object o2) {\r
55         if (o1 == null)\r
56             return o2 == null;\r
57         return o1.equals(o2);\r
58     }\r
59     \r
60     @Override\r
61     public ISessionContext setSessionContext(ISessionContext context) {\r
62         ISessionContext oldContext = this.context;\r
63         this.context = context;\r
64 \r
65         if (!safeEquals(oldContext, context))\r
66             fireChanged(oldContext, context);\r
67 \r
68         return oldContext;\r
69     }\r
70 \r
71     protected void fireChanged(ISessionContext oldValue, ISessionContext newValue) {\r
72         SessionContextChangedEvent event = new SessionContextChangedEvent(this, handle, oldValue, newValue);\r
73         for (Object o : listeners.getListeners())\r
74             ((ISessionContextChangedListener) o).sessionContextChanged(event);\r
75     }\r
76 \r
77     @Override\r
78     public String toString() {\r
79         return "SessionContextProvider [handle=" + handle + ", context=" + context + "]";\r
80     }\r
81 \r
82 }\r