KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > advanced > MemoryView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package examples.advanced;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import javax.swing.Timer JavaDoc;
31
32 /** Frame to display amount of free memory in the running application.
33 * <P>
34 * Handy for use with the IDE's internal execution. Then the statistic
35 * of free memory in the whole environment is displayed.
36 */

37 public class MemoryView extends javax.swing.JFrame JavaDoc {
38     /** bundle to use */
39     private static ResourceBundle JavaDoc bundle = ResourceBundle.getBundle ("examples.advanced.MemoryViewLocale");
40     /** message of free memory */
41     private static MessageFormat JavaDoc msgMemory = new MessageFormat JavaDoc (bundle.getString ("TXT_STATUS"));
42
43     /** default update time */
44     private static final int UPDATE_TIME = 1000;
45     /** timer to invoke updating */
46     private Timer JavaDoc timer;
47
48
49     /** Initializes the Form */
50     public MemoryView() {
51         initComponents ();
52
53         setTitle (bundle.getString ("TXT_TITLE"));
54         doGarbage.setText (bundle.getString ("TXT_GARBAGE"));
55         doRefresh.setText (bundle.getString ("TXT_REFRESH"));
56         doClose.setText (bundle.getString ("TXT_CLOSE"));
57
58         txtTime.setText (bundle.getString ("TXT_TIME"));
59         doTime.setText (bundle.getString ("TXT_SET_TIME"));
60         time.setText (String.valueOf (UPDATE_TIME));
61         time.selectAll ();
62         time.requestFocus ();
63
64         updateStatus ();
65
66         timer = new Timer JavaDoc (UPDATE_TIME, new ActionListener JavaDoc () {
67                                public void actionPerformed (ActionEvent JavaDoc ev) {
68                                    updateStatus ();
69                                }
70                            });
71         timer.setRepeats (true);
72
73         pack ();
74     }
75
76     /** Starts the timer.
77     */

78     public void addNotify () {
79         super.addNotify ();
80         timer.start ();
81     }
82
83     /** Stops the timer.
84     */

85     public void removeNotify () {
86         super.removeNotify ();
87         timer.stop ();
88     }
89
90     /** Updates the status of all components */
91     private void updateStatus () {
92         Runtime JavaDoc r = Runtime.getRuntime ();
93         long free = r.freeMemory ();
94         long total = r.totalMemory ();
95
96         // when bigger than integer then divide by two
97
while (total > Integer.MAX_VALUE) {
98             total = total >> 1;
99             free = free >> 1;
100         }
101
102         int taken = (int) (total - free);
103
104         status.setMaximum ((int)total);
105         status.setValue (taken);
106
107         text.setText (msgMemory.format (new Object JavaDoc[] {
108                                             new Long JavaDoc (total),
109                                             new Long JavaDoc (free),
110                                             new Integer JavaDoc (taken)
111                                         }));
112         text.invalidate ();
113         validate ();
114     }
115
116     /** This method is called from within the constructor to
117      * initialize the form.
118      * WARNING: Do NOT modify this code. The content of this method is
119      * always regenerated by the FormEditor.
120      */

121     private void initComponents() {//GEN-BEGIN:initComponents
122
jPanel1 = new javax.swing.JPanel JavaDoc();
123         text = new javax.swing.JLabel JavaDoc();
124         status = new javax.swing.JProgressBar JavaDoc();
125         jPanel2 = new javax.swing.JPanel JavaDoc();
126         doGarbage = new javax.swing.JButton JavaDoc();
127         doRefresh = new javax.swing.JButton JavaDoc();
128         doClose = new javax.swing.JButton JavaDoc();
129         jPanel3 = new javax.swing.JPanel JavaDoc();
130         txtTime = new javax.swing.JLabel JavaDoc();
131         time = new javax.swing.JTextField JavaDoc();
132         doTime = new javax.swing.JButton JavaDoc();
133
134         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
135             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
136                 exitForm(evt);
137             }
138         });
139
140         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
141
142         jPanel1.add(text, java.awt.BorderLayout.SOUTH);
143
144         jPanel1.add(status, java.awt.BorderLayout.CENTER);
145
146         getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
147
148         doGarbage.addActionListener(new java.awt.event.ActionListener JavaDoc() {
149             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
150                 doGarbageActionPerformed(evt);
151             }
152         });
153
154         jPanel2.add(doGarbage);
155
156         doRefresh.addActionListener(new java.awt.event.ActionListener JavaDoc() {
157             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
158                 doRefreshActionPerformed(evt);
159             }
160         });
161
162         jPanel2.add(doRefresh);
163
164         doClose.addActionListener(new java.awt.event.ActionListener JavaDoc() {
165             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
166                 doCloseActionPerformed(evt);
167             }
168         });
169
170         jPanel2.add(doClose);
171
172         getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
173
174         jPanel3.setLayout(new java.awt.BorderLayout JavaDoc(0, 20));
175
176         jPanel3.add(txtTime, java.awt.BorderLayout.WEST);
177
178         jPanel3.add(time, java.awt.BorderLayout.CENTER);
179
180         doTime.addActionListener(new java.awt.event.ActionListener JavaDoc() {
181             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
182                 setRefreshTime(evt);
183             }
184         });
185
186         jPanel3.add(doTime, java.awt.BorderLayout.EAST);
187
188         getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);
189
190     }//GEN-END:initComponents
191

