1 18 package org.apache.batik.util.gui; 19 20 import java.awt.BasicStroke ; 21 import java.awt.BorderLayout ; 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.FlowLayout ; 26 import java.awt.Font ; 27 import java.awt.Graphics ; 28 import java.awt.Graphics2D ; 29 import java.awt.GridBagConstraints ; 30 import java.awt.GridBagLayout ; 31 import java.awt.Insets ; 32 import java.awt.RenderingHints ; 33 import java.awt.Stroke ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.WindowAdapter ; 36 import java.awt.event.WindowEvent ; 37 import java.awt.geom.AffineTransform ; 38 import java.awt.geom.GeneralPath ; 39 import java.awt.geom.Line2D ; 40 import java.awt.geom.Rectangle2D ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.LinkedList ; 45 import java.util.List ; 46 import java.util.Locale ; 47 import java.util.Map ; 48 import java.util.ResourceBundle ; 49 50 import javax.swing.AbstractAction ; 51 import javax.swing.Action ; 52 import javax.swing.BorderFactory ; 53 import javax.swing.JComponent ; 54 import javax.swing.JFrame ; 55 import javax.swing.JPanel ; 56 57 import org.apache.batik.util.gui.resource.ActionMap; 58 import org.apache.batik.util.gui.resource.ButtonFactory; 59 import org.apache.batik.util.gui.resource.MissingListenerException; 60 import org.apache.batik.util.gui.resource.ResourceManager; 61 62 69 public class MemoryMonitor extends JFrame implements ActionMap { 70 73 protected final static String RESOURCE = 74 "org.apache.batik.util.gui.resources.MemoryMonitorMessages"; 75 76 79 protected static ResourceBundle bundle; 80 81 84 protected static ResourceManager resources; 85 86 static { 87 bundle = ResourceBundle.getBundle(RESOURCE, Locale.getDefault()); 88 resources = new ResourceManager(bundle); 89 } 90 91 94 protected Map listeners = new HashMap (); 95 96 99 protected Panel panel; 100 101 105 public MemoryMonitor() { 106 this(1000); 107 } 108 109 113 public MemoryMonitor(long time) { 114 super(resources.getString("Frame.title")); 115 listeners.put("CollectButtonAction", new CollectButtonAction()); 116 listeners.put("CloseButtonAction", new CloseButtonAction()); 117 118 panel = new Panel (time); 119 120 getContentPane().add(panel); 121 panel.setBorder(BorderFactory.createTitledBorder 122 (BorderFactory.createEtchedBorder(), 123 resources.getString("Frame.border_title"))); 124 125 JPanel p = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 126 ButtonFactory bf = new ButtonFactory(bundle, this); 127 p.add(bf.createJButton("CollectButton")); 128 p.add(bf.createJButton("CloseButton")); 129 getContentPane().add("South", p); 130 131 pack(); 132 133 addWindowListener(new WindowAdapter () { 134 public void windowActivated(WindowEvent e) { 135 RepaintThread t = panel.getRepaintThread(); 136 if (!t.isAlive()) { 137 t.start(); 138 } else { 139 t.safeResume(); 140 } 141 } 142 public void windowClosing(WindowEvent ev) { 143 panel.getRepaintThread().safeSuspend(); 144 } 145 public void windowDeiconified(WindowEvent e) { 146 panel.getRepaintThread().safeResume(); 147 } 148 public void windowIconified(WindowEvent e) { 149 panel.getRepaintThread().safeSuspend(); 150 } 151 }); 152 } 153 154 156 162 public Action getAction(String key) throws MissingListenerException { 163 return (Action )listeners.get(key); 164 } 165 166 169 protected class CollectButtonAction extends AbstractAction { 170 public void actionPerformed(ActionEvent e) { 171 System.gc(); 172 } 173 } 174 175 178 protected class CloseButtonAction extends AbstractAction { 179 public void actionPerformed(ActionEvent e) { 180 panel.getRepaintThread().safeSuspend(); 181 dispose(); 182 } 183 } 184 185 188 public static class Panel extends JPanel { 189 192 protected RepaintThread repaintThread; 193 194 199 public Panel() { 200 this(1000); 201 } 202 203 209 public Panel(long time) { 210 super(new GridBagLayout ()); 211 212 ExtendedGridBagConstraints constraints 213 = new ExtendedGridBagConstraints(); 214 constraints.insets = new Insets (5, 5, 5, 5); 215 216 List l = new ArrayList (); 217 JPanel p = new JPanel (new BorderLayout ()); 218 p.setBorder(BorderFactory.createLoweredBevelBorder()); 219 JComponent c = new Usage(); 220 p.add(c); 221 constraints.weightx = 0.3; 222 constraints.weighty = 1; 223 constraints.fill = GridBagConstraints.BOTH; 224 constraints.setGridBounds(0, 0, 1, 1); 225 add(p, constraints); 226 l.add(c); 227 228 p = new JPanel (new BorderLayout ()); 229 p.setBorder(BorderFactory.createLoweredBevelBorder()); 230 c = new MemoryMonitor.History(); 231 p.add(c); 232 constraints.weightx = 0.7; 233 constraints.setGridBounds(1, 0, 1, 1); 234 add(p, constraints); 235 l.add(c); 236 237 repaintThread = new RepaintThread(time, l); 238 } 239 240 243 public RepaintThread getRepaintThread() { 244 return repaintThread; 245 } 246 } 247 248 251 public static class Usage extends JPanel implements MemoryChangeListener { 252 255 public final static int PREFERRED_WIDTH = 90; 256 257 260 public final static int PREFERRED_HEIGHT = 100; 261 262 265 protected final static String UNITS 266 = resources.getString("Usage.units"); 267 268 271 protected final static String TOTAL 272 = resources.getString("Usage.total"); 273 274 277 protected final static String USED 278 = resources.getString("Usage.used"); 279 280 283 protected final static boolean POSTFIX 284 = resources.getBoolean("Usage.postfix"); 285 286 289 protected final static int FONT_SIZE = 9; 290 291 294 protected final static int BLOCK_MARGIN = 10; 295 296 299 protected final static int BLOCKS = 15; 300 301 304 protected final static double BLOCK_WIDTH = 305 PREFERRED_WIDTH-BLOCK_MARGIN*2; 306 307 310 protected final static double BLOCK_HEIGHT = 311 ((double)PREFERRED_HEIGHT-(3*FONT_SIZE)-BLOCKS) / BLOCKS; 312 313 316 protected final static int[] BLOCK_TYPE = 317 { 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2 }; 318 319 322 protected Color [] usedColors = { 323 new Color (255, 0, 0), 324 new Color (255, 165, 0), 325 new Color (0, 255, 0) 326 }; 327 328 331 protected Color [] freeColors = { 332 new Color (130, 0, 0), 333 new Color (130, 90, 0), 334 new Color (0, 130, 0) 335 }; 336 337 340 protected Font font = new Font ("SansSerif", Font.BOLD, FONT_SIZE); 341 342 345 protected Color textColor = Color.green; 346 347 350 protected long totalMemory; 351 352 355 protected long freeMemory; 356 357 360 public Usage() { 361 this.setBackground(Color.black); 362 setPreferredSize(new Dimension (PREFERRED_WIDTH, PREFERRED_HEIGHT)); 363 } 364 365 370 public void memoryStateChanged(long total, long free) { 371 totalMemory = total; 372 freeMemory = free; 373 } 374 375 378 public void setTextColor(Color c) { 379 textColor = c; 380 } 381 382 385 public void setLowUsedMemoryColor(Color c) { 386 usedColors[2] = c; 387 } 388 389 392 public void setMediumUsedMemoryColor(Color c) { 393 usedColors[1] = c; 394 } 395 396 399 public void setHighUsedMemoryColor(Color c) { 400 usedColors[0] = c; 401 } 402 403 406 public void setLowFreeMemoryColor(Color c) { 407 freeColors[2] = c; 408 } 409 410 413 public void setMediumFreeMemoryColor(Color c) { 414 freeColors[1] = c; 415 } 416 417 420 public void setHighFreeMemoryColor(Color c) { 421 freeColors[0] = c; 422 } 423 424 427 protected void paintComponent(Graphics g) { 428 super.paintComponent(g); 429 Graphics2D g2d = (Graphics2D )g; 430 431 Dimension dim = getSize(); 433 double sx = ((double)dim.width) / PREFERRED_WIDTH; 434 double sy = ((double)dim.height) / PREFERRED_HEIGHT; 435 g2d.transform(AffineTransform.getScaleInstance(sx, sy)); 436 437 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 439 RenderingHints.VALUE_ANTIALIAS_ON); 440 441 int nfree = (int)Math.round(((double)BLOCKS) 443 * freeMemory / totalMemory); 444 445 for (int i = 0; i < nfree; i++) { 446 Rectangle2D rect = new Rectangle2D.Double (10, 447 i*BLOCK_HEIGHT+i+FONT_SIZE+5, 448 BLOCK_WIDTH, 449 BLOCK_HEIGHT); 450 g2d.setPaint(freeColors[BLOCK_TYPE[i]]); 451 g2d.fill(rect); 452 } 453 454 for (int i = nfree; i < 15; i++) { 455 Rectangle2D rect = new Rectangle2D.Double (10, 456 i*BLOCK_HEIGHT+i+FONT_SIZE+5, 457 BLOCK_WIDTH, 458 BLOCK_HEIGHT); 459 g2d.setPaint(usedColors[BLOCK_TYPE[i]]); 460 g2d.fill(rect); 461 } 462 463 g2d.setPaint(textColor); 465 g2d.setFont(font); 466 467 long total = totalMemory / 1024; 468 long used = (totalMemory - freeMemory) / 1024; 469 String totalText; 470 String usedText; 471 if (POSTFIX) { 472 totalText = total + UNITS + " " + TOTAL; 473 usedText = used + UNITS + " " + USED; 474 } else { 475 totalText = TOTAL + " " + total + UNITS; 476 usedText = USED + " " + used + UNITS; 477 } 478 479 g2d.drawString(totalText, 10, 10); 480 g2d.drawString(usedText, 10, PREFERRED_HEIGHT-3); 481 } 482 } 483 484 487 public static class History extends JPanel implements MemoryChangeListener { 488 491 public final static int PREFERRED_WIDTH = 200; 492 493 496 public final static int PREFERRED_HEIGHT = 100; 497 498 501 protected final static Stroke GRID_LINES_STROKE = new BasicStroke (1); 502 503 506 protected final static Stroke CURVE_STROKE = 507 new BasicStroke (2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 508 509 512 protected final static Stroke BORDER_STROKE = new BasicStroke (2); 513 514 517 protected Color gridLinesColor = new Color (0, 130, 0); 518 519 522 protected Color curveColor = Color.yellow; 523 524 527 protected Color borderColor = Color.green; 528 529 532 protected List data = new LinkedList (); 533 534 537 protected int xShift = 0; 538 539 542 protected long totalMemory; 543 544 547 protected long freeMemory; 548 549 552 protected GeneralPath path = new GeneralPath (); 553 554 557 public History() { 558 this.setBackground(Color.black); 559 setPreferredSize(new Dimension (PREFERRED_WIDTH, PREFERRED_HEIGHT)); 560 } 561 562 567 public void memoryStateChanged(long total, long free) { 568 totalMemory = total; 569 freeMemory = free; 570 571 data.add(new Long (totalMemory - freeMemory)); 573 if (data.size() > 190) { 574 data.remove(0); 575 xShift = (xShift + 1) % 10; 576 } 577 578 Iterator it = data.iterator(); 580 GeneralPath p = new GeneralPath (); 581 long l = ((Long )it.next()).longValue(); 582 p.moveTo(5, ((float)(totalMemory - l) / totalMemory) * 80 + 10); 583 int i = 6; 584 while (it.hasNext()) { 585 l = ((Long )it.next()).longValue(); 586 p.lineTo(i, ((float)(totalMemory - l) / totalMemory) * 80 + 10); 587 i++; 588 } 589 path = p; 590 } 591 592 595 protected void paintComponent(Graphics g) { 596 super.paintComponent(g); 597 Graphics2D g2d = (Graphics2D )g; 598 599 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 601 RenderingHints.VALUE_ANTIALIAS_ON); 602 603 Dimension dim = getSize(); 605 double sx = ((double)dim.width) / PREFERRED_WIDTH; 606 double sy = ((double)dim.height) / PREFERRED_HEIGHT; 607 g2d.transform(AffineTransform.getScaleInstance(sx, sy)); 608 609 g2d.setPaint(gridLinesColor); 611 g2d.setStroke(GRID_LINES_STROKE); 612 for (int i = 1; i < 20; i++) { 613 int lx = i * 10 + 5 - xShift; 614 g2d.draw(new Line2D.Double (lx, 5, lx, PREFERRED_HEIGHT - 5)); 615 } 616 617 for (int i = 1; i < 9; i++) { 619 int ly = i * 10 + 5; 620 g2d.draw(new Line2D.Double (5, ly, PREFERRED_WIDTH - 5, ly)); 621 } 622 623 g2d.setPaint(curveColor); 625 g2d.setStroke(CURVE_STROKE); 626 627 g2d.draw(path); 628 629 g2d.setStroke(BORDER_STROKE); 631 g2d.setPaint(borderColor); 632 g2d.draw(new Rectangle2D.Double (5, 633 5, 634 PREFERRED_WIDTH - 10, 635 PREFERRED_HEIGHT - 10)); 636 637 } 638 } 639 640 644 public interface MemoryChangeListener { 645 650 void memoryStateChanged(long total, long free); 651 } 652 653 656 public static class RepaintThread extends Thread { 657 660 protected long timeout; 661 662 665 protected List components; 666 667 670 protected Runtime runtime = Runtime.getRuntime(); 671 672 675 protected boolean suspended; 676 677 682 public RepaintThread(long timeout, List components) { 683 this.timeout = timeout; 684 this.components = components; 685 setPriority(Thread.MIN_PRIORITY); 686 } 687 688 691 public void run() { 692 for (;;) { 693 long free = runtime.freeMemory(); 694 long total = runtime.totalMemory(); 695 Iterator it = components.iterator(); 696 while (it.hasNext()) { 697 Component c = (Component )it.next(); 698 ((MemoryChangeListener)c).memoryStateChanged(total, free); 699 c.repaint(); 700 } 701 try { 702 sleep(timeout); 703 synchronized(this) { 704 while (suspended) { 705 wait(); 706 } 707 } 708 } catch (InterruptedException e) {} 709 } 710 } 711 712 715 public synchronized void safeSuspend() { 716 if (!suspended) { 717 suspended = true; 718 } 719 } 720 721 724 public synchronized void safeResume() { 725 if (suspended) { 726 suspended = false; 727 notify(); 728 } 729 } 730 } 731 } 732 | Popular Tags |