001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package examples.advanced;
043:
044: import java.awt.Dimension;
045: import java.awt.Toolkit;
046: import java.awt.event.ActionListener;
047: import java.awt.event.ActionEvent;
048:
049: import java.text.MessageFormat;
050: import java.util.ResourceBundle;
051:
052: import javax.swing.Timer;
053:
054: /** Frame to display amount of free memory in the running application.
055: * <P>
056: * Handy for use with the IDE's internal execution. Then the statistic
057: * of free memory in the whole environment is displayed.
058: */
059: public class MemoryView extends javax.swing.JFrame {
060: /** bundle to use */
061: private static ResourceBundle bundle = ResourceBundle
062: .getBundle("examples.advanced.MemoryViewLocale");
063: /** message of free memory */
064: private static MessageFormat msgMemory = new MessageFormat(bundle
065: .getString("TXT_STATUS"));
066:
067: /** default update time */
068: private static final int UPDATE_TIME = 1000;
069: /** timer to invoke updating */
070: private Timer timer;
071:
072: /** Initializes the Form */
073: public MemoryView() {
074: initComponents();
075:
076: setTitle(bundle.getString("TXT_TITLE"));
077: doGarbage.setText(bundle.getString("TXT_GARBAGE"));
078: doRefresh.setText(bundle.getString("TXT_REFRESH"));
079: doClose.setText(bundle.getString("TXT_CLOSE"));
080:
081: txtTime.setText(bundle.getString("TXT_TIME"));
082: doTime.setText(bundle.getString("TXT_SET_TIME"));
083: time.setText(String.valueOf(UPDATE_TIME));
084: time.selectAll();
085: time.requestFocus();
086:
087: updateStatus();
088:
089: timer = new Timer(UPDATE_TIME, new ActionListener() {
090: public void actionPerformed(ActionEvent ev) {
091: updateStatus();
092: }
093: });
094: timer.setRepeats(true);
095:
096: pack();
097: }
098:
099: /** Starts the timer.
100: */
101: public void addNotify() {
102: super .addNotify();
103: timer.start();
104: }
105:
106: /** Stops the timer.
107: */
108: public void removeNotify() {
109: super .removeNotify();
110: timer.stop();
111: }
112:
113: /** Updates the status of all components */
114: private void updateStatus() {
115: Runtime r = Runtime.getRuntime();
116: long free = r.freeMemory();
117: long total = r.totalMemory();
118:
119: // when bigger than integer then divide by two
120: while (total > Integer.MAX_VALUE) {
121: total = total >> 1;
122: free = free >> 1;
123: }
124:
125: int taken = (int) (total - free);
126:
127: status.setMaximum((int) total);
128: status.setValue(taken);
129:
130: text.setText(msgMemory.format(new Object[] { new Long(total),
131: new Long(free), new Integer(taken) }));
132: text.invalidate();
133: validate();
134: }
135:
136: /** This method is called from within the constructor to
137: * initialize the form.
138: * WARNING: Do NOT modify this code. The content of this method is
139: * always regenerated by the FormEditor.
140: */
141: private void initComponents() {//GEN-BEGIN:initComponents
142: jPanel1 = new javax.swing.JPanel();
143: text = new javax.swing.JLabel();
144: status = new javax.swing.JProgressBar();
145: jPanel2 = new javax.swing.JPanel();
146: doGarbage = new javax.swing.JButton();
147: doRefresh = new javax.swing.JButton();
148: doClose = new javax.swing.JButton();
149: jPanel3 = new javax.swing.JPanel();
150: txtTime = new javax.swing.JLabel();
151: time = new javax.swing.JTextField();
152: doTime = new javax.swing.JButton();
153:
154: addWindowListener(new java.awt.event.WindowAdapter() {
155: public void windowClosing(java.awt.event.WindowEvent evt) {
156: exitForm(evt);
157: }
158: });
159:
160: jPanel1.setLayout(new java.awt.BorderLayout());
161:
162: jPanel1.add(text, java.awt.BorderLayout.SOUTH);
163:
164: jPanel1.add(status, java.awt.BorderLayout.CENTER);
165:
166: getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
167:
168: doGarbage
169: .addActionListener(new java.awt.event.ActionListener() {
170: public void actionPerformed(
171: java.awt.event.ActionEvent evt) {
172: doGarbageActionPerformed(evt);
173: }
174: });
175:
176: jPanel2.add(doGarbage);
177:
178: doRefresh
179: .addActionListener(new java.awt.event.ActionListener() {
180: public void actionPerformed(
181: java.awt.event.ActionEvent evt) {
182: doRefreshActionPerformed(evt);
183: }
184: });
185:
186: jPanel2.add(doRefresh);
187:
188: doClose.addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(java.awt.event.ActionEvent evt) {
190: doCloseActionPerformed(evt);
191: }
192: });
193:
194: jPanel2.add(doClose);
195:
196: getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
197:
198: jPanel3.setLayout(new java.awt.BorderLayout(0, 20));
199:
200: jPanel3.add(txtTime, java.awt.BorderLayout.WEST);
201:
202: jPanel3.add(time, java.awt.BorderLayout.CENTER);
203:
204: doTime.addActionListener(new java.awt.event.ActionListener() {
205: public void actionPerformed(java.awt.event.ActionEvent evt) {
206: setRefreshTime(evt);
207: }
208: });
209:
210: jPanel3.add(doTime, java.awt.BorderLayout.EAST);
211:
212: getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);
213:
214: }//GEN-END:initComponents
215:
216: /** Exit the Application */
217: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
218: System.exit(0);
219: }//GEN-LAST:event_exitForm
220:
221: private void setRefreshTime(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setRefreshTime
222: try {
223: int rate = Integer.valueOf(time.getText()).intValue();
224: timer.setDelay(rate);
225: } catch (NumberFormatException ex) {
226: time.setText(String.valueOf(timer.getDelay()));
227: }
228: time.selectAll();
229: time.requestFocus();
230: }//GEN-LAST:event_setRefreshTime
231:
232: private void doCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doCloseActionPerformed
233: exitForm(null);
234: }//GEN-LAST:event_doCloseActionPerformed
235:
236: private void doRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doRefreshActionPerformed
237: updateStatus();
238: }//GEN-LAST:event_doRefreshActionPerformed
239:
240: private void doGarbageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doGarbageActionPerformed
241: System.gc();
242: updateStatus();
243: }//GEN-LAST:event_doGarbageActionPerformed
244:
245: // Variables declaration - do not modify//GEN-BEGIN:variables
246: private javax.swing.JProgressBar status;
247: private javax.swing.JTextField time;
248: private javax.swing.JLabel txtTime;
249: private javax.swing.JButton doGarbage;
250: private javax.swing.JButton doClose;
251: private javax.swing.JButton doTime;
252: private javax.swing.JPanel jPanel3;
253: private javax.swing.JPanel jPanel2;
254: private javax.swing.JPanel jPanel1;
255: private javax.swing.JButton doRefresh;
256: private javax.swing.JLabel text;
257:
258: // End of variables declaration//GEN-END:variables
259:
260: /** Opens memory view window in middle of screen
261: */
262: public static void main(java.lang.String[] args) {
263: MemoryView mv = new MemoryView();
264: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
265: Dimension m = mv.getSize();
266: d.width -= m.width;
267: d.height -= m.height;
268: d.width /= 2;
269: d.height /= 2;
270: mv.setLocation(d.width, d.height);
271: mv.setVisible(true);
272: }
273:
274: }
|