All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xhtmlrenderer.demo.browser.swt.BrowserStatus Maven / Gradle / Ivy

There is a newer version: 9.9.4
Show newest version
/*
 * {{{ header & license
 * Copyright (c) 2007 Vianney le Clément
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */
package org.xhtmlrenderer.demo.browser.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class BrowserStatus extends Composite {

    private static final int REFRESH_INTERVAL = 5000;

    private final Label _status;
    private final Label _memory;
    private final MemoryThread _memoryThread;

    public BrowserStatus(Composite parent) {
        super(parent, SWT.NONE);
        // widgets
        _status = new Label(this, SWT.LEFT);
        _status.setText("Flying Saucer initialized");
        _memory = new Label(this, SWT.RIGHT);
        _memory.setText("?MB / ?MB");
        // layout
        setLayout(new FormLayout());
        FormData fd;
        fd = new FormData();
        fd.top = new FormAttachment(0);
        fd.left = new FormAttachment(0);
        fd.right = new FormAttachment(_memory, -5);
        fd.bottom = new FormAttachment(100);
        _status.setLayoutData(fd);
        fd = new FormData();
        fd.top = new FormAttachment(0);
        fd.right = new FormAttachment(100);
        fd.bottom = new FormAttachment(100);
        _memory.setLayoutData(fd);
        _memory.pack();
        // thread
        _memoryThread = new MemoryThread();
        _memoryThread.start();
        // listeners
        addDisposeListener(e -> _memoryThread.interrupt());
    }

    public void setStatus(final String text) {
        getDisplay().asyncExec(() -> {
            if (!_status.isDisposed()) {
                _status.setText(text);
            }
        });
    }

    public void refreshMemory() {
        _memoryThread.refresh();
    }

    private class MemoryThread extends Thread {
        public synchronized void run() {
            while (true) {
                try {
                    Runtime rt = Runtime.getRuntime();
                    long used = rt.totalMemory() - rt.freeMemory();
                    long total = rt.totalMemory();

                    used = used / (1024 * 1024);
                    total = total / (1024 * 1024);

                    final String text = used + "MB / " + total + "MB";
                    getDisplay().asyncExec(() -> {
                        if (!_memory.isDisposed()) {
                            _memory.setText(text);
                            _memory.pack();
                            layout();
                        }
                    });
                    wait(REFRESH_INTERVAL);
                } catch (InterruptedException ignore) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }

        /**
         * Force an immediate memory status refresh
         */
        public synchronized void refresh() {
            notifyAll();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy