001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.jung;
025    
026    import java.awt.Color;
027    import java.awt.Graphics;
028    import java.awt.Image;
029    import java.awt.Paint;
030    import java.awt.Shape;
031    import java.awt.event.MouseEvent;
032    import java.awt.geom.AffineTransform;
033    import java.awt.image.BufferedImage;
034    
035    import javax.swing.Icon;
036    
037    import org.ujmp.core.interfaces.GUIObject;
038    
039    import edu.uci.ics.jung.graph.ArchetypeVertex;
040    import edu.uci.ics.jung.graph.Edge;
041    import edu.uci.ics.jung.graph.Vertex;
042    import edu.uci.ics.jung.graph.decorators.EdgePaintFunction;
043    import edu.uci.ics.jung.graph.decorators.PickableEdgePaintFunction;
044    import edu.uci.ics.jung.graph.decorators.ToolTipFunction;
045    import edu.uci.ics.jung.graph.decorators.VertexIconFunction;
046    import edu.uci.ics.jung.visualization.PluggableRenderer;
047    
048    public class TopologyPanel extends JungGraphPanel {
049            private static final long serialVersionUID = -6852675670416844022L;
050    
051            // private static Map<VariableGUIObject, Icon> variableIcons = new
052            // HashMap<VariableGUIObject, Icon>();
053    
054            public TopologyPanel(Object iTopology) {
055                    TopologyGraphWrapper graph = new TopologyGraphWrapper(iTopology);
056    
057                    // ((PluggableRenderer) getRenderer()).setVertexShapeFunction(new
058                    // VertexShapeFunction() {
059                    //
060                    // public Shape getShape(Vertex v) {
061                    // AbstractObject object = (AbstractObject)
062                    // v.getUserDatum(Data.JDMPObject);
063                    // return TopologyPanel.getShape(object);
064                    // }
065                    // });
066    
067                    ((PluggableRenderer) getRenderer()).setVertexIconFunction(new VertexIconFunction() {
068    
069                            public Icon getIcon(ArchetypeVertex v) {
070                                    // CoreObject object = (CoreObject)
071                                    // v.getUserDatum(Data.JDMPObject);
072                                    // return TopologyPanel.getIcon(object.getGUIObject());
073                                    return null;
074                            }
075                    });
076    
077                    setToolTipFunction(new ToolTipFunction() {
078                            public String getToolTipText(Vertex v) {
079                                    return ((GUIObject) v.getUserDatum(Data.JDMPObject)).getToolTipText();
080                            }
081    
082                            public String getToolTipText(Edge e) {
083                                    return null;
084                            }
085    
086                            public String getToolTipText(MouseEvent event) {
087                                    return null;
088                            }
089                    });
090    
091                    // pluggableRenderer.getGraphLabelRenderer().setRotateEdgeLabels(false);
092    
093                    ((PluggableRenderer) getRenderer()).setEdgePaintFunction(new EdgePaintFunction() {
094                            public Paint getDrawPaint(Edge e) {
095                                    if (isShowEdges())
096                                            return Color.black;
097                                    return null;
098                            }
099    
100                            public Paint getFillPaint(Edge e) {
101                                    return null;
102                            }
103                    });
104    
105                    // ((PluggableRenderer) getRenderer()).setEdgeFontFunction(new
106                    // EdgeFontFunction() {
107                    // public Font getFont(Edge e) {
108                    // return UIDefaults.DEFAULTFONT;
109                    // }
110                    // });
111    
112                    // ((PluggableRenderer) getRenderer()).setVertexFontFunction(new
113                    // VertexFontFunction() {
114                    // public Font getFont(Vertex v) {
115                    // return UIDefaults.BOLDFONT;
116                    // }
117                    // });
118    
119                    ((PluggableRenderer) getRenderer()).setEdgePaintFunction(new PickableEdgePaintFunction(
120                                    ((PluggableRenderer) getRenderer()), Color.black, Color.cyan));
121    
122                    setGraph(graph);
123    
124            }
125    
126            public static final Icon getIcon(GUIObject o) {
127                    // if (o instanceof Algorithm) {
128                    // return UIManager.getIcon("Algorithm.icon");
129                    // } else if (o instanceof VariableGUIObject) {
130                    // Icon i = variableIcons.get(o);
131                    // if (i == null) {
132                    // VariableGUIObject v = (VariableGUIObject) o;
133                    // // i = new VariableIcon(v);
134                    // variableIcons.put(v, i);
135                    // }
136                    // return i;
137                    // } else {
138                    // return UIManager.getIcon("Image");
139                    // }
140                    return null;
141            }
142    
143            public static final Shape getShape(GUIObject o) {
144                    Icon icon = getIcon(o);
145                    Shape shape = getShape(icon, 30);
146                    int width = icon.getIconWidth();
147                    int height = icon.getIconHeight();
148                    AffineTransform transform = AffineTransform.getTranslateInstance(-width / 2, -height / 2);
149                    shape = transform.createTransformedShape(shape);
150                    return shape;
151            }
152    
153            public static Shape getShape(Icon icon, int max) {
154                    BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
155                    Graphics g = bi.createGraphics();
156                    icon.paintIcon(null, g, 0, 0);
157                    g.dispose();
158                    return getShape(bi, max);
159            }
160    
161            public static Shape getShape(Image image, int max) {
162                    BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
163                    Graphics g = bi.createGraphics();
164                    g.drawImage(image, 0, 0, null);
165                    g.dispose();
166                    return getShape(bi, max);
167            }
168    
169    }