Toolbar.java

/*
 * Copyright © 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.paint.views.components;

import com.nokia.example.paint.helpers.ImageLoader;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Toolbar {

    private int width;
    private Vector tools;
    private Image buffer;
    private Image background;
    private Graphics bg;
    private int buf_x = 0;
    private int buf_y = 0;
    public static int HEIGHT = 49;
    private int last_x = 0;

    public Toolbar(int screenwidth) {
        width = screenwidth;
        HEIGHT = (width > 240) ? 74 : 49;
        tools = new Vector();
        addBasicTools();
        buffer = Image.createImage(width, HEIGHT);
        if (HEIGHT == 74) {
            background = ImageLoader.getInstance().loadImage("/toolbar_large.png");
        }
        else {
            background = ImageLoader.getInstance().loadImage("/toolbar_small.png");
        }
        bg = buffer.getGraphics();
    }

    private void addBasicTools() {
        if (width > 240) {
            tools.addElement(new ExitS60());
            tools.addElement(new BrushSizeToolS60());
            tools.addElement(new SaveToolS60());
            tools.addElement(new ClearToolS60());
        }
        else {
            tools.addElement(new Exit());
            tools.addElement(new BrushSizeTool());
            tools.addElement(new SaveTool());
            tools.addElement(new ClearTool());
        }
        setToolsPositions();
    }

    private void setToolsPositions() {
        int l = tools.size();
        //starting positions
        int xpos = width - 1;
        int ypos = 1;
        for (int i = 0; i < l; i++) {

            Button tool = (Button) tools.elementAt(i);
            xpos -= tool.getWidth();
            // Space between exit button and other buttons
            if (i == 1) {
                xpos -= (HEIGHT > 49) ? 5 : 10;
            }
            tool.setPosition(xpos, ypos);
            if (xpos <= 0) {
                xpos = 5;
                ypos += tool.getHeight();
            }
        }
    }

    public void render(Graphics g) {
        bg.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.render(bg);
        }
        g.drawImage(buffer, buf_x, buf_y, Graphics.TOP | Graphics.LEFT);
    }

    public void addTool(Button tool, int index) {
        tools.insertElementAt(tool, index);
        setToolsPositions();
    }

    public void pointerPressed(int x, int y) {
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.pressed(x - buf_x, y);
        }
    }

    public void pointerUnpressed(int x, int y) {
        int l = tools.size();
        for (int i = 0; i < l; i++) {
            Button tool = (Button) tools.elementAt(i);
            tool.unpressed(x - buf_x, y);
        }
    }

    public void dragToolBar(int x, int y) {
        if (last_x == 0) {
            last_x = x;
        }
        int diff = x - last_x;
        if (diff > 0) {
            buf_x += 4;
            last_x = 0;
        }
        else if (diff < 0) {
            buf_x -= 4;
            last_x = 0;
        }
    }
}