More gui work, more testing to do.
[FinalKeyGui.git] / src / fkgui / PermitCountDownDialog.java
1 package fkgui;
2
3 import org.eclipse.swt.events.ShellEvent;
4 import org.eclipse.swt.events.ShellListener;
5 import org.eclipse.swt.graphics.Image;
6 import org.eclipse.swt.graphics.Point;
7 import org.eclipse.swt.widgets.Dialog;
8 import org.eclipse.swt.widgets.Display;
9 import org.eclipse.swt.widgets.Shell;
10 import org.eclipse.swt.layout.FormLayout;
11 import org.eclipse.wb.swt.SWTResourceManager;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.FormData;
15 import org.eclipse.swt.layout.FormAttachment;
16
17 public class PermitCountDownDialog extends Dialog {
18
19         protected Object result;
20         protected Shell shell;
21         public Display display;
22
23         String msg;
24         int msLeft;
25         private Label lblTimeLeft;
26         /**
27          * Create the dialog.
28          * @param parent
29          * @param style
30          */
31         public PermitCountDownDialog(Shell parent, int style, String title, String m, int msL) {
32                 super(parent, style);
33                 setText(title);
34                 msLeft = msL;
35                 msg = m;
36         
37         }
38
39         /**
40          * Open the dialog.
41          * @return the result
42          */
43         public Object open() {
44                 createContents();
45                 Point p = getParent().getSize();
46                 p.x /= 2;
47                 p.y /= 2;
48                 p.x -= shell.getSize().x/2;
49                 p.y -= shell.getSize().y/2;
50                 p.x += getParent().getLocation().x;
51                 p.y += getParent().getLocation().y;
52                 shell.setLocation( p );
53                 shell.setLayout(null);
54                 
55                 Label lblMsg = new Label(shell, SWT.NONE);
56                 lblMsg.setBounds(48, 10, 461, 54);
57                 lblMsg.setText(msg);
58                 
59                 lblTimeLeft = new Label(shell, SWT.NONE);
60                 lblTimeLeft.setBounds(48, 56, 201, 32);
61                 
62                 Animation animation = new Animation(shell, SWT.NONE, 4);
63                 animation.setBounds(10, 32, 32, 32);
64                 animation.setVisible(true);
65                 animation.setPlaying(true);
66                 
67                 
68
69                 shell.open();
70                 
71                 shell.layout();
72                 display = getParent().getDisplay();
73                                 
74                 animate();
75                 shell.setActive();
76                 
77                 while (!shell.isDisposed()) {
78                         if (!display.readAndDispatch()) {
79                                 display.sleep();
80                         }
81                 }
82                 return result;
83         }
84
85         private void animate() {
86                 display.timerExec(1000, new Runnable() {
87                         
88                         @Override
89                         public void run() {
90                                 if( !shell.isDisposed() )
91                                 {
92
93                                         lblTimeLeft.setText( Math.round(msLeft/1000) + Messages.PermitCountDownDialog_0);
94
95                                         display.update();
96                                         msLeft -= 1000;
97                                         if( msLeft<1)
98                                         {
99                                                 shell.close();
100                                                 
101                                         } else {                                
102                                                 animate();
103                                         }
104                                         
105                                 }
106                         }
107                 });
108                 
109         }
110
111         /**
112          * Create contents of the dialog.
113          */
114         private void createContents() {
115                 shell = new Shell(getParent(), getStyle());
116                 shell.setSize(521, 125);
117                 shell.setText(getText());
118         }
119 }