Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.mihalis.opal/src/main/java/org/mihalis/opal/heapManager/HeapManager.java')
-rw-r--r--org.mihalis.opal/src/main/java/org/mihalis/opal/heapManager/HeapManager.java321
1 files changed, 321 insertions, 0 deletions
diff --git a/org.mihalis.opal/src/main/java/org/mihalis/opal/heapManager/HeapManager.java b/org.mihalis.opal/src/main/java/org/mihalis/opal/heapManager/HeapManager.java
new file mode 100644
index 0000000..e04b2fa
--- /dev/null
+++ b/org.mihalis.opal/src/main/java/org/mihalis/opal/heapManager/HeapManager.java
@@ -0,0 +1,321 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Laurent CARON
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
+ *******************************************************************************/
+package org.mihalis.opal.heapManager;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
+import org.eclipse.swt.events.PaintEvent;
+import org.eclipse.swt.events.PaintListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Canvas;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Widget;
+import org.mihalis.opal.utils.ResourceManager;
+import org.mihalis.opal.utils.SWTGraphicUtil;
+
+/**
+ * Instances of this class are controls that display the memory used, the whole
+ * memory, and contains a button to perform a GC.
+ */
+public class HeapManager extends Composite {
+
+ /** The bar. */
+ private Canvas bar;
+
+ /** The button. */
+ private Button button;
+
+ /** The heap max size. */
+ private int heapMaxSize;
+
+ /** The heap size. */
+ private int heapSize;
+
+ /** The bar border color. */
+ private Color barBorderColor;
+
+ /** The bar inner color. */
+ private Color barInnerColor;
+
+ /** The bar text color. */
+ private Color barTextColor;
+
+ /** The bar gradient color top start. */
+ private Color barGradientColorTopStart;
+
+ /** The bar gradient color top end. */
+ private Color barGradientColorTopEnd;
+
+ /** The bar gradient color middle start. */
+ private Color barGradientColorMiddleStart;
+
+ /**
+ * Constructs a new instance of this class given its parent and a style
+ * value describing its behavior and appearance.
+ * <p>
+ * The style value is either one of the style constants defined in class
+ * <code>SWT</code> which is applicable to instances of this class, or must
+ * be built by <em>bitwise OR</em>'ing together (that is, using the
+ * <code>int</code> "|" operator) two or more of those <code>SWT</code>
+ * style constants. The class description lists the style constants that are
+ * applicable to the class. Style bits are also inherited from superclasses.
+ * </p>
+ *
+ * @param parent a widget which will be the parent of the new instance
+ * (cannot be null)
+ * @param style the style of widget to construct
+ * @see Composite#Composite(Composite, int)
+ * @see Widget#getStyle
+ * @exception IllegalArgumentException <ul>
+ * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
+ * </ul>
+ * @exception SWTException <ul>
+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+ * thread that created the parent</li>
+ * </ul>
+ */
+ public HeapManager(final Composite parent, final int style) {
+ super(parent, style);
+ final GridLayout gridLayout = new GridLayout(2, false);
+ gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
+ setLayout(gridLayout);
+
+ createBar();
+ createButton();
+ updateContent();
+ createDefaultColors();
+ }
+
+ /**
+ * Creates the bar that displays the memory.
+ */
+ private void createBar() {
+ this.bar = new Canvas(this, SWT.NONE);
+ final GridData gd = new GridData(GridData.FILL, GridData.FILL, true, false);
+ gd.minimumWidth = 100;
+ gd.heightHint = 30;
+ this.bar.setLayoutData(gd);
+ this.heapMaxSize = (int) (Runtime.getRuntime().maxMemory() / (1024 * 1024));
+ this.bar.addPaintListener(new PaintListener() {
+ @Override
+ public void paintControl(final PaintEvent e) {
+ drawBar(e);
+ }
+ });
+ }
+
+ /**
+ * Draw the bar.
+ *
+ * @param e {@link PaintEvent}
+ */
+ private void drawBar(final PaintEvent e) {
+ final GC gc = e.gc;
+ final Rectangle clientArea = this.bar.getClientArea();
+
+ gc.setForeground(this.barBorderColor);
+ gc.setBackground(this.barInnerColor);
+ gc.fillRectangle(clientArea);
+ gc.drawRectangle(clientArea.x, clientArea.y, clientArea.width - 1, clientArea.height - 1);
+
+ final float width = (clientArea.width - 2f) * this.heapSize / this.heapMaxSize;
+
+ gc.setForeground(this.barGradientColorTopStart);
+ gc.setBackground(this.barGradientColorTopEnd);
+ gc.fillGradientRectangle(clientArea.x + 1, clientArea.y + 1, (int) width, clientArea.height / 2, true);
+
+ gc.setForeground(this.barGradientColorMiddleStart);
+ gc.setBackground(this.barBorderColor);
+ gc.fillGradientRectangle(clientArea.x + 1, clientArea.height / 2, (int) width, clientArea.height / 2, true);
+
+ final String message = this.heapSize + " " + ResourceManager.getLabel(ResourceManager.MEGABYTES) + "/" + //
+ this.heapMaxSize + " " + ResourceManager.getLabel(ResourceManager.MEGABYTES);
+ final Point size = gc.stringExtent(message);
+
+ gc.setForeground(this.barTextColor);
+ gc.setFont(getFont());
+ gc.drawText(message, (clientArea.width - size.x) / 2, (clientArea.height - size.y) / 2, true);
+
+ gc.dispose();
+
+ }
+
+ /**
+ * Create the button used to perform GC.
+ */
+ private void createButton() {
+ this.button = new Button(this, SWT.PUSH);
+ final Image image = SWTGraphicUtil.createImageFromFile("images/trash.png");
+ this.button.setImage(image);
+ SWTGraphicUtil.addDisposer(this.button, image);
+ this.button.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));
+ this.button.addSelectionListener(new SelectionAdapter() {
+ /**
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ @Override
+ public void widgetSelected(final SelectionEvent e) {
+ // SONAR is not amused about self initiated garbage collector calls!
+// System.gc();
+ }
+ });
+ this.button.setToolTipText(ResourceManager.getLabel(ResourceManager.PERFORM_GC));
+ this.button.pack();
+ }
+
+ /**
+ * Update the content of the bar.
+ */
+ private void updateContent() {
+ getDisplay().timerExec(500, new Runnable() {
+ @Override
+ public void run() {
+ HeapManager.this.heapSize = (int) (Runtime.getRuntime().totalMemory() / (1024 * 1024));
+ if (!isDisposed()) {
+ HeapManager.this.bar.redraw();
+ if (!getDisplay().isDisposed()) {
+ getDisplay().timerExec(500, this);
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * Creates the default colors.
+ */
+ private void createDefaultColors() {
+ this.barTextColor = SWTGraphicUtil.getDefaultColor(this, 57, 98, 149);
+ this.barInnerColor = SWTGraphicUtil.getDefaultColor(this, 219, 230, 243);
+ this.barBorderColor = SWTGraphicUtil.getDefaultColor(this, 101, 148, 207);
+ this.barGradientColorTopStart = SWTGraphicUtil.getDefaultColor(this, 175, 202, 237);
+ this.barGradientColorTopEnd = SWTGraphicUtil.getDefaultColor(this, 136, 177, 229);
+ this.barGradientColorMiddleStart = SWTGraphicUtil.getDefaultColor(this, 112, 161, 223);
+ }
+
+ /**
+ * Gets the bar border color.
+ *
+ * @return the barBorderColor
+ */
+ public Color getBarBorderColor() {
+ return this.barBorderColor;
+ }
+
+ /**
+ * Sets the bar border color.
+ *
+ * @param barBorderColor the barBorderColor to set
+ */
+ public void setBarBorderColor(final Color barBorderColor) {
+ this.barBorderColor = barBorderColor;
+ }
+
+ /**
+ * Gets the bar inner color.
+ *
+ * @return the barInnerColor
+ */
+ public Color getBarInnerColor() {
+ return this.barInnerColor;
+ }
+
+ /**
+ * Sets the bar inner color.
+ *
+ * @param barInnerColor the barInnerColor to set
+ */
+ public void setBarInnerColor(final Color barInnerColor) {
+ this.barInnerColor = barInnerColor;
+ }
+
+ /**
+ * Gets the bar text color.
+ *
+ * @return the barTextColor
+ */
+ public Color getBarTextColor() {
+ return this.barTextColor;
+ }
+
+ /**
+ * Sets the bar text color.
+ *
+ * @param barTextColor the barTextColor to set
+ */
+ public void setBarTextColor(final Color barTextColor) {
+ this.barTextColor = barTextColor;
+ }
+
+ /**
+ * Gets the bar gradient color top start.
+ *
+ * @return the barGradientColorTopStart
+ */
+ public Color getBarGradientColorTopStart() {
+ return this.barGradientColorTopStart;
+ }
+
+ /**
+ * Sets the bar gradient color top start.
+ *
+ * @param barGradientColorTopStart the barGradientColorTopStart to set
+ */
+ public void setBarGradientColorTopStart(final Color barGradientColorTopStart) {
+ this.barGradientColorTopStart = barGradientColorTopStart;
+ }
+
+ /**
+ * Gets the bar gradient color top end.
+ *
+ * @return the barGradientColorTopEnd
+ */
+ public Color getBarGradientColorTopEnd() {
+ return this.barGradientColorTopEnd;
+ }
+
+ /**
+ * Sets the bar gradient color top end.
+ *
+ * @param barGradientColorTopEnd the barGradientColorTopEnd to set
+ */
+ public void setBarGradientColorTopEnd(final Color barGradientColorTopEnd) {
+ this.barGradientColorTopEnd = barGradientColorTopEnd;
+ }
+
+ /**
+ * Gets the bar gradient color middle start.
+ *
+ * @return the barGradientColorMiddleStart
+ */
+ public Color getBarGradientColorMiddleStart() {
+ return this.barGradientColorMiddleStart;
+ }
+
+ /**
+ * Sets the bar gradient color middle start.
+ *
+ * @param barGradientColorMiddleStart the barGradientColorMiddleStart to set
+ */
+ public void setBarGradientColorMiddleStart(final Color barGradientColorMiddleStart) {
+ this.barGradientColorMiddleStart = barGradientColorMiddleStart;
+ }
+
+}

Back to the top