KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > ProgressModalDialog


1 package snow.utils.gui;
2
3 import snow.concurrent.*;
4 import snow.Language.Language;
5
6 import java.awt.*;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import javax.swing.border.*;
10 import java.util.*;
11
12 /** A progress dialog
13    should be used from another thread BUT created from edt
14
15    1) construct it (in the EDT)
16    2) start()
17    3) if wasCancelled => stop process and call closeDialog()...
18    4) always call closeDialog() !
19
20    [Feb2005]: newly process stop button
21 */

22 public class ProgressModalDialog extends JDialog
23 {
24   final int fontSize = UIManager.getFont("Label.font").getSize();
25
26   SnowBackgroundPanel contentPane = new SnowBackgroundPanel(new BorderLayout());
27   final JProgressBar progress = new JProgressBar();
28   final JProgressBar messageProgress = new JProgressBar();
29
30   final JLabel progressLabel = new JLabel(Language.translate("Please wait")+"...");
31   final JLabel messageProgressLabel = new JLabel(Language.translate("Message progress")+": ");
32
33   private boolean wasCanceled = false;
34
35   final JButton cancelBT = new JButton(Language.translate("Cancel"));
36
37   // used to switch off the indeterminate progressbar
38
boolean firstTimeCall = true;
39
40   boolean withSecondProgress = false;
41
42   private Process JavaDoc process = null;
43
44   public final Interrupter interrupter = new Interrupter();
45
46
47   public ProgressModalDialog( JFrame owner, String JavaDoc title, boolean withSecondProgress, boolean modal)
48   {
49     super(owner,title,modal);
50     this.withSecondProgress = withSecondProgress;
51     EventQueue.invokeLater(new Runnable JavaDoc() { public void run() { // not nice, but solves our UI problems ??
52
initialize();
53     }});
54   }
55
56
57   public ProgressModalDialog( JDialog owner, String JavaDoc title, boolean withSecondProgress)
58   {
59     super(owner,title,true);
60     this.withSecondProgress = withSecondProgress;
61     EventQueue.invokeLater(new Runnable JavaDoc() { public void run() { // not nice, but solves our UI problems ??
62
initialize();
63     }});
64
65   }
66
67   public void setShowCancel(final boolean show)
68   {
69      EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
70         cancelBT.setVisible(show);
71      }});
72
73   }
74
75   public void setProgressBounds(final int max)
76   {
77     EventQueue.invokeLater(new Runnable JavaDoc()
78     {
79       public void run()
80       {
81         progress.setMaximum(max);
82       }
83     });
84   }
85
86
87   public void incrementProgress(final int n)
88   {
89     EventQueue.invokeLater(new Runnable JavaDoc()
90     {
91       public void run()
92       {
93         progress.setIndeterminate(false);
94         progress.setValue( progress.getValue() + n );
95       }
96     });
97   }
98
99   public void setProgressValue(final int val, final String JavaDoc comment)
100   {
101     EventQueue.invokeLater(new Runnable JavaDoc()
102     {
103       public void run()
104       {
105         if(firstTimeCall)
106         {
107            firstTimeCall = false;
108            progress.setIndeterminate(false);
109         }
110         progress.setValue(val);
111         progress.setString(comment);
112       }
113     });
114   }
115
116
117   public void setProgressComment(final String JavaDoc comment)
118   {
119     EventQueue.invokeLater(new Runnable JavaDoc()
120     {
121       public void run()
122       {
123         if(firstTimeCall)
124         {
125           firstTimeCall = false;
126           progress.setIndeterminate(false);
127         }
128         progress.setString(comment);
129       }
130     });
131   }
132
133   public void setCommentLabel(final String JavaDoc comment)
134   {
135     EventQueue.invokeLater(new Runnable JavaDoc()
136     {
137       public void run()
138       {
139         progressLabel.setText(comment);
140       }
141     });
142   }
143
144   /** this process will be killed if the user press cancel
145   */

