Advertisement
Guest User

bpeterson

a guest
Jun 3rd, 2010
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 41.33 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package hello;
  7.  
  8.  
  9. import javax.microedition.midlet.*;
  10. import javax.microedition.lcdui.*;
  11. import java.io.*;
  12. import java.util.*;
  13. import javax.microedition.io.*;
  14. import javax.microedition.io.file.*;
  15. import javax.microedition.lcdui.Displayable;
  16.  
  17.  
  18.  
  19.  
  20. /**
  21.  * @author User
  22.  */
  23. public class HelloMIDlet extends MIDlet implements ItemStateListener, CommandListener {
  24.    
  25.     private boolean midletPaused = false;
  26.  
  27.     //Main screen:
  28.  
  29.     private Display display;
  30.     private Form form;
  31.     private TextField input;
  32.     private StringItem text;
  33.     private Command exit, ok, about;
  34.  
  35.     //Select screen:
  36.  
  37.     private Display sdisplay;
  38.     private Form sform;
  39.     private ChoiceGroup cg;
  40.     private Command back, sok;
  41.     private StringItem stext;
  42.     private int select1;
  43.     private int select2;
  44.  
  45.     //ShowEditor:
  46.  
  47.     private Display edisplay;
  48.     private TextBox tb;
  49.     private Command quit, save, tab, newline, clear, tools;
  50.     private boolean HasSaved;
  51.     private Timer timer;
  52.     private TimerTask task;
  53.     private Timer timer2;
  54.     private TimerTask task2;
  55.     private int currentLength;
  56.  
  57.     //SaveScreen:
  58.  
  59.     private Display savedisplay;
  60.     private Form saveform;
  61.     private StringItem savetext;
  62.     private Command yes, no, saveback;
  63.  
  64.     //IncludeSaveScreen:
  65.  
  66.     private Display isavedisplay;
  67.     private Form isaveform;
  68.     private StringItem isavetext;
  69.     private Command iyes, ino, isaveback;
  70.  
  71.     //SaveOutput:
  72.  
  73.     private Display errordisplay;
  74.     private Form errorform;
  75.     private StringItem errortext;
  76.     private Command errorback;
  77.  
  78.     //IncludeSaveOutput:
  79.  
  80.     private Display ierrordisplay;
  81.     private Form ierrorform;
  82.     private StringItem ierrortext;
  83.     private Command ierrorback;
  84.  
  85.     //QuickSaveOutput:
  86.  
  87.     private Display qsdisplay;
  88.     private Form qsform;
  89.     private StringItem qstext;
  90.     private Command qsback;
  91.  
  92.     //ShowTools:
  93.  
  94.     private Display tdisplay;
  95.     private Form tform;
  96.     private StringItem ttext;
  97.     private Command tback;
  98.  
  99.     //ShowAbout:
  100.    
  101.     private Display adisplay;
  102.     private Form aform;
  103.     private StringItem atext;
  104.     private Command aback;
  105.  
  106.     //ShowIncludes:
  107.  
  108.     private Display idisplay;
  109.     private Form iform;
  110.     private StringItem itext;
  111.     private TextField iinput;
  112.     private Command iok, iback, includes;
  113.  
  114.     //ShowIncludesEditor:
  115.  
  116.     private Display i2display;
  117.     private TextBox itb;
  118.     private Command isave, iquit, inewline, itab, iclear, iadd;
  119.     private boolean HasSaved2;
  120.     private Timer itimer2;
  121.     private TimerTask itask2;
  122.     private Timer itimer22;
  123.     private TimerTask itask22;
  124.     private int currentLength2;
  125.  
  126.     //Others:
  127.  
  128.     private String name;
  129.     private String name2;
  130.     private String warning;
  131.  
  132.     //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">                      
  133.     private Command exitCommand;
  134.     //</editor-fold>                    
  135.  
  136.     /**
  137.      * The HelloMIDlet constructor.
  138.      */
  139.  
  140.     public int IsInvalidFileName(String input)
  141.     {
  142.         int count = 0;
  143.         int iCHR92Val = 92;
  144.         char cCHR92 = (char)iCHR92Val;
  145.         for(int x=0; x<input.length(); x++)
  146.         {
  147.             if(input.charAt(x) == cCHR92)
  148.             {
  149.                 count++;
  150.             }
  151.         }
  152.         return count;
  153.     }
  154.  
  155.     public int CountLines(String input)
  156.     {
  157.         int count = 1;
  158.         for(int x=0; x<input.length(); x++)
  159.         {
  160.             if(input.charAt(x) == '\n')
  161.             {
  162.                 count++;
  163.             }
  164.         }
  165.         return count;
  166.     }
  167.  
  168.     public int CountOpeningBrackets(String input)
  169.     {
  170.         int count = 0;
  171.         for(int x=0; x<input.length(); x++)
  172.         {
  173.             if(input.charAt(x) == '{')
  174.             {
  175.                 count++;
  176.             }
  177.         }
  178.         return count;
  179.     }
  180.  
  181.     public int CountClosingBrackets(String input)
  182.     {
  183.         int count = 0;
  184.         for(int x=0; x<input.length(); x++)
  185.         {
  186.             if(input.charAt(x) == '}')
  187.             {
  188.                 count++;
  189.             }
  190.         }
  191.         return count;
  192.     }
  193.  
  194.     public void ShowMainScreen()
  195.     {
  196.        
  197.         form = new Form("SA:MP Mobile Script Editor");
  198.         input = new TextField("Project managment - enter name:", "", 20, TextField.ANY);
  199.         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.");
  200.         exit = new Command("Exit", Command.EXIT, 1);
  201.         ok = new Command("OK", Command.OK, 2);
  202.         about = new Command("About", Command.ITEM, 3);
  203.         form.addCommand(ok);
  204.         form.addCommand(exit);
  205.         form.addCommand(about);
  206.         form.append(input);
  207.         form.append(text);
  208.         form.setCommandListener(this);
  209.         Display.getDisplay(this).setCurrent(form);
  210.     }
  211.  
  212.     public void ShowStyleSelect()
  213.     {
  214.         sdisplay = Display.getDisplay(this);
  215.         sform = new Form("SA:MP Mobile Script Editor - Style");
  216.         cg = new ChoiceGroup("Choose a doc. style:",Choice.MULTIPLE);
  217.         back = new Command("Back", Command.EXIT, 1);
  218.         sok = new Command("Continue", Command.OK, 2);
  219.         stext = new StringItem(null, "Identifiers defined: main functions defined" + '\n' + "Empty: no main function defines");
  220.         select1 = cg.append("Identifiers defined",null);
  221.         select2 = cg.append("Empty",null);
  222.         sform.append(cg);
  223.         sform.append(stext);
  224.         sform.addCommand(back);
  225.         sform.addCommand(sok);
  226.         sform.setCommandListener(this);
  227.         sform.setItemStateListener(this);
  228.         sdisplay.setCurrent(sform);
  229.     }
  230.  
  231.     public void ShowEditor(String data)
  232.     {
  233.         edisplay = Display.getDisplay(this);
  234.         tb = new TextBox("Input:", data, 50000, TextField.ANY);
  235.         quit = new Command("Back", Command.EXIT, 1);
  236.         save = new Command("Save", Command.OK, 2);
  237.         tab = new Command("Tab", Command.ITEM, 3);
  238.         newline = new Command("New line", Command.ITEM, 4);
  239.         clear = new Command("Clear input", Command.ITEM, 5);
  240.         tools = new Command("Tools", Command.ITEM, 6);
  241.         includes = new Command("Includes", Command.ITEM, 7);
  242.         tb.addCommand(tab);
  243.         tb.addCommand(newline);
  244.         tb.addCommand(tools);
  245.         tb.addCommand(includes);
  246.         tb.addCommand(clear);
  247.         tb.addCommand(quit);
  248.         tb.addCommand(save);
  249.         tb.setCommandListener(this);
  250.         edisplay.setCurrent(tb);
  251.         timer = new Timer();
  252.         task = new CheckTimerTask();
  253.         timer.schedule(task,100);
  254.         timer2 = new Timer();
  255.         task2 = new LengthTimerTask();
  256.         timer2.schedule(task2, 200);
  257.     }
  258.  
  259.     public void ShowSaveOutput(String text)
  260.     {
  261.         errordisplay = Display.getDisplay(this);
  262.         errorform = new Form("SA:MP Mobile Script Editor - Output");
  263.         errortext = new StringItem(null, text);
  264.         errorback = new Command("Back", Command.OK, 1);
  265.         errorform.append(errortext);
  266.         errorform.addCommand(errorback);
  267.         errorform.setCommandListener(this);
  268.         errordisplay.setCurrent(errorform);
  269.  
  270.     }
  271.  
  272.     public void ShowIncludeSaveOutput(String text)
  273.     {
  274.         ierrordisplay = Display.getDisplay(this);
  275.         ierrorform = new Form("SA:MP Mobile Script Editor - Output");
  276.         ierrortext = new StringItem(null, text);
  277.         ierrorback = new Command("Back", Command.OK, 1);
  278.         ierrorform.append(ierrortext);
  279.         ierrorform.addCommand(ierrorback);
  280.         ierrorform.setCommandListener(this);
  281.         ierrordisplay.setCurrent(ierrorform);
  282.  
  283.     }
  284.  
  285.     public void ShowQuickSaveOutput(String text)
  286.     {
  287.         qsdisplay = Display.getDisplay(this);
  288.         qsform = new Form("SA:MP Mobile Script Editor - Output");
  289.         qstext = new StringItem(null, text);
  290.         qsback = new Command("Back", Command.OK, 1);
  291.         qsform.append(qstext);
  292.         qsform.addCommand(qsback);
  293.         qsform.setCommandListener(this);
  294.         qsdisplay.setCurrent(qsform);
  295.  
  296.     }
  297.  
  298.     public void ShowAbout()
  299.     {
  300.         adisplay = Display.getDisplay(this);
  301.         aform = new Form("SA:MP Mobile Script Editor - About");
  302.         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");
  303.         aback = new Command("Back", Command.OK, 1);
  304.         aform.append(atext);
  305.         aform.addCommand(aback);
  306.         aform.setCommandListener(this);
  307.         adisplay.setCurrent(aform);
  308.     }
  309.  
  310.     public void ShowIncludes(String text)
  311.     {
  312.         idisplay = Display.getDisplay(this);
  313.         iform = new Form("SA:MP Mobile Script Editor - Includes");
  314.         iinput = new TextField("Include managment - enter the include's name without extension:", "", 30, TextField.ANY);
  315.         iok = new Command("OK", Command.OK, 1);
  316.         iback = new Command("Back", Command.BACK, 2);
  317.         itext = new StringItem(null, text);
  318.         iform.append(iinput);
  319.         iform.append(itext);
  320.         iform.addCommand(iok);
  321.         iform.addCommand(iback);
  322.         iform.setCommandListener(this);
  323.         idisplay.setCurrent(iform);
  324.     }
  325.  
  326.     public void ShowIncludesEditor(String data)
  327.     {
  328.         i2display = Display.getDisplay(this);
  329.         itb = new TextBox("Input:", data, 50000, TextField.ANY);
  330.         iquit = new Command("Back", Command.EXIT, 1);
  331.         iadd = new Command("Add", Command.OK, 2);
  332.         itab = new Command("Tab", Command.ITEM, 3);
  333.         inewline = new Command("New line", Command.ITEM, 4);
  334.         iclear = new Command("Clear input", Command.ITEM, 5);
  335.         isave = new Command("Save", Command.ITEM, 6);
  336.         itb.addCommand(isave);
  337.         itb.addCommand(itab);
  338.         itb.addCommand(inewline);
  339.         itb.addCommand(iclear);
  340.         itb.addCommand(iquit);
  341.         itb.addCommand(iadd);
  342.         itb.setCommandListener(this);
  343.         i2display.setCurrent(itb);
  344.         itimer2 = new Timer();
  345.         itask2 = new CheckTimerTask2();
  346.         itimer2.schedule(itask2,100);
  347.         itimer22 = new Timer();
  348.         itask22 = new LengthTimerTask2();
  349.         itimer22.schedule(itask22, 200);
  350.     }
  351.  
  352.     private class LengthTimerTask extends TimerTask
  353.      {
  354.         public final void run()
  355.         {
  356.             String textboxData = tb.getString();
  357.             currentLength = textboxData.length();
  358.         }
  359.     }
  360.  
  361.      private class CheckTimerTask extends TimerTask
  362.      {
  363.         public final void run()
  364.         {
  365.             //Valid filename check
  366.             int iCHR92Val = 92;
  367.             char cCHR92 = (char)iCHR92Val;
  368.             String get = input.getString();
  369.             name = get.replace('.', '_') .replace('<', '_') .replace('>', '_') .replace(':', '_') .replace('"', '_') .replace('/', '_') .replace('|', '_')
  370.             .replace('?', '_') .replace('*', '_') .replace(' ', '_') .replace(cCHR92, '_') .replace('\n', '_');
  371.             input.setString(name);
  372.  
  373.             //text changed check
  374.             String textboxData = tb.getString();
  375.             int actualLength = textboxData.length();
  376.             if (actualLength > currentLength)
  377.             {
  378.                 HasSaved = false;
  379.             }
  380.         }
  381.     }
  382.  
  383.      private class LengthTimerTask2 extends TimerTask
  384.      {
  385.         public final void run()
  386.         {
  387.             String textboxData2 = itb.getString();
  388.             currentLength2 = textboxData2.length();
  389.         }
  390.     }
  391.  
  392.      private class CheckTimerTask2 extends TimerTask
  393.      {
  394.         public final void run()
  395.         {
  396.             //Valid filename check
  397.             int iCHR92Val = 92;
  398.             char cCHR92 = (char)iCHR92Val;
  399.             String get = iinput.getString();
  400.             name2 = get.replace('.', '_') .replace('<', '_') .replace('>', '_') .replace(':', '_') .replace('"', '_') .replace('/', '_') .replace('|', '_')
  401.             .replace('?', '_') .replace('*', '_') .replace(' ', '_') .replace(cCHR92, '_') .replace('\n', '_');
  402.             iinput.setString(name2);
  403.  
  404.             //text changed check
  405.             String textboxData = itb.getString();
  406.             int actualLength = textboxData.length();
  407.             if (actualLength > currentLength2)
  408.             {
  409.                 HasSaved2 = false;
  410.             }
  411.         }
  412.     }
  413.  
  414.     public void ShowSaveScreen(String text)
  415.     {
  416.         savedisplay = Display.getDisplay(this);
  417.         saveform = new Form("SA:MP Mobile Script Editor - Save");
  418.         savetext = new StringItem(null, text);
  419.         yes = new Command("Yes", Command.OK, 1);
  420.         no = new Command("No", Command.EXIT, 2);
  421.         saveback = new Command("Back", Command.ITEM, 3);
  422.         saveform.append(savetext);
  423.         saveform.addCommand(yes);
  424.         saveform.addCommand(no);
  425.         saveform.addCommand(saveback);
  426.         saveform.setCommandListener(this);
  427.         savedisplay.setCurrent(saveform);
  428.     }
  429.  
  430.     public void ShowIncludeSaveScreen(String text)
  431.     {
  432.         isavedisplay = Display.getDisplay(this);
  433.         isaveform = new Form("SA:MP Mobile Script Editor - Save");
  434.         isavetext = new StringItem(null, text);
  435.         iyes = new Command("Yes", Command.OK, 1);
  436.         ino = new Command("No", Command.EXIT, 2);
  437.         isaveback = new Command("Back", Command.ITEM, 3);
  438.         isaveform.append(isavetext);
  439.         isaveform.addCommand(iyes);
  440.         isaveform.addCommand(ino);
  441.         isaveform.addCommand(isaveback);
  442.         isaveform.setCommandListener(this);
  443.         isavedisplay.setCurrent(isaveform);
  444.     }
  445.  
  446.     public void ShowTools(String text)
  447.     {
  448.         tdisplay = Display.getDisplay(this);
  449.         tform = new Form("SA:MP Mobile Script Editor - Tools");
  450.         ttext = new StringItem(null, text);
  451.         tback = new Command("Back", Command.OK, 1);
  452.         tform.append(ttext);
  453.         tform.addCommand(tback);
  454.         tform.setCommandListener(this);
  455.         tdisplay.setCurrent(tform);
  456.     }
  457.  
  458.     public boolean writeFile(String filename, byte[] data)
  459.     {
  460.         javax.microedition.io.Connection c = null;
  461.         java.io.OutputStream os = null;
  462.         try {
  463.             String galleryPath = System.getProperty("fileconn.dir.photos");
  464.             c = javax.microedition.io.Connector.open(galleryPath + filename + ".pwn", javax.microedition.io.Connector.READ_WRITE);
  465.             javax.microedition.io.file.FileConnection fc =
  466.                     (javax.microedition.io.file.FileConnection) c;
  467.             if (!fc.exists())
  468.                 fc.create();
  469.             else
  470.                 fc.truncate(0);
  471.             os = fc.openOutputStream();
  472.             os.write(data);
  473.             os.flush();
  474.             return true;
  475.         } catch (Exception e) {
  476.             return false;
  477.         } finally {
  478.             try {
  479.                 if (os != null)
  480.                     os.close();
  481.                 if (c != null)
  482.                     c.close();
  483.             } catch (Exception ex) {
  484.                 ex.printStackTrace();
  485.             }
  486.         }
  487.     }
  488.  
  489.     public boolean writeIncludeFile(String filename, byte[] data)
  490.     {
  491.         javax.microedition.io.Connection c7 = null;
  492.         java.io.OutputStream os = null;
  493.         try {
  494.             String galleryPath = System.getProperty("fileconn.dir.photos");
  495.             c7 = javax.microedition.io.Connector.open(galleryPath + filename + ".inc", javax.microedition.io.Connector.READ_WRITE);
  496.             javax.microedition.io.file.FileConnection fc8 =
  497.                     (javax.microedition.io.file.FileConnection) c7;
  498.             if (!fc8.exists())
  499.                 fc8.create();
  500.             else
  501.                 fc8.truncate(0);
  502.             os = fc8.openOutputStream();
  503.             os.write(data);
  504.             os.flush();
  505.             return true;
  506.         } catch (Exception e) {
  507.             return false;
  508.         } finally {
  509.             try {
  510.                 if (os != null)
  511.                     os.close();
  512.                 if (c7 != null)
  513.                     c7.close();
  514.             } catch (Exception ex) {
  515.                 ex.printStackTrace();
  516.             }
  517.         }
  518.     }
  519.  
  520.  
  521.  
  522.  
  523.  
  524.     public void itemStateChanged(Item item)
  525.     {
  526.         if (item == cg)
  527.         {
  528.             // are items checked?
  529.             if (cg.isSelected(select1) && cg.isSelected(select2))
  530.             {
  531.                 // Set all checkboxes to false
  532.                 for (int i = 0; i < cg.size(); i++)
  533.                 cg.setSelectedIndex(i, false);
  534.             }
  535.         }
  536.     }
  537.  
  538.     public HelloMIDlet() {
  539.     }
  540.  
  541.     //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">                      
  542.     //</editor-fold>                    
  543.  
  544.     //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">                                          
  545.     /**
  546.      * Initilizes the application.
  547.      * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
  548.      */
  549.     private void initialize() {                                        
  550.         // write pre-initialize user code here
  551.                                            
  552.         // write post-initialize user code here
  553.     }                            
  554.     //</editor-fold>                          
  555.  
  556.     //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">                                        
  557.     /**
  558.      * Performs an action assigned to the Mobile Device - MIDlet Started point.
  559.      */
  560.     public void startMIDlet() {                                      
  561.         ShowMainScreen();
  562.                                        
  563.         // write post-action user code here
  564.     }                            
  565.     //</editor-fold>                          
  566.  
  567.     //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">                                        
  568.     /**
  569.      * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
  570.      */
  571.     public void resumeMIDlet() {                                      
  572.         // write pre-action user code here
  573.                                          
  574.         // write post-action user code here
  575.     }                              
  576.     //</editor-fold>                            
  577.  
  578.     //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">                                              
  579.     /**
  580.      * 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.
  581.      * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
  582.      * @param nextDisplayable the Displayable to be set
  583.      */
  584.     public void switchDisplayable(Alert alert, Displayable nextDisplayable) {                                            
  585.         // write pre-switch user code here
  586.         Display display = getDisplay();                                              
  587.         if (alert == null) {
  588.             display.setCurrent(nextDisplayable);
  589.         } else {
  590.             display.setCurrent(alert, nextDisplayable);
  591.         }                                            
  592.         // write post-switch user code here
  593.     }                                  
  594.     //</editor-fold>                                
  595.  
  596.  
  597.  
  598.     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">                                  
  599.     /**
  600.      * Returns an initiliazed instance of exitCommand component.
  601.      * @return the initialized component instance
  602.      */
  603.     public Command getExitCommand() {
  604.         if (exitCommand == null) {                                
  605.             // write pre-init user code here
  606.             exitCommand = new Command("Exit", Command.EXIT, 0);                                  
  607.             // write post-init user code here
  608.         }                        
  609.         return exitCommand;
  610.     }
  611.     //</editor-fold>                      
  612.  
  613.     public void commandAction(Command command, Displayable s)
  614.     {
  615.         if (command == exit)
  616.         {
  617.             exitMIDlet();
  618.         }
  619.         else if (command == ok)
  620.         {
  621.             name = input.getString();
  622.             int length = name.length();
  623.             if (IsInvalidFileName(input.getString()) > 0)
  624.             {
  625.                 text.setText("ERROR: Invalid file name.");
  626.             }
  627.             else if(length > 20)
  628.             {
  629.                 text.setText("ERROR: The name of your project can not be longer than 20 characters.");
  630.             }
  631.             else if(length < 1)
  632.             {
  633.                 text.setText("ERROR: The name of your project must be atleast 1 character long.");
  634.             }
  635.             else if(length <= 20)
  636.             {
  637.                 text.setText(null);
  638.                 javax.microedition.io.Connection c = null;
  639.                 try
  640.                 {
  641.                     String galleryPath = System.getProperty("fileconn.dir.photos");
  642.                     c = javax.microedition.io.Connector.open(galleryPath + name + ".pwn", javax.microedition.io.Connector.READ_WRITE);
  643.                     javax.microedition.io.file.FileConnection fc =
  644.                     (javax.microedition.io.file.FileConnection) c;
  645.                     if (!fc.exists())
  646.                     {
  647.                         ShowStyleSelect();
  648.                     }
  649.                     else
  650.                     {
  651.                         try
  652.                         {
  653.                             FileConnection fc2 = (FileConnection)
  654.                             Connector.open(galleryPath + name + ".pwn");
  655.                             InputStream is = fc2.openInputStream();
  656.                             byte b[] = new byte[1048576];
  657.                             int length2 = is.read(b, 0, 1048576);
  658.                             ShowEditor(""+ new String(b, 0, length2));
  659.                          }
  660.                          catch (Exception e)
  661.                          {
  662.                              text.setText("ERROR: Failed to load the requested file from the Photos folder (phone memory).");
  663.                          }
  664.                      }
  665.                 }
  666.                 catch (Exception e)
  667.                 {
  668.                     text.setText("ERROR: File system access failed.");
  669.                 }
  670.                 finally
  671.                 {
  672.                     try
  673.                     {
  674.                         if (c != null)
  675.                             c.close();
  676.                         }
  677.                         catch (Exception ex)
  678.                         {
  679.                             ex.printStackTrace();
  680.                         }
  681.                     }
  682.                 }
  683.         }
  684.         else if (command == back)
  685.         {
  686.             ShowMainScreen();
  687.         }
  688.         else if (command == sok)
  689.         {
  690.             boolean get[] = new boolean[cg.size()];
  691.             cg.getSelectedFlags(get);
  692.             for (int i = 0; i < get.length; i++)
  693.             {
  694.                 if (get[i])
  695.                 {
  696.                     if(cg.getString(i).equals("Identifiers defined"))
  697.                     {
  698.                         try
  699.                         {
  700.                             String galleryPath = System.getProperty("fileconn.dir.photos");
  701.                             FileConnection fc2 = (FileConnection)
  702.                             Connector.open(galleryPath + "source.txt");
  703.                             InputStream is = fc2.openInputStream();
  704.                             byte b[] = new byte[1048576];
  705.                             int length2 = is.read(b, 0, 1048576);
  706.                             ShowEditor(""+ new String(b, 0, length2));
  707.                          }
  708.                          catch (Exception e)
  709.                          {
  710.                              stext.setText("ERROR: Failed to load the source file (source.txt) from the Photos folder (phone memory).");
  711.                          }
  712.                     }
  713.                     else if(cg.getString(i).equals("Empty"))
  714.                     {
  715.                         ShowEditor(null);
  716.                     }
  717.                 }
  718.             }
  719.         }
  720.         else if (command == quit)
  721.         {
  722.             String get = input.getString();
  723.             String get2 = tb.getString();
  724.             int tbLength = get2.length();
  725.             if (tbLength > 0)
  726.             {
  727.                 name = input.getString();
  728.                 javax.microedition.io.Connection c2 = null;
  729.                 try
  730.                 {
  731.                     String galleryPath = System.getProperty("fileconn.dir.photos");
  732.                     c2 = javax.microedition.io.Connector.open(galleryPath + name + ".pwn", javax.microedition.io.Connector.READ_WRITE);
  733.                     javax.microedition.io.file.FileConnection fc3 =
  734.                     (javax.microedition.io.file.FileConnection) c2;
  735.                     if (!fc3.exists())
  736.                     {
  737.                         ShowSaveScreen("Your project is not saved. Would you like to save it?");
  738.                     }
  739.                     else
  740.                     {
  741.                         if(HasSaved == false)
  742.                         {
  743.                             ShowSaveScreen("Project modified. Save changes?");
  744.                         }
  745.                         else if(HasSaved == true)
  746.                         {
  747.                             ShowMainScreen();
  748.                             HasSaved = false;
  749.                         }
  750.                     }
  751.                 }
  752.                 catch (Exception e)
  753.                 {
  754.                     text.setText("ERROR: File system access failed.");
  755.                 }
  756.                 finally
  757.                 {
  758.                     try
  759.                     {
  760.                         if (c2 != null)
  761.                             c2.close();
  762.                     }
  763.                     catch (Exception ex)
  764.                     {
  765.                         ex.printStackTrace();
  766.                     }
  767.                 }
  768.             }
  769.             else
  770.             {
  771.                 ShowMainScreen();
  772.                 HasSaved = false;
  773.             }
  774.         }
  775.         else if (command == save)
  776.         {
  777.             String ReadText = tb.getString();
  778.             int length = ReadText.length();
  779.             if (length < 1)
  780.             {
  781.                 ShowSaveOutput("ERROR: Your project must contain atleast 1 character to be saved.");
  782.                 HasSaved = false;
  783.             }
  784.             else if (length > 0)
  785.             {
  786.                 byte [] byteArray = ReadText.getBytes();
  787.                 writeFile(input.getString(), byteArray);
  788.                 HasSaved = true;
  789.                 if (CountOpeningBrackets(tb.getString()) != CountClosingBrackets(tb.getString()))
  790.                 {
  791.                     warning = '\n' + "* Warning: opening and closing brackets are NOT equal.";
  792.                 }
  793.                 else if (CountOpeningBrackets(tb.getString()) == CountClosingBrackets(tb.getString()))
  794.                 {
  795.                     warning = "";
  796.                 }
  797.                 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);
  798.             }
  799.         }
  800.         else if (command == tab)
  801.         {
  802.             String currentText = tb.getString();
  803.             String alignment = currentText + "\t";
  804.             tb.setString(alignment);
  805.             HasSaved = false;
  806.         }
  807.         else if (command == newline)
  808.         {
  809.             String currentText = tb.getString();
  810.             String alignment = currentText + "\n";
  811.             tb.setString(alignment);
  812.             HasSaved = false;
  813.         }
  814.         else if (command == yes)
  815.         {
  816.             String txt = tb.getString();
  817.             byte [] bytes = txt.getBytes();
  818.             writeFile(input.getString(), bytes);
  819.             int length = txt.length();
  820.             if (CountOpeningBrackets(tb.getString()) != CountClosingBrackets(tb.getString()))
  821.             {
  822.                 warning = '\n' + "* Warning: opening and closing brackets are NOT equal.";
  823.             }
  824.             else if (CountOpeningBrackets(tb.getString()) == CountClosingBrackets(tb.getString()))
  825.             {
  826.                 warning = "";
  827.             }
  828.             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);
  829.             HasSaved = true;
  830.         }
  831.         else if (command == no)
  832.         {
  833.             ShowMainScreen();
  834.             HasSaved = false;
  835.         }
  836.         else if (command == saveback)
  837.         {
  838.             ShowEditor(tb.getString());
  839.             HasSaved = false;
  840.         }
  841.         else if (command == errorback)
  842.         {
  843.             ShowEditor(tb.getString());
  844.         }
  845.         else if (command == clear)
  846.         {
  847.             tb.setString(null);
  848.         }
  849.         else if (command == tools)
  850.         {
  851.             try
  852.             {
  853.                 String galleryPath = System.getProperty("fileconn.dir.photos");
  854.                 FileConnection fc2 = (FileConnection)
  855.                 Connector.open(galleryPath + "tools.txt");
  856.                 InputStream is = fc2.openInputStream();
  857.                 byte b[] = new byte[1048576];
  858.                 int length2 = is.read(b, 0, 1048576);
  859.                 ShowTools(""+ new String(b, 0, length2));
  860.              }
  861.              catch (Exception e)
  862.              {
  863.                 ShowTools("ERROR: Failed to load the file (tools.txt) from the Photos folder (phone memory).");
  864.              }
  865.         }
  866.         else if (command == tback)
  867.         {
  868.             ShowEditor(tb.getString());
  869.         }
  870.         else if (command == about)
  871.         {
  872.             ShowAbout();
  873.         }
  874.         else if (command == aback)
  875.         {
  876.             ShowMainScreen();
  877.         }
  878.         else if (command == qsback)
  879.         {
  880.             ShowMainScreen();
  881.         }
  882.         else if (command == includes)
  883.         {
  884.             ShowIncludes("If the include does already exist, it will be loaded back in. Otherwise it will create a new include.");
  885.         }
  886.         else if (command == iback)
  887.         {
  888.             ShowEditor(tb.getString());
  889.         }
  890.         else if (command == iok)
  891.         {
  892.             name = input.getString();
  893.             int length = name.length();
  894.             if (IsInvalidFileName(input.getString()) > 0)
  895.             {
  896.                 itext.setText("ERROR: Invalid file name.");
  897.             }
  898.             else if(length > 20)
  899.             {
  900.                 itext.setText("ERROR: The name of your project can not be longer than 20 characters.");
  901.             }
  902.             else if(length < 1)
  903.             {
  904.                 itext.setText("ERROR: The name of your project must be atleast 1 character long.");
  905.             }
  906.             else if(length <= 20)
  907.             {
  908.                 itext.setText(null);
  909.                 javax.microedition.io.Connection c3 = null;
  910.                 try
  911.                 {
  912.                     String galleryPath = System.getProperty("fileconn.dir.photos");
  913.                     c3 = javax.microedition.io.Connector.open(galleryPath + name2 + ".inc", javax.microedition.io.Connector.READ_WRITE);
  914.                     javax.microedition.io.file.FileConnection fc4 =
  915.                     (javax.microedition.io.file.FileConnection) c3;
  916.                     if (!fc4.exists())
  917.                     {
  918.                         ShowIncludesEditor(null);
  919.                     }
  920.                     else
  921.                     {
  922.                         try
  923.                         {
  924.                             FileConnection fc5 = (FileConnection)
  925.                             Connector.open(galleryPath + name2 + ".inc");
  926.                             InputStream is = fc5.openInputStream();
  927.                             byte b[] = new byte[1048576];
  928.                             int length2 = is.read(b, 0, 1048576);
  929.                             ShowIncludesEditor(""+ new String(b, 0, length2));
  930.                          }
  931.                          catch (Exception e)
  932.                          {
  933.                              itext.setText("ERROR: Failed to load the requested include from the Photos folder (phone memory).");
  934.                          }
  935.                      }
  936.                 }
  937.                 catch (Exception e)
  938.                 {
  939.                     itext.setText("ERROR: File system access failed.");
  940.                 }
  941.                 finally
  942.                 {
  943.                     try
  944.                     {
  945.                         if (c3 != null)
  946.                             c3.close();
  947.                         }
  948.                         catch (Exception ex)
  949.                         {
  950.                             ex.printStackTrace();
  951.                         }
  952.                     }
  953.                 }
  954.         }
  955.         else if (command == iquit)
  956.         {
  957.             String get = iinput.getString();
  958.             String get2 = itb.getString();
  959.             int tbLength = get2.length();
  960.             if (tbLength > 0)
  961.             {
  962.                 name2 = iinput.getString();
  963.                 javax.microedition.io.Connection c4 = null;
  964.                 try
  965.                 {
  966.                     String galleryPath = System.getProperty("fileconn.dir.photos");
  967.                     c4 = javax.microedition.io.Connector.open(galleryPath + name2 + ".inc", javax.microedition.io.Connector.READ_WRITE);
  968.                     javax.microedition.io.file.FileConnection fc6 =
  969.                     (javax.microedition.io.file.FileConnection) c4;
  970.                     if (!fc6.exists())
  971.                     {
  972.                         ShowIncludeSaveScreen("Your include is not saved. Would you like to save it?");
  973.                     }
  974.                     else
  975.                     {
  976.                         if(HasSaved2 == false)
  977.                         {
  978.                             ShowIncludeSaveScreen("Include modified. Save changes?");
  979.                         }
  980.                         else if(HasSaved2 == true)
  981.                         {
  982.                             ShowEditor(tb.getString());
  983.                             HasSaved2 = false;
  984.                         }
  985.                     }
  986.                 }
  987.                 catch (Exception e)
  988.                 {
  989.                     itext.setText("ERROR: File system access failed.");
  990.                 }
  991.                 finally
  992.                 {
  993.                     try
  994.                     {
  995.                         if (c4 != null)
  996.                             c4.close();
  997.                     }
  998.                     catch (Exception ex)
  999.                     {
  1000.                         ex.printStackTrace();
  1001.                     }
  1002.                 }
  1003.             }
  1004.             else
  1005.             {
  1006.                 ShowMainScreen();
  1007.                 HasSaved2 = false;
  1008.             }
  1009.         }
  1010.         else if (command == isave)
  1011.         {
  1012.             String ReadText = itb.getString();
  1013.             int length = ReadText.length();
  1014.             if (length < 1)
  1015.             {
  1016.                 ShowIncludeSaveOutput("ERROR: Your include must contain atleast 1 character to be saved.");
  1017.                 HasSaved2 = false;
  1018.             }
  1019.             else if (length > 0)
  1020.             {
  1021.                 byte [] byteArray = ReadText.getBytes();
  1022.                 writeIncludeFile(iinput.getString(), byteArray);
  1023.                 HasSaved2 = true;
  1024.                 if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString()))
  1025.                 {
  1026.                     warning = '\n' + "* Warning: opening and closing brackets are NOT equal.";
  1027.                 }
  1028.                 else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString()))
  1029.                 {
  1030.                     warning = "";
  1031.                 }
  1032.                 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);
  1033.             }
  1034.         }
  1035.         else if (command == itab)
  1036.         {
  1037.             String currentText = itb.getString();
  1038.             String alignment = currentText + "\t";
  1039.             itb.setString(alignment);
  1040.             HasSaved2 = false;
  1041.         }
  1042.         else if (command == inewline)
  1043.         {
  1044.             String currentText = itb.getString();
  1045.             String alignment = currentText + "\n";
  1046.             itb.setString(alignment);
  1047.             HasSaved2 = false;
  1048.         }
  1049.         else if (command == iclear)
  1050.         {
  1051.             itb.setString(null);
  1052.         }
  1053.         else if (command == iadd)
  1054.         {
  1055.             String ReadText = itb.getString();
  1056.             int length = ReadText.length();
  1057.             if (length < 1)
  1058.             {
  1059.                 ShowIncludeSaveOutput("ERROR: Your include must contain atleast 1 character to be saved.");
  1060.                 HasSaved2 = false;
  1061.             }
  1062.             else if (length > 0)
  1063.             {
  1064.                 byte [] byteArray = ReadText.getBytes();
  1065.                 writeFile(iinput.getString(), byteArray);
  1066.                 HasSaved2 = true;
  1067.                 if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString()))
  1068.                 {
  1069.                     warning = '\n' + "* Warning: opening and closing brackets are NOT equal.";
  1070.                 }
  1071.                 else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString()))
  1072.                 {
  1073.                     warning = "";
  1074.                 }
  1075.                 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);
  1076.                 String getText = tb.getString();
  1077.                 String alignment = "#include <" + iinput.getString() + ">" + '\n' + getText;
  1078.                 tb.setString(alignment);
  1079.                 HasSaved = false;
  1080.                 HasSaved2 = true;
  1081.             }
  1082.         }
  1083.         else if (command == ierrorback)
  1084.         {
  1085.             ShowIncludesEditor(itb.getString());
  1086.         }
  1087.         else if (command == iyes)
  1088.         {
  1089.             String txt = itb.getString();
  1090.             byte [] bytes = txt.getBytes();
  1091.             writeIncludeFile(iinput.getString(), bytes);
  1092.             ShowEditor(tb.getString());
  1093.             int length = txt.length();
  1094.             if (CountOpeningBrackets(itb.getString()) != CountClosingBrackets(itb.getString()))
  1095.             {
  1096.                 warning = '\n' + "* Warning: opening and closing brackets are NOT equal.";
  1097.             }
  1098.             else if (CountOpeningBrackets(itb.getString()) == CountClosingBrackets(itb.getString()))
  1099.             {
  1100.                 warning = "";
  1101.             }
  1102.             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);
  1103.             HasSaved2 = true;
  1104.         }
  1105.         else if (command == ino)
  1106.         {
  1107.             ShowEditor(tb.getString());
  1108.             HasSaved2 = false;
  1109.         }
  1110.         else if (command == isaveback)
  1111.         {
  1112.             ShowIncludesEditor(itb.getString());
  1113.             HasSaved2 = false;
  1114.         }
  1115.     }
  1116.  
  1117.     /**
  1118.      * Returns a display instance.
  1119.      * @return the display instance.
  1120.      */
  1121.     public Display getDisplay () {
  1122.         return Display.getDisplay(this);
  1123.     }
  1124.  
  1125.     /**
  1126.      * Exits MIDlet.
  1127.      */
  1128.     public void exitMIDlet() {
  1129.         switchDisplayable (null, null);
  1130.         destroyApp(true);
  1131.         notifyDestroyed();
  1132.     }
  1133.  
  1134.     /**
  1135.      * Called when MIDlet is started.
  1136.      * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
  1137.      */
  1138.     public void startApp() {
  1139.         if (midletPaused) {
  1140.             resumeMIDlet ();
  1141.         } else {
  1142.             initialize ();
  1143.             startMIDlet ();
  1144.         }
  1145.         midletPaused = false;
  1146.     }
  1147.  
  1148.     /**
  1149.      * Called when MIDlet is paused.
  1150.      */
  1151.     public void pauseApp() {
  1152.         midletPaused = true;
  1153.     }
  1154.  
  1155.     /**
  1156.      * Called to signal the MIDlet to terminate.
  1157.      * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
  1158.      */
  1159.     public void destroyApp(boolean unconditional) {
  1160.     }
  1161.  
  1162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement