Index: src/org/openide/actions/GarbageCollectAction.java =================================================================== RCS file: /cvs/openide/src/org/openide/actions/GarbageCollectAction.java,v --- src/org/openide/actions/GarbageCollectAction.java 15 Oct 2003 20:26:23 -0000 1.18 +++ src/org/openide/actions/GarbageCollectAction.java 4 Dec 2003 12:11:54 -0000 @@ -1,154 +1,60 @@ /* * Sun Public License Notice - * + * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ - * + * * The Original Code is NetBeans. The Initial Developer of the Original - * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.actions; -import java.awt.Component; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.text.Format; -import java.text.MessageFormat; -import javax.swing.JButton; -import javax.swing.Timer; -import javax.swing.UIManager; -import javax.swing.border.EmptyBorder; import org.openide.util.HelpCtx; import org.openide.util.NbBundle; -import org.openide.util.RequestProcessor; import org.openide.util.actions.CallableSystemAction; -// Toolbar presenter like MemoryMeterAction, except: -// 1. Does not have a mark etc. -// 2. But pressing it runs GC. -// 3. Slim profile fits nicely in the menu bar (at top level). -// 4. Displays textual memory usage directly, not via tooltip. -// Intended to be unobtrusive enough to leave on for daily use. - -/** - * Perform a system garbage collection. - * @author Jesse Glick - */ -public class GarbageCollectAction extends CallableSystemAction { - - public String getName() { - return NbBundle.getBundle(GarbageCollectAction.class).getString("CTL_GarbageCollect"); // NOI18N - } - - public HelpCtx getHelpCtx() { - return new HelpCtx(GarbageCollectAction.class); - } - - public void performAction() { - gc(); - } - - private static void gc() { - // Can be slow, would prefer not to block on it. - RequestProcessor.getDefault().post(new Runnable() { - public void run() { - System.gc(); - System.runFinalization(); - System.gc(); - } - }); - } - - protected boolean asynchronous() { - return false; - } - - protected String iconResource() { - return "org/openide/resources/actions/garbageCollect.gif"; // NOI18N - } - - public Component getToolbarPresenter() { - return new MemButton(); - } - - /* - public static void main(String[] x) { - javax.swing.JFrame f = new javax.swing.JFrame(); - f.getContentPane().add(new javax.swing.JTextField("Hello world")); - javax.swing.JMenuBar b = new javax.swing.JMenuBar(); - b.add(new javax.swing.JMenu("Test")); - b.add(new MemButton()); - f.setJMenuBar(b); - f.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - f.pack(); - f.show(); - } - */ - - private static final class MemButton extends JButton implements ActionListener { - - private static final int TICK = 1000; - - private final Runtime r = Runtime.getRuntime(); - private final Format f = new MessageFormat("{0,number,0.0}/{1,number,0.0}Mb"); // NOI18N - private final Timer t; - private double proportion = 0.0d; - - public MemButton() { - t = new Timer(TICK, this); - addActionListener(this); - // To get the size right: - setText(f.format(new Object[] {new Double(999.0d), new Double(999.0d)})); - setOpaque(false); - setBorder(new EmptyBorder(1, 4, 1, 3)); - setPreferredSize(getPreferredSize()); - } - - public void addNotify() { - super.addNotify(); - t.start(); - update(); - } - - public void removeNotify() { - t.stop(); - super.removeNotify(); - } - - private void update() { - long total = r.totalMemory(); - long used = total - r.freeMemory(); - proportion = ((double)used) / total; - Double _total = new Double(((double)total) / 1024 / 1024); - Double _used = new Double(((double)used) / 1024 / 1024); - String text = f.format(new Object[] {_used, _total}); - setText(text); - } - - protected void paintComponent(Graphics g) { - Dimension size = getSize(); - g = g.create(); - g.clearRect(0, 0, size.width, size.height); - g.setColor(UIManager.getDefaults().getColor("Button.focus")); // NOI18N - g.fillRect(0, 0, (int)(size.width * proportion), size.height); - super.paintComponent(g); - } - - public void actionPerformed(ActionEvent e) { - if (e.getSource() == this) { - gc(); - } else { - // Timer - update(); - } - } - - } - +/** Perform a system garbage collection. +* +* @author Ian Formanek +*/ +public class GarbageCollectAction extends CallableSystemAction +{ + /** generated Serialized Version UID */ + static final long serialVersionUID = -3365766607488094613L; + + /* Human presentable name of the action. This should be + * presented as an item in a menu. + * @return the name of the action + */ + public String getName() + { + return NbBundle.getBundle(GarbageCollectAction.class).getString("CTL_GarbageCollect"); // NOI18N + } + + /* Help context where to find more about the action. + * @return the help context for this action + */ + public HelpCtx getHelpCtx() + { + return new HelpCtx (GarbageCollectAction.class); + } + + public void performAction() + { + System.gc(); + System.runFinalization(); + System.gc(); + } + + /* URL to this action. + * @return URL to the action icon + */ + protected String iconResource () + { + return "org/openide/resources/actions/garbageCollect.gif"; // NOI18N + } }