Google Web Toolkit (GWT) – Generate FileMenu from XML

21 06 2009

I have been playing around with GWT the last few weeks and wrote a nice little function that reads a XML file and builds a FileMenu.

Real GWT programmers will probably frown on the code but please keep in mind this is only for fun. The XML structure looks like this:


links

mainLink name='My Links'
subLink name='Blog' url='http://fredreh.wordpress.com' title='My Blog' /subLink
subLink name='Facebook' url='http://facebook.com' title='See some friends' /subLink
/mainLink

mainLink name='Test'
subLink name='Tester' url='http://www.test.ac.za/' title='Tester' /subLink
/mainLink

mainLink name='Search Engines'
subLink name='Google' url='http://www.google.com/' title='Google' /subLink
subLink name='Wolfram' url='http://www.google.com/' title='Wolfram' /subLink
/mainLink

/links

The file name is links.xml the function BuildMenu() looks like this:


public void BuildMenu()
{
//Build the menus
//The requist builder for the XML passwords
RequestBuilder buildL = new RequestBuilder(RequestBuilder.GET,"links.xml");
//The main menu
final MenuBar linksmen = new MenuBar(true);
try
{
buildL.sendRequest(null,new RequestCallback()
{
public void onError(Request request, Throwable exception)
{
Window.alert("Could not get a list of Links for MenuBar");
}

public void onResponseReceived(Request request, Response response)
{

//Check the xml file and load the nodes inside
Element TopLvl = XMLParser.parse(response.getText()).getDocumentElement();

//Get a list of nodes under the main node
NodeList mainLink = TopLvl.getElementsByTagName("mainLink");

//Loop through the list of mainLink nodes
for(int j=0;j<mainLink.getLength();j++)
{
//Create the Element representing a
Element main = (Element)mainLink.item(j);

//The menubar representing a
MenuBar MLink = new MenuBar(true);

//A list of nodes inside the each one is a
NodeList subLink = main.getElementsByTagName("subLink");

//Loop through the list of subLink nodes
for(int i=0;i<subLink.getLength();i++)
{
//Create the Element representing a
final Element sub = (Element)subLink.item(i);
//Add the subLink details to the Mlink MenuBar
MLink.addItem(sub.getAttribute("name"),new Command(){public void execute(){Window.open(sub.getAttribute("url"),sub.getAttribute("title"),"");}});
}

//Add the completed MLink menubar to the main linksmen MenuBar
linksmen.addItem( main.getAttribute("name"),MLink);
}

}
}
);
}
catch (RequestException e)
{
Window.alert("Error");
return;
}

GLeftMen.setWidget(0,0,lblLinks);
GLeftMen.setWidget(1,0,linksmen);
}

I commented the code heavily and it shoud be clear.

Looking at the code I wonder how difficult it would be to take the XML generated by Glade and then build an interface when the user loads the page.

Anyway I hope someone can use the code it is licenced under WTFPL


Actions

Information

One response

21 06 2009
Google Web Toolkit (GWT) – Generate FileMenu from XML « Adobe Tutorials

[...] See original here: Google Web Toolkit (GWT) – Generate FileMenu from XML [...]

Leave a comment