001: package org.dbbrowser.ui.widget;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.logging.Log;
005: import infrastructure.propertymanager.PropertyManager;
006:
007: import java.awt.BorderLayout;
008: import java.awt.Color;
009: import java.awt.Dimension;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import javax.swing.BorderFactory;
013: import javax.swing.BoxLayout;
014: import javax.swing.ImageIcon;
015: import javax.swing.JButton;
016: import javax.swing.JFrame;
017: import javax.swing.JLabel;
018: import javax.swing.JPanel;
019: import javax.swing.JProgressBar;
020:
021: public class MemoryMonitor {
022: private static final String TITLE = InternationalizationManager
023: .getInstance().getMessage("dbbrowser-ui",
024: "dbbrowser-ui-dbbrowser-window-title-label", null);
025: private static final String LABEL_FOR_RECLAIM_MEMORY_BUTTON = InternationalizationManager
026: .getInstance()
027: .getMessage(
028: "dbbrowser-ui",
029: "dbbrowser-ui-dbbrowser-window-memory-monitor-window-reclaim-memory-button-label",
030: null);
031: private static final String RECLAIM_MEMORY_ICON_FILENAME = PropertyManager
032: .getInstance().getProperty(
033: "dbbrowser-ui-dbbrowser-window-empty-trash-icon");
034:
035: private JFrame frame = null;
036: private JProgressBar progressBar = null;
037: private JLabel labelForMemoryStatus = new JLabel("");
038: private Runtime runtime = Runtime.getRuntime();
039: private double maxValue = 0;
040:
041: public MemoryMonitor() {
042: this .initialise();
043: }
044:
045: public void show() {
046: //Show the frame
047: this .frame.setVisible(true);
048: }
049:
050: private void initialise() {
051: //Set the max value
052: this .maxValue = (double) this .runtime.maxMemory();
053: //Set the size of frame
054: this .frame = new JFrame(TITLE);
055: this .frame.setSize(300, 150);
056: this .frame.setLocationRelativeTo(null);
057: this .frame.getContentPane().setLayout(new BorderLayout());
058: this .frame.setResizable(false);
059:
060: //Add the widgets
061: JPanel panelForProgress = new JPanel();
062: panelForProgress.setLayout(new BoxLayout(panelForProgress,
063: BoxLayout.PAGE_AXIS));
064:
065: //Add the progress bar
066: this .progressBar = new JProgressBar(0, 100);
067: this .progressBar.setStringPainted(true);
068: this .progressBar.setValue(0);
069: this .progressBar.setBorder(BorderFactory.createEtchedBorder());
070: panelForProgress.add(this .progressBar);
071:
072: //Add label to show memory
073: JPanel p = new JPanel();
074: p.setMaximumSize(new Dimension(300, 100));
075: p.add(labelForMemoryStatus);
076: panelForProgress.add(p);
077:
078: this .frame.getContentPane().add(panelForProgress,
079: BorderLayout.CENTER);
080:
081: //Add the buttons panel
082: JPanel panelForButton = new JPanel();
083: panelForButton.setLayout(new BorderLayout());
084: JButton buttonToRunGC = new JButton(
085: LABEL_FOR_RECLAIM_MEMORY_BUTTON, new ImageIcon(
086: RECLAIM_MEMORY_ICON_FILENAME));
087: buttonToRunGC.addActionListener(new ListenerForButtonToRunGC());
088: panelForButton.add(buttonToRunGC, BorderLayout.CENTER);
089: this .frame.getContentPane().add(panelForButton,
090: BorderLayout.SOUTH);
091:
092: //Add dummy panels
093: this .frame.getContentPane().add(new JPanel(),
094: BorderLayout.NORTH);
095: this .frame.getContentPane()
096: .add(new JPanel(), BorderLayout.WEST);
097: this .frame.getContentPane()
098: .add(new JPanel(), BorderLayout.EAST);
099:
100: MemoryMonitorTimer memoryMonitorTimer = new MemoryMonitorTimer();
101: memoryMonitorTimer.start();
102: }
103:
104: private class MemoryMonitorTimer extends Thread {
105: public void run() {
106: try {
107: while (true) {
108: Double d = new Double(runtime.totalMemory()
109: / maxValue * 100);
110: progressBar.setValue(d.intValue());
111:
112: Double[] parameters = new Double[] {
113: new Double(runtime.totalMemory() / 1000000),
114: new Double(runtime.maxMemory() / 1000000) };
115: String LABEL_FOR_MEMORY_STATUS = InternationalizationManager
116: .getInstance()
117: .getMessage(
118: "dbbrowser-ui",
119: "dbbrowser-ui-dbbrowser-window-memory-monitor-window-memory-consumption-label",
120: parameters);
121: labelForMemoryStatus
122: .setText(LABEL_FOR_MEMORY_STATUS);
123: Thread.sleep(1000);
124: }
125: } catch (Exception exc) {
126: Log.getInstance().fatalMessage(
127: "*** Fatal exception in memory monitor ***\n"
128: + exc.getMessage(),
129: MemoryMonitor.class.getName());
130: }
131: }
132: }
133:
134: private class ListenerForButtonToRunGC implements ActionListener {
135: public void actionPerformed(ActionEvent e) {
136: System.gc();
137: Log.getInstance().debugMessage(
138: "*** Garbage collector invoked ***",
139: MemoryMonitor.class.getName());
140: }
141: }
142: }
|