View Javadoc

1   package com.melloware.jukes.gui.view.dialogs;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Frame;
5   
6   import javax.swing.JButton;
7   import javax.swing.JComponent;
8   import javax.swing.JLabel;
9   import javax.swing.JPanel;
10  
11  import org.apache.commons.io.FileUtils;
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  import com.jgoodies.forms.builder.PanelBuilder;
16  import com.jgoodies.forms.factories.Borders;
17  import com.jgoodies.forms.factories.ButtonBarFactory;
18  import com.jgoodies.forms.layout.CellConstraints;
19  import com.jgoodies.forms.layout.FormLayout;
20  import com.jgoodies.forms.layout.Sizes;
21  import com.jgoodies.uif.AbstractDialog;
22  import com.jgoodies.uif.util.Resizer;
23  import com.jgoodies.uifextras.panel.HeaderPanel;
24  import com.jgoodies.uifextras.util.UIFactory;
25  import com.melloware.jukes.gui.tool.Resources;
26  
27  /**
28   * Provides JVM Memory usage and garbage collection option.
29   * <p>
30   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
31   * @author Emil A. Lefkof III <info@melloware.com>
32   * @version 4.0
33   */
34  public final class MemoryDialog extends AbstractDialog {
35  
36     private static final Log LOG = LogFactory.getLog(MemoryDialog.class);
37     private JLabel totalMemory;
38     private JLabel maxMemory;
39     private JLabel freeMemory;
40     private JPanel panel;
41     private JButton buttonClose;
42     private JComponent buttonBar;
43  
44     /**
45      * Constructs a default about dialog using the given owner.
46      * @param owner the dialog's owner
47      */
48     public MemoryDialog(Frame owner) {
49        super(owner);
50        LOG.debug("Memory Dialog created.");
51     }
52  
53     /**
54      * Builds and answers the dialog's content.
55      * @return the dialog's content with tabbed pane and button bar
56      */
57     @Override
58     protected JComponent buildContent() {
59        JPanel content = new JPanel(new BorderLayout());
60        content.add(buildMainPanel(), BorderLayout.CENTER);
61        buttonClose = createCancelButton();// AZ
62        buttonClose.setText(Resources.getString("label.Close")); // AZ
63        buttonClose.setEnabled(true);
64        buttonBar = ButtonBarFactory.buildRightAlignedBar(buttonClose);
65        // buttonBar = buildButtonBarWithClose();
66        content.add(buttonBar, BorderLayout.SOUTH);
67        return content;
68     }
69  
70     /**
71      * Builds and returns the dialog's header.
72      * @return the dialog's header component
73      */
74     @Override
75     protected JComponent buildHeader() {
76        return new HeaderPanel(Resources.getString("label.Memory"), Resources.getString("label.MemoryMessage"),
77                 Resources.MEMORY_LARGE_ICON);
78     }
79  
80     /**
81      * Builds and returns the dialog's pane.
82      * @return the dialog's pane component
83      */
84     protected JComponent buildMainPanel() {
85        totalMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().totalMemory()));
86        maxMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().maxMemory()));
87        freeMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().maxMemory()
88                 - Runtime.getRuntime().totalMemory()));
89  
90        JButton buttonGC = new JButton(Resources.getString("label.GarbageCollection"));
91        buttonGC.addActionListener(new java.awt.event.ActionListener() {
92           public void actionPerformed(java.awt.event.ActionEvent evt) {
93              garbageCollect();
94           }
95        });
96  
97        FormLayout layout = new FormLayout("7dlu, left:pref, right:pref, left:0:grow", "pref, 2dlu, pref, 14dlu, "
98                 + "pref, 2dlu, pref, pref, pref," + "pref, 2dlu, pref, 14dlu, pref");
99  
100       PanelBuilder builder = new PanelBuilder(layout);
101       builder.setDefaultDialogBorder();
102       CellConstraints cc = new CellConstraints();
103       int row = 1;
104       builder.addSeparator(Resources.getString("label.MemoryUsage"), cc.xyw(1, row++, 4));
105       row++;
106       builder.addLabel(Resources.getString("label.MaxMemory") + ": ", cc.xy(2, row));
107       builder.add(maxMemory, cc.xy(3, row++));
108       builder.addLabel(Resources.getString("label.UsedMemory") + ": ", cc.xy(2, row));
109       builder.add(totalMemory, cc.xy(3, row++));
110       builder.addLabel(Resources.getString("label.FreeMemory") + ": ", cc.xy(2, row));
111       builder.add(freeMemory, cc.xy(3, row++));
112       row++;
113       row++;
114       builder.add(buttonGC, cc.xy(2, row));
115       panel = builder.getPanel();
116       int width = Sizes.dialogUnitXAsPixel(220, panel);
117       panel.setPreferredSize(Resizer.DEFAULT.fromWidth(width));
118       panel.setBorder(Borders.DIALOG_BORDER);
119       return panel;
120    }
121 
122    /**
123     * Updates the labels with new memory settings.
124     */
125    private void updateMemoryStats() {
126       totalMemory.setText(convertMemoryNumber(Runtime.getRuntime().totalMemory()));
127       maxMemory.setText(convertMemoryNumber(Runtime.getRuntime().maxMemory()));
128       freeMemory.setText(convertMemoryNumber(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()));
129    }
130 
131    /**
132     * Converts a number like 134217728 to 128 MB.
133     * <p>
134     * @param aMemoryNumber the number to convert
135     * @return the String representation
136     */
137    private String convertMemoryNumber(long aMemoryNumber) {
138       return FileUtils.byteCountToDisplaySize(aMemoryNumber);
139    }
140 
141    /**
142     * Runs the garbage collector.
143     */
144    private void garbageCollect() {
145       LOG.debug("Running garbage collection.");
146       System.gc();
147       updateMemoryStats();
148       panel.updateUI();
149    }
150 
151    /**
152     * Resizes the given component to give it a quadratic aspect ratio.
153     * @param component the component to be resized
154     */
155    @Override
156    protected void resizeHook(JComponent component) {
157       Resizer.ONE2ONE.resizeDialogContent(component);
158    }
159 
160 }