/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; import java.util.*; import javax.microedition.io.*; import javax.microedition.io.file.*; import javax.microedition.lcdui.Displayable; /** * @author User */ public class HelloMIDlet extends MIDlet implements ItemStateListener, CommandListener { private boolean midletPaused = false; //Main screen: private Display display; private Form form; private TextField input; private StringItem text; private Command exit, ok, about; //Select screen: private Display sdisplay; private Form sform; private ChoiceGroup cg; private Command back, sok; private StringItem stext; private int select1; private int select2; //ShowEditor: private Display edisplay; private TextBox tb; private Command quit, save, tab, newline, clear, tools; private boolean HasSaved; private Timer timer; private TimerTask task; private Timer timer2; private TimerTask task2; private int currentLength; //SaveScreen: private Display savedisplay; private Form saveform; private StringItem savetext; private Command yes, no, saveback; //IncludeSaveScreen: private Display isavedisplay; private Form isaveform; private StringItem isavetext; private Command iyes, ino, isaveback; //SaveOutput: private Display errordisplay; private Form errorform; private StringItem errortext; private Command errorback; //IncludeSaveOutput: private Display ierrordisplay; private Form ierrorform; private StringItem ierrortext; private Command ierrorback; //QuickSaveOutput: private Display qsdisplay; private Form qsform; private StringItem qstext; private Command qsback; //ShowTools: private Display tdisplay; private Form tform; private StringItem ttext; private Command tback; //ShowAbout: private Display adisplay; private Form aform; private StringItem atext; private Command aback; //ShowIncludes: private Display idisplay; private Form iform; private StringItem itext; private TextField iinput; private Command iok, iback, includes; //ShowIncludesEditor: private Display i2display; private TextBox itb; private Command isave, iquit, inewline, itab, iclear, iadd; private boolean HasSaved2; private Timer itimer2; private TimerTask itask2; private Timer itimer22; private TimerTask itask22; private int currentLength2; //Others: private String name; private String name2; private String warning; // private Command exitCommand; // /** * The HelloMIDlet constructor. */ public int IsInvalidFileName(String input) { int count = 0; int iCHR92Val = 92; char cCHR92 = (char)iCHR92Val; for(int x=0; x', '_') .replace(':', '_') .replace('"', '_') .replace('/', '_') .replace('|', '_') .replace('?', '_') .replace('*', '_') .replace(' ', '_') .replace(cCHR92, '_') .replace('\n', '_'); input.setString(name); //text changed check String textboxData = tb.getString(); int actualLength = textboxData.length(); if (actualLength > currentLength) { HasSaved = false; } } } private class LengthTimerTask2 extends TimerTask { public final void run() { String textboxData2 = itb.getString(); currentLength2 = textboxData2.length(); } } private class CheckTimerTask2 extends TimerTask { public final void run() { //Valid filename check int iCHR92Val = 92; char cCHR92 = (char)iCHR92Val; String get = iinput.getString(); name2 = get.replace('.', '_') .replace('<', '_') .replace('>', '_') .replace(':', '_') .replace('"', '_') .replace('/', '_') .replace('|', '_') .replace('?', '_') .replace('*', '_') .replace(' ', '_') .replace(cCHR92, '_') .replace('\n', '_'); iinput.setString(name2); //text changed check String textboxData = itb.getString(); int actualLength = textboxData.length(); if (actualLength > currentLength2) { HasSaved2 = false; } } } public void ShowSaveScreen(String text) { savedisplay = Display.getDisplay(this); saveform = new Form("SA:MP Mobile Script Editor - Save"); savetext = new StringItem(null, text); yes = new Command("Yes", Command.OK, 1); no = new Command("No", Command.EXIT, 2); saveback = new Command("Back", Command.ITEM, 3); saveform.append(savetext); saveform.addCommand(yes); saveform.addCommand(no); saveform.addCommand(saveback); saveform.setCommandListener(this); savedisplay.setCurrent(saveform); } public void ShowIncludeSaveScreen(String text) { isavedisplay = Display.getDisplay(this); isaveform = new Form("SA:MP Mobile Script Editor - Save"); isavetext = new StringItem(null, text); iyes = new Command("Yes", Command.OK, 1); ino = new Command("No", Command.EXIT, 2); isaveback = new Command("Back", Command.ITEM, 3); isaveform.append(isavetext); isaveform.addCommand(iyes); isaveform.addCommand(ino); isaveform.addCommand(isaveback); isaveform.setCommandListener(this); isavedisplay.setCurrent(isaveform); } public void ShowTools(String text) { tdisplay = Display.getDisplay(this); tform = new Form("SA:MP Mobile Script Editor - Tools"); ttext = new StringItem(null, text); tback = new Command("Back", Command.OK, 1); tform.append(ttext); tform.addCommand(tback); tform.setCommandListener(this); tdisplay.setCurrent(tform); } public boolean writeFile(String filename, byte[] data) { javax.microedition.io.Connection c = null; java.io.OutputStream os = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c = javax.microedition.io.Connector.open(galleryPath + filename + ".pwn", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc = (javax.microedition.io.file.FileConnection) c; if (!fc.exists()) fc.create(); else fc.truncate(0); os = fc.openOutputStream(); os.write(data); os.flush(); return true; } catch (Exception e) { return false; } finally { try { if (os != null) os.close(); if (c != null) c.close(); } catch (Exception ex) { ex.printStackTrace(); } } } public boolean writeIncludeFile(String filename, byte[] data) { javax.microedition.io.Connection c7 = null; java.io.OutputStream os = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c7 = javax.microedition.io.Connector.open(galleryPath + filename + ".inc", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc8 = (javax.microedition.io.file.FileConnection) c7; if (!fc8.exists()) fc8.create(); else fc8.truncate(0); os = fc8.openOutputStream(); os.write(data); os.flush(); return true; } catch (Exception e) { return false; } finally { try { if (os != null) os.close(); if (c7 != null) c7.close(); } catch (Exception ex) { ex.printStackTrace(); } } } public void itemStateChanged(Item item) { if (item == cg) { // are items checked? if (cg.isSelected(select1) && cg.isSelected(select2)) { // Set all checkboxes to false for (int i = 0; i < cg.size(); i++) cg.setSelectedIndex(i, false); } } } public HelloMIDlet() { } // // // /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the startMIDlet method. */ private void initialize() { // write pre-initialize user code here // write post-initialize user code here } // // /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() { ShowMainScreen(); // write post-action user code here } // // /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() { // write pre-action user code here // write post-action user code here } // // /** * Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) { // write pre-switch user code here Display display = getDisplay(); if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); } // write post-switch user code here } // // /** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) { // write pre-init user code here exitCommand = new Command("Exit", Command.EXIT, 0); // write post-init user code here } return exitCommand; } // public void commandAction(Command command, Displayable s) { if (command == exit) { exitMIDlet(); } else if (command == ok) { name = input.getString(); int length = name.length(); if (IsInvalidFileName(input.getString()) > 0) { text.setText("ERROR: Invalid file name."); } else if(length > 20) { text.setText("ERROR: The name of your project can not be longer than 20 characters."); } else if(length < 1) { text.setText("ERROR: The name of your project must be atleast 1 character long."); } else if(length <= 20) { text.setText(null); javax.microedition.io.Connection c = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c = javax.microedition.io.Connector.open(galleryPath + name + ".pwn", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc = (javax.microedition.io.file.FileConnection) c; if (!fc.exists()) { ShowStyleSelect(); } else { try { FileConnection fc2 = (FileConnection) Connector.open(galleryPath + name + ".pwn"); InputStream is = fc2.openInputStream(); byte b[] = new byte[1048576]; int length2 = is.read(b, 0, 1048576); ShowEditor(""+ new String(b, 0, length2)); } catch (Exception e) { text.setText("ERROR: Failed to load the requested file from the Photos folder (phone memory)."); } } } catch (Exception e) { text.setText("ERROR: File system access failed."); } finally { try { if (c != null) c.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } else if (command == back) { ShowMainScreen(); } else if (command == sok) { boolean get[] = new boolean[cg.size()]; cg.getSelectedFlags(get); for (int i = 0; i < get.length; i++) { if (get[i]) { if(cg.getString(i).equals("Identifiers defined")) { try { String galleryPath = System.getProperty("fileconn.dir.photos"); FileConnection fc2 = (FileConnection) Connector.open(galleryPath + "source.txt"); InputStream is = fc2.openInputStream(); byte b[] = new byte[1048576]; int length2 = is.read(b, 0, 1048576); ShowEditor(""+ new String(b, 0, length2)); } catch (Exception e) { stext.setText("ERROR: Failed to load the source file (source.txt) from the Photos folder (phone memory)."); } } else if(cg.getString(i).equals("Empty")) { ShowEditor(null); } } } } else if (command == quit) { String get = input.getString(); String get2 = tb.getString(); int tbLength = get2.length(); if (tbLength > 0) { name = input.getString(); javax.microedition.io.Connection c2 = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c2 = javax.microedition.io.Connector.open(galleryPath + name + ".pwn", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc3 = (javax.microedition.io.file.FileConnection) c2; if (!fc3.exists()) { ShowSaveScreen("Your project is not saved. Would you like to save it?"); } else { if(HasSaved == false) { ShowSaveScreen("Project modified. Save changes?"); } else if(HasSaved == true) { ShowMainScreen(); HasSaved = false; } } } catch (Exception e) { text.setText("ERROR: File system access failed."); } finally { try { if (c2 != null) c2.close(); } catch (Exception ex) { ex.printStackTrace(); } } } else { ShowMainScreen(); HasSaved = false; } } else if (command == save) { String ReadText = tb.getString(); int length = ReadText.length(); if (length < 1) { ShowSaveOutput("ERROR: Your project must contain atleast 1 character to be saved."); HasSaved = false; } else if (length > 0) { byte [] byteArray = ReadText.getBytes(); writeFile(input.getString(), byteArray); HasSaved = true; if (CountOpeningBrackets(tb.getString()) != CountClosingBrackets(tb.getString())) { warning = '\n' + "* Warning: opening and closing brackets are NOT equal."; } else if (CountOpeningBrackets(tb.getString()) == CountClosingBrackets(tb.getString())) { warning = ""; } ShowSaveOutput("Your project has been saved into the Photos folder (phone memory)." + '\n' + '\n' + "Characters: " + length + '\n' + "Lines: " + CountLines(tb.getString()) + '\n' + "Opening brackets: " + CountOpeningBrackets(tb.getString()) + '\n' + "Closing brackets: " + CountClosingBrackets(tb.getString()) + '\n' + warning); } } else if (command == tab) { String currentText = tb.getString(); String alignment = currentText + "\t"; tb.setString(alignment); HasSaved = false; } else if (command == newline) { String currentText = tb.getString(); String alignment = currentText + "\n"; tb.setString(alignment); HasSaved = false; } else if (command == yes) { String txt = tb.getString(); byte [] bytes = txt.getBytes(); writeFile(input.getString(), bytes); int length = txt.length(); if (CountOpeningBrackets(tb.getString()) != CountClosingBrackets(tb.getString())) { warning = '\n' + "* Warning: opening and closing brackets are NOT equal."; } else if (CountOpeningBrackets(tb.getString()) == CountClosingBrackets(tb.getString())) { warning = ""; } ShowQuickSaveOutput("Your project has been saved into the Photos folder (phone memory)." + '\n' + '\n' + "Characters: " + length + '\n' + "Lines: " + CountLines(tb.getString()) + '\n' + "Opening brackets: " + CountOpeningBrackets(tb.getString()) + '\n' + "Closing brackets: " + CountClosingBrackets(tb.getString()) + '\n' + warning); HasSaved = true; } else if (command == no) { ShowMainScreen(); HasSaved = false; } else if (command == saveback) { ShowEditor(tb.getString()); HasSaved = false; } else if (command == errorback) { ShowEditor(tb.getString()); } else if (command == clear) { tb.setString(null); } else if (command == tools) { try { String galleryPath = System.getProperty("fileconn.dir.photos"); FileConnection fc2 = (FileConnection) Connector.open(galleryPath + "tools.txt"); InputStream is = fc2.openInputStream(); byte b[] = new byte[1048576]; int length2 = is.read(b, 0, 1048576); ShowTools(""+ new String(b, 0, length2)); } catch (Exception e) { ShowTools("ERROR: Failed to load the file (tools.txt) from the Photos folder (phone memory)."); } } else if (command == tback) { ShowEditor(tb.getString()); } else if (command == about) { ShowAbout(); } else if (command == aback) { ShowMainScreen(); } else if (command == qsback) { ShowMainScreen(); } else if (command == includes) { ShowIncludes("If the include does already exist, it will be loaded back in. Otherwise it will create a new include."); } else if (command == iback) { ShowEditor(tb.getString()); } else if (command == iok) { name = input.getString(); int length = name.length(); if (IsInvalidFileName(input.getString()) > 0) { itext.setText("ERROR: Invalid file name."); } else if(length > 20) { itext.setText("ERROR: The name of your project can not be longer than 20 characters."); } else if(length < 1) { itext.setText("ERROR: The name of your project must be atleast 1 character long."); } else if(length <= 20) { itext.setText(null); javax.microedition.io.Connection c3 = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c3 = javax.microedition.io.Connector.open(galleryPath + name2 + ".inc", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc4 = (javax.microedition.io.file.FileConnection) c3; if (!fc4.exists()) { ShowIncludesEditor(null); } else { try { FileConnection fc5 = (FileConnection) Connector.open(galleryPath + name2 + ".inc"); InputStream is = fc5.openInputStream(); byte b[] = new byte[1048576]; int length2 = is.read(b, 0, 1048576); ShowIncludesEditor(""+ new String(b, 0, length2)); } catch (Exception e) { itext.setText("ERROR: Failed to load the requested include from the Photos folder (phone memory)."); } } } catch (Exception e) { itext.setText("ERROR: File system access failed."); } finally { try { if (c3 != null) c3.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } else if (command == iquit) { String get = iinput.getString(); String get2 = itb.getString(); int tbLength = get2.length(); if (tbLength > 0) { name2 = iinput.getString(); javax.microedition.io.Connection c4 = null; try { String galleryPath = System.getProperty("fileconn.dir.photos"); c4 = javax.microedition.io.Connector.open(galleryPath + name2 + ".inc", javax.microedition.io.Connector.READ_WRITE); javax.microedition.io.file.FileConnection fc6 = (javax.microedition.io.file.FileConnection) c4; if (!fc6.exists()) { ShowIncludeSaveScreen("Your include is not saved. Would you like to save it?"); } else { if(HasSaved2 == false) { ShowIncludeSaveScreen("Include modified. Save changes?"); } else if(HasSaved2 == true) { ShowEditor(tb.getString()); HasSaved2 = false; } } } catch (Exception e) { itext.setText("ERROR: File system access failed."); } finally { try { if (c4 != null) c4.close(); } catch (Exception ex) { ex.printStackTrace(); } } } else { ShowMainScreen(); HasSaved2 = false; } } else if (command == isave) { String ReadText = itb.getString(); int length = ReadText.length(); if (length < 1) { ShowIncludeSaveOutput("ERROR: Your include must contain atleast 1 character to be saved."); HasSaved2 = false; } else if (length > 0) { byte [] byteArray = ReadText.getBytes(); writeIncludeFile(iinput.getString(), byteArray); HasSaved2 = true; if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString())) { warning = '\n' + "* Warning: opening and closing brackets are NOT equal."; } else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString())) { warning = ""; } ShowIncludeSaveOutput("Your include has been saved into the Photos folder (phone memory)." + '\n' + '\n' + "Characters: " + length + '\n' + "Lines: " + CountLines(itb.getString()) + '\n' + "Opening brackets: " + CountOpeningBrackets(itb.getString()) + '\n' + "Closing brackets: " + CountClosingBrackets(itb.getString()) + '\n' + warning); } } else if (command == itab) { String currentText = itb.getString(); String alignment = currentText + "\t"; itb.setString(alignment); HasSaved2 = false; } else if (command == inewline) { String currentText = itb.getString(); String alignment = currentText + "\n"; itb.setString(alignment); HasSaved2 = false; } else if (command == iclear) { itb.setString(null); } else if (command == iadd) { String ReadText = itb.getString(); int length = ReadText.length(); if (length < 1) { ShowIncludeSaveOutput("ERROR: Your include must contain atleast 1 character to be saved."); HasSaved2 = false; } else if (length > 0) { byte [] byteArray = ReadText.getBytes(); writeFile(iinput.getString(), byteArray); HasSaved2 = true; if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString())) { warning = '\n' + "* Warning: opening and closing brackets are NOT equal."; } else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString())) { warning = ""; } ShowIncludeSaveOutput("Your include has been saved into the Photos folder (phone memory), and included to your project." + '\n' + '\n' + "Characters: " + length + '\n' + "Lines: " + CountLines(itb.getString()) + '\n' + "Opening brackets: " + CountOpeningBrackets(itb.getString()) + '\n' + "Closing brackets: " + CountClosingBrackets(itb.getString()) + '\n' + warning); String getText = tb.getString(); String alignment = "#include <" + iinput.getString() + ">" + '\n' + getText; tb.setString(alignment); HasSaved = false; HasSaved2 = true; } } else if (command == ierrorback) { ShowIncludesEditor(itb.getString()); } else if (command == iyes) { String txt = itb.getString(); byte [] bytes = txt.getBytes(); writeIncludeFile(iinput.getString(), bytes); ShowEditor(tb.getString()); int length = txt.length(); if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString())) { warning = '\n' + "* Warning: opening and closing brackets are NOT equal."; } else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString())) { warning = ""; } ShowSaveOutput("Your include has been saved into the Photos folder (phone memory)." + '\n' + '\n' + "Characters: " + length + '\n' + "Lines: " + CountLines(itb.getString()) + '\n' + "Opening brackets: " + CountOpeningBrackets(itb.getString()) + '\n' + "Closing brackets: " + CountClosingBrackets(itb.getString()) + '\n' + warning); HasSaved2 = true; } else if (command == ino) { ShowEditor(tb.getString()); HasSaved2 = false; } else if (command == isaveback) { ShowIncludesEditor(itb.getString()); HasSaved2 = false; } } /** * Returns a display instance. * @return the display instance. */ public Display getDisplay () { return Display.getDisplay(this); } /** * Exits MIDlet. */ public void exitMIDlet() { switchDisplayable (null, null); destroyApp(true); notifyDestroyed(); } /** * Called when MIDlet is started. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. */ public void startApp() { if (midletPaused) { resumeMIDlet (); } else { initialize (); startMIDlet (); } midletPaused = false; } /** * Called when MIDlet is paused. */ public void pauseApp() { midletPaused = true; } /** * Called to signal the MIDlet to terminate. * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released. */ public void destroyApp(boolean unconditional) { } }