예제 #1
0
  public Display getGraphDisplay(
      GraphLayoutType glType, final ExperimentPanel expPanel, String highlightName) {
    Display display = new Display();
    int X = expPanel.getDetailWidth();
    int Y = expPanel.getDetailHeight();
    display.setSize(X, Y); // set display size
    // display.setHighQuality(true);
    // display.setPreferredSize(new Dimension(600,600));
    display.addControlListener(new DragControl()); // drag items around
    display.addControlListener(new PanControl()); // pan with background left-drag
    display.addControlListener(new WheelZoomControl()); // zoom with vertical right-drag
    display.addControlListener(
        new ZoomToFitControl(Visualization.ALL_ITEMS, 50, 500, Control.MIDDLE_MOUSE_BUTTON));
    display.addControlListener(new NeighborHighlightControl());

    Visualization vis = new Visualization();
    if (clusterGraph == null) {
      clusterGraph = this.toGraph();
    }
    vis.add("graph", clusterGraph);
    LabelRenderer r = new LabelRenderer("name");
    r.setHorizontalAlignment(Constants.CENTER);
    // r.setRoundedCorner(8, 8); // round the corners

    DefaultRendererFactory rf = new DefaultRendererFactory(r);
    rf.setDefaultEdgeRenderer(new EdgeRenderer(Constants.EDGE_TYPE_CURVE));
    vis.setRendererFactory(rf);
    int[] palette = new int[] {ColorLib.rgb(255, 180, 180), ColorLib.rgb(190, 190, 255)};
    DataColorAction fill =
        new DataColorAction(
            "graph.nodes", "type", Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
    fill.add(VisualItem.FIXED, ColorLib.rgb(255, 100, 100));
    fill.add(VisualItem.HIGHLIGHT, ColorLib.rgb(255, 200, 125));
    int[] text_palette = new int[] {ColorLib.gray(0), ColorLib.gray(255)};
    DataColorAction text =
        new DataColorAction(
            "graph.nodes",
            "indeterminate",
            Constants.NUMERICAL,
            VisualItem.TEXTCOLOR,
            text_palette);
    // ColorAction text = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.gray(0));
    ColorAction edges = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.gray(200));

    ActionList color = new ActionList(Activity.INFINITY);
    color.add(text);
    color.add(fill);
    color.add(edges);
    color.add(new RepaintAction());

    if (highlightName != null) {
      String pred = "name='" + highlightName + "'";
      Iterator iter = vis.items("graph.nodes", ExpressionParser.predicate(pred));
      while (iter.hasNext()) {
        NodeItem ni = (NodeItem) iter.next();
        highlightedItem = ni;
        ni.setFixed(true);
        Iterator iterEdge = ni.edges();
        while (iterEdge.hasNext()) {
          EdgeItem eItem = (EdgeItem) iterEdge.next();
          NodeItem nItem = eItem.getAdjacentItem(ni);
          if (eItem.isVisible()) {
            eItem.setHighlighted(true);
            nItem.setHighlighted(true);
          }
        }
      }
    }

    ActionList layout = new ActionList();
    switch (glType) {
      case BALLOON_TREE:
        layout.add(new BalloonTreeLayout("graph"));
        break;
      case FORCE_DIRECTED:
        layout = new ActionList(Activity.INFINITY);
        layout.add(new ForceDirectedLayout("graph"));
        break;
      case NODE_LINK_TREE:
        layout.add(new NodeLinkTreeLayout("graph"));
        break;
      case RADIAL_TREE:
        layout.add(new RadialTreeLayout("graph"));
        break;
    }
    // layout.add(fill);
    layout.add(new RepaintAction());

    vis.putAction("color", color);
    vis.putAction("layout", layout);
    display.setVisualization(vis);
    vis.run("color");
    vis.run("layout");
    // Rectangle2D bounds = vis.getBounds(Visualization.ALL_ITEMS);
    // GraphicsLib.expand(bounds, 50 + (int)(1/display.getScale()));
    // DisplayLib.fitViewToBounds(display,bounds,1);
    // vis.runAfter("layout",1000);

    Control shutoffHighlight =
        new HoverActionControl("color") {
          // public void itemEntered(VisualItem item, MouseEvent evt) {
          // }
          public void itemExited(VisualItem item, MouseEvent evt) {
            if (highlightedItem != null) {
              highlightedItem.setFixed(false);
              Iterator iterEdge = highlightedItem.edges();
              while (iterEdge.hasNext()) {
                EdgeItem eItem = (EdgeItem) iterEdge.next();
                NodeItem nItem = eItem.getAdjacentItem(highlightedItem);
                if (eItem.isVisible()) {
                  eItem.setHighlighted(false);
                  nItem.setHighlighted(false);
                }
              }
              highlightedItem = null;
            }
          }
        };
    display.addControlListener(shutoffHighlight);

    Control selectItem =
        new FocusControl() {

          public void itemClicked(VisualItem item, MouseEvent evt) {
            if (item.isInGroup("graph.nodes")) {
              if (item.getString("type").equals("peptide")) {
                Peptide pep = minPeptides.get(item.getString("name"));
                expPanel.showPeptide(pep, true);
              }
              if (item.getString("type").equals("protein")) {
                Protein pro = minProteins.get(item.getString("name"));
                expPanel.showProtein(pro, true);
              }
              String pred = "name='" + item.getString("name") + "'";
              Iterator iter =
                  item.getVisualization().items("graph.nodes", ExpressionParser.predicate(pred));
              while (iter.hasNext()) {
                NodeItem ni = (NodeItem) iter.next();
                highlightedItem = ni;
                ni.setFixed(true);
                Iterator iterEdge = ni.edges();
                while (iterEdge.hasNext()) {
                  EdgeItem eItem = (EdgeItem) iterEdge.next();
                  NodeItem nItem = eItem.getAdjacentItem(ni);
                  if (eItem.isVisible()) {
                    eItem.setHighlighted(true);
                    nItem.setHighlighted(true);
                  }
                }
              }
            }
          }
        };
    display.addControlListener(selectItem);

    final JPopupMenu menu = new JPopupMenu();

    // Create and add a menu item
    // JMenuItem printItem = new JMenuItem("Print This");
    // printItem.addActionListener(new java.awt.event.ActionListener() {

    //    public void actionPerformed(java.awt.event.ActionEvent evt) {
    //        PrintUtilities.printComponent(display);
    //    }
    // });
    // menu.add(printItem);

    // Set the component to show the popup menu
    display.addMouseListener(
        new MouseAdapter() {

          public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
          }

          public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
          }
        });
    display.pan(X / 2.0, Y / 2.0);
    return display;
  }
  public static JComponent demo(Graph g, String label) {

    // create a new, empty visualization for our data
    final Visualization vis = new Visualization();
    VisualGraph vg = vis.addGraph(graph, g);
    vis.setValue(edges, null, VisualItem.INTERACTIVE, Boolean.FALSE);
    int[] palette =
        new int[] {
          ColorLib.rgb(77, 175, 74),
          ColorLib.rgb(55, 126, 184),
          ColorLib.rgb(228, 26, 28),
          ColorLib.rgb(152, 78, 163),
          ColorLib.rgb(255, 127, 0)
        };

    int[] shape_palette =
        new int[] {Constants.SHAPE_ELLIPSE, Constants.SHAPE_RECTANGLE, Constants.SHAPE_TRIANGLE_UP};

    TupleSet focusGroup = vis.getGroup(Visualization.FOCUS_ITEMS);
    focusGroup.addTupleSetListener(
        new TupleSetListener() {
          public void tupleSetChanged(TupleSet ts, Tuple[] add, Tuple[] rem) {
            for (int i = 0; i < rem.length; ++i) ((VisualItem) rem[i]).setFixed(false);
            for (int i = 0; i < add.length; ++i) {
              ((VisualItem) add[i]).setFixed(false);
              ((VisualItem) add[i]).setFixed(true);
            }
            vis.run("draw");
          }
        });

    // set up the renderers

    /*      ShapeRenderer s = new ShapeRenderer();
    s.rectangle(0, 0, 30, 30);
    vis.setRendererFactory(new DefaultRendererFactory(s));*/

    MyRenderer tr = new MyRenderer(label);
    // MyRenderer tr = new MyRenderer(label);
    DefaultRendererFactory d = new DefaultRendererFactory(tr);

    EdgeRenderer MyEdgeRenderer = new EdgeRenderer();
    d.add(new InGroupPredicate(edges), MyEdgeRenderer);
    vis.setRendererFactory(d);

    // -- set up the actions ----------------------------------------------

    int maxhops = 1024, hops = 1024;
    final GraphDistanceFilter filter = new GraphDistanceFilter(graph, hops);

    ActionList draw = new ActionList();
    draw.add(filter);

    // draw.add(new ColorAction(nodes, VisualItem.FILLCOLOR, ColorLib.rgb(180,180,255)));
    draw.add(new ColorAction(nodes, VisualItem.STROKECOLOR, 0));
    draw.add(new ColorAction(nodes, VisualItem.TEXTCOLOR, ColorLib.rgb(0, 0, 0)));
    draw.add(new ColorAction(edges, VisualItem.FILLCOLOR, ColorLib.gray(100)));
    draw.add(new ColorAction(edges, VisualItem.STROKECOLOR, ColorLib.gray(170)));
    draw.add(new DataShapeAction(nodes, "type", shape_palette));

    DataColorAction fill =
        new DataColorAction(
            "graph.nodes", "type", Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
    // use black for node text
    ColorAction text = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.gray(0));
    // use light grey for edges
    ColorAction edges = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.gray(200));

    // create an action list containing all color assignments
    ActionList color = new ActionList();
    color.add(fill);
    color.add(text);
    color.add(edges);

    //        ColorAction fill = new ColorAction(nodes,
    //                VisualItem.FILLCOLOR, ColorLib.rgb(200,200,255));
    fill.add("_fixed", ColorLib.rgb(255, 100, 100));
    fill.add("_highlight", ColorLib.rgb(255, 200, 125));

    // Instead have to use  GridLayout here - how do i create
    ForceDirectedLayout fdl = new ForceDirectedLayout(graph);
    // GridLayout fdl = new GridLayout(graph);
    ForceSimulator fsim = fdl.getForceSimulator();
    fsim.getForces()[0].setParameter(0, -5f);

    ActionList animate = new ActionList(Activity.INFINITY);
    // ActionList animate = new ActionList(vis);

    animate.add(fdl);
    animate.add(fill);
    animate.add(new RepaintAction());

    // finally, we register our ActionList with the Visualization.
    // we can later execute our Actions by invoking a method on our
    // Visualization, using the name we've chosen below.
    vis.putAction("draw", draw);
    vis.putAction("layout", animate);
    vis.runAfter("draw", "layout");
    vis.putAction("color", color);
    // vis.putAction("layout", layout);

    // --------------------------------------------------------------------
    // STEP 4: set up a display to show the visualization

    Display display = new Display(vis);
    display.setSize(2000, 2000);
    display.setForeground(Color.GRAY);
    display.setBackground(Color.WHITE);

    // main display controls
    display.addControlListener(new FocusControl(1));
    display.addControlListener(new DragControl());
    display.addControlListener(new PanControl());
    display.addControlListener(new ZoomControl());
    display.addControlListener(new WheelZoomControl());
    display.addControlListener(new ZoomToFitControl());
    display.addControlListener(new NeighborHighlightControl());

    display.setForeground(Color.GRAY);
    display.setBackground(Color.WHITE);

    // --------------------------------------------------------------------
    // STEP 5: launching the visualization

    // create a panel for editing force values
    final JForcePanel fpanel = new JForcePanel(fsim);

    final JValueSlider slider = new JValueSlider("Distance", 0, maxhops, hops);
    slider.addChangeListener(
        new ChangeListener() {
          public void stateChanged(ChangeEvent e) {
            filter.setDistance(slider.getValue().intValue());
            vis.run("draw");
          }
        });
    slider.setBackground(Color.WHITE);
    slider.setPreferredSize(new Dimension(300, 30));
    slider.setMaximumSize(new Dimension(300, 30));

    Box cf = new Box(BoxLayout.Y_AXIS);
    cf.add(slider);
    cf.setBorder(BorderFactory.createTitledBorder("Connectivity Filter"));
    fpanel.add(cf);

    fpanel.add(Box.createVerticalGlue());

    // create a new JSplitPane to present the interface
    JSplitPane split = new JSplitPane();
    split.setLeftComponent(display);
    split.setRightComponent(fpanel);
    split.setOneTouchExpandable(true);
    split.setContinuousLayout(false);
    split.setDividerLocation(530);
    split.setDividerLocation(800);

    // position and fix the default focus node
    NodeItem focus = (NodeItem) vg.getNode(0);
    PrefuseLib.setX(focus, null, 400);
    PrefuseLib.setY(focus, null, 250);
    focusGroup.setTuple(focus);

    // now we run our action list and return
    return split;
  }