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