package com.onaro.sanscreen.client.view.task.actions.inventory; import com.onaro.sanscreen.task.interfaces.data.inventory.Device; import com.onaro.sanscreen.task.interfaces.data.inventory.InventoryException; import com.onaro.sanscreen.types.ObjectType; import java.util.Set; import java.util.HashSet; import java.util.Arrays; /** * Allows for viewing only the nodes of a particular device types. For example, it may * be used to display the nodes of host and generic-devices. */ public class NodeTreeModelFilter extends InventoryTreeModelFilter { private InventoryTreeModel.ListNode[] root; Set objectTypeFilters = null; /** * Allows for viewing only the nodes of a particular device types. For example, it may * be used to display the nodes of host and generic-devices. * The object types are assumed to be of the root device (for example {@link ObjectType.HOST}). * @param objectTypeFilter the device types that the filter will allow */ public NodeTreeModelFilter(ObjectType[] objectTypeFilter) { if (objectTypeFilter != null) { objectTypeFilters = new HashSet(); objectTypeFilters.addAll(Arrays.asList(objectTypeFilter)); } } public void setInventoryTreeModel(InventoryTreeModel inventoryTreeModel) throws InventoryException { super.setInventoryTreeModel(inventoryTreeModel); if (objectTypeFilters == null) { root = new InventoryTreeModel.ListNode[]{ getHostsListNode(), getStorageListNode(), getTapesListNode(), }; } else if (objectTypeFilters.contains(ObjectType.HOST)) { root = new InventoryTreeModel.ListNode[]{ getHostsListNode(), }; } else if (objectTypeFilters.contains(ObjectType.STORAGE)) { root = new InventoryTreeModel.ListNode[]{ getStorageListNode(), }; } else if (objectTypeFilters.contains(ObjectType.TAPE)) { root = new InventoryTreeModel.ListNode[]{ getTapesListNode(), }; } } public int getChildCount(Object parent) { if (parent == root) return root.length; int childCount = inventoryTreeModel.getChildCount(parent); if (parent instanceof Device){ Device device = (Device)parent; if (ObjectType.STORAGE.equals(device.getType())) return childCount-1; if (ObjectType.isPortContainer(device.getType())) return 0; } return inventoryTreeModel.getChildCount(parent); } public Object getRoot() { return root; } public Object getChild(Object parent, int index) { if (parent == root) return root[index]; if (parent instanceof Device){ Device device = (Device)parent; if (ObjectType.STORAGE.equals(device.getType())) index = index+1; } return inventoryTreeModel.getChild(parent, index); } public int getIndexOfChild(Object parent, Object child) { if (parent == null || child == null) { return -1; } if (parent == root) { for (int i = 0; i < root.length; ++i) { if (root[i] == parent) return i; } return root.length; } return inventoryTreeModel.getIndexOfChild(parent, child); } public boolean isLeaf(Object node) { if (node instanceof Device) { Device device = (Device) node; if (ObjectType.isNodeType(device.getType())) return true; } return false; } }