package mindmap.client;
import com.google.gwt.gears.client.Factory;
import com.google.gwt.gears.client.blob.Blob;
import com.google.gwt.gears.client.desktop.Desktop;
import com.google.gwt.gears.client.desktop.File;
import com.google.gwt.gears.client.desktop.OpenFilesHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.NativeCheckboxItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
/**
* A toolbar for the mind map web application.
* @author Stuart Clarke
*/
public class Navigator extends ToolStrip {
/**
* The TextItem used to entre the URL of the remote XML.
*/
static TextItem URL;
/**
* The TextItem used to entre the desired depth of the graph.
*/
static TextItem DEPTH;
/**
* The CheckboxItem to enable Force Interaction.
*/
static NativeCheckboxItem AUTO;
/**
* The CheckboxItem to enable 3D Graphs.
*/
static NativeCheckboxItem GRAPH_3D;
/**
* Constructs the Navigator ToolStrip placing the given URL
* into the URL TextItem.
* @param url The URL of the remote XML
*/
public Navigator(String url){
setWidth(Home.WIDTH);
setBackgroundColor("#F8F8F8");
setPosition(Positioning.ABSOLUTE);
DynamicForm form = new DynamicForm();
form.setWidth100();
form.setNumCols(11);
form.setColWidths(120,30,100,"*",60,80,80,10,160,10,120);
DEPTH = new TextItem("depth","Initial Depth");
DEPTH.setWidth("*");
DEPTH.setValue(String.valueOf(Home.VIEW - 1));
URL = new TextItem("location","Online XML");
URL.setWidth("*");
URL.setValue(url);
URL.addKeyPressHandler(new KeyPressHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.KeyPressHandler#onKeyPress(com.smartgwt.client.widgets.form.fields.events.KeyPressEvent)
*/
public void onKeyPress(KeyPressEvent event){
if(event.getKeyName().equals("Enter")){
request((String) URL.getValue(), Integer.valueOf((String) DEPTH.getValue()));
}
}
});
DEPTH.addKeyPressHandler(new KeyPressHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.KeyPressHandler#onKeyPress(com.smartgwt.client.widgets.form.fields.events.KeyPressEvent)
*/
public void onKeyPress(KeyPressEvent event){
if(event.getKeyName().equals("Enter")){
request((String) URL.getValue(), Integer.valueOf((String) DEPTH.getValue()));
}
}
});
ButtonItem load = new ButtonItem("load","Load");
load.setStartRow(false);
load.setEndRow(false);
load.setWidth("*");
load.addClickHandler(new ClickHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.ClickHandler#onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent)
*/
public void onClick(ClickEvent event){
request((String) URL.getValue(), Integer.valueOf((String) DEPTH.getValue()));
}
});
ButtonItem centre = new ButtonItem("centre","Centre");
centre.setStartRow(false);
centre.setEndRow(false);
centre.setWidth("*");
centre.addClickHandler(new ClickHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.ClickHandler#onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent)
*/
public void onClick(ClickEvent event){
MindMap.get(MindMap.FOCUS_ID).centre();
}
});
ButtonItem local = new ButtonItem("local","Local XML");
local.setStartRow(false);
local.setEndRow(false);
local.setAutoFit(false);
local.setWidth("*");
local.addClickHandler(new ClickHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.ClickHandler#onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent)
*/
public void onClick(ClickEvent event){
try{
Desktop desktop = Factory.getInstance().createDesktop();
desktop.openFiles(new OpenFilesHandler(){
/* (non-Javadoc)
* @see com.google.gwt.gears.client.desktop.OpenFilesHandler#onOpenFiles(com.google.gwt.gears.client.desktop.OpenFilesHandler.OpenFilesEvent)
*/
public void onOpenFiles(OpenFilesEvent event){
File[] files = event.getFiles();
File file = files[0];
Blob data = file.getBlob();
BlobReader reader = new BlobReader(data);
String map = "";
while(!reader.endOfBlob())
map = map + reader.readLine() + "\n";
Home.webError(map);
Home.URLRead(map,Integer.valueOf((String) DEPTH.getValue()));
}
}, true);
}catch(Exception e){
Home.webError("Google Gears Plugin Required for this feature.");
}
}
});
AUTO = new NativeCheckboxItem("auto");
AUTO.setTitle("Apply Force Interaction");
AUTO.setStartRow(false);
AUTO.setEndRow(false);
AUTO.setValue(false);
AUTO.addChangeHandler(new ChangeHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.ChangeHandler#onChange(com.smartgwt.client.widgets.form.fields.events.ChangeEvent)
*/
public void onChange(ChangeEvent event){
if(Boolean.valueOf(String.valueOf(AUTO.getValue()))){
Position.AUTO = false;
AUTO.setValue(false);
Position.cancel();
}
else{
Position.AUTO = true;
AUTO.setValue(true);
Position.forceDrivenPositioning();
}
}
});
GRAPH_3D = new NativeCheckboxItem("3d");
GRAPH_3D.setTitle("3D Graph");
GRAPH_3D.setStartRow(false);
GRAPH_3D.setEndRow(false);
GRAPH_3D.setValue(false);
GRAPH_3D.addChangeHandler(new ChangeHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.form.fields.events.ChangeHandler#onChange(com.smartgwt.client.widgets.form.fields.events.ChangeEvent)
*/
public void onChange(ChangeEvent event){
if(Boolean.valueOf(String.valueOf(GRAPH_3D.getValue()))){
Position.cancel3D();
Position.GRAPH_3D = false;
GRAPH_3D.setValue(false);
AUTO.enable();
Position.forceDrivenPositioning();
MindMap.setCanDragEntity(true);
}
else{
Position.AUTO = true;
Position.GRAPH_3D = true;
Position.setRandomZ();
AUTO.setValue(true);
GRAPH_3D.setValue(true);
AUTO.disable();
Position.forceDrivenPositioning();
MindMap.get(MindMap.FOCUS_ID).centre();
MindMap.setCanDragEntity(false);
}
}
});
form.setFields(DEPTH,URL,load,local,centre,AUTO,GRAPH_3D);
addMember(new Label(Home.VERSION));
addMember(form);
if(!url.equals(""))
request(url,Integer.parseInt(String.valueOf(Home.VIEW - 1)));
}
/**
* Builds a URL request for the remote XML, once received, sends to the
* Home class to process.
* @param url The URL of the remote XML
* @param levels The depth that the graph should be drawn to, only valid
* if the XML represents a new graph.
*/
public static void request(String url, final int levels){
RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
try {
requestBuilder.sendRequest(null, new RequestCallback() {
/* (non-Javadoc)
* @see com.google.gwt.http.client.RequestCallback#onError(com.google.gwt.http.client.Request, java.lang.Throwable)
*/
public void onError(Request request, Throwable exception) {
Home.webError("Error: " + exception);
}
/* (non-Javadoc)
* @see com.google.gwt.http.client.RequestCallback#onResponseReceived(com.google.gwt.http.client.Request, com.google.gwt.http.client.Response)
*/
public void onResponseReceived(Request request, Response response) {
Home.URLRead(response.getText(),levels);
}
});
} catch (RequestException ex) {
Home.webError("Error: " + ex);
}
}
/**
* Sets the content of the URL TextItem.
* @param url The URL of the remote XML
*/
public void setURL(String url){
URL.setValue(url);
}
/**
* Update the location of the Navigator ToolStrip, i.e. ensuring it is
* located in the centre at the top of the screen.
*/
public void update(){
setWidth(Window.getClientWidth());
}
/**
* Gets the content of the URL TextItem.
* @return The contents of the URL TextItem.
*/
public String getURL() {
return (String) URL.getValue();
}
/**
* Gets the content of the depth TextItem.
* @return The contents of the depth TextItem.
*/
public int getDepth() {
return Integer.valueOf((String) DEPTH.getValue());
}
}