Package trust.jfcm.gui

Source Code of trust.jfcm.gui.Jung2DViewer

package trust.jfcm.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.Transformer;

import trust.jfcm.CognitiveMap;
import trust.jfcm.FcmConnection;
import trust.jfcm.WeightedConnection;
import trust.jfcm.gui.popupVertexEdgeMenuMousePlugin.MyMouseMenus;
import trust.jfcm.gui.popupVertexEdgeMenuMousePlugin.PopupVertexEdgeMenuMousePlugin;
import trust.jfcm.gui.popupVertexEdgeMenuMousePlugin.GraphElements.MyEdge;
import trust.jfcm.gui.popupVertexEdgeMenuMousePlugin.GraphElements.MyVertex;







import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;



public class Jung2DViewer extends JPanel{
 
 
  /**
   *
   */
  private static final long serialVersionUID = 1L;

  private Map<String, CognitiveMap> maps;
 
  public DirectedSparseMultigraph<MyVertex, MyEdge> graph;
 
  /**
   * Vertex and Edge Lists
   */
 
  private List<MyVertex> vertices;
  private List<MyEdge> edges;
 
  /**
     * the visual component and renderer for the graph
     */
  VisualizationViewer<MyVertex, MyEdge> vv;
   
  /**
   * Factory
   */
  int nodecount = 0, edgecount = 0;
  Factory <String> vertexFactory;
    Factory<String> edgeFactory;
 
 
  @SuppressWarnings("unchecked")
  public Jung2DViewer(Map<String, CognitiveMap> m){
   
    maps = m;
    graph = new DirectedSparseMultigraph<MyVertex, MyEdge>();
   
    vertexFactory = new Factory<String>() { // My vertex factory
            public String create() {
                return ""+nodecount++;
            }
        };
        edgeFactory = new Factory<String>() { // My edge factory
            public String create() {
                return "Edge"+edgecount++;
            }
        };
   
    //insert maps
    Collection<CognitiveMap> mapCollection = maps.values();
    Iterator<CognitiveMap> it = mapCollection.iterator();
    Collection<FcmConnection> links;
    while(it.hasNext()){
      CognitiveMap map = it.next();
     
      //insert vertex
      Set<String> mapConcepts = map.getConcepts().keySet();
      vertices = new Vector<MyVertex>();
      for(String concept : mapConcepts){
        System.out.println(concept);
        MyVertex v = new MyVertex(concept);
        vertices.add(v);
        graph.addVertex(v);
      }
     
      //insert links
      links = maps.get(map.getName()).getConnections().values();
      Iterator<FcmConnection> itc = links.iterator();
      edges = new Vector<MyEdge>();
      while(itc.hasNext()){
        WeightedConnection link = (WeightedConnection)itc.next();
        //System.out.println("Edge from" + link.getFrom().getName() + "to" + link.getTo().getName());
       
         
          String from = link.getFrom().getName();
          String to = link.getTo().getName();
          MyEdge e = new MyEdge("Edge: " + from + " -> " + to);
          e.setWeight(link.getWeight());
          edges.add(e);
        graph.addEdge(e, getVertexByName(from), getVertexByName(to), EdgeType.DIRECTED);
      }
    }
   
    /********** draw graph **************/
   
    Layout<MyVertex, MyEdge> layout = new FRLayout(graph);
    layout.setSize(new Dimension(450,600));
   
   
    vv = new VisualizationViewer<MyVertex, MyEdge>(layout);
    vv.setPreferredSize(new Dimension(450,500));
   
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
    //vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
   
    // Setup up a new vertex to paint transformer...
    Transformer<MyVertex, Paint> vertexPaint = new Transformer<MyVertex, Paint>() {
      public Paint transform(MyVertex i) {
        if(i.getName().equals("trust"))
          return Color.GREEN;
        return Color.RED;
      }
    };
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
   
    // Setup up a new vertex to paint transformer...
    Transformer<MyEdge, String> edgeLabel = new Transformer<MyEdge, String>() {
      public String transform(MyEdge edge) {
        System.out.println(edge);
        return ""+edge.getWeight();
       
      }

    };
    vv.getRenderContext().setEdgeLabelTransformer(edgeLabel);
   
    /********* editing ***********/
   
    EditingModalGraphMouse gm =  new EditingModalGraphMouse(vv.getRenderContext(),
      this.vertexFactory, this.edgeFactory);
    gm.setMode(Mode.EDITING);
   
    // Trying out our new popup menu mouse plugin...
        PopupVertexEdgeMenuMousePlugin myPlugin = new PopupVertexEdgeMenuMousePlugin();
        // Add some popup menus for the edges and vertices to our mouse plugin.
        JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(MapEditor.jFrame);
        JPopupMenu vertexMenu = new MyMouseMenus.VertexMenu();
        myPlugin.setEdgePopup(edgeMenu);
        myPlugin.setVertexPopup(vertexMenu);
        gm.remove(gm.getPopupEditingPlugin())// Removes the existing popup editing plugin
        gm.add(myPlugin);   // Add our new plugin to the mouse
   
    vv.setGraphMouse(gm);
   
   
    /*
    // Create a graph mouse and add it to the visualization component
    DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
    gm.setMode(Mode.TRANSFORMING);

    ((VisualizationViewer<String, String>) vv).setGraphMouse(gm);
    */
  }
 
