Advertisement
Orhideous

Fishlabs games lang editor

Jun 5th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. import javax.microedition.midlet.MIDlet;
  2. import javax.microedition.lcdui.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.microedition.io.*;
  6. import javax.microedition.io.file.*;
  7.  
  8. public class LangEditor extends MIDlet implements CommandListener
  9. {
  10. FileConnection fc;
  11. DataInputStream datain;
  12. DataOutputStream dataout;
  13. Vector lang_data;
  14. Display display;
  15. List mainmenu;
  16. Form editor, filemanager, gomsg, save_ask;
  17. TextField select_field, edit_field, goto_field;
  18.  
  19. Command open;
  20. Command back;
  21. Command next;
  22. Command save;
  23. Command menu;
  24. Command select_msg;
  25. Command go_msg;
  26. Command yes;
  27. Command no;
  28.  
  29. int msg_now, msg_all, writecount;
  30.  
  31. String loadedString, nowString, writeString;
  32.  
  33.  
  34. public void startApp()
  35. {
  36.  display = Display.getDisplay(this);
  37.  create_menu();
  38.  create_filemanager();
  39.  create_editor();
  40.  create_gomsg();
  41.  create_save_ask();
  42.  lang_data = new Vector();
  43.  display.setCurrent(mainmenu);
  44. }
  45.  
  46. public void pauseApp()
  47. {}
  48.  
  49. public void destroyApp(boolean unconditional)
  50. {notifyDestroyed();}
  51.  
  52. public void create_menu()
  53. {
  54.  mainmenu = new List("Main menu", List.IMPLICIT);
  55.  mainmenu.append("Open *.lang file", null);
  56.  mainmenu.append("Exit", null);
  57.  mainmenu.setCommandListener(this);
  58. }
  59.  
  60. public void create_filemanager()
  61. {
  62.  filemanager = new Form("Open *.lang file");
  63.  select_field = new TextField("File path", "file:///c:/other/.lang" ,1024, TextField.ANY);
  64.  filemanager.append(select_field);
  65.  open = new Command("Open", Command.OK, 2);
  66.  filemanager.addCommand(open);
  67.  filemanager.setCommandListener(this);
  68. }
  69.  
  70. public void create_editor()
  71. {
  72.  editor = new Form(null);
  73.  edit_field = new TextField("", "" ,2400, TextField.ANY);
  74.  editor.append(edit_field);
  75.  next = new Command("Next", Command.OK, 1);
  76.  back = new Command("Back", Command.OK, 1);
  77.  select_msg = new Command("Go to...", Command.OK, 1);
  78.  save = new Command("Save", Command.OK, 1);
  79.  menu = new Command("Menu", Command.OK, 1);
  80.  editor.addCommand(next);
  81.  editor.addCommand(back);
  82.  editor.addCommand(select_msg);
  83.  editor.addCommand(save);
  84.  editor.addCommand(menu);
  85.  editor.setCommandListener(this);
  86. }
  87.  
  88. public void create_gomsg()
  89. {
  90.  gomsg = new Form(null);
  91.  goto_field = new TextField("", "" ,10, TextField.NUMERIC);
  92.  gomsg.append(goto_field);
  93.  go_msg = new Command("Go to...", Command.OK, 2);
  94.  gomsg.addCommand(go_msg);
  95.  gomsg.setCommandListener(this);
  96. }
  97.  
  98. public void create_save_ask()
  99. {
  100.  save_ask = new Form(null);
  101.  yes = new Command ("Yes", Command.OK, 2);
  102.  no = new Command ("No", Command.OK, 2);
  103.  save_ask.addCommand(yes);
  104.  save_ask.addCommand(no);
  105.  save_ask.setCommandListener(this);
  106.  save_ask.append("Save changes?");
  107. }
  108.  
  109. public void openlang()
  110. {
  111.  try
  112.  {
  113.   fc = (FileConnection)Connector.open (select_field.getString());
  114.   datain = fc.openDataInputStream();
  115.   dataout = fc.openDataOutputStream();
  116.  }
  117.  catch (IOException ioe)
  118.  {}
  119. }
  120.  
  121. public void readlang()
  122. {
  123.  do
  124.  {
  125.   try
  126.   {
  127.    loadedString = datain.readUTF();
  128.   }
  129.   catch (IOException ioe)
  130.   {break;}
  131.   catch (EOFException eofe)
  132.   {}
  133.   lang_data.addElement(loadedString);
  134.  }
  135.  while (loadedString != null);
  136. }
  137.  
  138. public void closelang()
  139. {
  140.  try
  141.  {
  142.   datain.close();
  143.   dataout.close();
  144.   fc.close();
  145.  }
  146.  catch (IOException ioe)
  147.  {}
  148. }
  149.  
  150. public void initialize_editor()
  151. {
  152.  msg_now = 1;
  153.  msg_all = lang_data.size();
  154.  edit_field.setString(lang_data.firstElement().toString());
  155.  editor.setTitle("Message " + msg_now + "/" + msg_all);
  156. }
  157.  
  158. public void save_current(int i)
  159. {
  160.  nowString = edit_field.getString();
  161.  lang_data.setElementAt(nowString, (i-1));
  162.  nowString = null;
  163. }
  164.  
  165. public void commandAction(Command cmd, Displayable dsp)
  166. {
  167.  if (dsp == mainmenu)
  168.  {
  169.   if (cmd == List.SELECT_COMMAND)
  170.   {
  171.    int selected = mainmenu.getSelectedIndex();
  172.    switch(selected)
  173.    {
  174.     case 0:
  175.      {display.setCurrent(filemanager);}
  176.     break;
  177.    
  178.     case 1:
  179.      {notifyDestroyed();}
  180.     break;
  181.    }
  182.   }
  183.  }
  184.  
  185.  if (dsp == filemanager)
  186.  {
  187.   if (cmd == open)
  188.   {
  189.    lang_data.removeAllElements();
  190.    openlang();
  191.    readlang();
  192.    initialize_editor();
  193.    closelang();
  194.    display.setCurrent(editor);  
  195.   }
  196.  }
  197.  
  198.  if (dsp == editor)
  199.  {
  200.   if (cmd == next && msg_now < msg_all)
  201.   {
  202.    save_current(msg_now);
  203.    msg_now++;
  204.    editor.setTitle("Message " + msg_now + "/" + msg_all);
  205.  
  206.    edit_field.setString(lang_data.elementAt(msg_now-1).toString());
  207.   }
  208.  
  209.   if (cmd == back && msg_now>1)
  210.   {
  211.    save_current(msg_now);
  212.    msg_now--;
  213.    editor.setTitle("Message " + msg_now + "/" + msg_all);
  214.    edit_field.setString(lang_data.elementAt(msg_now-1).toString());
  215.   }
  216.  
  217.   if (cmd == select_msg)
  218.   {
  219.    save_current(msg_now);
  220.    gomsg.setTitle("Go to 1.." + msg_all);
  221.    display.setCurrent(gomsg);
  222.   }
  223.  
  224.   if (cmd == save)
  225.   {
  226.    save_current(msg_now);
  227.    writecount = 0;
  228.    openlang();
  229.    do
  230.    {
  231.     writeString = lang_data.elementAt(writecount).toString();
  232.     try
  233.     {
  234.      dataout.writeUTF(writeString);
  235.     }
  236.     catch (IOException ioe)
  237.     {}
  238.     writecount++;
  239.    }
  240.    while (writecount < msg_all);
  241.    closelang();
  242.   }
  243.  
  244.   if (cmd == menu)
  245.   {display.setCurrent(save_ask);}
  246.  }
  247.  
  248.  if (dsp == gomsg)
  249.  {
  250.   if (cmd == go_msg)
  251.   {
  252.    msg_now = Integer.parseInt(goto_field.getString());
  253.    if (msg_now > 1 && msg_now < msg_all)
  254.    {
  255.     edit_field.setString(lang_data.elementAt(msg_now-1).toString());
  256.     editor.setTitle("Message " + msg_now + "/" + msg_all);
  257.     display.setCurrent(editor);
  258.    }
  259.    else
  260.    {
  261.     gomsg.setTitle("Incorrect message");
  262.    }
  263.   }
  264.  }
  265.  
  266.  if (dsp == save_ask)
  267.  {
  268.   if (cmd == yes)
  269.   {
  270.    save_current(msg_now);
  271.    writecount = 0;
  272.    openlang();
  273.    do
  274.    {
  275.     writeString = lang_data.elementAt(writecount).toString();
  276.     try
  277.     {
  278.      dataout.writeUTF(writeString);
  279.     }
  280.     catch (IOException ioe)
  281.     {}
  282.     writecount++;
  283.    }
  284.    while (writecount < msg_all);
  285.    closelang();
  286.    display.setCurrent(mainmenu);  
  287.   }
  288.  
  289.   if (cmd == no)
  290.   {display.setCurrent(mainmenu);}
  291.  }
  292. }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement