1 package net.suberic.pooka.gui; 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.util.List ; 5 6 import net.suberic.util.swing.*; 7 8 9 13 public class ProgressDialogImpl implements ProgressDialog { 14 15 JProgressBar progressBar; 16 JDialog dialog; 17 boolean cancelled = false; 18 int mCurrentValue; 19 String mTitle; 20 String mMessage; 21 22 JLabel nameLabel; 23 JPanel buttonPanel; 24 List listenerList = new java.util.ArrayList (); 25 26 30 public ProgressDialogImpl(int min, int max, int current, String title, String message) { 31 initDialog(min, max, current,title, message); 32 } 33 34 37 protected ProgressDialogImpl() { 38 39 } 40 41 protected void initDialog(int min, int max, int current, String title, String message) { 42 progressBar = new JProgressBar(min, max); 43 44 mTitle = title; 45 mMessage = message; 46 47 nameLabel = new JLabel(mTitle); 48 buttonPanel = new JPanel(); 49 JButton cancelButton = new JButton(net.suberic.pooka.Pooka.getProperty("button.cancel", "Cancel")); 50 cancelButton.addActionListener(new ActionListener() { 51 public void actionPerformed(ActionEvent e) { 52 cancelAction(); 53 } 54 }); 55 buttonPanel.add(cancelButton); 56 57 createDialog(); 58 59 setValue(current); 60 } 61 62 65 protected void createDialog() { 66 dialog = new JDialog(); 67 dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS)); 68 69 dialog.getContentPane().add(nameLabel); 70 dialog.getContentPane().add(progressBar); 71 dialog.getContentPane().add(buttonPanel); 72 73 dialog.pack(); 74 75 } 76 77 80 public void setMinimumValue(int minimum) { 81 progressBar.setMinimum(minimum); 82 } 83 84 87 public int getMinimumValue() { 88 return progressBar.getMinimum(); 89 } 90 91 94 public void setMaximumValue(int maximum) { 95 progressBar.setMaximum(maximum); 96 } 97 98 101 public int getMaximumValue() { 102 return progressBar.getMaximum(); 103 } 104 105 108 public void setValue(int current) { 109 mCurrentValue = current; 110 if (SwingUtilities.isEventDispatchThread()) { 111 if (mCurrentValue != getBarValue()) 112 progressBar.setValue(mCurrentValue); 113 } else 114 SwingUtilities.invokeLater(new Runnable () { 115 public void run() { 116 if (mCurrentValue != getBarValue()) 117 progressBar.setValue(mCurrentValue); 118 } 119 }); 120 } 121 122 125 public int getValue() { 126 return mCurrentValue; 127 } 128 129 132 public int getBarValue() { 133 return progressBar.getValue(); 134 } 135 136 139 public String getTitle() { 140 return mTitle; 141 } 142 143 146 public String getMessage() { 147 return mMessage; 148 } 149 150 153 public void cancelAction() { 154 cancelled = true; 155 for (int i = 0; i < listenerList.size(); i++) { 156 ProgressDialogListener current = (ProgressDialogListener) listenerList.get(i); 157 current.dialogCancelled(); 158 } 159 } 160 161 164 public void addCancelListener(ProgressDialogListener listener) { 165 listenerList.add(listener); 166 } 167 168 171 public boolean isCancelled() { 172 return cancelled; 173 } 174 175 178 public void show() { 179 if (SwingUtilities.isEventDispatchThread()) 180 dialog.setVisible(true); 181 else 182 SwingUtilities.invokeLater(new Runnable () { 183 public void run() { 184 dialog.setVisible(true); 185 } 186 }); 187 } 188 189 192 public void dispose() { 193 if (SwingUtilities.isEventDispatchThread()) 194 dialog.dispose(); 195 else 196 SwingUtilities.invokeLater(new Runnable () { 197 public void run() { 198 dialog.dispose(); 199 } 200 }); 201 } 202 } 203 | Popular Tags |