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