1 19 20 package com.sslexplorer.agent.client.gui.awt; 21 22 import java.awt.Button ; 23 import java.awt.Canvas ; 24 import java.awt.Color ; 25 import java.awt.Component ; 26 import java.awt.Dialog ; 27 import java.awt.Dimension ; 28 import java.awt.FlowLayout ; 29 import java.awt.FontMetrics ; 30 import java.awt.Frame ; 31 import java.awt.Graphics ; 32 import java.awt.Label ; 33 import java.awt.Panel ; 34 import java.awt.Window ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.awt.event.WindowAdapter ; 38 import java.awt.event.WindowEvent ; 39 import java.text.MessageFormat ; 40 41 import com.sshtools.ui.awt.UIUtil; 42 43 63 public class ProgressBar { 64 67 public final int BOXMSG_NONE = 0; 68 71 public final int BOXMSG_PERCENT = 1; 72 75 public final int BOXMSG_NUMBERS = 2; 76 77 78 private Frame parent; 79 private Window pbar; 80 private Label mess; 81 private ProgressBox pbox; 82 private Button cancel; 83 private boolean shouldCancel; 84 85 94 public ProgressBar(Component parentComponent, String message, String title, long maxValue) { 95 this(parentComponent, message, title, maxValue, true); 96 } 97 98 112 public ProgressBar(Component parentComponent, String message, String title, long maxValue, boolean allowCancel) { 113 shouldCancel = false; 114 116 parent = UIUtil.getFrameAncestor(parentComponent); 117 if (parent == null) { 118 pbar = new Frame (title); 119 } else { 120 if (!parent.isVisible()) { 121 parent.setVisible(true); 122 } 123 parent.toFront(); 124 pbar = new Dialog (parent, title); 125 } 126 127 if (parent != null) { 128 parent.addWindowListener(new WindowAdapter () { 129 public void windowClosing(WindowEvent e) { 130 parent.setVisible(false); 131 dispose(); 132 } 133 }); 134 } 135 136 Panel top = new Panel (new FlowLayout (FlowLayout.CENTER, 1, 1)); 138 mess = new Label (message); 139 top.add(mess); 140 pbar.add("North", top); 142 Panel middle = new Panel (new FlowLayout (FlowLayout.CENTER, 1, 1)); 144 pbox = new ProgressBox(maxValue); 145 middle.add(pbox); 146 pbar.add("Center", middle); 148 if (allowCancel) { 151 Panel bottom = new Panel (new FlowLayout (FlowLayout.CENTER, 1, 1)); 152 cancel = new Button (Messages.getString("ProgressBar.cancel")); cancel.addActionListener(new ActionListener () { 154 public void actionPerformed(ActionEvent e) { 155 shouldCancel = true; 157 } 158 }); 159 bottom.add(cancel); 160 pbar.add("South", bottom); } 162 163 Dimension d = pbar.getToolkit().getScreenSize(); 165 pbar.setLocation(d.width / 3, d.height / 3); pbar.pack(); if (pbar instanceof Dialog ) { 169 ((Dialog ) pbar).setResizable(false); } else { 172 ((Frame ) pbar).setResizable(false); } 175 pbar.setVisible(true); pbar.toFront(); 178 } 179 180 189 public void updateValue(long value) { 190 pbox.updateValue(value); 191 } 192 193 198 public void updateMaxValue(long value) { 199 pbox.updateMaxValue(value); 200 } 201 202 208 public long getCurrentValue() { 209 return pbox.getCurrentValue(); 210 } 211 212 219 public long getMaxValue() { 220 return pbox.getMaxValue(); 221 } 222 223 231 public void setBarText(int boxMsgValue) { 232 pbox.setBarMsg(boxMsgValue); 233 } 234 235 240 public void setMessage(String message) { 241 mess.setText(message); 242 } 244 245 248 public void dispose() { 249 try { 251 pbar.dispose(); 252 } catch (Exception e) { 253 } 254 255 } 261 262 270 public boolean isCancelClicked() { 271 return shouldCancel; 272 } 273 274 281 class ProgressBox extends Canvas { 282 285 public final int MSG_NONE = 0; 286 287 290 public final int MSG_PERCENT = 1; 291 292 295 public final int MSG_NUMBERS = 2; 296 297 private long maxVal, currentVal; 298 private int cols, width, height, dispText; 299 private Color barClr, borderClr, textClr; 300 301 306 public ProgressBox(long maxValue) { 307 this(maxValue, 40); 308 } 309 310 316 public ProgressBox(long maxValue, int width) { 317 maxVal = maxValue; 321 currentVal = 0; 322 cols = width; 323 dispText = MSG_PERCENT; 324 barClr = Color.decode("#D8DFEE"); borderClr = Color.gray; textClr = Color.darkGray; } 329 330 protected void measure() { 331 FontMetrics fm = this.getFontMetrics(this.getFont()); 333 if (fm == null) { 334 return; 335 } 336 width = fm.stringWidth("X") * cols; height = fm.getHeight() + 4; 338 } 339 340 public void addNotify() { 341 super.addNotify(); 345 measure(); 346 } 347 348 public Dimension getPreferredSize() { 349 return new Dimension (width + 4, height + 4); 351 } 352 353 public Dimension getMinimumSize() { 354 return getPreferredSize(); 357 } 358 359 364 public void updateValue(long value) { 365 currentVal = value; 369 this.repaint(); 370 } 371 372 377 public void updateMaxValue(long value) { 378 maxVal = value; 382 this.repaint(); 383 } 384 385 390 public long getCurrentValue() { 391 return currentVal; 393 } 394 395 400 public long getMaxValue() { 401 return maxVal; 403 } 404 405 410 public void setBarMsg(int msgValue) { 411 dispText = msgValue; 415 } 416 417 424 public void setColors(Color barColor, Color borderColor, Color textColor) { 425 if (barColor != null) { 427 barClr = barColor; 428 } 429 if (borderColor != null) { 430 borderClr = borderColor; 431 } 432 if (textColor != null) { 433 textClr = textColor; 434 } 435 } 436 437 public void paint(Graphics g) { 438 g.setColor(barClr); 441 if (currentVal > 0 && maxVal > 0) 442 g.fillRect(0, 0, (int) ((currentVal * width) / maxVal), height); 443 444 g.setColor(borderClr); 446 g.drawRect(0, 0, width, height); 447 448 g.setColor(textClr); 450 if (dispText == MSG_PERCENT) { 451 if (currentVal > 0 && maxVal > 0) { 452 centerText(String.valueOf((int) ((currentVal * 100) / maxVal)) + "%", g, width, height); } else if (currentVal == 0) { 454 centerText("0%", g, width, height); 456 } 457 } else if (dispText == MSG_NUMBERS) { 458 centerText(MessageFormat.format(Messages.getString("ProgressBar.numbers"), new Object [] { new Long (currentVal), new Long (maxVal) } ), g, width, height); } 460 } 461 462 private void centerText(String s, Graphics g, int w, int h) { 463 FontMetrics fm = this.getFontMetrics(this.getFont()); 465 if (fm == null) { 466 return; 467 } 468 int sWidth = fm.stringWidth(s); 469 int sx = (w - sWidth) / 2; 470 int sy = (h - fm.getHeight()) / 2 + fm.getAscent(); 471 g.drawString(s, sx, sy); 472 } 473 474 } 476 } | Popular Tags |