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.BorderLayout; 027 import java.awt.Color; 028 import java.awt.Paint; 029 import java.awt.event.MouseEvent; 030 import java.awt.event.MouseListener; 031 032 import javax.swing.JPanel; 033 034 import org.ujmp.core.exceptions.MatrixException; 035 import org.ujmp.gui.MatrixGUIObject; 036 import org.ujmp.gui.colormap.ColorMap; 037 038 import edu.uci.ics.jung.graph.ArchetypeVertex; 039 import edu.uci.ics.jung.graph.Edge; 040 import edu.uci.ics.jung.graph.Graph; 041 import edu.uci.ics.jung.graph.Vertex; 042 import edu.uci.ics.jung.graph.decorators.EdgeShape; 043 import edu.uci.ics.jung.graph.decorators.ToolTipFunction; 044 import edu.uci.ics.jung.graph.decorators.VertexPaintFunction; 045 import edu.uci.ics.jung.graph.decorators.VertexStringer; 046 import edu.uci.ics.jung.graph.impl.DirectedSparseGraph; 047 import edu.uci.ics.jung.visualization.AbstractLayout; 048 import edu.uci.ics.jung.visualization.DefaultSettableVertexLocationFunction; 049 import edu.uci.ics.jung.visualization.PluggableRenderer; 050 import edu.uci.ics.jung.visualization.ShapePickSupport; 051 import edu.uci.ics.jung.visualization.VisualizationViewer; 052 import edu.uci.ics.jung.visualization.contrib.KKLayout; 053 import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse; 054 import edu.uci.ics.jung.visualization.control.ModalGraphMouse; 055 056 public class ProjectionPanel extends JPanel implements MouseListener { 057 private static final long serialVersionUID = -6575744654019554947L; 058 059 // private HasVariables iVariables = null; 060 061 private final int xAxis = 0; 062 063 private final int yAxis = 1; 064 065 private Graph g = null; 066 067 private VisualizationViewer vv = null; 068 069 AbstractLayout layout = null; 070 071 DefaultSettableVertexLocationFunction vertexLocations = new DefaultSettableVertexLocationFunction(); 072 073 public ProjectionPanel(Object iVariables) throws MatrixException { 074 // this.iVariables = iVariables; 075 076 this.addMouseListener(this); 077 078 g = new DirectedSparseGraph(); 079 080 layout = new KKLayout(g); 081 082 // Layout layout = new ISOMLayout(g); 083 PluggableRenderer pr = new PluggableRenderer(); 084 085 vv = new VisualizationViewer(layout, pr); 086 087 vv.setPickSupport(new ShapePickSupport()); 088 pr.setEdgeShapeFunction(new EdgeShape.QuadCurve()); 089 pr.setVertexStringer(new VertexStringer() { 090 public String getLabel(ArchetypeVertex v) { 091 return "" + v.getUserDatum(JungGraphPanel.Data.Label); 092 } 093 }); 094 095 pr.setVertexPaintFunction(new VertexPaintFunction() { 096 public Paint getFillPaint(Vertex v) { 097 return fromDouble((Double) v.getUserDatum(JungGraphPanel.Data.Time)); 098 } 099 100 public Paint getDrawPaint(Vertex v) { 101 return Color.BLACK; 102 } 103 }); 104 105 // pr.setVertexShapeFunction(new VertexShapeFunction() { 106 // public Shape getShape(Vertex v) { 107 // if (v.getUserDatum("label").equals("test1")) 108 // return new Rectangle2D.Float(-25, -15, 50, 30); 109 // else 110 // return new Rectangle2D.Float(-15, -15, 30, 30); 111 // } 112 // }); 113 114 vv.setToolTipFunction(new ToolTipFunction() { 115 116 public String getToolTipText(Vertex v) { 117 return ((MatrixGUIObject) v.getUserDatum(JungGraphPanel.Data.Matrix)).getToolTipText(); 118 } 119 120 public String getToolTipText(Edge e) { 121 return ""; 122 } 123 124 public String getToolTipText(MouseEvent event) { 125 return ""; 126 } 127 }); 128 129 // pr.setEdgePaintFunction(new PickableEdgePaintFunction(pr, 130 // Color.black, 131 // Color.cyan)); 132 133 this.setLayout(new BorderLayout()); 134 135 final ModalGraphMouse graphMouse = new DefaultModalGraphMouse(); 136 // graphMouse.setMode(ModalGraphMouse.Mode.PICKING); 137 vv.setGraphMouse(graphMouse); 138 139 updateGraph(); 140 141 this.add(vv, BorderLayout.CENTER); 142 } 143 144 public void updateGraph() throws MatrixException { 145 vv.stop(); 146 147 g.removeAllEdges(); 148 g.removeAllVertices(); 149 150 // Variable var = null; 151 152 // if (var != null) { 153 // for (int i = 0; i < var.getMatrixList().size(); i++) { 154 // Matrix m = var.getMatrixList().get(i); 155 // Vertex v = new DirectedSparseVertex(); 156 // v.setUserDatum(Data.Label, m.getLabel(), UserData.SHARED); 157 // v.setUserDatum(Data.Matrix, m, UserData.SHARED); 158 // g.addVertex(v); 159 // double x = m.getAsDouble(this.xAxis % m.getRowCount(), this.xAxis / 160 // m.getRowCount()); 161 // double y = m.getAsDouble(this.yAxis % m.getRowCount(), this.yAxis / 162 // m.getRowCount()); 163 // v.setUserDatum(layout.getBaseKey(), new Coordinates(x * 50, y * 50), 164 // UserData.SHARED); 165 // // layout.lockVertex(v); 166 // } 167 // } 168 vv.restart(); 169 } 170 171 public void mouseClicked(MouseEvent e) { 172 System.out.println("click"); 173 try { 174 updateGraph(); 175 } catch (MatrixException e1) { 176 // TODO Auto-generated catch block 177 e1.printStackTrace(); 178 } 179 } 180 181 public void mousePressed(MouseEvent e) { 182 // TODO Auto-generated method stub 183 184 } 185 186 public void mouseReleased(MouseEvent e) { 187 // TODO Auto-generated method stub 188 189 } 190 191 public void mouseEntered(MouseEvent e) { 192 // TODO Auto-generated method stub 193 194 } 195 196 public void mouseExited(MouseEvent e) { 197 // TODO Auto-generated method stub 198 199 } 200 201 private static final Color fromDouble(double v) { 202 // inf = 255 255 0 yellow 203 // 1 = 0 255 0 green 204 // 0 = 0 0 0 black 205 // -1 = 255 0 0 red 206 // -inf = 255 0 255 magenta 207 // nan = 0 255 255 cyan 208 if (v == Double.MIN_VALUE || Double.isNaN(v)) 209 return (Color.MAGENTA); 210 else if (Double.isInfinite(v)) 211 return (Color.CYAN); 212 else if (v > 1.0) 213 return (ColorMap.colorGreenToYellow[(int) (255.0 * Math.tanh((v - 1.0) / 10.0))]); 214 else if (v > 0.0) 215 return (ColorMap.colorBlackToGreen[(int) (255.0 * v)]); 216 else if (v > -1.0) 217 return (ColorMap.colorRedToBlack[(int) (255.0 * (v + 1.0))]); 218 else 219 return (ColorMap.colorRedToMagenta[(int) (255.0 * Math.tanh((-v - 1.0) / 10.0))]); 220 } 221 222 }