1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package wjhk.jupload2.gui;
24
25 import java.awt.BorderLayout;
26 import java.awt.Cursor;
27 import java.awt.Dimension;
28 import java.awt.Frame;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.ComponentEvent;
32 import java.awt.event.ComponentListener;
33 import java.lang.reflect.InvocationTargetException;
34
35 import javax.swing.BorderFactory;
36 import javax.swing.Box;
37 import javax.swing.BoxLayout;
38 import javax.swing.JButton;
39 import javax.swing.JDialog;
40 import javax.swing.JPanel;
41 import javax.swing.JTextArea;
42 import javax.swing.Timer;
43
44 import wjhk.jupload2.exception.JUploadException;
45 import wjhk.jupload2.policies.UploadPolicy;
46
47
48
49
50
51
52
53
54
55
56 @SuppressWarnings("serial")
57 public class DialogUploadRetry extends JDialog implements ActionListener,
58 ComponentListener {
59
60 JButton buttonYes;
61
62 JButton buttonNo;
63
64 JButton buttonDetails;
65
66 JTextArea jtextArea;
67
68 JTextArea detailTestArea;
69
70 JUploadException juploadException = null;
71
72 UploadPolicy uploadPolicy = null;
73
74
75
76
77
78 Timer countdownTimer = null;
79
80
81
82
83
84 int countdownValue;
85
86
87
88
89
90
91 boolean retryValidated = false;
92
93
94
95
96
97
98
99
100
101
102
103 public DialogUploadRetry(Frame owner, JUploadException juploadException,
104 int numRetry, UploadPolicy uploadPolicy) throws JUploadException {
105 super(owner, uploadPolicy.getLocalizedString("dialogUploadRetryTitle"),
106 true);
107
108 this.uploadPolicy = uploadPolicy;
109 this.juploadException = juploadException;
110
111
112
113
114
115
116
117
118 double retryDelayFactor = Math.pow(1.5, numRetry);
119 this.countdownValue = (int) (uploadPolicy.getRetryNbSecondsBetween() * retryDelayFactor);
120 this.countdownTimer = new Timer(1000, this);
121 this.countdownTimer.start();
122
123
124
125 try {
126 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
127 public void run() {
128 createGUI();
129 }
130 });
131 } catch (InterruptedException e) {
132 throw new JUploadException("Error while creating the "
133 + this.getClass().getName(), e.getCause());
134 } catch (InvocationTargetException e) {
135 throw new JUploadException("Error while creating the "
136 + this.getClass().getName(), e.getCause());
137 }
138
139
140
141
142
143 setVisible(true);
144
145
146 }
147
148
149
150
151
152
153
154 public boolean isRetryValidated() {
155 return this.retryValidated;
156 }
157
158
159
160
161 private void createGUI() {
162
163
164
165 JPanel jPanelText = new JPanel();
166 this.jtextArea = new JTextArea(this.uploadPolicy.getLocalizedString(
167 "dialogUploadRetryText", this.countdownValue));
168 this.jtextArea.setEditable(false);
169 this.jtextArea.setMinimumSize(new Dimension(400, 100));
170 this.jtextArea.setBackground(jPanelText.getBackground());
171 jPanelText.add(this.jtextArea);
172 getContentPane().add(jPanelText, BorderLayout.CENTER);
173
174
175
176
177
178 this.buttonYes = new JButton(this.uploadPolicy
179 .getLocalizedString("buttonYes"));
180 this.buttonYes.setMinimumSize(new Dimension(80, 30));
181 this.buttonYes.setMaximumSize(new Dimension(100, 100));
182 this.buttonYes.addActionListener(this);
183
184 this.buttonNo = new JButton(this.uploadPolicy
185 .getLocalizedString("buttonNo"));
186 this.buttonNo.setMinimumSize(new Dimension(80, 30));
187 this.buttonNo.setMaximumSize(new Dimension(100, 100));
188 this.buttonNo.addActionListener(this);
189
190 this.buttonDetails = new JButton(this.uploadPolicy
191 .getLocalizedString("buttonDetails"));
192 this.buttonDetails.setMinimumSize(new Dimension(80, 30));
193 this.buttonDetails.setMaximumSize(new Dimension(100, 100));
194 this.buttonDetails.addActionListener(this);
195
196 JPanel jPanelButton = new JPanel();
197 jPanelButton
198 .setLayout(new BoxLayout(jPanelButton, BoxLayout.LINE_AXIS));
199 jPanelButton.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
200 jPanelButton.add(Box.createHorizontalGlue());
201 jPanelButton.add(this.buttonYes);
202 jPanelButton.add(Box.createRigidArea(new Dimension(10, 0)));
203 jPanelButton.add(this.buttonNo);
204 jPanelButton.add(Box.createRigidArea(new Dimension(10, 0)));
205 jPanelButton.add(this.buttonDetails);
206
207
208
209
210 JPanel jPanelDetail = new JPanel();
211 this.detailTestArea = new JTextArea(this.juploadException.getMessage());
212 this.detailTestArea.setEditable(false);
213 this.detailTestArea.setMinimumSize(new Dimension(400, 500));
214 this.detailTestArea.setBackground(jPanelDetail.getBackground());
215 jPanelDetail.add(this.detailTestArea);
216 getContentPane().add(jPanelDetail, BorderLayout.CENTER);
217
218
219
220
221 JPanel mainPanel = new JPanel();
222 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
223 mainPanel.add(this.jtextArea);
224 mainPanel.add(jPanelButton);
225 mainPanel.add(jPanelDetail);
226
227 getContentPane().add(mainPanel);
228 getRootPane().setDefaultButton(this.buttonYes);
229
230
231 pack();
232
233
234 this.detailTestArea.setVisible(false);
235 }
236
237
238
239
240 public void actionPerformed(ActionEvent event) {
241 if (event.getSource() == this.countdownTimer) {
242
243 this.countdownValue -= 1;
244 if (this.countdownValue <= 0) {
245 disposeDialog(true);
246 } else {
247 this.jtextArea.setText(this.uploadPolicy.getLocalizedString(
248 "dialogUploadRetryText", this.countdownValue));
249 }
250 } else if (event.getActionCommand() == this.buttonDetails
251 .getActionCommand()) {
252 this.uploadPolicy
253 .displayDebug(
254 "[DialogUploadRetry] User choose to display or hide details",
255 10);
256 this.detailTestArea.setVisible(!this.detailTestArea.isVisible());
257
258 if (this.detailTestArea.isVisible()) {
259 this.detailTestArea.setMinimumSize(new Dimension(400, 500));
260 this.detailTestArea.setMaximumSize(new Dimension(400, 500));
261 } else {
262 this.detailTestArea.setMinimumSize(new Dimension(0, 0));
263 this.detailTestArea.setMaximumSize(new Dimension(0, 0));
264 }
265 } else if (event.getActionCommand() == this.buttonNo.getActionCommand()) {
266 this.uploadPolicy.displayDebug(
267 "[DialogUploadRetry] User choose buttonNo", 10);
268 disposeDialog(false);
269 } else if (event.getActionCommand() == this.buttonYes
270 .getActionCommand()) {
271 this.uploadPolicy.displayDebug(
272 "[DialogUploadRetry] User choose buttonYes", 10);
273 disposeDialog(true);
274 }
275 }
276
277
278
279
280
281
282 private void disposeDialog(boolean retryValidated) {
283 this.retryValidated = retryValidated;
284 this.countdownTimer.stop();
285 this.dispose();
286 }
287
288
289 public void componentHidden(ComponentEvent arg0) {
290
291 }
292
293
294 public void componentMoved(ComponentEvent arg0) {
295
296 }
297
298
299 public void componentResized(ComponentEvent arg0) {
300
301 }
302
303
304 public void componentShown(ComponentEvent arg0) {
305
306 setCursor(new Cursor(Cursor.HAND_CURSOR));
307 }
308 }