1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: AboutDialog.java 396 2005-12-31 12:57:52Z mat007 $ 29 */ 30 31 package palmed.ui; 32 33 import javax.microedition.lcdui.Command; 34 import javax.microedition.lcdui.CommandListener; 35 import javax.microedition.lcdui.Display; 36 import javax.microedition.lcdui.Displayable; 37 import javax.microedition.lcdui.Font; 38 import javax.microedition.lcdui.Item; 39 import javax.microedition.lcdui.StringItem; 40 import javax.microedition.midlet.MIDlet; 41 42 /*** 43 * This dialog shows an 'about' box. 44 * 45 * @author Mathieu Champlon 46 * @version $Revision: 396 $ $Date: 2005-12-31 21:57:52 +0900 (sam., 31 déc. 2005) $ 47 */ 48 public final class AboutDialog extends Dialog implements CommandListener 49 { 50 /*** 51 * The number of characters for memory fields. 52 */ 53 private static final int NUMBER_OF_DIGITS = 8; 54 /*** 55 * The display. 56 */ 57 private final Display display_; 58 /*** 59 * The displayable to show when closing the dialog. 60 */ 61 private final Displayable next_; 62 /*** 63 * The item displaying the amount of free memory. 64 */ 65 private final StringItem freeMemory_; 66 /*** 67 * The item displaying the amount of used memory. 68 */ 69 private final StringItem usedMemory_; 70 /*** 71 * The item displaying the total amount of memory. 72 */ 73 private final StringItem totalMemory_; 74 75 /*** 76 * Create an 'about' dialog. 77 * 78 * @param midlet the midlet to display the information about 79 * @param display the application display 80 * @param next the displayable to show when 'OK' is pressed 81 */ 82 public AboutDialog( final MIDlet midlet, final Display display, final Displayable next ) 83 { 84 super( "About " + midlet.getAppProperty( "MIDlet-Name" ) ); 85 if( display == null ) 86 throw new IllegalArgumentException( "parameter 'display' is null" ); 87 if( next == null ) 88 throw new IllegalArgumentException( "parameter 'next' is null" ); 89 display_ = display; 90 next_ = next; 91 appendMessage( midlet.getAppProperty( "MIDlet-Name" ) + " " + midlet.getAppProperty( "MIDlet-Version" ) ); 92 append( createUrlItem( midlet.getAppProperty( "MIDlet-Info-URL" ) ) ); 93 appendMessage( "Icons by the Tango Project" ); 94 append( createUrlItem( "http://tango-project.org" ) ); 95 appendMessage( "Memory" ); 96 freeMemory_ = appendMessage(); 97 usedMemory_ = appendMessage(); 98 totalMemory_ = appendMessage(); 99 refresh(); 100 addCommand( new Command( "Ok", Command.OK, 1 ) ); 101 setCommandListener( this ); 102 } 103 104 /*** 105 * Refresh informations. 106 * 107 * @return this 108 */ 109 public Displayable refresh() 110 { 111 Runtime.getRuntime().gc(); 112 refreshFreeMemory(); 113 refreshUsedMemory(); 114 refreshTotalMemory(); 115 return this; 116 } 117 118 private void refreshFreeMemory() 119 { 120 final String memory = String.valueOf( Runtime.getRuntime().freeMemory() ); 121 freeMemory_.setText( format( memory ) + " bytes free" ); 122 } 123 124 private void refreshUsedMemory() 125 { 126 final String memory = String.valueOf( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() ); 127 usedMemory_.setText( format( memory ) + " bytes used" ); 128 } 129 130 private void refreshTotalMemory() 131 { 132 final String memory = String.valueOf( Runtime.getRuntime().totalMemory() ); 133 totalMemory_.setText( format( memory ) + " bytes total" ); 134 } 135 136 private String format( final String string ) 137 { 138 final StringBuffer buffer = new StringBuffer( string ); 139 for( int i = 0; i < NUMBER_OF_DIGITS - string.length(); ++i ) 140 buffer.insert( 0, ' ' ); 141 return buffer.toString(); 142 } 143 144 private StringItem appendMessage() 145 { 146 final StringItem item = new StringItem( null, null ); 147 item.setLayout( Item.LAYOUT_NEWLINE_BEFORE ); 148 item.setFont( Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ) ); 149 append( item ); 150 return item; 151 } 152 153 private Item createUrlItem( final String url ) 154 { 155 final StringItem item = new StringItem( null, url, Item.HYPERLINK ); 156 item.setLayout( Item.LAYOUT_NEWLINE_BEFORE ); 157 return item; 158 } 159 160 /*** 161 * {@inheritDoc} 162 */ 163 public void commandAction( final Command command, final Displayable displayable ) 164 { 165 display_.setCurrent( next_ ); 166 } 167 }