1 | package org.expeditee.items.widgets;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 |
|
---|
8 | import javax.swing.JPanel;
|
---|
9 | import javax.swing.SwingUtilities;
|
---|
10 |
|
---|
11 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
12 | import org.expeditee.items.Text;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A widget for displaying the heap space uasage at runtime
|
---|
16 | *
|
---|
17 | * @author Brook Novak
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public class MemoryMonitor extends InteractiveWidget {
|
---|
21 |
|
---|
22 | private long totalMemoryInBytes;
|
---|
23 | private float currentMemoryUsage;
|
---|
24 | private MonitorThread monitorThread = null;
|
---|
25 |
|
---|
26 | private static final Font USAGE_FONT = new Font("Arial", Font.BOLD, 12);
|
---|
27 |
|
---|
28 |
|
---|
29 | public MemoryMonitor(Text source, String[] args) {
|
---|
30 | super(source, new JPanel(), 40, 40, 40, 40);
|
---|
31 | updateMemoryUsage();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | protected String[] getArgs() {
|
---|
36 | return null;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private void updateMemoryUsage()
|
---|
40 | {
|
---|
41 | totalMemoryInBytes = Runtime.getRuntime().totalMemory();
|
---|
42 | currentMemoryUsage = (totalMemoryInBytes - Runtime.getRuntime().freeMemory()) / (float)totalMemoryInBytes;
|
---|
43 | invalidateSelf();
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void paint(Graphics g) {
|
---|
48 |
|
---|
49 | int height = getHeight();
|
---|
50 | int width = getWidth();
|
---|
51 |
|
---|
52 | int memHeight = (int)(height * currentMemoryUsage);
|
---|
53 |
|
---|
54 | Color memColor;
|
---|
55 | if (currentMemoryUsage > 0.8f) {
|
---|
56 | memColor = Color.RED;
|
---|
57 | } else if (currentMemoryUsage > 0.5f) {
|
---|
58 | memColor = Color.ORANGE;
|
---|
59 | } else {
|
---|
60 | memColor = Color.GREEN;
|
---|
61 | }
|
---|
62 |
|
---|
63 | g.setColor(memColor);
|
---|
64 | g.fillRect(getX(), getY() + height - memHeight, width, memHeight);
|
---|
65 |
|
---|
66 | g.setColor(Color.WHITE);
|
---|
67 | g.fillRect(getX(), getY(), width, height - memHeight);
|
---|
68 |
|
---|
69 | int percent = (int)(currentMemoryUsage * 100.0f);
|
---|
70 |
|
---|
71 | g.setColor(Color.BLACK);
|
---|
72 | g.setFont(USAGE_FONT);
|
---|
73 | g.drawString(percent + "%", getX() + 12, getY() + (height / 2) + 8);
|
---|
74 |
|
---|
75 | paintLink((Graphics2D)g);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void onParentStateChanged(int eventType) {
|
---|
81 | super.onParentStateChanged(eventType);
|
---|
82 |
|
---|
83 | switch (eventType) {
|
---|
84 | case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
|
---|
85 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
|
---|
86 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
|
---|
87 | if (monitorThread != null) {
|
---|
88 | monitorThread.destroy();
|
---|
89 | monitorThread = null;
|
---|
90 | }
|
---|
91 | break;
|
---|
92 |
|
---|
93 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
|
---|
94 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
|
---|
95 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
|
---|
96 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
|
---|
97 | if (monitorThread == null) {
|
---|
98 | monitorThread = new MonitorThread();
|
---|
99 | monitorThread.start();
|
---|
100 | }
|
---|
101 | break;
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | private class MonitorThread extends Thread
|
---|
108 | {
|
---|
109 | private MonitorGUIUpdator guiRunner = new MonitorGUIUpdator();
|
---|
110 |
|
---|
111 | public MonitorThread()
|
---|
112 | {
|
---|
113 | setDaemon(true);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void destroy()
|
---|
117 | {
|
---|
118 | interrupt();
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void run()
|
---|
122 | {
|
---|
123 | try {
|
---|
124 | while(!isInterrupted())
|
---|
125 | {
|
---|
126 | SwingUtilities.invokeLater(guiRunner);
|
---|
127 | sleep(5000);
|
---|
128 | }
|
---|
129 | } catch (InterruptedException e) {
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private class MonitorGUIUpdator implements Runnable
|
---|
134 | {
|
---|
135 | public void run()
|
---|
136 | {
|
---|
137 | updateMemoryUsage();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|