package mindmap.client;
import java.util.LinkedList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.Document;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.xml.client.XMLParser;
import com.smartgwt.client.widgets.layout.VLayout;
/**
* Entry Point for the Mind Map Web Application.
* @author Stuart Clarke
*/
public class Home implements EntryPoint {
/**
* The MindMap object commencing at the focus.
*/
protected static MindMap FOCUS;
/**
* Radius of a single level circle or sphere.
*/
protected static int RADIUS = 200;
/**
* Size of the focus image and all entities on the
* z=0 plane.
*/
protected static int IMAGE_SIZE;
/**
* Level to set as the view point on the z axis.
*/
protected static int VIEW = 2;
/**
* Height of the client window.
*/
protected static int HEIGHT;
/**
* Width of the client window.
*/
protected static int WIDTH;
/**
* Image size if place 1 level from view point.
*/
protected static int IMAGE_PIXEL_SIZE = 200;
/**
* Version.
*/
protected static String VERSION = "V0.5";
/**
* Navigation bar for the Mind Map web application.
*/
protected static Navigator NAVIGATOR;
/**
* The Canvas object to draw the Mind Map on.
*/
protected static DraggableCanvas CANVAS;
/**
* Displays error message.
* @param position Position error occured in the code.
*/
public static void webError(String position){
Window.alert("Error loading the page.\n" + position);
}
/**
* Parse the XML and creates or updates as required.
* @param map The XML string to be read.
* @param levels The number of levels to display.
*/
public static void URLRead(String map, int levels){
try{
Document doc = XMLParser.parse(map);
XMLParser.removeWhitespace(doc);
if (doc != null) {
Node rootnode = null;
for(int ii = 0; ii < doc.getChildNodes().getLength(); ii++){
if(doc.getChildNodes().item(ii).getNodeName().equals("graph"))
rootnode = doc.getChildNodes().item(ii);
}
if(rootnode != null){
for(int i = 0;i < rootnode.getAttributes().getLength();i++){
if(rootnode.getAttributes().item(i).getNodeName().equals("levels")){
levels = Integer.parseInt(rootnode.getAttributes().item(i).getNodeValue());
Navigator.DEPTH.setValue(String.valueOf(levels));
}
if(rootnode.getAttributes().item(i).getNodeName().equals("radius")){
RADIUS = Integer.parseInt(rootnode.getAttributes().item(i).getNodeValue());
Position.setB();
}
if(rootnode.getAttributes().item(i).getNodeName().equals("viewlevel"))
VIEW = Integer.parseInt(rootnode.getAttributes().item(i).getNodeValue());
if(rootnode.getAttributes().item(i).getNodeName().equals("imgsizeatview"))
IMAGE_PIXEL_SIZE = Integer.parseInt(rootnode.getAttributes().item(i).getNodeValue());
if(rootnode.getAttributes().item(i).getNodeName().equals("forces"))
if(rootnode.getAttributes().item(i).getNodeValue().equals("Y")){
Position.AUTO = true;
Navigator.AUTO.setValue(true);
}
if(rootnode.getAttributes().item(i).getNodeName().equals("threedimension"))
if(rootnode.getAttributes().item(i).getNodeValue().equals("Y")){
Position.GRAPH_3D = true;
Navigator.GRAPH_3D.setValue(true);
}
if(rootnode.getAttributes().item(i).getNodeName().equals("wrapoperation"))
if(rootnode.getAttributes().item(i).getNodeValue().equals("Y"))
Operations.WRAP = true;
}
Node node = rootnode.getFirstChild();
if(node.getNodeName().equals("focus"))
load(node,levels);
else if(node.getNodeName().equals("update")){
if(FOCUS != null){
reload(node);
}
}else
webError("XML Error, check schema at " + node.getNodeName());
}else
webError("XML Error, check schema at graph " + doc.getNodeName());
}else
webError("Document Error.");
}catch(Exception e){
webError("XML Parsing Error.\n" + e.toString().substring(0, 1000) + "...");
}
Position.forceDrivenPositioning();
}
/**
* Reload the MindMap with the specified update.
* @param node XML node representing the update required.
*/
public static void reload(Node node) {
for(int i = 0; i < node.getChildNodes().getLength(); i++){
Node childNode = node.getChildNodes().item(i);
if(childNode.getNodeName().equals("add")){
int parentID = Integer.parseInt(childNode.getAttributes().getNamedItem("parent_id").getNodeValue());
MindMap entity = MindMap.get(parentID);
if(entity!=null)
entity.add(childNode.getChildNodes().item(0));
}else if(childNode.getNodeName().equals("remove")){
String string = childNode.getFirstChild().getNodeValue();
String[] values = string.split("\\s");
for(String id : values){
MindMap entity = MindMap.get(Integer.parseInt(id));
if(entity!=null)
entity.remove();
}
Connection.update(MindMap.CONNECTIONS.values());
}else if(childNode.getNodeName().equals("set")){
for(int ii = 0; ii<childNode.getChildNodes().getLength(); ii++){
Node setNode = childNode.getChildNodes().item(ii);
if(setNode.getNodeName().equals("entity")){
int id = Integer.parseInt(setNode.getAttributes().getNamedItem("id").getNodeValue());
MindMap entity = MindMap.get(id);
if(entity!=null)
entity.set(setNode);
}
}
}else
Home.webError(childNode.getNodeName());
}
}
/**
* Load the new MindMap from the specified XML node.
* @param node XML node representing an entity within the network.
* @param levels The number of levels to display.
*/
public static void load(Node node, int levels) {
MindMap.clear();
IMAGE_SIZE = (int) (IMAGE_PIXEL_SIZE / (2*VIEW));
Position position = new Position(new Arc(),new Coord(3*HEIGHT/2,3*WIDTH/2));
FOCUS = new MindMap(node,position);
FOCUS.draw(levels);
}
/* (non-Javadoc)
* @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
*/
public void onModuleLoad() {
WIDTH = Window.getClientWidth();
HEIGHT = Window.getClientHeight();
Operations.TOTAL_OPERATIONS = new LinkedList<VLayout>();
NAVIGATOR = new Navigator(History.getToken());
History.addValueChangeHandler(new ValueChangeHandler<String>(){
/**
* Loads the XML according to the client browser nav bar.
* @param event The value change event fired as a result of the client
* browser nav bar changing.
*/
public void onValueChange(ValueChangeEvent<String> event){
NAVIGATOR.setURL(History.getToken());
int depth = NAVIGATOR.getDepth();
Navigator.request(NAVIGATOR.getURL(),depth);
}
});
RootPanel.get().add(NAVIGATOR, Window.getScrollLeft(), Window.getScrollTop());
Window.addResizeHandler(new ResizeHandler(){
/* (non-Javadoc)
* @see com.google.gwt.event.logical.shared.ResizeHandler#onResize(com.google.gwt.event.logical.shared.ResizeEvent)
*/
public void onResize(ResizeEvent event){
WIDTH = Window.getClientWidth();
HEIGHT = Window.getClientHeight();
NAVIGATOR.update();
CANVAS.setWidth(3*WIDTH);
CANVAS.setHeight(3*HEIGHT);
CANVAS.setLeft(-WIDTH);
CANVAS.setTop(-HEIGHT);
if(FOCUS!=null)
FOCUS.centre();
MindMap.bringEntitiesToFront();
}
});
Window.enableScrolling(false);
Position.setB();
Position.setTimer();
CANVAS = new DraggableCanvas();
RootPanel.get().add(CANVAS,0,0);
CANVAS.setLeft(-WIDTH);
CANVAS.setTop(-HEIGHT);
CANVAS.sendToBack();
MindMap.initiate();
}
}