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: PalmEd.java 406 2006-01-02 07:37:11Z mat007 $ 29 */ 30 31 package palmed; 32 33 import javax.microedition.lcdui.Alert; 34 import javax.microedition.lcdui.AlertType; 35 import javax.microedition.lcdui.Display; 36 import javax.microedition.midlet.MIDlet; 37 import palmed.buffer.BufferFactory; 38 import palmed.buffer.IBufferFactory; 39 import palmed.buffer.IBufferManager; 40 import palmed.buffer.RecordBufferManager; 41 import palmed.command.AboutCommand; 42 import palmed.command.CloseCommand; 43 import palmed.command.CopyCommand; 44 import palmed.command.CopyPasteManager; 45 import palmed.command.CutCommand; 46 import palmed.command.ExitCommand; 47 import palmed.command.NewCommand; 48 import palmed.command.NextCommand; 49 import palmed.command.OpenCommand; 50 import palmed.command.PasteCommand; 51 import palmed.command.PreferencesCommand; 52 import palmed.command.PreviousCommand; 53 import palmed.command.RedoCommand; 54 import palmed.command.SaveAsCommand; 55 import palmed.command.SaveCommand; 56 import palmed.command.UndoCommand; 57 import palmed.edit.TextBox; 58 import palmed.file.FileBrowser; 59 import palmed.io.RecordException; 60 import palmed.io.RecordFactory; 61 import palmed.ui.ICommand; 62 import palmed.ui.ICommandListener; 63 import palmed.ui.Toolbar; 64 65 /*** 66 * This class implements the application bootstrap. 67 * 68 * @author Mathieu Champlon 69 * @version $Revision: 406 $ $Date: 2006-01-02 16:37:11 +0900 (lun., 02 janv. 2006) $ 70 */ 71 public final class PalmEd extends MIDlet implements ICommandListener 72 { 73 /*** 74 * The display. 75 */ 76 private final Display display_; 77 /*** 78 * The text box. 79 */ 80 private final TextBox textBox_; 81 /*** 82 * The command to execute upon exit. 83 */ 84 private final ICommand exit_; 85 86 /*** 87 * Create the application. 88 */ 89 public PalmEd() 90 { 91 display_ = Display.getDisplay( this ); 92 textBox_ = new TextBox(); 93 final RecordFactory mainFactory = new RecordFactory( "palmed" ); 94 final RecordFactory buffersFactory = new RecordFactory( "palmed.buffers" ); 95 final IBufferFactory factory = new BufferFactory( buffersFactory, textBox_ ); 96 final IBufferManager manager = new RecordBufferManager( factory ); 97 try 98 { 99 exit_ = new ExitCommand( this, manager, mainFactory ); 100 } 101 catch( RecordException e ) 102 { 103 mainFactory.purge(); 104 buffersFactory.purge(); 105 throw new RecordException( "Incompatible buffer format detected : quit and restart", e ); 106 } 107 textBox_.add( createToolbar( manager ) ); 108 } 109 110 private Toolbar createToolbar( final IBufferManager manager ) 111 { 112 final Result result = new Result( display_, textBox_ ); 113 final FileOpenListener openListener = new FileOpenListener( manager, result ); 114 final FileSaveListener saveListener = new FileSaveListener( manager, result ); 115 final FileOverwriteListener overwriteListener = new FileOverwriteListener( display_, textBox_, saveListener ); 116 final FileBrowser fileBrowser = new FileBrowser( display_, textBox_ ); 117 final CopyPasteManager copyPaste = new CopyPasteManager( textBox_ ); 118 final Toolbar toolbar = new Toolbar( this, textBox_ ); 119 toolbar.add( exit_, "/icons/system-log-out.png" ); 120 toolbar.add( new NewCommand( manager ), "/icons/document-new.png" ); 121 toolbar.add( new OpenCommand( display_, fileBrowser, openListener ), "/icons/document-open.png" ); 122 final ICommand saveAsCommand = new SaveAsCommand( display_, fileBrowser, overwriteListener ); 123 toolbar.add( new SaveCommand( manager, saveAsCommand ), "/icons/document-save-as.png" ); 124 toolbar.add( saveAsCommand, "/icons/document-save.png" ); 125 toolbar.add( new CloseCommand( display_, manager, result ), "/icons/process-stop.png" ); 126 toolbar.add( new PreviousCommand( manager ), "/icons/go-previous.png" ); 127 toolbar.add( new NextCommand( manager ), "/icons/go-next.png" ); 128 toolbar.add( new CutCommand( copyPaste ), "/icons/edit-cut.png" ); 129 toolbar.add( new CopyCommand( copyPaste ), "/icons/edit-copy.png" ); 130 toolbar.add( new PasteCommand( copyPaste ), "/icons/edit-paste.png" ); 131 toolbar.add( new UndoCommand( textBox_ ), "icons/edit-undo.png" ); 132 toolbar.add( new RedoCommand( textBox_ ), "icons/edit-redo.png" ); 133 final RecordFactory preferencesFactory = new RecordFactory( "palmed.preferences" ); 134 toolbar.add( new PreferencesCommand( display_, textBox_, preferencesFactory, textBox_ ), 135 "icons/preferences-desktop.png" ); 136 toolbar.add( new AboutCommand( this, display_, textBox_ ), "/icons/help-browser.png" ); 137 return toolbar; 138 } 139 140 /*** 141 * {@inheritDoc} 142 */ 143 public void startApp() 144 { 145 display_.setCurrent( textBox_ ); 146 } 147 148 /*** 149 * {@inheritDoc} 150 */ 151 public void pauseApp() 152 { 153 } 154 155 /*** 156 * {@inheritDoc} 157 */ 158 public void destroyApp( final boolean unconditional ) 159 { 160 triggered( exit_ ); 161 } 162 163 /*** 164 * {@inheritDoc} 165 */ 166 public void triggered( final ICommand command ) 167 { 168 try 169 { 170 command.execute(); 171 } 172 catch( OutOfMemoryError e ) 173 { 174 e.printStackTrace(); 175 error( "Out of memory", e.getMessage() ); 176 } 177 catch( Throwable e ) 178 { 179 e.printStackTrace(); 180 error( "Error", e.getMessage() ); 181 } 182 } 183 184 private void error( final String title, final String message ) 185 { 186 display_.setCurrent( new Alert( title, message, null, AlertType.ERROR ) ); 187 } 188 }