/* This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.*/ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import com.siemens.mp.io.*; import java.io.*; import java.lang.*; public class Slovnik extends MIDlet implements CommandListener, Runnable { //vizualni prvky Display disp; Command cmd_back; Command cmd_about; Command cmd_konec; Command cmd_search; Form frm_hledat; Form frm_about; Form frm_result; Form frm_loading; TextField tf_slovo; String slovo = ""; public Slovnik() { // globalni prikazy cmd_back = new Command("Zpět", Command.BACK, 3); cmd_about = new Command("About", Command.SCREEN, 2); cmd_konec = new Command("Konec", Command.EXIT, 4); cmd_search = new Command("Hledat", Command.SCREEN, 1); //hlavni menu frm_hledat = new Form("EN-CZ Dictionary"); tf_slovo = new TextField("Anglický výraz:","",18,TextField.ANY); frm_hledat.append(tf_slovo); frm_hledat.addCommand(cmd_search); frm_hledat.addCommand(cmd_about); frm_hledat.addCommand(cmd_konec); frm_hledat.addCommand(cmd_back); frm_loading = new Form(""); frm_loading.append("Hledám..."); frm_loading.setTicker(new Ticker("Busy")); } //vyhodnocovani udalosti public void commandAction(Command c, Displayable d) { // kliknutí je voláno z menu if (d == frm_hledat && c != cmd_back) { if (c == cmd_search) { //vyhledej(tf_slovo.getString()); slovo = tf_slovo.getString(); disp.setCurrent(frm_loading); Thread t = new Thread(this); // Spuštění druhého vlákna, ve kterém probíhá vyhledávání t.start(); } if (c == cmd_about) { about(); } if (c == cmd_konec) { destroyApp(true); } } //zpet if (c == cmd_back) { disp = Display.getDisplay(this); frm_hledat.setCommandListener(this); disp.setCurrent(frm_hledat); } } //submenu public void about() { disp = Display.getDisplay(this); frm_about = new Form("EN-CZ Dictionary"); StringItem si_verze = new StringItem("Verze", "1.1"); StringItem si_jazyk = new StringItem("Jazyk", "CZ"); StringItem si_autor = new StringItem("Autor midletu", "Destil"); StringItem si_email = new StringItem("E-mail", "destil@volny.cz"); StringItem si_homepage = new StringItem("Homepage", "http://destilapps.wz.cz"); frm_about.append(si_verze); frm_about.append("\n"); frm_about.append(si_jazyk); frm_about.append(si_autor); frm_about.append(si_email); frm_about.append(si_homepage); frm_about.addCommand(cmd_back); frm_about.setCommandListener(this); disp.setCurrent(frm_about); } //midlet startuje public void startApp() { disp = Display.getDisplay(this); frm_hledat.setCommandListener(this); disp.setCurrent(frm_hledat); } //pauza aplikace public void pauseApp() {} //konec aplikace public void destroyApp(boolean unconditional) { notifyDestroyed(); } /** * Metoda rozhraní Runnable na vyhledání a zobrazení * ve formuláři - provádí ji druhé vlákno */ public void run() { try { // hledani prvnich dvou pismen String prefix; if (slovo.length()==1) prefix = slovo + '['; else prefix = slovo.substring(0,2); //inicializace souboru File file = new File(); int indexDesc = file.open("index.dstl"); int delka = file.length(indexDesc); //hledani daneho prefixu v indexu int nalezeno = 0; byte[] b = new byte[1]; int i = 0; String s; String aktualni = ""; String preklad = ""; String cislo = ""; int cislujeme = 0; do { file.read(indexDesc,b,0,b.length); s = new String(b); i = i+1; //konec radky if (s.hashCode() == 13 || s.hashCode()==10) { if (aktualni.equals(prefix)) { nalezeno = 1; } else { aktualni = ""; cislo = ""; cislujeme = 0; } } else { //konec prefixu - strednik if (s.hashCode() == 59) { cislujeme = 1; } else{ if (cislujeme == 0) aktualni = aktualni + s; if (cislujeme == 1) cislo = cislo + s; } } } while (nalezeno==0 && iskok2-2) { if (aktualni.equals(slovo)) { nalezeno = 1; } else { aktualni = prefix; preklad = ""; prekladame = 0; } } else { //konec angl.slova if (s.hashCode() == 59) { prekladame = 1; } else{ if (prekladame == 0) aktualni = aktualni + s; if (prekladame == 1) preklad = preklad + s; } } } while (nalezeno==0 && i<=skok2); file.close(fileDesc); } //nenalezeno if (nalezeno == 0) { aktualni = slovo; preklad = "Slovo nenalezeno"; } //a jdem zobrazit vysledek disp = Display.getDisplay(this); frm_result = new Form(aktualni); frm_result.append(preklad); tf_slovo.setString(""); frm_result.addCommand(cmd_back); frm_result.setCommandListener(this); disp.setCurrent(frm_result); } catch (Exception e) {System.out.println("Nastala vyjimka: "+e.toString());} } }