]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ImageLoaderJob.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ImageLoaderJob.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import java.util.concurrent.atomic.AtomicBoolean;\r
15 \r
16 import org.eclipse.core.runtime.IProgressMonitor;\r
17 import org.eclipse.core.runtime.IStatus;\r
18 import org.eclipse.core.runtime.Status;\r
19 import org.eclipse.core.runtime.jobs.Job;\r
20 import org.eclipse.ui.progress.IProgressConstants;\r
21 import org.simantics.DatabaseJob;\r
22 \r
23 /**\r
24  * @author Tuukka Lehtonen\r
25  */\r
26 public class ImageLoaderJob extends DatabaseJob {\r
27 \r
28         private GraphExplorerImplBase ge;\r
29         private AtomicBoolean isScheduled = new AtomicBoolean();\r
30 \r
31         public ImageLoaderJob(GraphExplorerImplBase ge) {\r
32                 super("Image Loader");\r
33                 this.ge = ge;\r
34                 setSystem(true);\r
35                 setUser(false);\r
36                 setPriority(Job.DECORATE);\r
37                 setProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, Boolean.TRUE);\r
38         }\r
39 \r
40         /**\r
41          * Schedules the job with {@link Job#schedule()} if necessary, i.e. if\r
42          * not already scheduled. This method avoids some unnecessary overhead\r
43          * caused by repeatedly invoking {@link Job#schedule()}.\r
44          * \r
45          * @return <code>true</code> if scheduled, <code>false</code> otherwise\r
46          */\r
47         public boolean scheduleIfNecessary(long delay) {\r
48                 if (isScheduled.compareAndSet(false, true)) {\r
49                         schedule(delay);\r
50                         return true;\r
51                 }\r
52                 return false;\r
53         }\r
54 \r
55         @Override\r
56         public boolean shouldRun() {\r
57                 return ge != null;\r
58         }\r
59 \r
60         @Override\r
61         public boolean shouldSchedule() {\r
62                 return ge != null;\r
63         }\r
64 \r
65         @Override\r
66         protected IStatus run(IProgressMonitor monitor) {\r
67                 try {\r
68                         isScheduled.set(false);\r
69                         GraphExplorerImplBase ge = this.ge;\r
70                         return (ge != null) ? ge.setPendingImages(monitor) : Status.CANCEL_STATUS;\r
71                 } finally {\r
72                         monitor.done();\r
73                 }\r
74         }\r
75 \r
76         public void dispose() {\r
77                 ge = null;\r
78         }\r
79 }