com.google.gwt.sample.stockwatcher.client.GuiWorldTree.java Source code

Java tutorial

Introduction

Here is the source code for com.google.gwt.sample.stockwatcher.client.GuiWorldTree.java

Source

package com.google.gwt.sample.stockwatcher.client;

import java.util.Random;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.sample.stockwatcher.client.WorldCsgOp.Type;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.*;

class GuiWorldTree extends SplitLayoutPanel {
    class TreeSelectionHandler implements SelectionHandler<TreeItem> {
        TreeSelectionHandler(GuiWorldTree tree) {
            this.tree = tree;
        }

        public void onSelection(SelectionEvent<TreeItem> event) {
            tree.onSelection((GuiObjectTreeItem) event.getSelectedItem());
        }

        GuiWorldTree tree;
    }

    class ObjectCommandInsertCsgOp implements Command {
        ObjectCommandInsertCsgOp(Type type) {
            this.type = type;
        }

        public void execute() {
            WorldNode node = new WorldCsgOp(type);
            listener.onWorldNodeInsert(node);
        }

        Type type;
    }

    class ObjectCommandInsertSphere implements Command {
        public void execute() {
            Random rand = new Random();
            Vec3 rp = new Vec3(0.0f);
            rp.x = (rand.nextFloat() * 2.0f - 1.0f) * 1.0f;
            rp.y = (rand.nextFloat() * 2.0f - 1.0f) * 1.0f;
            rp.z = rand.nextFloat() * 2.0f * 1.0f;
            WorldNode node = new WorldSphere(rp, 0.5f);
            listener.onWorldNodeInsert(node);
        }
    }

    class ObjectCommandInsertBox implements Command {
        public void execute() {
            Random rand = new Random();
            Vec3 rp = new Vec3(0.0f);
            rp.x = (rand.nextFloat() * 2.0f - 1.0f) * 1.0f;
            rp.y = (rand.nextFloat() * 2.0f - 1.0f) * 1.0f;
            rp.z = rand.nextFloat() * 2.0f * 1.0f;
            WorldNode node = new WorldBox(rp, new Vec3(0.5f));
            listener.onWorldNodeInsert(node);
        }
    }

    class ObjectCommandDelete implements Command {
        public void execute() {
            GuiObjectTreeItem item = (GuiObjectTreeItem) guiTree.getSelectedItem();
            listener.onWorldNodeDelete(item.objectId);
        }
    }

    class ObjectCommandMoveUp implements Command {
        public void execute() {
            GuiObjectTreeItem item = (GuiObjectTreeItem) guiTree.getSelectedItem();
            listener.onWorldNodeMoveUp(item.objectId);
        }
    }

    class ObjectCommandMoveDown implements Command {
        public void execute() {
            GuiObjectTreeItem item = (GuiObjectTreeItem) guiTree.getSelectedItem();
            listener.onWorldNodeMoveDown(item.objectId);
        }
    }

    public class Icon implements SafeHtml {
        private static final long serialVersionUID = 1L;

        Icon(String name) {
            this.name = name;
        }

        public String asString() {
            return "<img src='" + GWT.getModuleBaseURL() + "../images/" + name + "' height='16px' width='16px'/>";
        }

        String name;
    }

    public GuiWorldTree(GuiWorldTreeListener listener) {
        this.listener = listener;

        MenuBar insertMenu = new MenuBar(true);
        insertMenu.addItem("Sphere", new ObjectCommandInsertSphere());
        insertMenu.addItem("Box", new ObjectCommandInsertBox());
        insertMenu.addSeparator();
        insertMenu.addItem("Add", new ObjectCommandInsertCsgOp(Type.TypeAdd));
        insertMenu.addItem("Subtract", new ObjectCommandInsertCsgOp(Type.TypeSub));
        insertMenu.addItem("Intersect", new ObjectCommandInsertCsgOp(Type.TypeAnd));

        MenuBar objectMenu = new MenuBar();
        objectMenu.addItem(new Icon("shape_square_add.png"), insertMenu);
        objectMenu.addItem(new Icon("shape_square_delete.png"), new ObjectCommandDelete());
        objectMenu.addItem(new Icon("arrow_up.png"), new ObjectCommandMoveUp());
        objectMenu.addItem(new Icon("arrow_down.png"), new ObjectCommandMoveDown());

        guiTree.addSelectionHandler(new TreeSelectionHandler(this));

        FlowPanel menuFlowPanel = new FlowPanel();
        menuFlowPanel.add(objectMenu);
        menuFlowPanel.add(guiTree);
        menuPanel.add(menuFlowPanel);

        addNorth(menuPanel, 200);
    }

    public void setObjectTree(GuiObjectTreeItem treeItem) {
        guiTree.removeItems();
        guiTree.addItem(treeItem);
        //    for (int i = 0; i < treeItem.getChildCount(); ++i) {
        //      guiTree.addItem(treeItem.getChild(i));
        //    }
    }

    public void onSelection(GuiObjectTreeItem item) {
        if (item == null) {
            remove(editor);
            editorVisible = false;
        } else {
            listener.onSelection(item.objectId);
        }
    }

    public void editProperties(WorldNode node) {
        editor.clear();
        editor.add(new Label(node.typeName()));
        if (!editorVisible) {
            add(editor);
            editorVisible = true;
        }
    }

    GuiWorldTreeListener listener;
    ScrollPanel menuPanel = new ScrollPanel();
    Tree guiTree = new Tree();
    ScrollPanel editor = new ScrollPanel();
    boolean editorVisible = false;
}