source: trunk/src/org/expeditee/items/widgets/MemoryMonitor.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.8 KB
Line 
1/**
2 * MemoryMonitor.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items.widgets;
20
21import java.awt.Color;
22import java.awt.Font;
23import java.awt.Graphics;
24import java.awt.Graphics2D;
25
26import javax.swing.JPanel;
27import javax.swing.SwingUtilities;
28
29import org.expeditee.items.ItemParentStateChangedEvent;
30import org.expeditee.items.Text;
31
32/**
33 * A widget for displaying the heap space uasage at runtime
34 *
35 * @author Brook Novak
36 *
37 */
38public class MemoryMonitor extends InteractiveWidget {
39
40 private long totalMemoryInBytes;
41 private float currentMemoryUsage;
42 private MonitorThread monitorThread = null;
43
44 private static final Font USAGE_FONT = new Font("Arial", Font.BOLD, 12);
45
46
47 public MemoryMonitor(Text source, String[] args) {
48 super(source, new JPanel(), 40, 40, 40, 40);
49 updateMemoryUsage();
50 }
51
52 @Override
53 protected String[] getArgs() {
54 return null;
55 }
56
57 private void updateMemoryUsage()
58 {
59 totalMemoryInBytes = Runtime.getRuntime().totalMemory();
60 currentMemoryUsage = (totalMemoryInBytes - Runtime.getRuntime().freeMemory()) / (float)totalMemoryInBytes;
61 invalidateSelf();
62 }
63
64 @Override
65 public void paint(Graphics g) {
66
67 int height = getHeight();
68 int width = getWidth();
69
70 int memHeight = (int)(height * currentMemoryUsage);
71
72 Color memColor;
73 if (currentMemoryUsage > 0.8f) {
74 memColor = Color.RED;
75 } else if (currentMemoryUsage > 0.5f) {
76 memColor = Color.ORANGE;
77 } else {
78 memColor = Color.GREEN;
79 }
80
81 g.setColor(memColor);
82 g.fillRect(getX(), getY() + height - memHeight, width, memHeight);
83
84 g.setColor(Color.WHITE);
85 g.fillRect(getX(), getY(), width, height - memHeight);
86
87 int percent = (int)(currentMemoryUsage * 100.0f);
88
89 g.setColor(Color.BLACK);
90 g.setFont(USAGE_FONT);
91 g.drawString(percent + "%", getX() + 12, getY() + (height / 2) + 8);
92
93 paintLink((Graphics2D)g);
94 }
95
96
97 @Override
98 protected void onParentStateChanged(int eventType) {
99 super.onParentStateChanged(eventType);
100
101 switch (eventType) {
102 case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
103 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
104 case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
105 if (monitorThread != null) {
106 monitorThread.destroy();
107 monitorThread = null;
108 }
109 break;
110
111 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
112 case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
113 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
114 case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
115 if (monitorThread == null) {
116 monitorThread = new MonitorThread();
117 monitorThread.start();
118 }
119 break;
120
121 }
122
123 }
124
125 private class MonitorThread extends Thread
126 {
127 private MonitorGUIUpdator guiRunner = new MonitorGUIUpdator();
128
129 public MonitorThread()
130 {
131 setDaemon(true);
132 }
133
134 public void destroy()
135 {
136 interrupt();
137 }
138
139 public void run()
140 {
141 try {
142 while(!isInterrupted())
143 {
144 SwingUtilities.invokeLater(guiRunner);
145 sleep(5000);
146 }
147 } catch (InterruptedException e) {
148 }
149 }
150
151 private class MonitorGUIUpdator implements Runnable
152 {
153 public void run()
154 {
155 updateMemoryUsage();
156 }
157 }
158 }
159}
Note: See TracBrowser for help on using the repository browser.