2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.analytics.gmap.utils;
22 import javax.swing.SwingUtilities;
25 * This is the 3rd version of SwingWorker (also known as
26 * SwingWorker 3), an abstract class that you subclass to
27 * perform GUI-related work in a dedicated thread. For
28 * instructions on using this class, see:
30 * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
32 * Note that the API changed slightly in the 3rd version:
33 * You must now invoke start() on the SwingWorker after
36 public abstract class SwingWorker {
37 private Object value; // see getValue(), setValue()
38 private Thread thread;
41 * Class to maintain reference to current worker thread
42 * under separate synchronization control.
44 private static class ThreadVar {
45 private Thread thread;
46 ThreadVar(Thread t) { thread = t; }
47 synchronized Thread get() { return thread; }
48 synchronized void clear() { thread = null; }
51 private ThreadVar threadVar;
54 * Get the value produced by the worker thread, or null if it
55 * hasn't been constructed yet.
57 protected synchronized Object getValue() {
62 * Set the value produced by worker thread
64 private synchronized void setValue(Object x) {
69 * Compute the value to be returned by the <code>get</code> method.
71 public abstract Object construct();
74 * Called on the event dispatching thread (not on the worker thread)
75 * after the <code>construct</code> method has returned.
77 public void finished() {
81 * A new method that interrupts the worker thread. Call this method
82 * to force the worker to stop what it's doing.
84 public void interrupt() {
85 Thread t = threadVar.get();
93 * Return the value created by the <code>construct</code> method.
94 * Returns null if either the constructing thread or the current
95 * thread was interrupted before a value was produced.
97 * @return the value created by the <code>construct</code> method
101 Thread t = threadVar.get();
108 catch (InterruptedException e) {
109 Thread.currentThread().interrupt(); // propagate
117 * Start a thread that will call the <code>construct</code> method
120 public SwingWorker() {
121 final Runnable doFinished = new Runnable() {
122 public void run() { finished(); }
125 Runnable doConstruct = new Runnable() {
128 setValue(construct());
134 SwingUtilities.invokeLater(doFinished);
138 Thread t = new Thread(doConstruct);
139 threadVar = new ThreadVar(t);
143 * Start the worker thread.
145 public void start() {
146 Thread t = threadVar.get();
152 public Thread getThread() {
153 return threadVar.get();