This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 37256
Collapse All | Expand All

(-)src/org/openide/actions/GarbageCollectAction.java (-137 / +43 lines)
Lines 1-154 Link Here
1
/*
1
/*
2
 *                 Sun Public License Notice
2
 *                 Sun Public License Notice
3
 *
3
 * 
4
 * The contents of this file are subject to the Sun Public License
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
7
 * http://www.sun.com/
8
 *
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
11
 * Microsystems, Inc. All Rights Reserved.
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
12
 */
13
13
14
package org.openide.actions;
14
package org.openide.actions;
15
15
16
import java.awt.Component;
17
import java.awt.Dimension;
18
import java.awt.Graphics;
19
import java.awt.event.ActionEvent;
20
import java.awt.event.ActionListener;
21
import java.text.Format;
22
import java.text.MessageFormat;
23
import javax.swing.JButton;
24
import javax.swing.Timer;
25
import javax.swing.UIManager;
26
import javax.swing.border.EmptyBorder;
27
import org.openide.util.HelpCtx;
16
import org.openide.util.HelpCtx;
28
import org.openide.util.NbBundle;
17
import org.openide.util.NbBundle;
29
import org.openide.util.RequestProcessor;
30
import org.openide.util.actions.CallableSystemAction;
18
import org.openide.util.actions.CallableSystemAction;
31
19
32
// Toolbar presenter like MemoryMeterAction, except:
20
/** Perform a system garbage collection.
33
// 1. Does not have a mark etc.
21
*
34
// 2. But pressing it runs GC.
22
* @author   Ian Formanek
35
// 3. Slim profile fits nicely in the menu bar (at top level).
23
*/
36
// 4. Displays textual memory usage directly, not via tooltip.
24
public class GarbageCollectAction extends CallableSystemAction 
37
// Intended to be unobtrusive enough to leave on for daily use.
25
{
38
26
	/** generated Serialized Version UID */
39
/**
27
	static final long serialVersionUID = -3365766607488094613L;
40
 * Perform a system garbage collection.
28
41
 * @author Jesse Glick
29
	/* Human presentable name of the action. This should be
42
 */
30
	 * presented as an item in a menu.
43
public class GarbageCollectAction extends CallableSystemAction {
31
	 * @return the name of the action
44
    
32
	 */
45
    public String getName() {
33
	public String getName() 
46
        return NbBundle.getBundle(GarbageCollectAction.class).getString("CTL_GarbageCollect"); // NOI18N
34
	{
47
    }
35
		return NbBundle.getBundle(GarbageCollectAction.class).getString("CTL_GarbageCollect"); // NOI18N
48
    
36
	}
49
    public HelpCtx getHelpCtx() {
37
50
        return new HelpCtx(GarbageCollectAction.class);
38
	/* Help context where to find more about the action.
51
    }
39
	 * @return the help context for this action
52
    
40
	 */
53
    public void performAction() {
41
	public HelpCtx getHelpCtx() 
54
        gc();
42
	{
55
    }
43
		return new HelpCtx (GarbageCollectAction.class);
56
    
44
	}
57
    private static void gc() {
45
58
        // Can be slow, would prefer not to block on it.
46
	public void performAction() 
59
        RequestProcessor.getDefault().post(new Runnable() {
47
	{
60
            public void run() {
48
		System.gc();
61
                System.gc();
49
		System.runFinalization();
62
                System.runFinalization();
50
		System.gc();
63
                System.gc();
51
	}
64
            }
52
    
65
        });
53
	/* URL to this action.
66
    }
54
	 * @return URL to the action icon
67
    
55
	 */
68
    protected boolean asynchronous() {
56
	protected String iconResource () 
69
        return false;
57
	{
70
    }
58
		return "org/openide/resources/actions/garbageCollect.gif"; // NOI18N
71
    
59
	}
72
    protected String iconResource() {
73
        return "org/openide/resources/actions/garbageCollect.gif"; // NOI18N
74
    }
75
    
76
    public Component getToolbarPresenter() {
77
        return new MemButton();
78
    }
79
    
80
    /*
81
    public static void main(String[] x) {
82
        javax.swing.JFrame f = new javax.swing.JFrame();
83
        f.getContentPane().add(new javax.swing.JTextField("Hello world"));
84
        javax.swing.JMenuBar b = new javax.swing.JMenuBar();
85
        b.add(new javax.swing.JMenu("Test"));
86
        b.add(new MemButton());
87
        f.setJMenuBar(b);
88
        f.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
89
        f.pack();
90
        f.show();
91
    }
92
     */
93
    
94
    private static final class MemButton extends JButton implements ActionListener {
95
        
96
        private static final int TICK = 1000;
97
        
98
        private final Runtime r = Runtime.getRuntime();
99
        private final Format f = new MessageFormat("{0,number,0.0}/{1,number,0.0}Mb"); // NOI18N
100
        private final Timer t;
101
        private double proportion = 0.0d;
102
103
        public MemButton() {
104
            t = new Timer(TICK, this);
105
            addActionListener(this);
106
            // To get the size right:
107
            setText(f.format(new Object[] {new Double(999.0d), new Double(999.0d)}));
108
            setOpaque(false);
109
            setBorder(new EmptyBorder(1, 4, 1, 3));
110
            setPreferredSize(getPreferredSize());
111
        }
112
113
        public void addNotify() {
114
            super.addNotify();
115
            t.start();
116
            update();
117
        }
118
119
        public void removeNotify() {
120
            t.stop();
121
            super.removeNotify();
122
        }
123
124
        private void update() {
125
            long total = r.totalMemory();
126
            long used = total - r.freeMemory();
127
            proportion = ((double)used) / total;
128
            Double _total = new Double(((double)total) / 1024 / 1024);
129
            Double _used = new Double(((double)used) / 1024 / 1024);
130
            String text = f.format(new Object[] {_used, _total});
131
            setText(text);
132
        }
133
134
        protected void paintComponent(Graphics g) {
135
            Dimension size = getSize();
136
            g = g.create();
137
            g.clearRect(0, 0, size.width, size.height);
138
            g.setColor(UIManager.getDefaults().getColor("Button.focus")); // NOI18N
139
            g.fillRect(0, 0, (int)(size.width * proportion), size.height);
140
            super.paintComponent(g);
141
        }
142
143
        public void actionPerformed(ActionEvent e) {
144
            if (e.getSource() == this) {
145
                gc();
146
            } else {
147
                // Timer
148
                update();
149
            }
150
        }
151
    
152
    }
153
    
154
}
60
}

Return to bug 37256