]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/TitleUpdater.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / TitleUpdater.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.ui.workbench;
13
14 import java.util.function.Consumer;
15 import java.util.function.Supplier;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.swt.widgets.Display;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.ui.internal.Activator;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class TitleUpdater implements Listener<String> {
27
28     private final Display           display;
29     private final Consumer<String>  callback;
30     private final Supplier<Boolean> disposed;
31
32     public TitleUpdater(Display display, Consumer<String> titleChangedCallback, Supplier<Boolean> disposed) {
33         this.display = display;
34         this.callback = titleChangedCallback;
35         this.disposed = disposed;
36     }
37
38     @Override
39     public void execute(final String result) {
40         display.asyncExec(new Runnable() {
41             @Override
42             public void run() {
43                 if (!isDisposed())
44                     callback.accept(result);
45             }
46         });
47     }
48
49     @Override
50     public boolean isDisposed() {
51         return disposed.get();
52     }
53
54     @Override
55     public void exception(Throwable t) {
56         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Title update failed.", t));
57     }
58
59 }