001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.ui.actions;
005    
006    import graphlab.platform.core.BlackBoard;
007    import graphlab.ui.UIUtils;
008    import graphlab.ui.components.GComponentInterface;
009    
010    import javax.swing.*;
011    import javax.swing.border.EmptyBorder;
012    import javax.swing.plaf.basic.BasicButtonUI;
013    import java.awt.*;
014    import java.awt.event.ActionEvent;
015    import java.awt.event.ActionListener;
016    
017    /**
018     * @author Azin Azadi
019     */
020    public class MemoryUsageStatusAction extends graphlab.platform.core.AbstractAction implements ActionListener, GComponentInterface {
021        /**
022         * constructor
023         *
024         * @param bb the blackboard of the action
025         */
026        public MemoryUsageStatusAction(BlackBoard bb) {
027            super(bb);
028            new Timer(1000, this).start();
029            bname = UIUtils.getComponentVariableKeyNameInBlackBoard("memory usage");
030            blackboard.addListener(bname, this);
031        }
032    
033        /**
034         * called when the variable bname is changed
035         *
036         * @param eventName
037         * @param value
038         */
039        public void performAction(String eventName, Object value) {
040            button = blackboard.getData(bname);
041            button.setToolTipText("Press to free the memory");
042            button.setBorder(new EmptyBorder(0, 0, 0, 0));
043            button.addActionListener(new ActionListener() {
044                public void actionPerformed(ActionEvent e) {
045                    System.gc();
046                }
047            });
048        }
049    
050        /**
051         * occurs when the button pressed
052         */
053        public void actionPerformed(ActionEvent e) {
054            long totalmem = Runtime.getRuntime().totalMemory();
055            long used = totalmem - Runtime.getRuntime().freeMemory();
056            try {
057                button.setText(getOutStr(used, totalmem));
058                button.setSize(100, 15);
059            } catch (Exception ex) {
060            }
061        }
062    
063        /**
064         * :)) my simple formating methods. :D
065         */
066        private String getOutStr(long usedmem, long totalmem) {
067            return "<html><body>" +
068                    _strout("mem(kb): ") +
069                    _numout(usedmem / 1000) +
070                    _strout(" of ") +
071                    _numout(totalmem / 1000);
072        }
073    
074        /**
075         * :)) my simple formating methods. :D
076         */
077        private String _numout(long num) {
078            return "<font color='#222244'>" + num + "</font>";
079        }
080    
081        /**
082         * :)) my simple formating methods. :D
083         */
084        private String _strout(String str) {
085            return "<font color='#444477'>" + str + "</font>";
086        }
087    
088        private String bname;
089        private JButton button = new JButton("memory usage");
090    
091        public Component getComponent(BlackBoard b) {
092            button.setUI(new BasicButtonUI());
093            return button;
094        }
095    }