  private MyVertex getVertexByName(String name) {
    Iterator<MyVertex> it = vertices.iterator();
    while(it.hasNext()){
      MyVertex v = it.next();
      if(v.getName().equals(name))
        return v;
    }

    return null;
  }

  public BasicVisualizationServer<MyVertex, MyEdge> getRenderer(){
    return vv;
  }
 
  public Jung2DViewer(){
   
    /*
    // show graph
    // Graph<V, E> where V is the type of the vertices
    // and E is the type of the edges
    graph = new DirectedSparseMultigraph<String, MyLink>();
    // Add some vertices. From above we defined these to be type Integer.
    graph.addVertex("1");
    graph.addVertex("2");
    graph.addVertex("3");
    // Add some edges. From above we defined these to be of type String
    // Note that the default is for undirected edges.
    //graph.addEdge("Edge-A", "1", "2"); // Note that Java 1.5 auto-boxes primitives
    //graph.addEdge("Edge-B", "2", "3");
    // Let's see what we have. Note the nice output from the
    // SparseMultigraph<V,E> toString() method
    System.out.println("The graph g = " + graph.toString());
    // Note that we can use the same nodes and edges in two different graphs.
    Graph<Integer, String> g2 = new SparseMultigraph<Integer, String>();
    g2.addVertex((Integer)1);
    g2.addVertex((Integer)2);
    g2.addVertex((Integer)3);
    g2.addEdge("Edge-A", 1,3);
    g2.addEdge("Edge-B", 2,3, EdgeType.DIRECTED);
    g2.addEdge("Edge-C", 3, 2, EdgeType.DIRECTED);
    g2.addEdge("Edge-P", 2,3); // A parallel edge
    System.out.println("The graph g2 = " + g2.toString());
    */
   
  }
 
 
 
    /**
     * a driver for this demo
     */
    @SuppressWarnings("unchecked")
  public static void main(String[] args) {
     
      Jung2DViewer sgv = new Jung2DViewer();
      Layout<String, String> layout = new CircleLayout(sgv.graph);
    layout.setSize(new Dimension(300,300)); // sets the initial size of the space
    // The BasicVisualizationServer<V,E> is parameterized by the edge types
    VisualizationViewer<String,String> vv = new VisualizationViewer<String,String>(layout);
    vv.setPreferredSize(new Dimension(350,350)); //Sets the viewing area size
   
    // Setup up a new vertex to paint transformer...
    Transformer<String, Paint> vertexPaint = new Transformer<String, Paint>() {
      public Paint transform(String i) {
        return Color.GREEN;
      }

    };
   
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
   
     
    JFrame frame = new JFrame("Simple Graph View");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv);
    //frame.getContentPane().add(sgv);
    frame.pack();
    frame.setVisible(true);
    }
   
 
}
TOP

Related Classes of trust.jfcm.gui.Jung2DViewer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.