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 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 boolean firstTimeCall = true; 39 40 boolean withSecondProgress = false; 41 42 private Process process = null; 43 44 public final Interrupter interrupter = new Interrupter(); 45 46 47 public ProgressModalDialog( JFrame owner, String title, boolean withSecondProgress, boolean modal) 48 { 49 super(owner,title,modal); 50 this.withSecondProgress = withSecondProgress; 51 EventQueue.invokeLater(new Runnable () { public void run() { initialize(); 53 }}); 54 } 55 56 57 public ProgressModalDialog( JDialog owner, String title, boolean withSecondProgress) 58 { 59 super(owner,title,true); 60 this.withSecondProgress = withSecondProgress; 61 EventQueue.invokeLater(new Runnable () { public void run() { initialize(); 63 }}); 64 65 } 66 67 public void setShowCancel(final boolean show) 68 { 69 EventQueue.invokeLater(new Runnable () { public void run() { 70 cancelBT.setVisible(show); 71 }}); 72 73 } 74 75 public void setProgressBounds(final int max) 76 { 77 EventQueue.invokeLater(new Runnable () 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 () 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 comment) 100 { 101 EventQueue.invokeLater(new Runnable () 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 comment) 118 { 119 EventQueue.invokeLater(new Runnable () 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 comment) 134 { 135 EventQueue.invokeLater(new Runnable () 136 { 137 public void run() 138 { 139 progressLabel.setText(comment); 140 } 141 }); 142 } 143 144 146 public void setProcessToOptionalKill(Process 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 () 157 { 158 public void run() 159 { 160 contentPane.startAnimation(); 161 setVisible(true); } 163 }); 164 } 165 166 168 public void closeDialog() 169 { 170 SwingSafeRunnable ssr = new SwingSafeRunnable(new Runnable () 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 interrupter.stopEvaluation(); 218 219 if(process!=null) 220 { 221 wasCanceled = true; 222 try 223 { 224 process.destroy(); 225 closeDialog(); 226 227 } catch(Exception ex) {ex.printStackTrace();} 228 } 229 wasCanceled = true; 230 } 232 }); 233 234 progress.setStringPainted(true); 235 messageProgress.setStringPainted(true); 236 237 messageProgress.setVisible(false); 238 messageProgressLabel.setVisible(false); 239 240 241 if(withSecondProgress) 243 { 244 this.setSize( fontSize*28 ,fontSize*15 ); 245 } 246 else 247 { 248 this.setSize( fontSize*28 ,fontSize*11 ); 249 } 250 251 final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 253 this.setLocation( (screen.width - this.getWidth())/2, 254 (screen.height - this.getHeight())/2 ); 255 256 257 } 259 260 public void setMessageProgressActive(final int max) 261 { 262 EventQueue.invokeLater(new Runnable () 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 } 274 }); 275 } 276 277 public void setMessageProgressValue(final int val) 278 { 279 EventQueue.invokeLater(new Runnable () 280 { 281 public void run() 282 { 283 messageProgress.setValue(val); 284 } 285 }); 286 } 287 288 public JProgressBar getMessageProgress() { return messageProgress; } 289 290 } | Popular Tags |