/*
* Embedded Jopr Project
* Copyright (C) 2006-2009 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.jboss.on.embedded.ui;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceType;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.core.Events;
import org.jboss.on.embedded.manager.ResourceManager;
import org.jboss.on.embedded.manager.ResourceManagerFactory;
import org.jboss.on.embedded.ui.nav.BaseTreeNode;
import org.jboss.on.embedded.ui.nav.DummyTreeNode;
import org.jboss.on.embedded.ui.nav.JONTreeNode;
import org.jboss.on.embedded.ui.nav.PlatformResourceTreeNode;
import org.jboss.on.embedded.ui.nav.ResourceTypeTreeNode;
/**
* NavigationContent class encapsulates the JONTreeNode object which contains the objects rendered in the navigation
* tree. Contains the methods which will update this structure as resources are added/deleted
*
* @author Charles Crouch
*/
@Name("navigationContent")
public class NavigationContent
{
private final Log log = LogFactory.getLog(this.getClass());
// used by resourceNavigation.xhtml to render the nav
@In(value = "rootNode", create = true)
@Out(value = "rootNode", scope = ScopeType.APPLICATION)
private JONTreeNode rootNode;
@Factory(value = "rootNode")
public JONTreeNode createJONTreeNode()
{
log.trace("createJONTreeNode factory method being called.");
DummyTreeNode dummy = new DummyTreeNode();
PlatformResourceTreeNode platform = new PlatformResourceTreeNode(ResourceManagerFactory.resourceManager().getPlatform());
dummy.addChild(platform);
Events.instance().raiseEvent(ResourceManager.NAV_TREE_INITIALIZED);
return dummy;
}
@Observer(ResourceManager.RESOURCE_CREATED_EVENT)
public void updateNavWithNewResource(ResourceType typeOfNewResource, Resource parentResource)
{
log.trace("Resource of type [" + typeOfNewResource + "] added beneath Resource [" + parentResource
+ "] - updating nav tree...");
JONTreeNode parentResourceNode = findNode(getResourcePath(parentResource));
JONTreeNode resourceTypeNode = findNodeByResourceTypeAndParent(typeOfNewResource, parentResourceNode);
if (resourceTypeNode == null)
{
// the resource type node doesn't exist yet, so lets refresh its parent to give it a chance
// to be generated. This would be the case for non-singleton resource types which started off
// with no children and can't have resource added directly to them, e.g. Embedded EJB deployments.
parentResourceNode.reInitializeChildrenMap();
}
else
{
resourceTypeNode.reInitializeChildrenMap();
}
}
@Observer(ResourceManager.RESOURCE_DELETED_EVENT)
public void updateNavWithDeletedResource(Resource deletedResource)
{
log.trace("Resource [" + deletedResource + "] deleted - updating nav tree...");
JONTreeNode node = findNode(getResourcePath(deletedResource));
if (node == null)
{
log.warn("Unable to find node for deleted resource [" + deletedResource + "].");
return;
}
JONTreeNode parentNode = node.getParent();
parentNode.reInitializeChildrenMap();
}
@Observer(ResourceManager.RESOURCE_UPDATED_EVENT)
public void updateNavWithUpdatedResource(Resource updatedResource)
{
// doesn't do anything right now, but in the future this could
// regenerate the nav similar to how updateNavWithDeletedResource
// does it. This would catch any resource renames that happened
// as part of the update
}
// the following methods are all copied from NavigationAction
private JONTreeNode findNode(String path)
{
return rootNode.findNode(path);
}
// TODO consider having this return TreeNodeWithResourceType
private ResourceTypeTreeNode findNodeByResourceTypeAndParent(ResourceType resourceType, JONTreeNode parentResourceNode)
{
return ((BaseTreeNode)parentResourceNode).findNodeByType(resourceType);
}
private String getResourcePath(Resource resource)
{
return getResourcePath(resource.getId());
}
private String getResourcePath(Integer resourceId)
{
// this just happens to be the current implementation for generating the path
// if we wanted to change it, we just need to change the methods in this class
// and ResourceTreeNode
// TODO this could call a static method on ResourceTreeNode to keep this
// implementation better encapsulated
return String.valueOf(resourceId);
}
}