SOURCE CODE - did not fit in previous post
Here is the complete code of my component:
BASE CLASS
public abstract class SwtBase extends BaseWidget implements BaseSwtPortlet, RequesterCallback, EventingClient {
public String swtId;
public Requester requestBuilder;
/** If there is an error we put the message here; If not empty, portlet displays label with that message instead of frame*/
protected String errorContent = “”;
/**
* Event listener called from another widget.
/
public abstract void sageOnEvent(String action, String data);
/*
* A reference to the internal GWT panel
/
public abstract Widget getPanel();
/*
* The XML parser; Gets portlet metadata from XML;
* If something goes wrong, saves error message in errorContent
/
public abstract void parseXml(String xmlData);
/*
* The widget ID used to associate events.
*/
public void setPortletId(String swtId) {
this.swtId = swtId;
}
/**
* The widget ID used to associate events.
*/
public String getPortletId() {
return swtId;
}
/**
* This is the (asynchronous) AJAX metadata call
*/
protected void getData(String url) {
requestBuilder = new Requester(url, this);
}
/**
* This is the common AJAX response callback.
*/
public void onRequestFinished(Requester requester) {
if(requester.error){
Window.alert(requester.response);
}else{
parseXml(requester.response);
buildPortlet();
}
}
/**
* The portlet builder. This builds the portlet based on data got from parseXml;
*/
protected abstract void buildPortlet();
/**
* Event sender to another widget. Events are sent via the event channel
*/
public void sagePublish(String nameSpace,
String topic,
String action,
String data) {
EventChannel.sagePublish(nameSpace,topic,action,data);
}
/**
* Subscribe for event from other widgets. Events are delivered via the event channel
*/
public void sageSubscribe(String nameSpace,
String topic,
String portletId,
Object subscriber) {
EventChannel.sageSubscribe(nameSpace,topic,portletId,subscriber);
}
/**
* <b>Native JSNI:</b> Call an external customised javascript function
*/
protected native void callJs(String script) /*-{
$wnd.setTimeout(script, 100);
}-*/;
}
THE COMPOSITE COMPONENT
public class TabGroupPortlet extends SwtBase implements TabListener,PortletConstants {
private static final String THIS_WIDGET_TYPE = "TabGroupPortlet";
private final FocusPanel wrapPanel;
private final DecoratedTabPanel tabPanel;
private static final TreeMap metadata = new TreeMap();
private static boolean eventReceived = false;
/**
* The constructor reads its bootstrap SData location from the enclosing page.
* It then makes an AJAX call to the bootstrap location for its initialising metadata.
*/
public TabGroupPortlet(WorkspaceItemMetadata itemMetadata) {
setPortletId(itemMetadata.id);
tabPanel = new DecoratedTabPanel();
tabPanel.getDeckPanel().setAnimationEnabled(true);
tabPanel.addTabListener(this);
tabPanel.setWidth("100%");
tabPanel.setHeight("100%");
wrapPanel = new FocusPanel();
wrapPanel.add(tabPanel);
wrapPanel.setWidth("100%");
wrapPanel.setHeight("100%");
getData(itemMetadata.binding);
}
/**
* Access the internal panel widget for display.
*/
@Override
public Widget getPanel() {
return wrapPanel;
}
/**
* The event callback.
*/
@Override
public void sageOnEvent(String action, String data) {
Window.alert(this.getPortletId() + " received an event: " + action + ", " + data);
}
/**
* The SData parser
*/
@Override
public void parseXml(String xmlData) {
Document doc = null;
try {
doc = XMLParser.parse(xmlData);
} catch (DOMException e) {
Window.alert("Can't parse XML:
" + e.toString());
}
if (null != doc) {
Element rootNode = doc.getDocumentElement();
XMLParser.removeWhitespace(rootNode);
NodeList entries = rootNode.getElementsByTagName(ENTRY_NODE);
for (int i=0; i<entries.getLength(); i++) {
Element entry = (Element) entries.item(i);
NamedNodeMap attributes = entry.getAttributes();
if (attributes.getLength() > 0) {
HashMap metaEntry = new HashMap();
metaEntry.put("title", attributes.getNamedItem(TITLE_ATTRIBUTE).getNodeValue());
if (null != attributes.getNamedItem(URL_ATTRIBUTE) &&
!attributes.getNamedItem(URL_ATTRIBUTE).getNodeValue().equals("")) {
metaEntry.put("url", attributes.getNamedItem(URL_ATTRIBUTE).getNodeValue());
}
if (null != attributes.getNamedItem(SCRIPT_ATTRIBUTE) &&
!attributes.getNamedItem(SCRIPT_ATTRIBUTE).getNodeValue().equals("")) {
metaEntry.put("script", attributes.getNamedItem(SCRIPT_ATTRIBUTE).getNodeValue());
}
// Add inter-widget onclick events if present
NodeList eventNodes = entry.getElementsByTagName(EVENT_NODE);
if (null != eventNodes && eventNodes.getLength() > 0) {
ArrayList events = new ArrayList();
for (int j=0; j<eventNodes.getLength(); j++) {
Element event = (Element)eventNodes.item(j);
NamedNodeMap eventAttributes = event.getAttributes();
if (null != eventAttributes && eventAttributes.getLength() > 0) {
String nameSpace = eventAttributes.getNamedItem(NAMESPACE_ATTRIBUTE).getNodeValue();
String direction = eventAttributes.getNamedItem(DIRECTION_ATTRIBUTE).getNodeValue();
HashMap eventItem = new HashMap();
eventItem.put("nameSpace", nameSpace);
eventItem.put("direction", direction);
if (null != eventAttributes.getNamedItem(ACTION_ATTRIBUTE)) {
String action = eventAttributes.getNamedItem(ACTION_ATTRIBUTE).getNodeValue();
eventItem.put("action", action);
}
if (null != event.getElementsByTagName(TOPIC_NODE)) {
String topic = ((Element)event.getElementsByTagName(TOPIC_NODE).item(0)).getFirstChild().getNodeValue();
eventItem.put("topic", topic);
}
if (null != event.getElementsByTagName(DATA_NODE)) {
if (null != ((Element)event.getElementsByTagName(DATA_NODE).item(0))) {
Element dataElm = (Element)event.getElementsByTagName(DATA_NODE).item(0);
if (null != dataElm.getFirstChild()) {
String data = dataElm.getFirstChild().getNodeValue();
eventItem.put("data", data);
}
}
}
events.add(eventItem);
}
}
metaEntry.put("event", events);
}
metadata.put(metaEntry.get("title"), metaEntry);
}
}
} else {
Window.alert("Cannot parse XML: " + xmlData);
}
}
/**
* The tab group builder. This builds the tab group from the SData received.
*/
protected void buildPortlet() {
if (metadata.size() > 0) {
for (Iterator itr=metadata.entrySet().iterator(); itr.hasNext();) {
Map.Entry entry = (Map.Entry)itr.next();
HashMap value = (HashMap)metadata.get(entry.getKey());
if (null != value && value.size() > 0) {
String tabName = (String)value.get("title");
String tabUrl = (String)value.get("url");
Frame frame = new Frame(tabUrl);
tabPanel.add(frame, tabName);
// Subscribe to events if set
if (null != value.get("event")) {
ArrayList events = (ArrayList)value.get("event");
for (int i=0; i<events.size(); i++) {
final HashMap eventItem = (HashMap)events.get(i);
final String nameSpace = (String)eventItem.get("nameSpace");
final String direction = (String)eventItem.get("direction");
final String topic = (String)eventItem.get("topic");
if (direction.equals(SUBSCRIBE)) {
sageSubscribe(nameSpace, topic, this.getPortletId(), this);
}
}
}
}
}
tabPanel.selectTab(0);
}
}
/*
* Tab event listener triggered before the select event is sent.
*/
public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
if (!eventReceived) {
String url = (String)((HashMap)(metadata.values().toArray()[tabIndex])).get("url");
String tabTitle = (String)((HashMap)(metadata.values().toArray()[tabIndex])).get("title");
String script = (String)((HashMap)(metadata.values().toArray()[tabIndex])).get("script");
// Send an event if set in metadata
if (null != ((HashMap)(metadata.values().toArray()[tabIndex])).get("event")) {
final ArrayList events = (ArrayList)((HashMap)(metadata.values().toArray()[tabIndex])).get("event");
for (int i=0; i<events.size(); i++) {
final HashMap eventItem = (HashMap)events.get(i);
final String nameSpace = (String)eventItem.get("nameSpace");
final String direction = (String)eventItem.get("direction");
final String action = (String)eventItem.get("action");
final String topic = (String)eventItem.get("topic");
final String data = (String)eventItem.get("data");
if (direction.equals(PUBLISH)) {
sagePublish(nameSpace, topic, action, data);
}
}
}
// Navigate to an url if set in metadata
if (null != url && !url.equals("")) {
Frame frame = new Frame(url);
tabPanel.remove(tabIndex);
tabPanel.insert(frame, tabTitle, tabIndex);
}
// Call an external javascript function
if (null != script && !script.equals("")) {
callJs(script);
}
}
return true;
}
/*
* Tab event listener triggered when the select event is sent.
*/
public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
//resize(tabIndex);
}
private void resize(int tabIndex) {
// resize fix:- see http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/033d073d49de3598
int height = Window.getClientHeight() - tabPanel.getDeckPanel().getAbsoluteTop() - 8;
tabPanel.getWidget(tabIndex).setHeight("" + height);
}
}