]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/impl/ProfileActivationListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.profile / src / org / simantics / scenegraph / profile / impl / ProfileActivationListener.java
1 package org.simantics.scenegraph.profile.impl;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.single.SingleSetSyncListener;
9 import org.simantics.db.common.utils.Logger;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.scenegraph.profile.ProfileEntry;
12 import org.simantics.scenegraph.profile.common.ProfileObserver;
13 import org.simantics.utils.datastructures.disposable.IDisposable;
14
15 public class ProfileActivationListener extends SingleSetSyncListener<ProfileEntry> {
16
17     final Resource runtime;
18     final ProfileObserver observer;
19     final IDisposable disposable;
20     final Set<ProfileEntry> active = new HashSet<ProfileEntry>();
21     
22     public ProfileActivationListener(Resource runtime, ProfileObserver observer, IDisposable disposable) {
23         this.runtime = runtime;
24         this.observer = observer;
25         this.disposable = disposable;
26     }
27     
28     @Override
29     public boolean start(ReadGraph graph) throws DatabaseException {
30         
31         if (observer == null || observer.isDisposed())
32             return false;
33         if (runtime == null)
34             return false;
35
36         return true;
37         
38     }
39
40     @Override
41     public void add(ReadGraph graph, ProfileEntry item) throws DatabaseException {
42         observer.update();
43         item.activate(graph, runtime, observer);
44         active.add(item);
45         if (DebugPolicy.DEBUG_PROFILE_STYLE_ACTIVATION)
46             System.out.println("ACTIVATED PROFILE ENTRY: " + item);
47     }
48
49     @Override
50     public void remove(ReadGraph graph, ProfileEntry item) throws DatabaseException {
51         observer.update();
52         active.remove(item);
53         if (DebugPolicy.DEBUG_PROFILE_STYLE_ACTIVATION)
54             System.out.println("DE-ACTIVATING PROFILE ENTRY: " + item);
55         item.deactivate(runtime, observer);
56     }
57
58     @Override
59     public void finished(ReadGraph graph) throws DatabaseException {
60 //        observer.update();
61     }
62
63     @Override
64     public void exception(ReadGraph graph, Throwable t) {
65         Logger.defaultLogError(t);
66     }
67
68     @Override
69     public boolean isDisposed() {
70         return disposable.isDisposed();
71     }
72     
73     public void cleanup() {
74         if(!active.isEmpty()) {
75                 for(ProfileEntry entry : active) entry.deactivate(runtime, observer);
76                 active.clear();
77         }
78     }
79
80 }