1 7 8 package org.gjt.jclasslib.util; 9 10 import javax.swing.*; 11 import java.awt.*; 12 import java.awt.event.ComponentAdapter ; 13 import java.awt.event.ComponentEvent ; 14 15 21 public class ProgressDialog extends JDialog { 22 23 private static final int PROGRESS_BAR_WIDTH = 200; 24 25 private Runnable runnable; 26 27 private JProgressBar progressBar; 28 private JLabel lblMessage; 29 30 36 public ProgressDialog(JFrame parent, Runnable runnable, String message) { 37 super(parent); 38 init(runnable, message); 39 } 40 41 47 public ProgressDialog(JDialog parent, Runnable runnable, String message) { 48 super(parent); 49 init(runnable, message); 50 } 51 52 56 public void setMessage(String message) { 57 lblMessage.setText(message); 58 } 59 60 64 public void setRunnable(Runnable runnable) { 65 this.runnable = runnable; 66 } 67 68 public void setVisible(boolean visible) { 69 if (visible) { 70 progressBar.setIndeterminate(true); 71 GUIHelper.centerOnParentWindow(this, getOwner()); 72 } else { 73 progressBar.setIndeterminate(false); 74 } 75 super.setVisible(visible); 76 } 77 78 private void init(Runnable runnable, String message) { 79 setupControls(); 80 setupComponent(); 81 setupEventHandlers(); 82 setMessage(message); 83 setRunnable(runnable); 84 } 85 86 private void setupControls() { 87 88 progressBar = new JProgressBar(); 89 Dimension preferredSize = progressBar.getPreferredSize(); 90 preferredSize.width = PROGRESS_BAR_WIDTH; 91 progressBar.setPreferredSize(preferredSize); 92 lblMessage = new JLabel(" "); 93 } 94 95 private void setupComponent() { 96 97 JPanel contentPane = (JPanel)getContentPane(); 98 contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 99 contentPane.setLayout(new GridBagLayout()); 100 GridBagConstraints gc = new GridBagConstraints(); 101 gc.gridx = 0; 102 gc.gridy = GridBagConstraints.RELATIVE; 103 gc.anchor = GridBagConstraints.NORTHWEST; 104 contentPane.add(lblMessage, gc); 105 gc.weightx = 1; 106 gc.fill = GridBagConstraints.HORIZONTAL; 107 contentPane.add(progressBar, gc); 108 109 setTitle(GUIHelper.MESSAGE_TITLE); 110 setModal(true); 111 pack(); 112 113 } 114 115 private void setupEventHandlers() { 116 117 addComponentListener(new ComponentAdapter () { 118 public void componentShown(ComponentEvent event) { 119 final Thread task = new Thread (runnable); 120 task.start(); 121 new Thread () { 122 public void run() { 123 try { 124 task.join(); 125 } catch (InterruptedException e) { 126 } 127 SwingUtilities.invokeLater(new Runnable () { 128 public void run() { 129 setVisible(false); 130 } 131 }); 132 } 133 }.start(); 134 } 135 }); 136 } 137 138 139 } 140 | Popular Tags |