src/jdict/AskWord.java
author viric@llimona
Tue, 07 Aug 2007 21:28:51 +0200
changeset 33 c9323448360c
parent 25 764c48b54327
child 37 509497421b36
permissions -rw-r--r--
Version moved to 0.2.3

package jdict;

import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.*;

public class AskWord
	implements CommandListener
{
	private Form myform;
	private Command cmd_eliri;
	private Command cmd_sercxi;
	private Command cmd_montri;
	private Command cmd_pri;
	private Command cmd_vortarinformo;
	private Command cmd_agordoj;
    private TextField word;
    private ChoiceGroup translit;
    private String translit_neniu;
    private String translit_iksa;
    private String translit_rusa;
    private StringItem showSearch;
    private String vortaro;
	
	public AskWord()
	{
        get_vortaro();

		show();
	}

	public void get_vortaro()
    {
        InputStream vfile = getClass().getResourceAsStream("/dicts/VORTARO");
        if (vfile == null)
        {
            vortaro = "ERROR";
            return;
        }
        /* 50 bytes maximum for vortaro nomo */
        byte array[] = new byte[50];
        int total;
        try {
            total = vfile.read(array, 0, 50);
        } catch (IOException e)
        {
            System.out.println("Cannot open VORTARO file. Going to ERROR B.");
            vortaro = "ERROR";
            return;
        }
        /* This will have '\n' */
        vortaro = new String(array, 0, total - 1 /* - \n */);
    }

	public void show()
	{
        translit_neniu = T.t("Neniu");
        translit_iksa = T.t("Iksa sistemo");
        translit_rusa = T.t("ASCII-rusa");

		myform = new Form(vortaro);

        word = new TextField(T.t("Vorto:"), "", 20, TextField.ANY);
        myform.append(word);

        translit = new ChoiceGroup(T.t("Transliterigo"), ChoiceGroup.EXCLUSIVE);

        String tselected = Mem.getVariable("Transliterigo");
        int index;
        index = translit.append(translit_neniu, null);
        if (tselected != null && tselected.equals(translit_neniu))
            translit.setSelectedIndex(index, true);
        index = translit.append(translit_iksa, null);
        if (tselected != null && tselected.equals(translit_iksa))
            translit.setSelectedIndex(index, true);
        translit.append(translit_rusa, null);
        if (tselected != null && tselected.equals(translit_rusa))
            translit.setSelectedIndex(index, true);
        if (tselected == null)
            translit.setSelectedIndex(0, true);
        myform.append(translit);

        /* DEBUG */
        showSearch = new StringItem(T.t("Serĉonte:"), "");
        myform.append(showSearch);

		cmd_sercxi = new Command(T.t("Serĉi"), Command.OK, 0);
		myform.addCommand(cmd_sercxi);
		/* Commands */
		cmd_montri = new Command(T.t("Montri"), Command.HELP, 1);
		myform.addCommand(cmd_montri);
		/* Commands */
		cmd_pri = new Command(T.t("Pri ĉi tio"), Command.HELP, 2);
		myform.addCommand(cmd_pri);
		/* Commands */
		cmd_vortarinformo = new Command(T.t("Vortarinformo"), Command.HELP, 3);
		myform.addCommand(cmd_vortarinformo);
		/* Commands */
		cmd_agordoj = new Command(T.t("Agordoj"), Command.HELP, 3);
		myform.addCommand(cmd_agordoj);
		/* Commands */
		cmd_eliri = new Command(T.t("Eliri"), Command.BACK, 4);
		myform.addCommand(cmd_eliri);

		myform.setCommandListener(this);
		Main.display.setCurrent(myform);
	}

    private String transliterate(String from)
    {
        String out = from;
        if (translit.getString(translit.getSelectedIndex())
                .equals(translit_iksa))
            out = Replace.IksojAlCxapeloj(from);
        else if (translit.getString(translit.getSelectedIndex())
                .equals(translit_rusa))
            out = Replace.AsciiAlRusa(from);

        return out;
    }

	public void commandAction(Command c, Displayable d)
	{
        if (c == cmd_sercxi)
        {
            DictIndex index = new DictIndex(vortaro);

            String toSearch = word.getString();
            toSearch = transliterate(toSearch);

            showSearch.setText(toSearch);
            SearchThread t = new SearchThread(toSearch, vortaro, myform);

            SearchScreen s = new SearchScreen(toSearch);
            t.start();
        }
        else if (c == cmd_eliri)
        {
            String tselected = translit.getString(translit.getSelectedIndex());
            Mem.setVariable("Transliterigo", tselected);
            Main.main.quit();
        }
        else if (c == cmd_montri)
        {
            String toSearch = word.getString();
            toSearch = transliterate(toSearch);

            showSearch.setText(toSearch);
        }
        else if (c == cmd_pri)
        {
            ShowText text = new ShowText(
                    /* Versio */
                    "JDict v0.2.3 - Vortara programo laŭ stilo dictd " +
                    "(http://www.dict.org)\n" +
                    "Kopirajto (C) 2007 Lluís Batlle i Rossell\n" +
                    "Tiun ĉi programon mi distribuas laŭ permesilo " +
                    "GPL versio 2 aŭ posta. Vidu la fontokodon por " +
                    "pliaj detaloj.\n" +
                    "Plia informo, pluaj elŝutoj: " +
                    "http://vicerveza.homeunix.net/~viric/dict/jdict"
                    , myform);
        }
        else if (c == cmd_vortarinformo)
        {
            DictIndex index = new DictIndex(vortaro);
            Vector results;
            String text = "";

            /* Already at start */
            results = index.SearchDefinition("00databaseinfo", 5);
            if (results.size() > 0)
                text += "Database Info:\n" + (String) results.elementAt(0)
                    + "\n";

            results = index.SearchDefinition("00databaseshort", 5);
            if (results.size() > 0)
                text += "Database Short:\n" + (String) results.elementAt(0)
                    + "\n";

            results = index.SearchDefinition("00databaseurl", 5);
            if (results.size() > 0)
                text += "Database URL:\n" + (String) results.elementAt(0)
                    + "\n";

            ShowText textwin = new ShowText(text, myform);
        }
        else if (c == cmd_agordoj)
        {
            /* This opens a new form */
            Config config = new Config(this, myform);
        }
	}
}