]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ImageLoaderJob.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ImageLoaderJob.java
1 /*******************************************************************************
2  * Copyright (c) 2013 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.browsing.ui.swt;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.ui.progress.IProgressConstants;
21 import org.simantics.DatabaseJob;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class ImageLoaderJob extends DatabaseJob {
27
28         private GraphExplorerImplBase ge;
29         private AtomicBoolean isScheduled = new AtomicBoolean();
30
31         public ImageLoaderJob(GraphExplorerImplBase ge) {
32                 super("Image Loader");
33                 this.ge = ge;
34                 setSystem(true);
35                 setUser(false);
36                 setPriority(Job.DECORATE);
37                 setProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, Boolean.TRUE);
38         }
39
40         /**
41          * Schedules the job with {@link Job#schedule()} if necessary, i.e. if
42          * not already scheduled. This method avoids some unnecessary overhead
43          * caused by repeatedly invoking {@link Job#schedule()}.
44          * 
45          * @return <code>true</code> if scheduled, <code>false</code> otherwise
46          */
47         public boolean scheduleIfNecessary(long delay) {
48                 if (isScheduled.compareAndSet(false, true)) {
49                         schedule(delay);
50                         return true;
51                 }
52                 return false;
53         }
54
55         @Override
56         public boolean shouldRun() {
57                 return ge != null;
58         }
59
60         @Override
61         public boolean shouldSchedule() {
62                 return ge != null;
63         }
64
65         @Override
66         protected IStatus run(IProgressMonitor monitor) {
67                 try {
68                         isScheduled.set(false);
69                         GraphExplorerImplBase ge = this.ge;
70                         return (ge != null) ? ge.setPendingImages(monitor) : Status.CANCEL_STATUS;
71                 } finally {
72                         monitor.done();
73                 }
74         }
75
76         public void dispose() {
77                 ge = null;
78         }
79 }