146   public void setProcessToOptionalKill(Process JavaDoc p)
147   {
148     cancelBT.setVisible(true);
149     this.process = p;
150   }
151
152   public boolean wasCancelled() { return this.wasCanceled; }
153
154   public void start()
155   {
156     EventQueue.invokeLater(new Runnable JavaDoc()
157     {
158       public void run()
159       {
160         contentPane.startAnimation();
161         setVisible(true); //modal
162
}
163     });
164   }
165
166   /** must always be called at the end !
167   */

168   public void closeDialog()
169   {
170       SwingSafeRunnable ssr = new SwingSafeRunnable(new Runnable JavaDoc()
171       {
172         public void run()
173         {
174           contentPane.stopAnimation();
175           setVisible(false);
176           dispose();
177         }
178       }, true);
179       ssr.run();
180   }
181
182   private void initialize()
183   {
184     setContentPane(contentPane);
185
186     JPanel progressPanel = new JPanel();
187     progressPanel.setOpaque(false);
188     progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.Y_AXIS));
189     getContentPane().add(progressPanel, BorderLayout.CENTER);
190     progressPanel.setBorder(new EmptyBorder(1,2,1,2));
191
192     progressPanel.add(progressLabel);
193     progressPanel.add(progress);
194     progressPanel.add(messageProgressLabel);
195     progressPanel.add(messageProgress);
196
197
198     progressLabel.setBorder(new EmptyBorder(fontSize/4, fontSize/2, fontSize/4, fontSize/2));
199     messageProgressLabel.setBorder(new EmptyBorder(fontSize/4, fontSize/2, fontSize/4, fontSize/2));
200
201     progress.setOpaque(false);
202     messageProgress.setOpaque(false);
203     progress.setIndeterminate(true);
204
205
206     JPanel controlPanel = new JPanel();
207     controlPanel.setOpaque(false);
208     getContentPane().add(controlPanel, BorderLayout.SOUTH);
209     controlPanel.add(cancelBT);
210
211     cancelBT.setMargin(new Insets(0,2,0,2));
212     cancelBT.addActionListener(new ActionListener()
213     {
214       public void actionPerformed(ActionEvent e)
215       {
216          //System.out.println("Destroy "+process);
217
interrupter.stopEvaluation();
218
219          if(process!=null)
220          {
221            wasCanceled = true;
222            try
223            {
224              process.destroy();
225              closeDialog();
226
227            } catch(Exception JavaDoc ex) {ex.printStackTrace();}
228          }
229          wasCanceled = true;
230          // now the process to be canceled must check...
231
}
232     });
233
234     progress.setStringPainted(true);
235     messageProgress.setStringPainted(true);
236
237     messageProgress.setVisible(false);
238     messageProgressLabel.setVisible(false);
239
240
241     // fixed, but fontsize relative size :
242
if(withSecondProgress)
243     {
244       this.setSize( fontSize*28 ,fontSize*15 );
245     }
246     else
247     {
248       this.setSize( fontSize*28 ,fontSize*11 );
249     }
250
251     // center it on the frame :
252
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
253     this.setLocation( (screen.width - this.getWidth())/2,
254                        (screen.height - this.getHeight())/2 );
255
256
257   } // initialize
258

259
260   public void setMessageProgressActive(final int max)
261   {
262     EventQueue.invokeLater(new Runnable JavaDoc()
263     {
264       public void run()
265       {
266
267          messageProgress.setVisible(true);
268          messageProgressLabel.setVisible(true);
269          messageProgress.setMaximum(max);
270          messageProgress.setValue(0);
271
272          //setSize( fontSize*28 , fontSize*16 );
273
}
274     });
275   }
276
277   public void setMessageProgressValue(final int val)
278   {
279     EventQueue.invokeLater(new Runnable JavaDoc()
280     {
281       public void run()
282       {
283          messageProgress.setValue(val);
284       }
285     });
286   }
287
288   public JProgressBar getMessageProgress() { return messageProgress; }
289
290 } // ProgressModalDialog
Popular Tags