DictionaryWidget.java :  » J2ME » Persian-Mobile-Dictionary » pmd » gui » Java Open Source

Java Open Source » J2ME » Persian Mobile Dictionary 
Persian Mobile Dictionary » pmd » gui » DictionaryWidget.java
/* 
 * Copyright (c) 2007, persianmobidict.sourceforge.net
 * Released under the GNU General Public License
 */

package pmd.gui;

import javax.microedition.lcdui.*;

import pmd.dic.En2FaDictionary;
import pmd.midlet.Properties;
import pmd.gui.border.MarginBorder;
import pmd.gui.event.*;

/**
 *
 * @author  Hooman Valibeigi
 */
public class DictionaryWidget extends Widget implements PropertyChangeListener {
    
    public static final int DISPLAY_WORDLIST = 1;
    public static final int DISPLAY_TRANSLATION = 2;
    
    private En2FaDictionary dic;
    private InputWidget inputWidget = new InputWidget();
    private ListWidget listWidget = new ListWidget(this);
    private TranslationWidget translationWidget = new TranslationWidget();
    private int currentDisplay = DISPLAY_WORDLIST;
    
    
    public DictionaryWidget() {
        setBackground(Properties.BACKGROUND_GRAY);
        setBorder(new MarginBorder(3, 3, 3, 3));
        
        inputWidget.addEventListener(this);
        listWidget.addEventListener(this);
        translationWidget.addEventListener(this);
        
        translationWidget.setVisible(false);
        
        addWidget(inputWidget);
        addWidget(listWidget);
        addWidget(translationWidget);
    }
    
    
    public void propertyChanged(Object source, String propertyName) {
        if (source == inputWidget && propertyName.equals("Text")) {
            dic.gotoWord(inputWidget.getText());
            listWidget.repaint();
        }
        else if ((source == listWidget || source == translationWidget) && propertyName.equals("SelectedItem")) {
            inputWidget.setText(dic.getSelectedWord(), false);
        }
    }
    
    public void setCurrentDisplay(int display) {
        if (currentDisplay == display)
            return;
        
        switch (display) {
            case DISPLAY_WORDLIST:
                translationWidget.setVisible(false);
                listWidget.setVisible(true);
                inputWidget.setEditable(true);
                inputWidget.highlight(true);
                break;
            case DISPLAY_TRANSLATION:
                listWidget.setVisible(false);
                translationWidget.reset();
                translationWidget.setVisible(true);
                inputWidget.setEditable(false);
                inputWidget.highlight(false);
                // showKeyboard(false, false);
                break;
            default:
                throw new IllegalArgumentException("No such display");
        }
        
        currentDisplay = display;
        firePropertyChange("Display");
    }
    
    public int getCurrentDisplay() {
        return currentDisplay;
    }
    
    public void processKeyEvent(int eventType, int keyCode, int gameAction, boolean virtual) {
        switch (eventType) {
            case Event.KEY_PRESSED:
                if (keyCode <= 0) {
                    if (gameAction == Canvas.FIRE) {
                        setCurrentDisplay(currentDisplay == DISPLAY_WORDLIST ? DISPLAY_TRANSLATION : DISPLAY_WORDLIST);
                    }
                }
                break;
        }
    }
    
    public void processWidgetEvent(int eventType) {
        if (eventType == Event.WIDGET_RESIZED) {
            Rectangle inBounds = getInteriorBounds();
            int x = inBounds.getX();
            int y = inBounds.getY();
            int w = inBounds.getWidth();
            int h = inputWidget.getMinimumHeight();
            inputWidget.setBounds(x, y, w, h);
            
            y = y + h + 2;
            h = inBounds.getHeight() - h - 2;
            Rectangle rect = new Rectangle(x, y, w, h);
            listWidget.setBounds(rect);
            translationWidget.setBounds(rect);
            
            dic = new En2FaDictionary(listWidget.getVisibleCellCount());
            listWidget.setEn2FaDictionary(dic);
            translationWidget.setEn2FaDictionary(dic);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.