Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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;
- //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
- private Command exitCommand;
- //</editor-fold>
- /**
- * The HelloMIDlet constructor.
- */
- public int IsInvalidFileName(String input)
- {
- int count = 0;
- int iCHR92Val = 92;
- char cCHR92 = (char)iCHR92Val;
- for(int x=0; x<input.length(); x++)
- {
- if(input.charAt(x) == cCHR92)
- {
- count++;
- }
- }
- return count;
- }
- public int CountLines(String input)
- {
- int count = 1;
- for(int x=0; x<input.length(); x++)
- {
- if(input.charAt(x) == '\n')
- {
- count++;
- }
- }
- return count;
- }
- public int CountOpeningBrackets(String input)
- {
- int count = 0;
- for(int x=0; x<input.length(); x++)
- {
- if(input.charAt(x) == '{')
- {
- count++;
- }
- }
- return count;
- }
- public int CountClosingBrackets(String input)
- {
- int count = 0;
- for(int x=0; x<input.length(); x++)
- {
- if(input.charAt(x) == '}')
- {
- count++;
- }
- }
- return count;
- }
- public void ShowMainScreen()
- {
- form = new Form("SA:MP Mobile Script Editor");
- input = new TextField("Project managment - enter name:", "", 20, TextField.ANY);
- text = new StringItem(null, "Welcome, enter a project name to get started. If your project does already exist in the Photos folder (phone memory), it will be loaded back in.");
- exit = new Command("Exit", Command.EXIT, 1);
- ok = new Command("OK", Command.OK, 2);
- about = new Command("About", Command.ITEM, 3);
- form.addCommand(ok);
- form.addCommand(exit);
- form.addCommand(about);
- form.append(input);
- form.append(text);
- form.setCommandListener(this);
- Display.getDisplay(this).setCurrent(form);
- }
- public void ShowStyleSelect()
- {
- sdisplay = Display.getDisplay(this);
- sform = new Form("SA:MP Mobile Script Editor - Style");
- cg = new ChoiceGroup("Choose a doc. style:",Choice.MULTIPLE);
- back = new Command("Back", Command.EXIT, 1);
- sok = new Command("Continue", Command.OK, 2);
- stext = new StringItem(null, "Identifiers defined: main functions defined" + '\n' + "Empty: no main function defines");
- select1 = cg.append("Identifiers defined",null);
- select2 = cg.append("Empty",null);
- sform.append(cg);
- sform.append(stext);
- sform.addCommand(back);
- sform.addCommand(sok);
- sform.setCommandListener(this);
- sform.setItemStateListener(this);
- sdisplay.setCurrent(sform);
- }
- public void ShowEditor(String data)
- {
- edisplay = Display.getDisplay(this);
- tb = new TextBox("Input:", data, 50000, TextField.ANY);
- quit = new Command("Back", Command.EXIT, 1);
- save = new Command("Save", Command.OK, 2);
- tab = new Command("Tab", Command.ITEM, 3);
- newline = new Command("New line", Command.ITEM, 4);
- clear = new Command("Clear input", Command.ITEM, 5);
- tools = new Command("Tools", Command.ITEM, 6);
- includes = new Command("Includes", Command.ITEM, 7);
- tb.addCommand(tab);
- tb.addCommand(newline);
- tb.addCommand(tools);
- tb.addCommand(includes);
- tb.addCommand(clear);
- tb.addCommand(quit);
- tb.addCommand(save);
- tb.setCommandListener(this);
- edisplay.setCurrent(tb);
- timer = new Timer();
- task = new CheckTimerTask();
- timer.schedule(task,100);
- timer2 = new Timer();
- task2 = new LengthTimerTask();
- timer2.schedule(task2, 200);
- }
- public void ShowSaveOutput(String text)
- {
- errordisplay = Display.getDisplay(this);
- errorform = new Form("SA:MP Mobile Script Editor - Output");
- errortext = new StringItem(null, text);
- errorback = new Command("Back", Command.OK, 1);
- errorform.append(errortext);
- errorform.addCommand(errorback);
- errorform.setCommandListener(this);
- errordisplay.setCurrent(errorform);
- }
- public void ShowIncludeSaveOutput(String text)
- {
- ierrordisplay = Display.getDisplay(this);
- ierrorform = new Form("SA:MP Mobile Script Editor - Output");
- ierrortext = new StringItem(null, text);
- ierrorback = new Command("Back", Command.OK, 1);
- ierrorform.append(ierrortext);
- ierrorform.addCommand(ierrorback);
- ierrorform.setCommandListener(this);
- ierrordisplay.setCurrent(ierrorform);
- }
- public void ShowQuickSaveOutput(String text)
- {
- qsdisplay = Display.getDisplay(this);
- qsform = new Form("SA:MP Mobile Script Editor - Output");
- qstext = new StringItem(null, text);
- qsback = new Command("Back", Command.OK, 1);
- qsform.append(qstext);
- qsform.addCommand(qsback);
- qsform.setCommandListener(this);
- qsdisplay.setCurrent(qsform);
- }
- public void ShowAbout()
- {
- adisplay = Display.getDisplay(this);
- aform = new Form("SA:MP Mobile Script Editor - About");
- atext = new StringItem(null, "SA:MP Mobile Script Editor" + '\n' + '\n' + "Free .pwn reader/writer by bpeterson." + '\n' + '\n' + "Version: 1.1" + '\n' + '\n' + "Copyright 2010");
- aback = new Command("Back", Command.OK, 1);
- aform.append(atext);
- aform.addCommand(aback);
- aform.setCommandListener(this);
- adisplay.setCurrent(aform);
- }
- public void ShowIncludes(String text)
- {
- idisplay = Display.getDisplay(this);
- iform = new Form("SA:MP Mobile Script Editor - Includes");
- iinput = new TextField("Include managment - enter the include's name without extension:", "", 30, TextField.ANY);
- iok = new Command("OK", Command.OK, 1);
- iback = new Command("Back", Command.BACK, 2);
- itext = new StringItem(null, text);
- iform.append(iinput);
- iform.append(itext);
- iform.addCommand(iok);
- iform.addCommand(iback);
- iform.setCommandListener(this);
- idisplay.setCurrent(iform);
- }
- public void ShowIncludesEditor(String data)
- {
- i2display = Display.getDisplay(this);
- itb = new TextBox("Input:", data, 50000, TextField.ANY);
- iquit = new Command("Back", Command.EXIT, 1);
- iadd = new Command("Add", Command.OK, 2);
- itab = new Command("Tab", Command.ITEM, 3);
- inewline = new Command("New line", Command.ITEM, 4);
- iclear = new Command("Clear input", Command.ITEM, 5);
- isave = new Command("Save", Command.ITEM, 6);
- itb.addCommand(isave);
- itb.addCommand(itab);
- itb.addCommand(inewline);
- itb.addCommand(iclear);
- itb.addCommand(iquit);
- itb.addCommand(iadd);
- itb.setCommandListener(this);
- i2display.setCurrent(itb);
- itimer2 = new Timer();
- itask2 = new CheckTimerTask2();
- itimer2.schedule(itask2,100);
- itimer22 = new Timer();
- itask22 = new LengthTimerTask2();
- itimer22.schedule(itask22, 200);
- }
- private class LengthTimerTask extends TimerTask
- {
- public final void run()
- {
- String textboxData = tb.getString();
- currentLength = textboxData.length();
- }
- }
- private class CheckTimerTask extends TimerTask
- {
- public final void run()
- {
- //Valid filename check
- int iCHR92Val = 92;
- char cCHR92 = (char)iCHR92Val;
- String get = input.getString();
- name = get.replace('.', '_') .replace('<', '_') .replace('>', '_') .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() {
- }
- //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
- /**
- * Initilizes the application.
- * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
- */
- private void initialize() {
- // write pre-initialize user code here
- // write post-initialize user code here
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
- /**
- * Performs an action assigned to the Mobile Device - MIDlet Started point.
- */
- public void startMIDlet() {
- ShowMainScreen();
- // write post-action user code here
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
- /**
- * 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
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
- /**
- * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> 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 <code>null</code>, then <code>nextDisplayable</code> 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
- }
- //</editor-fold>
- //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">
- /**
- * 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;
- }
- //</editor-fold>
- 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) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement