]> gerrit.simantics Code Review - simantics/platform.git/blob
f855524d09f9739f1391bef034606eebbdbc72f0
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2014 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.workbench.internal.contributions.search;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.procedure.adapter.DisposableListener;
22 import org.simantics.workbench.internal.Activator;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 class ActiveModelsListener extends DisposableListener<Collection<Resource>> {
28
29     private Runnable callback;
30     private Set<Resource> previouslyActive;
31
32     public ActiveModelsListener(Runnable callback) {
33         if (callback == null)
34             throw new NullPointerException("null callback");
35         this.callback = callback;
36     }
37
38     @Override
39     public void execute(Collection<Resource> models) {
40         Set<Resource> set = new HashSet<Resource>(models);
41         Set<Resource> previous = previouslyActive;
42         if (previous != null && !previous.equals(set))
43             callback.run();
44         previouslyActive = set;
45     }
46
47     @Override
48     public void exception(Throwable t) {
49         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, t.getLocalizedMessage(), t));
50     }
51
52     @Override
53     public void dispose() {
54         super.dispose();
55         previouslyActive = null;
56     }
57
58 }