Exemplo n.º 1
0
  public void build() {
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('F');

    saveEdgelistAction.setParent(this);
    JMenuItem saveEdgelist = new JMenuItem(saveEdgelistAction);
    fileMenu.add(saveEdgelist);

    saveAdjAction.setParent(this);
    fileMenu.add(new JMenuItem(saveAdjAction));

    saveAlterAttributesCSVAction.setParent(this);
    fileMenu.add(new JMenuItem(saveAlterAttributesCSVAction));

    saveConsensusAction.setParent(this);
    fileMenu.add(new JMenuItem(saveConsensusAction));

    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    setJMenuBar(mb);

    SparseGraph<WholeNetworkAlter, WholeNetworkTie> graph =
        new SparseGraph<WholeNetworkAlter, WholeNetworkTie>();

    for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) {
      graph.addVertex(alter);
      logger.info("Adding vertex " + alter);
    }

    for (WholeNetworkTie tie : net.getWholeNetworkTies()) {
      graph.addEdge(tie, tie.getA(), tie.getB());
      logger.info("Adding edge " + tie);
    }

    Layout<WholeNetworkAlter, WholeNetworkTie> layout =
        new ELSFRLayout2<WholeNetworkAlter, WholeNetworkTie>(graph);
    VisualizationViewer<WholeNetworkAlter, WholeNetworkTie> vv =
        new VisualizationViewer<WholeNetworkAlter, WholeNetworkTie>(layout);
    vv.setGraphMouse(new DefaultModalGraphMouse());
    vv.setPickSupport(new ShapePickSupport<WholeNetworkAlter, WholeNetworkTie>(vv));
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<WholeNetworkAlter>());
    vv.getRenderContext()
        .setEdgeShapeTransformer(new EdgeShape.Line<WholeNetworkAlter, WholeNetworkTie>());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(vv, BorderLayout.CENTER);

    setContentPane(panel);
    pack();
  }
  /** create an instance of a simple graph with controls to demo the zoom features. */
  public GraphZoomScrollPaneDemo() {

    // create a simple graph for the demo
    graph = new DirectedSparseGraph();
    Vertex[] v = createVertices(10);
    createEdges(v);

    ImageIcon sandstoneIcon = null;
    String imageLocation = "/samples/images/Sandstone.jpg";
    try {
      sandstoneIcon = new ImageIcon(getClass().getResource(imageLocation));
    } catch (Exception ex) {
      System.err.println("Can't load \"" + imageLocation + "\"");
    }
    final ImageIcon icon = sandstoneIcon;
    PluggableRenderer pr = new PluggableRenderer();

    vv = new VisualizationViewer(new FRLayout(graph), pr);
    vv.setPickSupport(new ShapePickSupport());
    pr.setEdgeShapeFunction(new EdgeShape.QuadCurve());

    if (icon != null) {
      vv.addPreRenderPaintable(
          new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
              Dimension d = vv.getSize();
              g.drawImage(icon.getImage(), 0, 0, d.width, d.height, vv);
            }

            public boolean useTransform() {
              return false;
            }
          });
    }
    vv.addPostRenderPaintable(
        new VisualizationViewer.Paintable() {
          int x;
          int y;
          Font font;
          FontMetrics metrics;
          int swidth;
          int sheight;
          String str = "GraphZoomScrollPane Demo";

          public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
              font = new Font(g.getFont().getName(), Font.BOLD, 30);
              metrics = g.getFontMetrics(font);
              swidth = metrics.stringWidth(str);
              sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
              x = (d.width - swidth) / 2;
              y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.lightGray);
            g.drawString(str, x, y);
            g.setColor(oldColor);
          }

          public boolean useTransform() {
            return false;
          }
        });

    vv.addGraphMouseListener(new TestGraphMouseListener());

    // add my listener for ToolTips
    vv.setToolTipFunction(new DefaultToolTipFunction());

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphMouse graphMouse = new DefaultModalGraphMouse();
    vv.setGraphMouse(graphMouse);
    JMenuBar menu = new JMenuBar();
    menu.add(((DefaultModalGraphMouse) graphMouse).getModeMenu());
    panel.setCorner(menu);
    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
          }
        });
    JButton minus = new JButton("-");
    minus.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
          }
        });
    JButton reset = new JButton("reset");
    reset.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            vv.getLayoutTransformer().setToIdentity();
            vv.getViewTransformer().setToIdentity();
          }
        });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
  }