| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
package palmed.command; |
| 32 |
|
|
| 33 |
|
import java.io.DataInputStream; |
| 34 |
|
import java.io.DataOutputStream; |
| 35 |
|
import java.io.IOException; |
| 36 |
|
import java.io.InputStream; |
| 37 |
|
import java.io.OutputStream; |
| 38 |
|
import javax.microedition.lcdui.Alert; |
| 39 |
|
import javax.microedition.lcdui.AlertType; |
| 40 |
|
import javax.microedition.lcdui.Choice; |
| 41 |
|
import javax.microedition.lcdui.ChoiceGroup; |
| 42 |
|
import javax.microedition.lcdui.Command; |
| 43 |
|
import javax.microedition.lcdui.CommandListener; |
| 44 |
|
import javax.microedition.lcdui.Display; |
| 45 |
|
import javax.microedition.lcdui.Displayable; |
| 46 |
|
import javax.microedition.lcdui.Item; |
| 47 |
|
import javax.microedition.lcdui.Spacer; |
| 48 |
|
import palmed.edit.ITextBox; |
| 49 |
|
import palmed.io.IRecord; |
| 50 |
|
import palmed.io.IRecordFactory; |
| 51 |
|
import palmed.io.ISerializable; |
| 52 |
|
import palmed.ui.Dialog; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
public final class PreferencesDialog extends Dialog implements CommandListener, ISerializable |
| 61 |
|
{ |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
private static final int POPUP_MENUS_SHIFT = 20; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
0 |
private static final String[] LINE_SEPARATORS = |
| 70 |
|
{ |
| 71 |
|
"auto", "Windows (\\r\\n)", "Linux (\\n)", "MacOS (\\r)" |
| 72 |
|
}; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
0 |
private static final String[] FONTS = |
| 77 |
|
{ |
| 78 |
|
"ProFontWindows_12", "ProggyCleanTTSZ_16", "Fixedsys TTF V5.00c_15" |
| 79 |
|
}; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
private final Display display_; |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
private final Displayable next_; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
private final ITextBox textBox_; |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
0 |
private final Command okCommand_ = new Command( "Ok", Command.OK, 1 ); |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
0 |
private final Command cancelCommand_ = new Command( "Cancel", Command.CANCEL, 1 ); |
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
0 |
private final Command revertCommand_ = new Command( "Revert", Command.CANCEL, 1 ); |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
private final Choice separator_; |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
private final Choice font_; |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
private final IRecord record_; |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
public PreferencesDialog( final Display display, final Displayable next, final IRecordFactory factory, |
| 126 |
|
final ITextBox textBox ) |
| 127 |
|
{ |
| 128 |
0 |
super( "Preferences" ); |
| 129 |
0 |
if( display == null ) |
| 130 |
0 |
throw new IllegalArgumentException( "parameter 'display' is null" ); |
| 131 |
0 |
if( next == null ) |
| 132 |
0 |
throw new IllegalArgumentException( "parameter 'next' is null" ); |
| 133 |
0 |
if( textBox == null ) |
| 134 |
0 |
throw new IllegalArgumentException( "parameter 'textBox' is null" ); |
| 135 |
0 |
display_ = display; |
| 136 |
0 |
next_ = next; |
| 137 |
0 |
textBox_ = textBox; |
| 138 |
0 |
separator_ = appendChoice( "Line Separator", LINE_SEPARATORS ); |
| 139 |
0 |
font_ = appendChoice( "Font", FONTS ); |
| 140 |
0 |
record_ = factory.getIndex( this ); |
| 141 |
0 |
addCommand( okCommand_ ); |
| 142 |
0 |
addCommand( cancelCommand_ ); |
| 143 |
0 |
addCommand( revertCommand_ ); |
| 144 |
0 |
setCommandListener( this ); |
| 145 |
0 |
record_.restore(); |
| 146 |
0 |
apply(); |
| 147 |
0 |
} |
| 148 |
|
|
| 149 |
|
private Choice appendChoice( final String label, final String[] choices ) |
| 150 |
|
{ |
| 151 |
0 |
final Spacer spacer = new Spacer( POPUP_MENUS_SHIFT, 0 ); |
| 152 |
0 |
spacer.setLayout( Item.LAYOUT_NEWLINE_BEFORE + Item.LAYOUT_2 ); |
| 153 |
0 |
final ChoiceGroup group = new ChoiceGroup( null, Choice.POPUP ); |
| 154 |
0 |
group.setLayout( Item.LAYOUT_2 ); |
| 155 |
0 |
for( int choice = 0; choice < choices.length; ++choice ) |
| 156 |
0 |
group.append( choices[choice], null ); |
| 157 |
0 |
appendMessage( label ); |
| 158 |
0 |
append( spacer ); |
| 159 |
0 |
append( group ); |
| 160 |
0 |
return group; |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
public void commandAction( final Command command, final Displayable displayable ) |
| 167 |
|
{ |
| 168 |
|
try |
| 169 |
|
{ |
| 170 |
0 |
triggered( command ); |
| 171 |
|
} |
| 172 |
0 |
catch( OutOfMemoryError e ) |
| 173 |
|
{ |
| 174 |
0 |
error( "Out of memory", e.getMessage() ); |
| 175 |
|
} |
| 176 |
0 |
catch( Throwable e ) |
| 177 |
|
{ |
| 178 |
0 |
error( "Error", e.getMessage() ); |
| 179 |
0 |
} |
| 180 |
0 |
} |
| 181 |
|
|
| 182 |
|
private void error( final String title, final String message ) |
| 183 |
|
{ |
| 184 |
0 |
display_.setCurrent( new Alert( title, message, null, AlertType.ERROR ) ); |
| 185 |
0 |
} |
| 186 |
|
|
| 187 |
|
private void triggered( final Command command ) |
| 188 |
|
{ |
| 189 |
0 |
if( command == okCommand_ ) |
| 190 |
|
{ |
| 191 |
0 |
apply(); |
| 192 |
0 |
record_.persist(); |
| 193 |
0 |
display_.setCurrent( next_ ); |
| 194 |
0 |
} |
| 195 |
0 |
else if( command == cancelCommand_ ) |
| 196 |
|
{ |
| 197 |
0 |
record_.restore(); |
| 198 |
0 |
display_.setCurrent( next_ ); |
| 199 |
0 |
} |
| 200 |
|
else |
| 201 |
|
{ |
| 202 |
0 |
record_.restore(); |
| 203 |
|
} |
| 204 |
0 |
} |
| 205 |
|
|
| 206 |
|
private void apply() |
| 207 |
|
{ |
| 208 |
|
String separator; |
| 209 |
0 |
switch( separator_.getSelectedIndex() ) |
| 210 |
|
{ |
| 211 |
|
case 1 : |
| 212 |
0 |
separator = "\r\n"; |
| 213 |
0 |
break; |
| 214 |
|
case 2 : |
| 215 |
0 |
separator = "\n"; |
| 216 |
0 |
break; |
| 217 |
|
case 3 : |
| 218 |
0 |
separator = "\r"; |
| 219 |
0 |
break; |
| 220 |
|
default : |
| 221 |
0 |
separator = null; |
| 222 |
|
} |
| 223 |
0 |
textBox_.setLineSeparator( separator ); |
| 224 |
0 |
textBox_.setFont( FONTS[font_.getSelectedIndex()] ); |
| 225 |
0 |
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
public void unmarshall( final InputStream stream ) throws IOException |
| 231 |
|
{ |
| 232 |
0 |
final DataInputStream input = new DataInputStream( stream ); |
| 233 |
0 |
separator_.setSelectedIndex( input.readInt(), true ); |
| 234 |
0 |
font_.setSelectedIndex( input.readInt(), true ); |
| 235 |
0 |
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
public void marshall( final OutputStream stream ) throws IOException |
| 241 |
|
{ |
| 242 |
0 |
final DataOutputStream output = new DataOutputStream( stream ); |
| 243 |
0 |
output.writeInt( separator_.getSelectedIndex() ); |
| 244 |
0 |
output.writeInt( font_.getSelectedIndex() ); |
| 245 |
0 |
} |
| 246 |
|
} |