View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
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 the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  package org.devaki.nextobjects.ui.others;
21  import java.awt.Graphics;
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.util.Vector;
26  import javax.swing.BorderFactory;
27  import javax.swing.JPanel;
28  import org.devaki.nextobjects.util.NOPreferences;
29  import org.devaki.nextobjects.util.NOGraphicsUtil;
30  /***
31   * The JVMInfo shows statistics of JVM memory usage.
32   * @author   eflorent@devaki.org
33   */
34  public class JVMInfo extends JPanel implements Runnable
35  {
36      /*** run */
37      private boolean run = true;
38      /*** java2 runtime */
39      private Runtime runtime = Runtime.getRuntime();
40      /***
41       * Constructor for the JVMInfos object
42       */
43      public JVMInfo()
44      {
45          this.setLayout(new GridBagLayout());
46          this.setBorder(
47              BorderFactory.createTitledBorder("RT - JVM Memory Usage"));
48          this.add(
49              new JVMGraphPanel(),
50              new GridBagConstraints(
51                  0,
52                  0,
53                  1,
54                  1,
55                  1.0,
56                  1.0,
57                  GridBagConstraints.CENTER,
58                  GridBagConstraints.BOTH,
59                  new Insets(5, 5, 0, 0),
60                  0,
61                  0));
62          new Thread(this).start();
63      }
64      /*** Repaints the graph and the labels unitl it's time to stop. */
65      public final void run()
66      {
67          while (run)
68          {
69              repaint();
70              try
71              {
72                  Thread.sleep(1000);
73              }
74              catch (InterruptedException ie)
75              {
76                  ie.printStackTrace();
77              }
78          }
79      }
80      /***
81       * The main  panel.
82       */
83      class JVMGraphPanel extends JPanel
84      {
85          /*** mems */
86          private Vector mems = new Vector();
87          /*** seconds */
88          private int seconds = 20;
89          /*** width */
90          private int width;
91          /*** height */
92          private int height;
93          /*** y offset */
94          private long y;
95          /*** Constructor for the MemoryPanel object. */
96          JVMGraphPanel()
97          {
98          }
99          /***
100          * Paints the panel.
101          *
102          * @param g  the Graphics object to use when painting the panel
103          */
104         public final void paint(final Graphics g)
105         {
106             long totalMemory = runtime.totalMemory() / 1024;
107             long freeMemory = totalMemory - runtime.freeMemory() / 1024;
108             //g.setFont(NOPreferences.NO_LOG_FONT);
109             width = this.getWidth();
110             height = this.getHeight();
111             y = height / 2;
112             mems.add(new Long(freeMemory));
113             if (mems.size() > seconds + 1)
114             {
115                 mems.remove(0);
116             }
117             g.clearRect(0, 0, width, height);
118             g.setColor(
119                 NOGraphicsUtil.hexString2Color(
120                     NOPreferences.getProperty("nextobjects.logger.color")));
121             g.fillRect(0, 0, width, height);
122             g.setColor(
123                 NOGraphicsUtil.hexString2Color(
124                     NOPreferences.getProperty("nextobjects.logger.bgcolor")));
125             long x = 0;
126             long tempX;
127             long tempY;
128             //number of Y grid;
129             int wGrid = 5;
130             //X Axis
131             for (int i = 0; i < mems.size(); i++)
132             {
133                 g.drawLine(width / seconds * i, 0, width / seconds * i, height);
134             }
135             //Y Axis
136             for (int i = 0; i < wGrid; i++)
137             {
138                 g.drawLine(0, height / wGrid * i, width, height / wGrid * i);
139             }
140             for (int i = 0; i < Math.min(mems.size(), seconds + 1); i++)
141             {
142                 tempX = width / seconds * i;
143                 tempY =
144                     (totalMemory - ((Long) mems.get(i)).longValue())
145                         * height
146                         / totalMemory;
147                 if (i != 0)
148                 {
149                     g.drawLine((int) x, (int) y, (int) tempX, (int) tempY);
150                 }
151                 x = tempX;
152                 y = tempY;
153             }
154             g.setColor(
155                 NOGraphicsUtil.hexString2Color(
156                     NOPreferences.getProperty("nextobjects.logger.color")));
157             g.fillRect(0, 0, width, 10);
158             g.setColor(
159                 NOGraphicsUtil.hexString2Color(
160                     NOPreferences.getProperty("nextobjects.logger.bgcolor")));
161             int tab = width / 3;
162             g.drawString(
163                 new StringBuffer()
164                     .append("Free Mem : ")
165                     .append(freeMemory)
166                     .append(" kb")
167                     .toString(),
168                 2,
169                 10);
170             g.drawString(
171                 new StringBuffer()
172                     .append("Total Mem. : ")
173                     .append(String.valueOf(totalMemory))
174                     .append(" kb")
175                     .toString(),
176                 1 * tab,
177                 10);
178             g.drawString(
179                 new StringBuffer()
180                     .append("Thread # : ")
181                     .append(Thread.activeCount())
182                     .toString(),
183                 2 * tab,
184                 10);
185         }
186     }
187 }