01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.client.util;
19:
20: import java.awt.BorderLayout;
21: import java.awt.Color;
22: import java.awt.Dimension;
23: import java.awt.Graphics;
24: import java.awt.Graphics2D;
25: import java.awt.event.ActionEvent;
26: import java.awt.event.ActionListener;
27:
28: import javax.swing.JButton;
29: import javax.swing.JComponent;
30: import javax.swing.JPanel;
31: import javax.swing.SwingUtilities;
32:
33: import de.finix.contelligent.client.base.ContelligentConstants;
34: import de.finix.contelligent.client.i18n.Resources;
35:
36: public class MemoryUsagePanel extends JPanel {
37: public MemoryUsagePanel() {
38: setLayout(new BorderLayout());
39: JButton garbage = new JButton(Resources.deleteIcon);
40: garbage.setPreferredSize(new Dimension(20, 20));
41: garbage.setBorderPainted(false);
42: garbage.addActionListener(new ActionListener() {
43: public void actionPerformed(ActionEvent e) {
44: System.gc();
45: }
46: });
47: MemoryUsage memory = new MemoryUsage();
48: add(memory, BorderLayout.CENTER);
49: add(garbage, BorderLayout.EAST);
50: }
51:
52: class MemoryUsage extends JComponent {
53: public MemoryUsage() {
54: java.util.Timer updateTimer = new java.util.Timer(true); // generate
55: // a
56: // timer
57: // using
58: // a
59: // daemon
60: // thread
61: updateTimer.scheduleAtFixedRate(new java.util.TimerTask() {
62: public void run() {
63: if (!SwingUtilities.isEventDispatchThread()) {
64: SwingUtilities.invokeLater(new Runnable() {
65: public void run() {
66: MemoryUsage.this .repaint();
67: }
68: });
69: }
70: }
71: }, 2500, 2500);
72: }
73:
74: public void paint(Graphics g) {
75: float max = Runtime.getRuntime().maxMemory();
76: float total = Runtime.getRuntime().totalMemory();
77: float used = total - Runtime.getRuntime().freeMemory();
78: int usedMemory = (int) ((used / max) * getWidth());
79: int allocatedMemory = (int) ((total / max) * getWidth());
80: Graphics2D g2d = (Graphics2D) g;
81: g2d.setColor(Color.white);
82: g2d.fillRect(0, 0, getWidth(), getHeight());
83: g2d.setColor(Color.yellow);
84: g2d.fillRect(0, 0, allocatedMemory, getHeight());
85: g2d.setColor(ContelligentConstants.CI_COLOR);
86: g2d.fillRect(0, 0, usedMemory, getHeight());
87: g2d.setColor(Color.darkGray);
88: g2d.drawRect(0, 0, getWidth(), getHeight());
89: g2d.drawRect(0, 0, usedMemory, getHeight());
90: g2d.drawRect(0, 0, allocatedMemory, getHeight());
91: g2d.setColor(Color.black);
92: g2d.drawString((int) (used / (1024 * 1024)) + "M used, "
93: + (int) (total / (1024 * 1024)) + "M allocated, "
94: + (int) (max / (1024 * 1024)) + "M available", 5,
95: 15);
96: }
97: }
98: }
|