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.layout.CellConstraints;
18  import com.jgoodies.forms.layout.FormLayout;
19  import com.jgoodies.forms.layout.Sizes;
20  import com.jgoodies.uif.AbstractDialog;
21  import com.jgoodies.uif.util.Resizer;
22  import com.jgoodies.uifextras.panel.HeaderPanel;
23  import com.jgoodies.uifextras.util.UIFactory;
24  import com.melloware.jukes.gui.tool.Resources;
25  
26  /**
27   * Provides JVM Memory usage and garbage collection option.
28   * <p>
29   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
30   * @author Emil A. Lefkof III <info@melloware.com>
31   * @version 4.0
32   */
33  public final class MemoryDialog
34      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      /**
42       * Constructs a default about dialog using the given owner.
43       *
44       * @param owner   the dialog's owner
45       */
46      public MemoryDialog(Frame owner) {
47          super(owner);
48          LOG.debug("Memory Dialog created.");
49      }
50  
51      /**
52       * Builds and answers the dialog's content.
53       *
54       * @return the dialog's content with tabbed pane and button bar
55       */
56      protected JComponent buildContent() {
57          JPanel content = new JPanel(new BorderLayout());
58          content.add(buildMainPanel(), BorderLayout.CENTER);
59          content.add(buildButtonBarWithClose(), BorderLayout.SOUTH);
60          return content;
61      }
62  
63      /**
64       * Builds and returns the dialog's header.
65       *
66       * @return the dialog's header component
67       */
68      protected JComponent buildHeader() {
69          return new HeaderPanel("Memory ",
70                                 "You can see the memory usage of this application.\n"
71                                 + "Please press garbage collection button to reclaim resources.",
72                                 Resources.MEMORY_LARGE_ICON);
73      }
74  
75      /**
76       * Builds and returns the dialog's pane.
77       *
78       * @return the dialog's  pane component
79       */
80      protected JComponent buildMainPanel() {
81          totalMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().totalMemory()));
82          maxMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().maxMemory()));
83          freeMemory = UIFactory.createBoldLabel(convertMemoryNumber(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()));
84          
85          JButton buttonGC = new JButton("Garbage Collection"); 
86          buttonGC.addActionListener(new java.awt.event.ActionListener() {
87              public void actionPerformed(java.awt.event.ActionEvent evt) {
88                  garbageCollect();
89              }
90          });
91          
92  
93          FormLayout layout = new FormLayout(
94                  "7dlu, left:pref, right:pref, left:0:grow", 
95                  "pref, 2dlu, pref, 14dlu, " +
96                  "pref, 2dlu, pref, pref, pref," +
97                  "pref, 2dlu, pref, 14dlu, pref" );
98                  
99          PanelBuilder builder = new PanelBuilder(layout);
100         builder.setDefaultDialogBorder();
101         CellConstraints cc = new CellConstraints();
102         int row = 1;
103         builder.addSeparator("Memory Usage",      cc.xyw(1, row++, 4));
104         row++;
105         builder.addLabel("Max Memory: ",           cc.xy(2, row));
106         builder.add(maxMemory, cc.xy(3, row++));
107         builder.addLabel("Used Memory: ",           cc.xy(2, row));
108         builder.add(totalMemory, cc.xy(3, row++));
109         builder.addLabel("Free Memory: ",           cc.xy(2, row));
110         builder.add(freeMemory, cc.xy(3, row++));
111         row++;
112         row++;
113         builder.add(buttonGC,           cc.xy(2, row));
114         panel = builder.getPanel();
115         int width = Sizes.dialogUnitXAsPixel(220, panel);
116         panel.setPreferredSize(Resizer.DEFAULT.fromWidth(width));
117         panel.setBorder(Borders.DIALOG_BORDER);
118         return panel;
119     }
120     
121     /**
122      * Updates the labels with new memory settings.
123      */
124     private void updateMemoryStats() {
125         totalMemory.setText(convertMemoryNumber(Runtime.getRuntime().totalMemory()));
126         maxMemory.setText(convertMemoryNumber(Runtime.getRuntime().maxMemory()));
127         freeMemory.setText(convertMemoryNumber(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()));
128     }
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      *
154      * @param component   the component to be resized
155      */
156     protected void resizeHook(JComponent component) {
157         Resizer.ONE2ONE.resizeDialogContent(component);
158     }
159 
160 }