1 5 package net.sf.panoptes.swing.panels; 6 7 import java.awt.BorderLayout ; 8 import java.awt.Dimension ; 9 import java.awt.FlowLayout ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import javax.swing.BorderFactory ; 17 import javax.swing.BoxLayout ; 18 import javax.swing.JButton ; 19 import javax.swing.JFrame ; 20 import javax.swing.JLabel ; 21 import javax.swing.JPanel ; 22 import javax.swing.JProgressBar ; 23 import javax.swing.SwingUtilities ; 24 import javax.swing.Timer ; 25 import javax.swing.UIManager ; 26 import javax.swing.border.BevelBorder ; 27 28 import net.sf.panoptes.swing.Operation; 29 import net.sf.panoptes.swing.OperationFactory; 30 31 import foxtrot.Job; 32 import foxtrot.Worker; 33 34 40 public class StatusBar extends JPanel { 41 42 JPanel operationsContainer; 43 JPanel progressPanel; 44 JPanel operationPanel; 45 JPanel statusPanel; 46 private JLabel statusLabel; 47 private long defaultTimeToLive = 5000; 48 private Timer timer; 49 private Factory factory; 50 51 private StatusBarOperation currentOperation = null; 52 private StatusMessage currentMessage = null; 53 54 public StatusBar() { 55 factory = new Factory (this); 56 setLayout(new BorderLayout (2, 5)); 57 setBorder(BorderFactory.createEmptyBorder()); 58 59 operationPanel = new JPanel (); 60 operationPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 61 operationPanel.setLayout(new BorderLayout ()); 62 63 statusPanel = new JPanel (); 64 statusPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 65 statusPanel.setLayout(new BorderLayout ()); 66 statusLabel = new JLabel (); 67 statusPanel.add(statusLabel, BorderLayout.CENTER); 68 add(statusPanel, BorderLayout.CENTER); 69 70 statusLabel.setPreferredSize(new Dimension (0, statusLabel.getFont().getSize() + 2)); 71 } 72 73 public void removeNotify() { 74 super.removeNotify(); 75 timer.stop(); 76 } 77 78 public void addNotify() { 79 super.addNotify(); 80 timer = new Timer (100, new ActionListener () { 81 public void actionPerformed(ActionEvent e) { 82 83 if (currentMessage != null) { 84 long currentTime = System.currentTimeMillis(); 85 if (currentMessage.getExpiresAt() < currentTime) { 86 currentMessage = null; 87 statusLabel.setText(""); 88 } 89 } 90 if (currentOperation != null) 91 if (currentOperation.isIndefinite()) { 92 int progress = currentOperation.getProgress(); 93 if (progress >= 5) 94 currentOperation.setProgress(0, 5); 95 else 96 currentOperation.setProgress(++progress, 5); 97 } 98 } 99 100 }); 101 timer.start(); 102 } 103 public void setDefaultStatusMessageTimeToLive(long timeToLive) { 104 this.defaultTimeToLive = timeToLive; 105 } 106 107 public long getDefaultStatusMessageTimeToLive() { 108 return defaultTimeToLive; 109 } 110 111 public void addStatusMessage(String message, long timeToLive) { 112 StatusMessage msg = new StatusMessage(this, message, timeToLive); 113 currentMessage = msg; 114 statusLabel.setText(message); 115 } 116 117 public void addStatusMessage(String message) { 118 addStatusMessage(message, defaultTimeToLive); 119 } 120 121 public Operation createOperation(String operationName, boolean indefinite) { 122 StatusBarOperation op = new StatusBarOperation(this, operationName); 123 currentOperation = op; 124 op.setIndefinite(indefinite); 125 operationPanel.add(op.getPanel()); 126 add(operationPanel, BorderLayout.EAST); 127 return op; 128 } 129 130 public OperationFactory getFactory() { 131 return factory; 132 } 133 134 private void removeOperation(StatusBarOperation op) { 135 operationPanel.remove(op.getPanel()); 136 remove(operationPanel); 137 doLayout(); 138 } 139 140 private static class StatusMessage { 141 private String message; 142 private long expires; 143 private StatusBar taskBar; 144 private JLabel text; 145 146 private StatusMessage(StatusBar taskBar, String message, long timeToLive) { 147 this.taskBar = taskBar; 148 this.message = message; 149 this.expires = System.currentTimeMillis() + timeToLive; 150 this.text = new JLabel (message); 151 } 152 153 private JLabel getLabel() { 154 return text; 155 } 156 157 private long getExpiresAt() { 158 return expires; 159 } 160 } 161 162 public static class StatusBarOperation implements net.sf.panoptes.swing.Operation { 163 private String operationName; 164 private JLabel text; 165 private StatusBar taskBar; 166 private JProgressBar progressBar; 167 private JPanel panel; 168 private int progress = -1; 169 private boolean indefinite = false; 170 171 private StatusBarOperation(StatusBar taskBar, String operationName) { 172 this.taskBar = taskBar; 173 this.operationName = operationName; 174 this.progressBar = new JProgressBar (); 175 176 panel = new JPanel (new BorderLayout (10, 1)); 177 text = new JLabel (""); 178 newTask(null); 179 panel.add(text, BorderLayout.WEST); 180 panel.add(progressBar, BorderLayout.EAST); 181 } 182 183 private JPanel getPanel() { 184 return panel; 185 } 186 187 public String getName() { 188 return operationName; 189 } 190 191 public void newTask(String taskName) { 192 if (taskName == null) { 193 text.setText(operationName + ":"); 194 } else { 195 text.setText(operationName + " (" + taskName + "):"); 196 } 197 } 198 199 public void setProgress(int currentProgress, int total) { 200 if (total != progressBar.getMaximum()) 201 progressBar.setMaximum(total); 202 progressBar.setValue(currentProgress); 203 progressBar.revalidate(); 204 progress = currentProgress; 205 } 206 207 public int getProgress() { 208 return progress; 209 } 210 211 public void operationFinished() { 212 taskBar.removeOperation(this); 213 } 214 215 public void setIndefinite(boolean indefinite) { 216 this.indefinite = indefinite; 217 if (indefinite) 218 progress = 0; 219 } 220 221 public boolean isIndefinite() { 222 return indefinite; 223 } 224 225 } 226 227 public static class Factory extends OperationFactory { 228 private StatusBar statusBar; 229 public Factory(StatusBar statusBar) { 230 this.statusBar = statusBar; 231 } 232 233 public Operation createOperation(String name, boolean indefinite) { 234 return statusBar.createOperation(name, indefinite); 235 } 236 237 public void createStatusMessage(String message, long timeToLive) { 238 statusBar.addStatusMessage(message, timeToLive); 239 } 240 241 public void createStatusMessage(String message) { 242 statusBar.addStatusMessage(message); 243 } 244 245 } 246 247 public static void main(String [] args) { 248 try { 249 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 250 } catch (Exception e) { 251 e.printStackTrace(); 252 } 253 JFrame f = new JFrame ("ProgressTest"); 254 f.setContentPane(new JPanel (new BorderLayout ())); 255 256 final StatusBar taskBar = new StatusBar(); 257 f.getContentPane().add(taskBar, BorderLayout.SOUTH); 258 259 JButton btnTest = new JButton ("Add some messages"); 260 btnTest.addActionListener(new ActionListener () { 261 public void actionPerformed(ActionEvent e) { 262 new Thread (new Runnable () { 263 public void run() { 264 for (int i = 0; i < 10; i++) { 265 StringBuffer buf = new StringBuffer (); 266 for (int j = 10 - i; j > 0; j--) 267 buf.append("dag"); 268 taskBar.addStatusMessage(buf.toString()); 269 try { 270 Thread.sleep(100); 271 } catch (InterruptedException ex) { 272 273 } 274 } 275 276 } 277 }).start(); 278 } 279 }); 280 281 f.getContentPane().add(btnTest, BorderLayout.NORTH); 282 283 f.pack(); 284 f.setSize(new Dimension (400, 400)); 285 f.show(); 286 287 Operation indef = taskBar.createOperation("Indefinite", true); 288 indef.setIndefinite(true); 289 290 final Operation op1 = taskBar.createOperation("Operation 1", false); 291 new Thread (new Runnable () { 292 public void run() { 293 taskBar.addStatusMessage("Operation 1 started"); 294 op1.setProgress(0, 100); 295 for (int i = 0; i < 100; i++) { 296 try { 297 Thread.sleep(100); 298 } catch (InterruptedException e) { 299 break; 300 } 301 op1.setProgress(i, 100); 302 } 303 taskBar.addStatusMessage("Operation 1 finished"); 304 op1.operationFinished(); 305 } 306 }).start(); 307 308 final Operation op2 = taskBar.createOperation("Operation 2", false); 309 new Thread (new Runnable () { 310 public void run() { 311 op2.newTask("Initializing"); 312 op2.setProgress(0, 100); 313 for (int i = 0; i < 100;) { 314 i += 5; 315 try { 316 Thread.sleep(50); 317 } catch (InterruptedException e) { 318 break; 319 } 320 op2.setProgress(i, 100); 321 } 322 taskBar.addStatusMessage("Done initializing"); 323 op2.newTask("Loading"); 324 op2.setProgress(0, 100); 325 for (int i = 0; i < 100;) { 326 i += 5; 327 try { 328 Thread.sleep(50); 329 } catch (InterruptedException e) { 330 break; 331 } 332 op2.setProgress(i, 100); 333 } 334 op2.operationFinished(); 335 } 336 }).start(); 337 } 338 } 339 | Popular Tags |