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
,
96 this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE
);
100 if (waitingText
!= null) {
101 JLabel waitingTextLabel
= new JLabel(waitingText
);
102 this.add(waitingTextLabel
);
103 waitingTextLabel
.setBorder(new EmptyBorder(10, 10, 10, 10));
108 pgl
= new ProgressListener() {
110 public void progress(Progress progress
, String name
) {
111 if (WaitingDialog
.this.pg
.isDone()) {
112 // Must be done out of this thread (cannot remove a pgl
113 // from a running pgl)
114 new SwingWorker
<Void
, Void
>() {
116 protected Void
doInBackground() throws Exception
{
129 pg
.addProgressListener(pgl
);
137 final long delay
= delayMs
;
138 new Thread(new Runnable() {
143 } catch (InterruptedException e
) {
146 synchronized (waitLock
) {
157 * Notify this {@link WaitingDialog} that the job is done, and dismiss it if
158 * it was already showing on screen (or never show it if it was not).
160 * Will also dispose the {@link WaitingDialog}.
162 public void dismiss() {
163 synchronized (waitLock
) {
171 pg
.removeProgressListener(pgl
);