Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.lcdui.*;
- import java.io.*;
- import java.util.*;
- import javax.microedition.io.*;
- import javax.microedition.io.file.*;
- public class LangEditor extends MIDlet implements CommandListener
- {
- FileConnection fc;
- DataInputStream datain;
- DataOutputStream dataout;
- Vector lang_data;
- Display display;
- List mainmenu;
- Form editor, filemanager, gomsg, save_ask;
- TextField select_field, edit_field, goto_field;
- Command open;
- Command back;
- Command next;
- Command save;
- Command menu;
- Command select_msg;
- Command go_msg;
- Command yes;
- Command no;
- int msg_now, msg_all, writecount;
- String loadedString, nowString, writeString;
- public void startApp()
- {
- display = Display.getDisplay(this);
- create_menu();
- create_filemanager();
- create_editor();
- create_gomsg();
- create_save_ask();
- lang_data = new Vector();
- display.setCurrent(mainmenu);
- }
- public void pauseApp()
- {}
- public void destroyApp(boolean unconditional)
- {notifyDestroyed();}
- public void create_menu()
- {
- mainmenu = new List("Main menu", List.IMPLICIT);
- mainmenu.append("Open *.lang file", null);
- mainmenu.append("Exit", null);
- mainmenu.setCommandListener(this);
- }
- public void create_filemanager()
- {
- filemanager = new Form("Open *.lang file");
- select_field = new TextField("File path", "file:///c:/other/.lang" ,1024, TextField.ANY);
- filemanager.append(select_field);
- open = new Command("Open", Command.OK, 2);
- filemanager.addCommand(open);
- filemanager.setCommandListener(this);
- }
- public void create_editor()
- {
- editor = new Form(null);
- edit_field = new TextField("", "" ,2400, TextField.ANY);
- editor.append(edit_field);
- next = new Command("Next", Command.OK, 1);
- back = new Command("Back", Command.OK, 1);
- select_msg = new Command("Go to...", Command.OK, 1);
- save = new Command("Save", Command.OK, 1);
- menu = new Command("Menu", Command.OK, 1);
- editor.addCommand(next);
- editor.addCommand(back);
- editor.addCommand(select_msg);
- editor.addCommand(save);
- editor.addCommand(menu);
- editor.setCommandListener(this);
- }
- public void create_gomsg()
- {
- gomsg = new Form(null);
- goto_field = new TextField("", "" ,10, TextField.NUMERIC);
- gomsg.append(goto_field);
- go_msg = new Command("Go to...", Command.OK, 2);
- gomsg.addCommand(go_msg);
- gomsg.setCommandListener(this);
- }
- public void create_save_ask()
- {
- save_ask = new Form(null);
- yes = new Command ("Yes", Command.OK, 2);
- no = new Command ("No", Command.OK, 2);
- save_ask.addCommand(yes);
- save_ask.addCommand(no);
- save_ask.setCommandListener(this);
- save_ask.append("Save changes?");
- }
- public void openlang()
- {
- try
- {
- fc = (FileConnection)Connector.open (select_field.getString());
- datain = fc.openDataInputStream();
- dataout = fc.openDataOutputStream();
- }
- catch (IOException ioe)
- {}
- }
- public void readlang()
- {
- do
- {
- try
- {
- loadedString = datain.readUTF();
- }
- catch (IOException ioe)
- {break;}
- catch (EOFException eofe)
- {}
- lang_data.addElement(loadedString);
- }
- while (loadedString != null);
- }
- public void closelang()
- {
- try
- {
- datain.close();
- dataout.close();
- fc.close();
- }
- catch (IOException ioe)
- {}
- }
- public void initialize_editor()
- {
- msg_now = 1;
- msg_all = lang_data.size();
- edit_field.setString(lang_data.firstElement().toString());
- editor.setTitle("Message " + msg_now + "/" + msg_all);
- }
- public void save_current(int i)
- {
- nowString = edit_field.getString();
- lang_data.setElementAt(nowString, (i-1));
- nowString = null;
- }
- public void commandAction(Command cmd, Displayable dsp)
- {
- if (dsp == mainmenu)
- {
- if (cmd == List.SELECT_COMMAND)
- {
- int selected = mainmenu.getSelectedIndex();
- switch(selected)
- {
- case 0:
- {display.setCurrent(filemanager);}
- break;
- case 1:
- {notifyDestroyed();}
- break;
- }
- }
- }
- if (dsp == filemanager)
- {
- if (cmd == open)
- {
- lang_data.removeAllElements();
- openlang();
- readlang();
- initialize_editor();
- closelang();
- display.setCurrent(editor);
- }
- }
- if (dsp == editor)
- {
- if (cmd == next && msg_now < msg_all)
- {
- save_current(msg_now);
- msg_now++;
- editor.setTitle("Message " + msg_now + "/" + msg_all);
- edit_field.setString(lang_data.elementAt(msg_now-1).toString());
- }
- if (cmd == back && msg_now>1)
- {
- save_current(msg_now);
- msg_now--;
- editor.setTitle("Message " + msg_now + "/" + msg_all);
- edit_field.setString(lang_data.elementAt(msg_now-1).toString());
- }
- if (cmd == select_msg)
- {
- save_current(msg_now);
- gomsg.setTitle("Go to 1.." + msg_all);
- display.setCurrent(gomsg);
- }
- if (cmd == save)
- {
- save_current(msg_now);
- writecount = 0;
- openlang();
- do
- {
- writeString = lang_data.elementAt(writecount).toString();
- try
- {
- dataout.writeUTF(writeString);
- }
- catch (IOException ioe)
- {}
- writecount++;
- }
- while (writecount < msg_all);
- closelang();
- }
- if (cmd == menu)
- {display.setCurrent(save_ask);}
- }
- if (dsp == gomsg)
- {
- if (cmd == go_msg)
- {
- msg_now = Integer.parseInt(goto_field.getString());
- if (msg_now > 1 && msg_now < msg_all)
- {
- edit_field.setString(lang_data.elementAt(msg_now-1).toString());
- editor.setTitle("Message " + msg_now + "/" + msg_all);
- display.setCurrent(editor);
- }
- else
- {
- gomsg.setTitle("Incorrect message");
- }
- }
- }
- if (dsp == save_ask)
- {
- if (cmd == yes)
- {
- save_current(msg_now);
- writecount = 0;
- openlang();
- do
- {
- writeString = lang_data.elementAt(writecount).toString();
- try
- {
- dataout.writeUTF(writeString);
- }
- catch (IOException ioe)
- {}
- writecount++;
- }
- while (writecount < msg_all);
- closelang();
- display.setCurrent(mainmenu);
- }
- if (cmd == no)
- {display.setCurrent(mainmenu);}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement