KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > GarbageCollectAction


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 package org.openide.actions;
20
21 import org.openide.util.HelpCtx;
22 import org.openide.util.NbBundle;
23 import org.openide.util.RequestProcessor;
24 import org.openide.util.actions.CallableSystemAction;
25
26 import java.awt.*;
27 import java.awt.event.*;
28
29
30 import javax.swing.*;
31
32
33 // Toolbar presenter like MemoryMeterAction, except:
34
// 1. Does not have a mark etc.
35
// 2. But pressing it runs GC.
36
// 3. Slim profile fits nicely in the menu bar (at top level).
37
// 4. Displays textual memory usage directly, not via tooltip.
38
// Intended to be unobtrusive enough to leave on for daily use.
39

40 /**
41  * Perform a system garbage collection.
42  * @author Jesse Glick, Tim Boudreau
43  */

44 public class GarbageCollectAction extends CallableSystemAction {
45     public String JavaDoc getName() {
46         return NbBundle.getBundle(GarbageCollectAction.class).getString("CTL_GarbageCollect"); // NOI18N
47
}
48
49     public HelpCtx getHelpCtx() {
50         return new HelpCtx(GarbageCollectAction.class);
51     }
52
53     public void performAction() {
54         gc();
55     }
56
57     private static void gc() {
58         // Can be slow, would prefer not to block on it.
59
RequestProcessor.getDefault().post(
60             new Runnable JavaDoc() {
61                 public void run() {
62                     System.gc();
63                     System.runFinalization();
64                     System.gc();
65                 }
66             }
67         );
68     }
69
70     protected boolean asynchronous() {
71         return false;
72     }
73
74     public Component getToolbarPresenter() {
75         return new HeapViewWrapper();
76 // return new MemButton();
77
}
78
79     private static final class HeapViewWrapper extends JComponent {
80         public HeapViewWrapper() {
81             add(new HeapView());
82             setLayout(null);
83         }
84         
85         public boolean isOpaque() {
86             return false;
87         }
88         
89         public Dimension getMinimumSize() {
90             return calcPreferredSize();
91         }
92
93         public Dimension getPreferredSize() {
94             return calcPreferredSize();
95         }
96
97         public Dimension getMaximumSize() {
98             Dimension pref = calcPreferredSize();
99             Container parent = getParent();
100             if (parent != null && parent.getHeight() > 0) {
101                 pref.height = parent.getHeight();
102             }
103             return pref;
104         }
105         
106         public Dimension calcPreferredSize() {
107             Dimension pref = getHeapView().heapViewPreferredSize();
108             pref.height += 1;
109             pref.width += 6;
110             return pref;
111         }
112
113         public void layout() {
114             int w = getWidth();
115             int h = getHeight();
116             HeapView heapView = getHeapView();
117             heapView.setBounds(4, 2, w - 6, h - 4);
118         }
119
120         private HeapView getHeapView() {
121             return (HeapView)getComponent(0);
122         }
123     }
124     
125 }
126
Popular Tags