/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.SVGImage;
import org.netbeans.microedition.svg.SVGActionListener;
import org.netbeans.microedition.svg.SVGButton;
import org.netbeans.microedition.svg.SVGComponent;
import org.netbeans.microedition.svg.SVGForm;
import org.netbeans.microedition.svg.SVGLabel;

/**
 * @author avk
 */
public class VisualMIDlet extends MIDlet {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private SVGForm svgForm;
    private SVGButton svgButton;
    private SVGLabel svgLabel;
    private SVGButton svgButton1;
    private SVGForm svgForm1;
    private SVGLabel svgLabel1;
    private SVGLabel svgLabel2;
    private SVGButton svgButton2;
    private SVGImage svgImage;
    private SVGImage svgImage1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The VisualMIDlet constructor.
     */
    public VisualMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSvgForm().getSvgCanvas());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgForm ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of svgForm component.
     * @return the initialized component instance
     */
    public SVGForm getSvgForm() {
        if (svgForm == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            svgForm = new SVGForm(getSvgImage(), getDisplay());//GEN-BEGIN:|14-getter|1|14-postInit
            svgForm.setTitle("svgForm");
            svgForm.add(getSvgLabel());
            svgForm.add(getSvgButton());
            svgForm.add(getSvgButton1());//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return svgForm;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgForm1 ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of svgForm1 component.
     * @return the initialized component instance
     */
    public SVGForm getSvgForm1() {
        if (svgForm1 == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            svgForm1 = new SVGForm(getSvgImage1(), getDisplay());//GEN-BEGIN:|21-getter|1|21-postInit
            svgForm1.setTitle("svgForm1");
            svgForm1.add(getSvgLabel1());
            svgForm1.add(getSvgLabel2());
            svgForm1.add(getSvgButton2());//GEN-END:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return svgForm1;
    }
    //</editor-fold>//GEN-END:|21-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgLabel ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of svgLabel component.
     * @return the initialized component instance
     */
    public SVGLabel getSvgLabel() {
        if (svgLabel == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            svgLabel = new SVGLabel(getSvgForm(), "label_0");//GEN-BEGIN:|41-getter|1|41-postInit
            svgLabel.setText("Collect Blocks");//GEN-END:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return svgLabel;
    }
    //</editor-fold>//GEN-END:|41-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgButton ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of svgButton component.
     * @return the initialized component instance
     */
    public SVGButton getSvgButton() {
        if (svgButton == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            svgButton = new SVGButton(getSvgForm(), "button_0");//GEN-BEGIN:|43-getter|1|43-postInit
            svgButton.setText("Play");//GEN-END:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return svgButton;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgButton1 ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initiliazed instance of svgButton1 component.
     * @return the initialized component instance
     */
    public SVGButton getSvgButton1() {
        if (svgButton1 == null) {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            svgButton1 = new SVGButton(getSvgForm(), "button_1");//GEN-BEGIN:|45-getter|1|45beforeSwitch
            svgButton1.setText("Exit");
            svgButton1.addActionListener(new SVGActionListener() {
                public void actionPerformed(SVGComponent svgComponent) {//GEN-END:|45-getter|1|45beforeSwitch
                    //Some action before exit Midlet
                    exitMIDlet();//GEN-BEGIN:|45-getter|2|45-postInit
                }
            });//GEN-END:|45-getter|2|45-postInit
            // write post-init user code here
        }//GEN-BEGIN:|45-getter|3|
        return svgButton1;
    }
    //</editor-fold>//GEN-END:|45-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgImage ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of svgImage component.
     * @return the initialized component instance
     */
    public SVGImage getSvgImage() {
        if (svgImage == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|22-getter|1|22-@java.io.IOException
                svgImage = (SVGImage) SVGImage.createImage(getClass().getResourceAsStream("/svg/main_form.svg"), null);
            } catch (java.io.IOException e) {//GEN-END:|22-getter|1|22-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|22-getter|2|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|3|
        return svgImage;
    }
    //</editor-fold>//GEN-END:|22-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgLabel1 ">//GEN-BEGIN:|48-getter|0|48-preInit
    /**
     * Returns an initiliazed instance of svgLabel1 component.
     * @return the initialized component instance
     */
    public SVGLabel getSvgLabel1() {
        if (svgLabel1 == null) {//GEN-END:|48-getter|0|48-preInit
            // write pre-init user code here
            svgLabel1 = new SVGLabel(getSvgForm1(), "label_0");//GEN-BEGIN:|48-getter|1|48-postInit
            svgLabel1.setText("The Winner Is:");//GEN-END:|48-getter|1|48-postInit
            // write post-init user code here
        }//GEN-BEGIN:|48-getter|2|
        return svgLabel1;
    }
    //</editor-fold>//GEN-END:|48-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgLabel2 ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of svgLabel2 component.
     * @return the initialized component instance
     */
    public SVGLabel getSvgLabel2() {
        if (svgLabel2 == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            svgLabel2 = new SVGLabel(getSvgForm1(), "label_1");//GEN-BEGIN:|50-getter|1|50-postInit
            svgLabel2.setText("Label");//GEN-END:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return svgLabel2;
    }
    //</editor-fold>//GEN-END:|50-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgButton2 ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of svgButton2 component.
     * @return the initialized component instance
     */
    public SVGButton getSvgButton2() {
        if (svgButton2 == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            svgButton2 = new SVGButton(getSvgForm1(), "button_0");//GEN-BEGIN:|52-getter|1|52beforeSwitch
            svgButton2.addActionListener(new SVGActionListener() {
                public void actionPerformed(SVGComponent svgComponent) {//GEN-END:|52-getter|1|52beforeSwitch
                    //Some action before switch
                    switchDisplayable(null, getSvgForm().getSvgCanvas());//GEN-LINE:|52-getter|2|52afterSwitch
                    //Some action after switch
                }//GEN-BEGIN:|52-getter|3|52-postInit
            });//GEN-END:|52-getter|3|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|4|
        return svgButton2;
    }
    //</editor-fold>//GEN-END:|52-getter|4|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgImage1 ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of svgImage1 component.
     * @return the initialized component instance
     */
    public SVGImage getSvgImage1() {
        if (svgImage1 == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|47-getter|1|47-@java.io.IOException
                svgImage1 = (SVGImage) SVGImage.createImage(getClass().getResourceAsStream("/svg/game_result.svg"), null);
            } catch (java.io.IOException e) {//GEN-END:|47-getter|1|47-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|47-getter|2|47-postInit
            // write post-init user code here
        }//GEN-BEGIN:|47-getter|3|
        return svgImage1;
    }
    //</editor-fold>//GEN-END:|47-getter|3|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
