Here we go…
Code for creating the navigation tree:
private NavigationTree getNavigationComponents()
{
setTreeComponents(new NavigationTree());
Button buttonItem1 = new Button("Section.1");
buttonItem1.setIcon(navigationIconSection);
Button buttonItem2 = new Button("Section.2");
buttonItem2.setIcon(navigationIconSection);
Button buttonItem3 = new Button("Section.3");
buttonItem3.setIcon(navigationIconSection);
Button buttonItem4 = new Button("Section.4");
buttonItem4.setIcon(navigationIconSection);
getTreeComponents().addItem("Section 1", buttonItem1, new ThemeResource(ResourceFactory.ICON_TREE_ITEM));
getTreeComponents().addItem("Section 2", buttonItem2, new ThemeResource(ResourceFactory.ICON_TREE_ITEM));
getTreeComponents().addItem("Section 3", buttonItem3, new ThemeResource(ResourceFactory.ICON_TREE_ITEM));
getTreeComponents().addItem("Section 4", buttonItem4, new ThemeResource(ResourceFactory.ICON_TREE_ITEM));
ItemClickListener myListener = new ItemClickListener() {
public void itemClick(ItemClickEvent event) {
Item clicked = event.getItem();
Component c = (Component) clicked.getItemProperty("view").getValue();
getMainApplication().showTrayNotification("DEBUG - " + getContext(), "Loading Section: " + c.getCaption());
selectNavigationItem(c.getCaption());
}
};
getTreeComponents().addListener(myListener);
return getTreeComponents();
}
Code for selecting/loading an item in the navigation tree:
public void selectNavigationItem(String pNavItem)
{
StringTokenizer st = new StringTokenizer(pNavItem, ".");
if (st.countTokens() < 2)
{
getWindow().showNotification ("ERROR - " + getContext(), "Unable to process the navigation request, invalid Section (" + pNavItem + ")", Notification.TYPE_TRAY_NOTIFICATION);
}
String subSection = st.nextToken();
String methodName = "load" + subSection;
loadContainer(subSection, st.nextToken());
}
private void loadContainer(String pSubSection, String pComp)
{
// release the previously loaded section
loadedSection = null;
try {
Class compClass = null;
Class[] argsClass = new Class[]
{ MyWebClient.class, String.class };
Object[] argsObject = new Object[]
{ getMainApplication(), getContext() };
Constructor argsConstructor;
String packageName = this.getClass().getPackage().getName();
// instantiate class through reflection
compClass = Class.forName(packageName + "." + pSubSection.toLowerCase() + "." + pComp + "Container");
// check if constructor is available
argsConstructor = compClass.getConstructor(argsClass);
// Create new instance of the section
Object tmpSection = argsConstructor.newInstance(argsObject);
Method getMethod = tmpSection.getClass().getMethod("getContentPanel", null);
getMainPanel().setSecondComponent((MyCustomVerticalSplitPanel) getMethod.invoke(tmpSection, null));
// keep reference to this object so that we can release it later when required
loadedSection = tmpSection;
}
catch (Exception exc)
{
String errMsg = "section \"" + pSubSection + "\" could not be loaded (" + exc.getMessage() + ")";
getWindow().showNotification ("ERROR", errMsg, Notification.TYPE_TRAY_NOTIFICATION);
}
}
public MyCustomVerticalSplitPanel getContentPanel()
{
if (contentPanel == null)
{
try
{
initComponentPanel();
initRelationPanel();
contentPanel = new MyCustomVerticalSplitPanel(200);
contentPanel.setFirstComponent(getComponentPanel());
contentPanel.setSecondComponent(getRelationPanel());
}
catch (Exception exc)
{
getMainApplication().showErrorMessage("An unexpected error occured while loading the content panel for " + getBeanType().getName() + " (" + exc.getMessage() + ")");
if (!getMainApplication().getApplicationServlet().isProductionMode())
{
exc.printStackTrace(System.err);
}
}
}
return contentPanel;
}
public void initComponentPanel()
{
setComponentPanel(new ComponentPanel(getMainApplication()));
getComponentPanel().addComponent(getToolbar());
getComponentPanel().addComponent(getTable());
getComponentPanel().setExpandRatio(getTable(), 1.0f);
}
public void initRelationPanel()
{
setRelationPanel(new RelationPanel(getMainApplication()));
createRelationTabs();
loadRelationTabs(null);
}
My custom classes:
Custom ComponentPanel class:
public class ComponentPanel extends VerticalLayout {
private MyWebClient mainApplication = null;
public ComponentPanel(MyWebClient pMainApplication)
{
setMainApplication(pMainApplication);
init();
}
protected void init()
{
setSizeFull();
}
public void setMainApplication(MyWebClient pMainApplication) {
mainApplication = pMainApplication;
}
public MyWebClient getMainApplication() {
return mainApplication;
}
}
Custom RelationPanel class:
public class RelationPanel extends TabSheet {
private MyWebClient mainApplication = null;
public RelationPanel(MyWebClient pMainApplication)
{
setMainApplication(pMainApplication);
init();
}
protected void init()
{
setWidth("100%");
setStyleName("minimal");
addStyleName("admin");
}
public void setMainApplication(MyWebClient pMainApplication) {
mainApplication = pMainApplication;
}
public MyWebClient getMainApplication() {
return mainApplication;
}
}
Custom NavigationTree class:
public class NavigationTree extends Tree {
public NavigationTree()
{
init();
}
private void init()
{
setImmediate(true);
addContainerProperty("caption", String.class, null);
addContainerProperty("view", Component.class, null);
addContainerProperty("icon", ThemeResource.class, null);
setItemCaptionPropertyId("caption");
setItemIconPropertyId("icon");
}
public void addItem(String pText, Component pComp)
{
this.addItem(pText, pComp, (ThemeResource) null);
}
public void addItem(String pText, Component pComp, String pIcon)
{
addItem(pText, pComp, new ThemeResource(pIcon));
}
public void addItem(String pText, Component pComp, ThemeResource pIcon)
{
Object id = addItem();
Item item = getItem(id);
item.getItemProperty("caption").setValue(pText);
item.getItemProperty("view").setValue(pComp);
item.getItemProperty("icon").setValue(pIcon);
setChildrenAllowed(id, false);
}
}