Package palmed.ui

Source Code of palmed.ui.AboutDialog

/**
* Redistribution  and use  in source  and binary  forms, with  or without
* modification, are permitted provided  that the following conditions are
* met :
*
* . Redistributions  of  source  code  must  retain  the  above copyright
*   notice, this list of conditions and the following disclaimer.
*
* . Redistributions in  binary form  must reproduce  the above  copyright
*   notice, this list of conditions  and the following disclaimer in  the
*   documentation and/or other materials provided with the distribution.
*
* . The name of the author may not be used to endorse or promote products
*   derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
* IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
* WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
* DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
* INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
* (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
* SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
* HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: AboutDialog.java 396 2005-12-31 12:57:52Z mat007 $
*/

package palmed.ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

/**
* This dialog shows an 'about' box.
*
* @author Mathieu Champlon
* @version $Revision: 396 $ $Date: 2005-12-31 06:57:52 -0600 (Sat, 31 Dec 2005) $
*/
public final class AboutDialog extends Dialog implements CommandListener
{
    /**
     * The number of characters for memory fields.
     */
    private static final int NUMBER_OF_DIGITS = 8;
    /**
     * The display.
     */
    private final Display display_;
    /**
     * The displayable to show when closing the dialog.
     */
    private final Displayable next_;
    /**
     * The item displaying the amount of free memory.
     */
    private final StringItem freeMemory_;
    /**
     * The item displaying the amount of used memory.
     */
    private final StringItem usedMemory_;
    /**
     * The item displaying the total amount of memory.
     */
    private final StringItem totalMemory_;

    /**
     * Create an 'about' dialog.
     *
     * @param midlet the midlet to display the information about
     * @param display the application display
     * @param next the displayable to show when 'OK' is pressed
     */
    public AboutDialog( final MIDlet midlet, final Display display, final Displayable next )
    {
        super( "About " + midlet.getAppProperty( "MIDlet-Name" ) );
        if( display == null )
            throw new IllegalArgumentException( "parameter 'display' is null" );
        if( next == null )
            throw new IllegalArgumentException( "parameter 'next' is null" );
        display_ = display;
        next_ = next;
        appendMessage( midlet.getAppProperty( "MIDlet-Name" ) + " " + midlet.getAppProperty( "MIDlet-Version" ) );
        append( createUrlItem( midlet.getAppProperty( "MIDlet-Info-URL" ) ) );
        appendMessage( "Icons by the Tango Project" );
        append( createUrlItem( "http://tango-project.org" ) );
        appendMessage( "Memory" );
        freeMemory_ = appendMessage();
        usedMemory_ = appendMessage();
        totalMemory_ = appendMessage();
        refresh();
        addCommand( new Command( "Ok", Command.OK, 1 ) );
        setCommandListener( this );
    }

    /**
     * Refresh informations.
     *
     * @return this
     */
    public Displayable refresh()
    {
        Runtime.getRuntime().gc();
        refreshFreeMemory();
        refreshUsedMemory();
        refreshTotalMemory();
        return this;
    }

    private void refreshFreeMemory()
    {
        final String memory = String.valueOf( Runtime.getRuntime().freeMemory() );
        freeMemory_.setText( format( memory ) + " bytes free" );
    }

    private void refreshUsedMemory()
    {
        final String memory = String.valueOf( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() );
        usedMemory_.setText( format( memory ) + " bytes used" );
    }

    private void refreshTotalMemory()
    {
        final String memory = String.valueOf( Runtime.getRuntime().totalMemory() );
        totalMemory_.setText( format( memory ) + " bytes total" );
    }

    private String format( final String string )
    {
        final StringBuffer buffer = new StringBuffer( string );
        for( int i = 0; i < NUMBER_OF_DIGITS - string.length(); ++i )
            buffer.insert( 0, ' ' );
        return buffer.toString();
    }

    private StringItem appendMessage()
    {
        final StringItem item = new StringItem( null, null );
        item.setLayout( Item.LAYOUT_NEWLINE_BEFORE );
        item.setFont( Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ) );
        append( item );
        return item;
    }

    private Item createUrlItem( final String url )
    {
        final StringItem item = new StringItem( null, url, Item.HYPERLINK );
        item.setLayout( Item.LAYOUT_NEWLINE_BEFORE );
        return item;
    }

    /**
     * {@inheritDoc}
     */
    public void commandAction( final Command command, final Displayable displayable )
    {
        display_.setCurrent( next_ );
    }
}
TOP

Related Classes of palmed.ui.AboutDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.