AddLandmarkView.java

/**
 * Copyright (c) 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.compass.views;

import com.nokia.example.compass.CompassMidlet;
import com.nokia.example.compass.LandmarkManager;
import com.nokia.example.compass.helpers.ImageLoader;
import com.nokia.example.compass.helpers.KeyCodes;
import java.io.IOException;

import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.location.QualifiedCoordinates;

public class AddLandmarkView {

    private Image background;

    private Image locator_input;
    private Image locator_input_highlight;

    private boolean ishighlight = false;

    private boolean isShown = false;
    private String landmarkname = "";
    private String shortenedName = "";

    private LandmarkNameBox box;

    private LandmarkManager manager;

    private int y = 240;

    private boolean hide = false;
    private static final int default_anchor = Graphics.TOP|Graphics.LEFT;

    public AddLandmarkView(CompassView view) {

        loadImages();
        box = new LandmarkNameBox(this);
        manager = new LandmarkManager();
    }

    private void loadImages() {
        try {
            background = ImageLoader.getInstance().loadImage("/menu-background.png");
            locator_input = ImageLoader.getInstance().loadImage("/locator-input.png");
            locator_input_highlight = ImageLoader.getInstance().loadImage("/locator-input-highlight.png");
        }catch(IOException io) {
            System.out.println("IOError:"+io.getMessage());
        }
    }

    public void draw(Graphics g) {

        if(isShown) {
            //animate
            if(y > 0 && !hide) {
                y -= 40;
                g.setClip(0, y, 240, 320 -y);
            }else if(hide && y < 320) {
                y += 40;
                g.setClip(0, y, 240, 320 -y);
                if(y == 320) {
                    isShown = false;
                    hide = false;
                }
            }
            g.drawImage(background, 0, y, default_anchor);
            g.setColor(0xFFFFFF);
            g.setFont(CompassView.SMALL_FONT);
            g.drawString("Save current location",120 ,y + 100, Graphics.TOP|Graphics.HCENTER);
            g.drawString("cancel", 240, y+320, Graphics.BOTTOM| Graphics.RIGHT);
            g.drawString("Save", 0, y+320, g.BOTTOM|g.LEFT);
            g.drawString("name:", 20,y+ 242- CompassView.SMALL_FONT.getHeight(), default_anchor);
            if(!ishighlight) {
                g.drawImage(locator_input, 0,y+ 242, default_anchor);
            }else {
                g.drawImage(locator_input_highlight, 0,y+ 242, default_anchor);
                g.drawString("Write", 120,y+ 320, g.HCENTER|g.BOTTOM);
            }
            g.drawString(shortenedName,120,y+ 242, g.HCENTER|g.TOP);

        }
    }
    public void show() {
        shortenedName = "";
        landmarkname = "";
        isShown = true;
        y = 240;
    }
    public boolean isShown() {
        return isShown;
    }

    public void notifyKeyEvents(int keyCode) {

        switch(keyCode) {
            case KeyCodes.RIGHT_SOFTKEY:
                hide = true;
                y = 80;
                break;
            case KeyCodes.LEFT_SOFTKEY:
                if (landmarkname.length() > 0) {
                    QualifiedCoordinates coords = CompassMidlet.getInstance().getCompassView().getCurrentCoordinates();
                    if (manager.saveLandmark(landmarkname, coords)) {
                        isShown = false;
                        CompassMidlet.getInstance().showAlert("Saved!");
                        CompassMidlet.getInstance().getCompassView().refreshLandmarks();
                    }
                }else {
                    CompassMidlet.getInstance().showAlert("Name missing!", AlertType.ERROR);
                }
                break;
            case KeyCodes.DOWN:
            case KeyCodes.UP:
                if(ishighlight) {
                    ishighlight = false;
                }else {
                    ishighlight = true;
                }
                break;
            case KeyCodes.MIDDLE_SOFT_KEY:
                if(ishighlight) {
                    Display.getDisplay(CompassMidlet.getInstance()).setCurrent(box);

                }
                break;
        }

    }
    public void setLandmarkName(String name) {
        landmarkname = name;
        int l = landmarkname.length();
        if(CompassView.SMALL_FONT.stringWidth(landmarkname) > 180) {
            for(int i = l -1; i > 0; i--) {
                if(CompassView.SMALL_FONT.substringWidth(landmarkname, 0, i) < 180) {
                    shortenedName = landmarkname.substring(0, (i - 3)) + "...";
                    break;
                }
            }
         }else {
            shortenedName = landmarkname;
         }
    }

}