View Javadoc

1   package com.eviware.soapui.support.monitor;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.geom.Line2D;
6   import java.awt.geom.Rectangle2D;
7   import java.awt.image.BufferedImage;
8   
9   /***
10   * @author Angelo De Caro
11   */
12  public class MonitorPanel extends JPanel {
13      public Surface surf;
14  
15      public MonitorPanel(MonitorSource monitorSource) {
16          setLayout(new BorderLayout());
17          add(surf = createSurface());
18          setMonitorSource(monitorSource);
19      }
20  
21      public void start() {
22          surf.start();
23      }
24  
25      public void stop() {
26          surf.stop();
27      }
28  
29      public MonitorSource getMonitorSource() {
30          return surf.getMonitorSource();
31      }
32  
33      public void setMonitorSource(MonitorSource monitorSource) {
34          surf.setMonitorSource(monitorSource);
35      }
36  
37      protected Surface createSurface() {
38          return new Surface(null);
39      }
40  
41      protected class Surface extends JPanel implements Runnable {
42          private Thread thread;
43          private MonitorSource monitorSource;
44  
45          private long sleepAmount = 1000;
46  
47          private int w, h;
48          private BufferedImage backImage;
49          private Graphics2D backImageGrfx;
50  
51          private Font font = new Font("Times New Roman", Font.PLAIN, 11);
52          private int columnInc;
53  
54          private double[] points;
55          private int validPoints;
56  
57          private int ascent, descent;
58  
59          private Rectangle2D mfRect = new Rectangle2D.Float();
60          private Rectangle2D muRect = new Rectangle2D.Float();
61          private Line2D graphLine = new Line2D.Float();
62          private Color graphColor = new Color(46, 139, 87);
63          private Color mfColor = new Color(0, 100, 0);
64  
65          public Surface(MonitorSource monitorSource) {
66              this.monitorSource = monitorSource;
67              setBackground(Color.black);
68          }
69  
70          public void paint(Graphics g) {
71              Dimension d = getSize();
72  
73              if (d.width != w || d.height != h) {
74                  w = d.width;
75                  h = d.height;
76  
77                  backImage = (BufferedImage) createImage(w, h);
78                  backImageGrfx = backImage.createGraphics();
79                  backImageGrfx.setFont(font);
80  
81                  FontMetrics fm = backImageGrfx.getFontMetrics(font);
82                  ascent = fm.getAscent();
83                  descent = fm.getDescent();
84              }
85  
86              backImageGrfx.setBackground(Color.DARK_GRAY);
87              backImageGrfx.clearRect(0, 0, w, h);
88  
89              float totalMemory = monitorSource.getTotal();
90              float usedMemory = monitorSource.getUsed();
91              float freeMemory = totalMemory - usedMemory;
92  
93              // Draw allocated and used strings
94              backImageGrfx.setColor(Color.green);
95              backImageGrfx.drawString(String.valueOf((int) totalMemory >> 10) + "K allocated", 4.0f, (float) ascent + 0.5f);
96              String usedStr = String.valueOf(((int) usedMemory) >> 10) + "K used";
97              backImageGrfx.drawString(usedStr, 4, h - descent);
98  
99              // Calculate remaining size
100             float ssH = ascent + descent;
101             float remainingHeight = (h - ssH * 2 - 0.5f);
102             float blockHeight = remainingHeight / 10;
103             float blockWidth = 20.0f;
104 
105             // Memory Free
106             backImageGrfx.setColor(mfColor);
107             int MemUsage = (int) (freeMemory / totalMemory * 10);
108             int i = 0;
109             for (; i < MemUsage; i++) {
110                 mfRect.setRect(5, ssH + i * blockHeight, blockWidth, blockHeight - 1);
111                 backImageGrfx.fill(mfRect);
112             }
113 
114             // Memory Used
115             backImageGrfx.setColor(Color.green);
116             for (; i < 10; i++) {
117                 muRect.setRect(5, ssH + i * blockHeight, blockWidth, blockHeight - 1);
118                 backImageGrfx.fill(muRect);
119             }
120 
121             // Draw History Graph
122             backImageGrfx.setColor(graphColor);
123             int graphX = 30;
124             int graphY = (int) ssH;
125             int graphW = w - graphX - 5;
126             int graphH = (int) (ssH + (9 * blockHeight) + blockHeight - 1);
127 
128             i = 0;
129             for (; i < 10; i++) {
130                 muRect.setRect(graphX, ssH + i * blockHeight - 1, graphW, blockHeight);
131                 backImageGrfx.draw(muRect);
132             }
133 
134             // Draw animated column movement
135             int graphColumn = graphW / 15;
136 
137             if (columnInc == 0) {
138                 columnInc = graphColumn;
139             }
140 
141             for (int j = graphX + columnInc; j < graphW + graphX; j += graphColumn) {
142                 graphLine.setLine(j, graphY, j, ssH + i * blockHeight - 1);
143                 backImageGrfx.draw(graphLine);
144             }
145 
146             --columnInc;
147 
148             if (points == null) {
149                 points = new double[graphW];
150                 validPoints = 0;
151             } else if (points.length != graphW) {
152                 double[] tmp;
153                 if (validPoints < graphW) {
154                     tmp = new double[validPoints];
155                     System.arraycopy(points, 0, tmp, 0, tmp.length);
156                 } else {
157                     tmp = new double[graphW];
158                     System.arraycopy(points, points.length - tmp.length, tmp, 0, tmp.length);
159                     validPoints = tmp.length - 2;
160                 }
161                 points = new double[graphW];
162                 System.arraycopy(tmp, 0, points, 0, tmp.length);
163             } else {
164                 backImageGrfx.setColor(Color.yellow);
165                 int x = w - 5;
166                 int sum = graphH - (ascent + descent);
167 
168                 for (int j = x - validPoints, k = 0; k < validPoints; k++, j++) {
169                     if (k != 0) {
170                         if (points[k] != points[k - 1]) {
171                             backImageGrfx.drawLine(j - 1, graphY + (int) (sum * points[k - 1]), j, graphY + (int) (sum * points[k]));
172                         } else {
173                             backImageGrfx.fillRect(j, graphY + (int) (sum * points[k]), 1, 1);
174                         }
175                     }
176                 }
177             }
178 
179             g.drawImage(backImage, 0, 0, this);
180         }
181 
182         public void run() {
183             while (thread != null) {
184                 float totalMemory = monitorSource.getTotal();
185                 float usedMemory = monitorSource.getUsed();
186                 float freeMemory = totalMemory - usedMemory;
187 
188                 if (points == null) {
189                     points = new double[1];
190                     validPoints = 0;
191                 } else if (points.length < validPoints + 1) {
192                     double[] tmp;
193 
194                     int graphW = validPoints +1;
195                     if (validPoints < graphW) {
196                         tmp = new double[validPoints];
197                         System.arraycopy(points, 0, tmp, 0, tmp.length);
198                     } else {
199                         tmp = new double[graphW];
200                         System.arraycopy(points, points.length - tmp.length, tmp, 0, tmp.length);
201                         validPoints = tmp.length - 2;
202                     }
203                     points = new double[graphW];
204                     System.arraycopy(tmp, 0, points, 0, tmp.length);
205                 } else {
206                     points[validPoints] = (freeMemory / totalMemory);
207                     if (validPoints + 2 == points.length) {
208                         // throw out oldest point
209                         System.arraycopy(points, 1, points, 0, validPoints);
210                         --validPoints;
211                     } else {
212                         validPoints++;
213                     }
214                 }
215 
216                 repaint();
217 
218                 try {
219                     Thread.sleep(sleepAmount);
220                 } catch (InterruptedException e) {
221                     break;
222                 }
223             }
224         }
225 
226         public void start() {
227             if (monitorSource == null)
228                 throw new IllegalStateException("Monitor Source cannot be null.");
229 
230             thread = new Thread(this);
231             thread.setPriority(Thread.MIN_PRIORITY);
232             thread.setDaemon(true);
233             thread.setName("MemoryMonitor");
234             thread.start();
235         }
236 
237         public synchronized void stop() {
238             thread = null;
239             notify();
240         }
241 
242         public MonitorSource getMonitorSource() {
243             return monitorSource;
244         }
245 
246         public void setMonitorSource(MonitorSource monitorSource) {
247             this.monitorSource = monitorSource;
248         }
249 
250         public Dimension getMinimumSize() {
251             return getPreferredSize();
252         }
253 
254         public Dimension getMaximumSize() {
255             return getPreferredSize();
256         }
257 
258         public Dimension getPreferredSize() {
259             return new Dimension(135, 80);
260         }
261     }
262 }