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

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 4.0 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.Font;
22import java.awt.Graphics;
23import java.awt.Graphics2D;
24
25import javax.swing.JPanel;
26import javax.swing.SwingUtilities;
27
28import org.expeditee.core.Colour;
29import org.expeditee.gio.swing.SwingConversions;
30import org.expeditee.gio.swing.SwingMiscManager;
31import org.expeditee.items.ItemParentStateChangedEvent;
32import org.expeditee.items.Text;
33
34/**
35 * A widget for displaying the heap space uasage at runtime
36 *
37 * @author Brook Novak
38 *
39 */
40public class MemoryMonitor extends SwingWidget {
41
42 private long totalMemoryInBytes;
43 private float currentMemoryUsage;
44 private MonitorThread monitorThread = null;
45
46 private static final Font USAGE_FONT = new Font("Arial", Font.BOLD, 12);
47
48
49 public MemoryMonitor(Text source, String[] args) {
50 super(source, new JPanel(), 40, 40, 40, 40);
51 updateMemoryUsage();
52 }
53
54 @Override
55 protected String[] getArgs() {
56 return null;
57 }
58
59 private void updateMemoryUsage()
60 {
61 totalMemoryInBytes = Runtime.getRuntime().totalMemory();
62 currentMemoryUsage = (totalMemoryInBytes - Runtime.getRuntime().freeMemory()) / (float)totalMemoryInBytes;
63 invalidateSelf();
64 }
65
66 @Override
67 public void paintSwingWidget(Graphics2D g)
68 {
69 int height = getHeight();
70 int width = getWidth();
71
72 int memHeight = (int)(height * currentMemoryUsage);
73
74 Colour memColor;
75 if (currentMemoryUsage > 0.8f) {
76 memColor = Colour.RED;
77 } else if (currentMemoryUsage > 0.5f) {
78 memColor = Colour.ORANGE;
79 } else {
80 memColor = Colour.GREEN;
81 }
82
83 g.setColor(SwingConversions.toSwingColor(memColor));
84 g.fillRect(getX(), getY() + height - memHeight, width, memHeight);
85
86 g.setColor(SwingConversions.toSwingColor(Colour.WHITE));
87 g.fillRect(getX(), getY(), width, height - memHeight);
88
89 int percent = (int)(currentMemoryUsage * 100.0f);
90
91 g.setColor(SwingConversions.toSwingColor(Colour.BLACK));
92 g.setFont(USAGE_FONT);
93 g.drawString(percent + "%", getX() + 12, getY() + (height / 2) + 8);
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.