15dca6cce1655352e72ebba268086c6c239cdcca
1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.Window
;
5 import javax
.swing
.JDialog
;
6 import javax
.swing
.JLabel
;
7 import javax
.swing
.SwingWorker
;
8 import javax
.swing
.border
.EmptyBorder
;
10 import be
.nikiroo
.utils
.Progress
;
11 import be
.nikiroo
.utils
.Progress
.ProgressListener
;
14 * A small waiting dialog that will show only if more than X milliseconds passed
15 * before we dismiss it.
19 public class WaitingDialog
extends JDialog
{
20 private static final long serialVersionUID
= 1L;
22 private boolean waitScreen
;
23 private Object waitLock
= new Object();
26 private ProgressListener pgl
;
29 * Create a new {@link WaitingDialog}.
32 * the parent/owner of this {@link WaitingDialog}
34 * the delay after which to show the dialog if it is still not
35 * dismiss (see {@link WaitingDialog#dismiss()})
37 public WaitingDialog(Window parent
, long delayMs
) {
38 this(parent
, delayMs
, null, null);
42 * Create a new {@link WaitingDialog}.
45 * the parent/owner of this {@link WaitingDialog}
47 * the delay after which to show the dialog if it is still not
48 * dismiss (see {@link WaitingDialog#dismiss()})
50 * the {@link Progress} to listen on -- when it is
51 * {@link Progress#done()}, this {@link WaitingDialog} will
52 * automatically be dismissed as if
53 * {@link WaitingDialog#dismiss()} was called
55 public WaitingDialog(Window parent
, long delayMs
, Progress pg
) {
56 this(parent
, delayMs
, pg
, null);
60 * Create a new {@link WaitingDialog}.
63 * the parent/owner of this {@link WaitingDialog}
65 * the delay after which to show the dialog if it is still not
66 * dismiss (see {@link WaitingDialog#dismiss()})
68 * a waiting text to display (note: you may want to subclass it
71 public WaitingDialog(Window parent
, long delayMs
, String waitingText
) {
72 this(parent
, delayMs
, null, waitingText
);
76 * Create a new {@link WaitingDialog}.
79 * the parent/owner of this {@link WaitingDialog}
81 * the delay after which to show the dialog if it is still not
82 * dismiss (see {@link WaitingDialog#dismiss()})
84 * the {@link Progress} to listen on -- when it is
85 * {@link Progress#done()}, this {@link WaitingDialog} will
86 * automatically be dismissed as if
87 * {@link WaitingDialog#dismiss()} was called
89 * a waiting text to display (note: you may want to subclass it
92 public WaitingDialog(Window parent
, long delayMs
, Progress pg
,
98 if (waitingText
!= null) {
99 JLabel waitingTextLabel
= new JLabel(waitingText
);
100 this.add(waitingTextLabel
);
101 waitingTextLabel
.setBorder(new EmptyBorder(10, 10, 10, 10));
106 pgl
= new ProgressListener() {
108 public void progress(Progress progress
, String name
) {
109 if (WaitingDialog
.this.pg
.isDone()) {
110 // Must be done out of this thread (cannot remove a pgl
111 // from a running pgl)
112 new SwingWorker
<Void
, Void
>() {
114 protected Void
doInBackground() throws Exception
{
127 pg
.addProgressListener(pgl
);
135 final long delay
= delayMs
;
136 new Thread(new Runnable() {
141 } catch (InterruptedException e
) {
144 synchronized (waitLock
) {
155 * Notify this {@link WaitingDialog} that the job is done, and dismiss it if
156 * it was already showing on screen (or never show it if it was not).
158 public void dismiss() {
159 synchronized (waitLock
) {
167 pg
.removeProgressListener(pgl
);