192     /** Exit the Application */
193     private void exitForm (java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
194
System.exit( 0 );
195     }//GEN-LAST:event_exitForm
196

197     private void setRefreshTime (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_setRefreshTime
198
try {
199             int rate = Integer.valueOf (time.getText ()).intValue ();
200             timer.setDelay (rate);
201         } catch (NumberFormatException JavaDoc ex) {
202             time.setText (String.valueOf (timer.getDelay ()));
203         }
204         time.selectAll ();
205         time.requestFocus ();
206     }//GEN-LAST:event_setRefreshTime
207

208
209     private void doCloseActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_doCloseActionPerformed
210
exitForm (null);
211     }//GEN-LAST:event_doCloseActionPerformed
212

213
214     private void doRefreshActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_doRefreshActionPerformed
215
updateStatus ();
216     }//GEN-LAST:event_doRefreshActionPerformed
217

218     private void doGarbageActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_doGarbageActionPerformed
219
System.gc ();
220         updateStatus ();
221     }//GEN-LAST:event_doGarbageActionPerformed
222

223
224     // Variables declaration - do not modify//GEN-BEGIN:variables
225
private javax.swing.JProgressBar JavaDoc status;
226     private javax.swing.JTextField JavaDoc time;
227     private javax.swing.JLabel JavaDoc txtTime;
228     private javax.swing.JButton JavaDoc doGarbage;
229     private javax.swing.JButton JavaDoc doClose;
230     private javax.swing.JButton JavaDoc doTime;
231     private javax.swing.JPanel JavaDoc jPanel3;
232     private javax.swing.JPanel JavaDoc jPanel2;
233     private javax.swing.JPanel JavaDoc jPanel1;
234     private javax.swing.JButton JavaDoc doRefresh;
235     private javax.swing.JLabel JavaDoc text;
236     // End of variables declaration//GEN-END:variables
237

238
239     /** Opens memory view window in middle of screen
240     */

241     public static void main(java.lang.String JavaDoc[] args) {
242         MemoryView mv = new MemoryView ();
243         Dimension JavaDoc d = Toolkit.getDefaultToolkit ().getScreenSize ();
244         Dimension JavaDoc m = mv.getSize ();
245         d.width -= m.width;
246         d.height -= m.height;
247         d.width /= 2;
248         d.height /= 2;
249         mv.setLocation (d.width, d.height);
250         mv.setVisible (true);
251     }
252
253 }
254
Popular Tags