/* Licence: williamsmobile.co.uk/licence.htm */ package mypackage; import net.rim.device.api.system.KeypadListener; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import java.io.*; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; class TextPad extends UiApplication { public static void main(String[] args) { new TextPad().enterEventDispatcher(); } TextPad() { pushScreen(new MyScreen()); } } class MyScreen extends MainScreen { EditField textField; Font customFont; int tabWidth = 4; String path = ""; MyScreen() { setTitle("TextPad 1.0 - williamsmobile.co.uk"); addMenuItem(new MenuItem("Save", 0, 0) { public void run() { save(); } }); addMenuItem(new MenuItem("Save As", 0, 0) { public void run() { saveAs(); } }); addMenuItem(new MenuItem("Open", 0, 0) { public void run() { open(); } }); addMenuItem(new MenuItem("Tab Width", 0, 0) { public void run() { createTabDlg(); } }); textField = new EditField(); customFont = createFont(); textField.setFont(customFont); add(textField); } private void createTabDlg() { int selectedTabWidth = Dialog.ask("Set width of tab inserted by Alt+Space:", new String[]{"2", "4", "8"}, 1); setTabWidth(Integer.parseInt(new String[]{"2", "4", "8"}[selectedTabWidth])); } private Font createFont() { Font customFont = null; try { FontFamily fontFamily = FontFamily.forName("Andale Mono"); customFont = fontFamily.getFont(Font.PLAIN, 18); } catch (ClassNotFoundException e) { e.printStackTrace(); } return customFont; } public boolean keyChar(char key, int status, int time) { if ((status & KeypadListener.STATUS_ALT) != 0 && key == ' ') { String currentText = textField.getText(); StringBuffer newText = new StringBuffer(currentText); for (int i = 0; i < tabWidth; i++) { newText.append(' '); } textField.setText(newText.toString()); return true; } return super.keyChar(key, status, time); } public void saveAs() { Dialog saveDlg = new Dialog(Dialog.OK, "Save", 0, null, 0); EditField nameField = new EditField("Name: ", ""); EditField savePathField = new EditField("Path: ", "file:///SDCard/BlackBerry/documents/"); ((DialogFieldManager) saveDlg.getDelegate()).getCustomManager().add(nameField); ((DialogFieldManager) saveDlg.getDelegate()).getCustomManager().add(savePathField); saveDlg.doModal(); path = savePathField.getText() + nameField.getText(); save(); } public void save() { if (path.equals("")) { Dialog.alert("Use 'Save As' or 'Open' first"); } else { FileConnection fileConnection = null; OutputStream outputStream = null; String text = textField.getText(); try { fileConnection = (FileConnection) Connector.open(path, Connector.READ_WRITE); if (!fileConnection.exists()) { fileConnection.create(); } else { fileConnection.truncate(0); } outputStream = fileConnection.openOutputStream(); outputStream.write(text.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (outputStream != null) outputStream.close(); if (fileConnection != null) fileConnection.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void open() { Dialog openDlg = new Dialog(Dialog.OK, "Open", 0, null, 0); EditField openPathField = new EditField("Path: ", "file:///SDCard/BlackBerry/documents/"); ((DialogFieldManager) openDlg.getDelegate()).getCustomManager().add(openPathField); openDlg.doModal(); path = openPathField.getText(); FileConnection fileConnection = null; InputStream inputStream = null; try { fileConnection = (FileConnection) Connector.open(path, Connector.READ); if (fileConnection.exists()) { inputStream = fileConnection.openInputStream(); byte[] data = new byte[(int) fileConnection.fileSize()]; inputStream.read(data); String fileContents = new String(data); textField.setText(fileContents); } else { Dialog.alert("File does not exist"); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); if (fileConnection != null) fileConnection.close(); } catch (IOException e) { e.printStackTrace(); } } } public void setTabWidth(int newTabWidth) { tabWidth = newTabWidth; } public boolean onSavePrompt() { return true; } }