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.BasicStroke;
027    import java.awt.Color;
028    import java.awt.Paint;
029    import java.awt.Shape;
030    import java.awt.Stroke;
031    import java.awt.geom.Ellipse2D;
032    
033    import org.ujmp.core.Matrix;
034    import org.ujmp.core.exceptions.MatrixException;
035    import org.ujmp.gui.MatrixGUIObject;
036    import org.ujmp.gui.util.GraphicsExecutor;
037    
038    import edu.uci.ics.jung.graph.ArchetypeVertex;
039    import edu.uci.ics.jung.graph.Edge;
040    import edu.uci.ics.jung.graph.Vertex;
041    import edu.uci.ics.jung.graph.decorators.EdgeArrowFunction;
042    import edu.uci.ics.jung.graph.decorators.EdgePaintFunction;
043    import edu.uci.ics.jung.graph.decorators.EdgeShape;
044    import edu.uci.ics.jung.graph.decorators.EdgeStrokeFunction;
045    import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
046    import edu.uci.ics.jung.graph.decorators.VertexShapeFunction;
047    import edu.uci.ics.jung.graph.decorators.VertexStringer;
048    import edu.uci.ics.jung.visualization.ArrowFactory;
049    import edu.uci.ics.jung.visualization.PluggableRenderer;
050    
051    public class MatrixGraphPanel extends JungGraphPanel {
052            private static final long serialVersionUID = 5267278346111012739L;
053    
054            private final MatrixGUIObject matrix = null;
055    
056            public MatrixGraphPanel(Matrix matrix) throws MatrixException {
057                    this((MatrixGUIObject) matrix.getGUIObject());
058            }
059    
060            public MatrixGraphPanel(MatrixGUIObject matrix) throws MatrixException {
061                    this();
062                    setMatrix(matrix);
063            }
064    
065            public MatrixGraphPanel() {
066                    setShowEdgeLabels(false);
067    
068                    ((PluggableRenderer) getRenderer())
069                                    .setEdgeShapeFunction(new EdgeShape.Line());
070    
071                    ((PluggableRenderer) getRenderer())
072                                    .setEdgePaintFunction(new EdgePaintFunction() {
073                                            public Paint getDrawPaint(Edge e) {
074                                                    if (isShowEdges()) {
075                                                            // Double v = (Double) e.getUserDatum(Data.Value);
076                                                            return new Color(128, 128, 128, 80);
077                                                    }
078                                                    return null;
079                                            }
080    
081                                            public Paint getFillPaint(Edge e) {
082                                                    return null;
083                                            }
084                                    });
085    
086                    ((PluggableRenderer) getRenderer())
087                                    .setVertexStringer(new VertexStringer() {
088                                            public String getLabel(ArchetypeVertex v) {
089                                                    // if (v.degree() > 29 && v.getUserDatum(Data.Label) !=
090                                                    // null) {
091                                                    // System.out.println(v.getUserDatum(Data.Label));
092                                                    return "" + v.getUserDatum(Data.Label);
093                                                    // }
094                                                    // return null;
095                                            }
096                                    });
097    
098                    ((PluggableRenderer) getRenderer())
099                                    .setVertexPaintFunction(new VertexPaintFunction() {
100                                            public Paint getFillPaint(Vertex v) {
101                                                    // boolean inhibitory = (Boolean)
102                                                    // v.getUserDatum(Data.Inhibitory);
103                                                    // boolean excitatory = (Boolean)
104                                                    // v.getUserDatum(Data.Excitatory);
105                                                    // if (inhibitory)
106                                                    // return new Color(255, 64, 64);
107                                                    // if (excitatory)
108                                                    // return new Color(64, 255, 64);
109                                                    if (v.degree() == 0) {
110                                                            return new Color(0, 200, 0, 20);
111                                                    } else if (v.degree() > 29) {
112                                                            return new Color(200, 0, 0, 80);
113                                                    } else {
114                                                            return new Color(0, 200, 0, 80);
115                                                    }
116                                            }
117    
118                                            public Paint getDrawPaint(Vertex v) {
119                                                    return null;
120                                            }
121                                    });
122    
123                    ((PluggableRenderer) getRenderer())
124                                    .setEdgeStrokeFunction(new EdgeStrokeFunction() {
125                                            public Stroke getStroke(Edge e) {
126                                                    // double value = (Double) e.getUserDatum(Data.Value);
127                                                    // double absValue = Math.abs(value);
128                                                    // if (absValue < 0.5)
129                                                    // return new BasicStroke(0.5f);
130                                                    // if (absValue < 1.0)
131                                                    // return new BasicStroke(1.0f);
132                                                    // if (absValue < 2.0)
133                                                    // return new BasicStroke(2);
134                                                    // if (absValue < 3.0)
135                                                    // return new BasicStroke(3);
136                                                    return new BasicStroke(0.5f);
137                                            }
138                                    });
139    
140                    ((PluggableRenderer) getRenderer())
141                                    .setEdgeArrowFunction(new EdgeArrowFunction() {
142                                            public Shape getArrow(Edge e) {
143                                                    // double value = (Double) e.getUserDatum(Data.Value);
144                                                    // if (value > 0)
145                                                    // return ArrowFactory.getNotchedArrow(12, 12, 5);
146                                                    return ArrowFactory.getNotchedArrow(6, 6, 2);
147                                                    // else
148                                                    // return new Ellipse2D.Double(-5, -5, 12, 12);
149                                            }
150                                    });
151    
152                    ((PluggableRenderer) getRenderer())
153                                    .setVertexShapeFunction(new VertexShapeFunction() {
154                                            public Shape getShape(Vertex v) {
155                                                    // if (v.getUserDatum(Data.Label).equals("test1"))
156                                                    // return new Rectangle2D.Float(-25, -15, 50, 30);
157                                                    // else
158                                                    // int size = 2 + v.degree() * 2;
159                                                    int size = 2;
160                                                    return new Ellipse2D.Float(-size / 2, -size / 2, size,
161                                                                    size);
162                                            }
163                                    });
164    
165            }
166    
167            public void setMatrix(MatrixGUIObject matrix) throws MatrixException {
168                    setGraph(new MatrixGraphWrapper(matrix));
169                    GraphicsExecutor.scheduleUpdate(this);
170            }
171    
172            public MatrixGUIObject getMatrix() {
173                    return matrix;
174            }
175    
176    }