/*
* NexsmVertexIconFunction.java
*
* Copyright (C) 2006 Sergio Guzman Lorz <sergio AT gridshield.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* Created on March 23, 2006, 5:35 PM
*/
package net.gridshield.nexsm.graphmanager;
import edu.uci.ics.jung.graph.ArchetypeVertex;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.VertexIconFunction;
import edu.uci.ics.jung.graph.decorators.VertexShapeFunction;
import edu.uci.ics.jung.visualization.FourPassImageShaper;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import net.gridshield.nexsm.common.StatusType;
import net.gridshield.nexsm.entityclasses.HostStatus;
import net.gridshield.nexsm.entityclasses.ServiceStatus;
/**
* NexsmVertexIconFunction
* @author sergio
*/
public class NexsmVertexIconFunction implements VertexIconFunction, VertexShapeFunction{
/** Contains the original icons for the vertices */
protected Map iconMap = new HashMap();
/** Contains the shapes for the icons */
protected Map <Image, Shape>shapeMap = new HashMap<Image, Shape>();
/** the function to use as an alternative shape function */
protected VertexShapeFunction delegate;
/** Contains the icon references to the states */
protected Map <String, Icon>statesMap = new HashMap<String, Icon>();
/** Creates a new instance of NexsmVertexIconFunction */
public NexsmVertexIconFunction(VertexShapeFunction delegate) {
this.delegate = delegate;
}
public void setStatesMap(Map <String, Icon>statesmap) {
this.statesMap = statesmap;
}
public Map<String, Icon> getStatesMap() {
return statesMap;
}
/**
* @return Returns the delegate.
*/
public VertexShapeFunction getDelegate() {
return delegate;
}
/**
* @param delegate The delegate to set.
*/
public void setDelegate(VertexShapeFunction delegate) {
this.delegate = delegate;
}
/**
* Returns the icon storage as a <code>Map</code>.
*/
public Map getIconMap() {
return iconMap;
}
/**
* Sets the icon storage to the specified <code>Map</code>.
*/
public void setIconMap(Map iconMap) {
this.iconMap = iconMap;
}
/**
*Returns the icon corresponding to the vertex v and the state of the hosts
*represented by the vertex
*@param v The vertex to get its icon
*@return The icon for the vertex and its state added to the LayeredIcon
*/
public Icon getIcon(ArchetypeVertex v) {
Object state = v.getUserDatum("state");
LayeredIcon myIcon = (LayeredIcon)iconMap.get(v);
if (myIcon != null && myIcon.getImage() != null && state != null && state instanceof StatusType) {
StatusType st = (StatusType)state;
myIcon.remove(statesMap.get("up"));
myIcon.remove(statesMap.get("down"));
myIcon.remove(statesMap.get("down_acked"));
myIcon.remove(statesMap.get("unreachable"));
myIcon.remove(statesMap.get("up_servicesdown"));
myIcon.remove(statesMap.get("up_servicesacked"));
boolean serviceDown = false;
boolean serviceAcked = false;
Map <String, ServiceStatus> services = (Map<String, ServiceStatus>)v.getUserDatum("servicesstatus");
if (services != null) {
for (ServiceStatus ss: services.values()) {
if (ss.getCurrentState() != StatusType.Service_State_OK) {
if (ss.getProblemHasBeenAcknowledged()) {
serviceAcked = true;
} else {
serviceDown = true;
}
}
}
}
switch (st) {
case Host_Up:
if (serviceDown) {
if (statesMap.get("up_servicesdown") != null) {
myIcon.add(statesMap.get("up_servicesdown"));
}
} else if (serviceAcked) {
if (statesMap.get("up_servicesacked") != null) {
myIcon.add(statesMap.get("up_servicesacked"));
}
} else {
if (statesMap.get("up") != null) {
myIcon.add(statesMap.get("up"));
}
}
break;
case Host_Down:
Object ostatus = v.getUserDatum("hoststatus");
HostStatus hstatus = (HostStatus)ostatus;
if (hstatus.getProblemHasBeenAcknowledged()) {
if (statesMap.get("down_acked") != null) {
myIcon.add(statesMap.get("down_acked"));
}
} else {
if (statesMap.get("down") != null)
myIcon.add(statesMap.get("down"));
}
break;
case Host_Unreachable:
if (statesMap.get("unreachable") != null)
myIcon.add(statesMap.get("unreachable"));
break;
}
}
return (Icon)myIcon;
}
/**
*Returns the Shape for the icon used by the vertex v, if a background
*is set then the background icon is used if not the normal icon is used.
*@param v The vertex to get its icon shape
*@return The shape for the icon used by the vertex
*/
public Shape getShape(Vertex v) {
Icon icon = getIcon(v);
if (icon != null && icon instanceof LayeredIcon) {
Image image = null;
ImageIcon i = ((LayeredIcon) icon).getBackground();
if (i != null) {
image = i.getImage();
} else {
image = ((LayeredIcon) icon).getImage();
}
Shape shape = shapeMap.get(image);
if (shape == null) {
shape = FourPassImageShaper.getShape(image, 30);
int width = image.getWidth(null);
int height = image.getHeight(null);
AffineTransform transform = AffineTransform
.getTranslateInstance(-width / 2, -height / 2);
shape = transform.createTransformedShape(shape);
shapeMap.put(image, shape);
}
return shape;
} else {
return delegate.getShape(v);
}
}
}