KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > swing > status > MemoryDisplay


1 package com.opensymphony.workflow.designer.swing.status;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.EmptyBorder JavaDoc;
7
8 import com.opensymphony.workflow.designer.ResourceManager;
9 import com.opensymphony.workflow.designer.swing.plaf.BlueButtonUI;
10
11 /**
12  * @author Hani Suleiman (hani@formicary.net)
13  * Date: Dec 24, 2003
14  * Time: 4:07:15 PM
15  */

16 public class MemoryDisplay extends DisplayItem
17 {
18   private static final Color edgeColor = new Color(82, 115, 214);
19   private static final Color centerColor = new Color(180, 200, 230);
20   private final Icon gcIcon = ResourceManager.getIcon("gc");
21   private MemoryPanel panel;
22   private JButton invokeGC;
23   private int updateInterval = 2000;
24   private Timer timer;
25
26   class MemoryPanel extends JPanel
27   {
28     public void paint(Graphics g)
29     {
30       super.paint(g);
31       Dimension dimension = getSize();
32       Insets insets = getInsets();
33       int left = insets.left;
34       int top = insets.top;
35       Runtime JavaDoc runtime = Runtime.getRuntime();
36       long freeMemory = runtime.freeMemory();
37       long totalMemory = runtime.totalMemory();
38       int insideWidth = dimension.width - (insets.left + insets.right);
39       int usedAmount = insideWidth - (int)(((long)insideWidth * freeMemory) / totalMemory);
40       int insideHeight = dimension.height - (insets.bottom + insets.top);
41       Graphics2D g2 = (Graphics2D)g;
42       g2.setPaint(new GradientPaint(left, top, edgeColor, left, insideHeight / 2, centerColor, true));
43       g.fillRect(left, top, usedAmount, insideHeight);
44       g.setColor(getBackground());
45       g.fillRect(left + usedAmount, top, insideWidth - usedAmount, insideHeight);
46       g.setFont(getFont());
47       g.setColor(Color.black);
48       String JavaDoc memory = (Long.toString((totalMemory - freeMemory) / 1048576L) + "M of " + Long.toString(totalMemory / 1048576L) + 'M');
49       FontMetrics fontmetrics = g.getFontMetrics();
50       int stringWidth = fontmetrics.charsWidth(memory.toCharArray(), 0, memory.length());
51       int stringHeight = fontmetrics.getHeight() - fontmetrics.getDescent();
52       g.drawString(memory, left + (insideWidth - stringWidth) / 2, top + (insideHeight + stringHeight) / 2);
53     }
54   }
55
56   public MemoryDisplay()
57   {
58     super();
59     setLayout(new BorderLayout(5, 0));
60     panel = new MemoryPanel();
61     panel.setOpaque(false);
62     invokeGC = new JButton(gcIcon);
63     invokeGC.setRequestFocusEnabled(false);
64     invokeGC.setFocusable(false);
65     setBorder(new EmptyBorder JavaDoc(1, 1, 1, 1));
66     invokeGC.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
67     invokeGC.setOpaque(false);
68       invokeGC.setUI(new BlueButtonUI());
69     invokeGC.addActionListener(new ActionListener()
70     {
71         public void actionPerformed(ActionEvent e)
72         {
73             System.gc();
74             MemoryDisplay.this.panel.repaint();
75         }
76     });
77     add(panel, BorderLayout.CENTER);
78     add(invokeGC, BorderLayout.WEST);
79     timer = new Timer(updateInterval, new ActionListener()
80     {
81       public void actionPerformed(ActionEvent actionevent)
82       {
83         MemoryDisplay.this.panel.repaint();
84       }
85     });
86     timer.start();
87   }
88
89   public int getPreferredWidth()
90   {
91     return 30 + getFontMetrics(getFont()).stringWidth("0000M of 0000M");
92   }
93
94   public String JavaDoc getItemName()
95   {
96     return "Memory";
97   }
98
99   public void setUpdateInterval(int i)
100   {
101     timer.stop();
102     updateInterval = i;
103     timer.setDelay(i);
104     timer.start();
105   }
106
107   public int getUpdateInterval()
108   {
109     return updateInterval;
110   }
111
112   public void removeNotify()
113   {
114     super.removeNotify();
115     stop();
116     timer = null;
117   }
118
119   public void stop()
120   {
121     timer.stop();
122   }
123
124  }
125
Popular Tags