[Schmitzm-commits] r519 - in branches/1.0-gt2-2.6/src/skrueger/swing: . formatter

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 17 13:49:13 CET 2009


Author: alfonx
Date: 2009-11-17 13:49:12 +0100 (Tue, 17 Nov 2009)
New Revision: 519

Added:
   branches/1.0-gt2-2.6/src/skrueger/swing/HeapBar.java
   branches/1.0-gt2-2.6/src/skrueger/swing/formatter/
   branches/1.0-gt2-2.6/src/skrueger/swing/formatter/MbDecimalFormatter.java
Log:
* Moved MbDecimalFormatter from atlas to schmitzm
* Used in new class HeapBar.. a JProgressBar component that shows the current HEAP use

Added: branches/1.0-gt2-2.6/src/skrueger/swing/HeapBar.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/swing/HeapBar.java	2009-11-13 18:16:38 UTC (rev 518)
+++ branches/1.0-gt2-2.6/src/skrueger/swing/HeapBar.java	2009-11-17 12:49:12 UTC (rev 519)
@@ -0,0 +1,89 @@
+package skrueger.swing;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.util.Locale;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import javax.swing.JProgressBar;
+import javax.swing.SwingUtilities;
+
+import org.apache.log4j.Logger;
+
+import schmitzm.geotools.gui.GeotoolsGUIUtil;
+import schmitzm.lang.LangUtil;
+import schmitzm.lang.ResourceProvider;
+import schmitzm.swing.SwingUtil;
+import skrueger.swing.formatter.MbDecimalFormatter;
+
+public class HeapBar extends JProgressBar {
+	private static final Logger LOGGER = Logger.getLogger(HeapBar.class);
+
+	protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
+			.extendPackagePath(SwingUtil.class,
+					"resource.locales.SwingResourceBundle"), Locale.ENGLISH);
+
+	private Timer updateTimer;
+	String valueAsText = "";
+	MbDecimalFormatter formatter = new MbDecimalFormatter();
+
+	public HeapBar() {
+		updateTimer = new Timer("check memory");
+		updateTimer.scheduleAtFixedRate(new UpdateMemoryBarTask(), 100, 1000);
+
+		setMinimum(0);
+		setMaximum(100);
+
+		SwingUtil.setPreferredWidth(this, 200);
+	}
+
+	@Override
+	protected void paintComponent(Graphics g) {
+		super.paintComponent(g);
+		g.setColor(Color.black);
+		g.drawString(valueAsText, 5, 12);
+	}
+
+	class UpdateMemoryBarTask extends TimerTask {
+
+		@Override
+		public void run() {
+
+			// Get current size of heap in bytes
+			long heapSize = Runtime.getRuntime().totalMemory();
+
+			// Get maximum size of heap in bytes. The heap cannot grow beyond
+			// this size.
+			// Any attempt will result in an OutOfMemoryException.
+			long heapMaxSize = Runtime.getRuntime().maxMemory();
+
+			// Get amount of free memory within the heap in bytes. This size
+			// will increase
+			// after garbage collection and decrease as new objects are created.
+			long heapFreeSize = Runtime.getRuntime().freeMemory();
+
+			long used = (heapSize - heapFreeSize);
+
+			int percentaUsed = (int) (used * 100 / heapMaxSize);
+
+			if (percentaUsed > 80)
+				LOGGER.warn(percentaUsed + "% belegt");
+
+			if (percentaUsed > 70) {
+				if (percentaUsed > 90)
+					setForeground(Color.red);
+				else
+					setForeground(Color.orange);
+			} else {
+				setForeground(Color.green);
+			}
+
+			setValue(percentaUsed);
+			valueAsText = RESOURCE.getString("HeapMemoryBar.status", formatter
+					.format(used), percentaUsed, formatter.format(heapMaxSize));
+
+			repaint();
+		}
+	}
+}


Property changes on: branches/1.0-gt2-2.6/src/skrueger/swing/HeapBar.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native

Added: branches/1.0-gt2-2.6/src/skrueger/swing/formatter/MbDecimalFormatter.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/swing/formatter/MbDecimalFormatter.java	2009-11-13 18:16:38 UTC (rev 518)
+++ branches/1.0-gt2-2.6/src/skrueger/swing/formatter/MbDecimalFormatter.java	2009-11-17 12:49:12 UTC (rev 519)
@@ -0,0 +1,64 @@
+package skrueger.swing.formatter;
+
+import java.text.DecimalFormat;
+import java.text.FieldPosition;
+
+/**
+ * A formatter that formats double and long byte number into a Kb, Mb or Gb
+ * format.
+ */
+
+public class MbDecimalFormatter extends DecimalFormat {
+
+	public MbDecimalFormatter() {
+		super("#########0.0");
+	}
+
+	@Override
+	public StringBuffer format(double number, StringBuffer result,
+			FieldPosition fieldPosition) {
+
+		String unit = "Mb";
+		number /= 1024. * 1024.;
+		if (number >= 512.) {
+			unit = "Gb";
+			number /= 1024.;
+		}
+
+		if (number >= 0.1) {
+			return super.format(number, result, fieldPosition).append(unit);
+		} else {
+			return super.format(0.1, result, fieldPosition).append(unit)
+					.insert(0, "<");
+		}
+
+	};
+
+	@Override
+	public StringBuffer format(long number_l, StringBuffer result,
+			FieldPosition fieldPosition) {
+
+		if (number_l < Double.MAX_VALUE) {
+			// The number is not that big.. convert to Double and go...
+			return format(Double.valueOf(number_l), result, fieldPosition);
+		}
+
+		// We have a BIG number... Rounding-errors don't count anymore.
+		number_l /= 1024. * 1024.;
+		String unit = "Mb";
+
+		if (number_l >= 512.) {
+			unit = "Gb";
+			number_l /= 1024.;
+		}
+
+		if (number_l >= 0.1) {
+			return super.format(number_l, result, fieldPosition).append(unit);
+		} else {
+			return super.format(0.1, result, fieldPosition).append(unit)
+					.insert(0, "<");
+		}
+
+	};
+
+}


Property changes on: branches/1.0-gt2-2.6/src/skrueger/swing/formatter/MbDecimalFormatter.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list