/**
   * Add support for mouse over popups.
   *
   * @param e
   */
  @Override
  public void mouseMoved(MouseEvent e) {
    super.mouseMoved(e);
    VisualizationViewer<V, E> vv = (VisualizationViewer<V, E>) e.getSource();
    GraphElementAccessor<V, E> pickSupport = vv.getPickSupport();
    Layout<V, E> layout = vv.getGraphLayout();
    Point2D loc = e.getPoint();
    V vertex = pickSupport.getVertex(layout, loc.getX(), loc.getY());
    final ITermNode node = (ITermNode) vertex;
    if (vertex == null) {
      if (!GUI2.getInstance().getGraphPanel().getShowNewTerms()) {
        GUI2.getInstance()
            .getGraphPanel()
            .setStatusbarText("New assertions are not shown in the graph.");
      } else {
        GUI2.getInstance().getGraphPanel().setStatusbarText("");
      }
      return;
    }
    String sbtext = "   ";

    if (node.getTerm().isMolecular()) sbtext += node.getTerm().toString();
    else sbtext += node.toString();

    if (!node.getInEdges().isEmpty()) {
      sbtext +=
          " is in relation"
              + (node.getInEdges().size() == 1 ? "" : "s")
              + ": "
              + new HashSet(node.getRelationsPartOf()); // Use a HashSet to eliminate duplicates.
    }
    GUI2.getInstance().getGraphPanel().setStatusbarText(sbtext);
  }
 @SuppressWarnings("unchecked")
 public void mouseClicked(MouseEvent e) {
   final VisualizationViewer<RouterVertex, LinkEdge> vv =
       (VisualizationViewer<RouterVertex, LinkEdge>) e.getSource();
   Point2D p = e.getPoint();
   // LEFT MOUSE BUTTON
   if (e.getButton() == MouseEvent.BUTTON1) {
     GraphElementAccessor<RouterVertex, LinkEdge> pickSupport = vv.getPickSupport();
     if (pickSupport != null) {
       final RouterVertex routerVertex =
           pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());
       if (routerVertex != null) {
         switch (owner.getMapGraphComponentMode()) {
           case MapGraphComponentMode.SHOW_SHORTEST_PATH:
             if (!routerVertex.isMultilink()) {
               owner.setShortestTreeCenter(routerVertex);
               owner.showShortestPath();
             }
             break;
           case MapGraphComponentMode.VERTEX_LOCKING:
             owner.lockVertexPosition(routerVertex);
             break;
         }
       }
       final LinkEdge linkEdge = pickSupport.getEdge(vv.getGraphLayout(), p.getX(), p.getY());
       if (linkEdge != null) {
         if (owner.getMapGraphComponentMode() == MapGraphComponentMode.LINK_FAULT) {
           if (linkEdge.getFaultCount() > 0) {
             owner.getOwner().getManager().showLinkFaultDialog(linkEdge.getLinkIDv4());
           }
         }
       }
     }
     if (owner.getMapGraphComponentMode() == MapGraphComponentMode.ZOOM) {
       owner.zoomPlus(p);
     }
   }
   // RIGHT MOUSE BUTTON
   if (e.getButton() == MouseEvent.BUTTON3) {
     if (owner.getMapGraphComponentMode() == MapGraphComponentMode.ZOOM) {
       owner.zoomMinus(p);
     }
   }
 }
  /**
   * Add support for right clicking verticies to expand them.
   *
   * @param e
   */
  @Override
  public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    GUI2.getInstance().getGraphPanel().highlightedNodes.clear();
    if (e.getButton() != MouseEvent.BUTTON3) {
      popup.setVisible(false);
    }
    if (e.getButton() == MouseEvent.BUTTON3) {
      final VisualizationViewer<V, E> vv = (VisualizationViewer<V, E>) e.getSource();
      GraphElementAccessor<V, E> pickSupport = vv.getPickSupport();
      Layout<V, E> layout = vv.getGraphLayout();
      Point2D loc = e.getPoint();
      V vertex = pickSupport.getVertex(layout, loc.getX(), loc.getY());
      final TermNode node = (TermNode) vertex;
      if (vertex == null) {
        popup.setVisible(false);
        return;
      } else {
        popup.setVisible(false);
        popup.removeAll();

        int dncsvis = node.getDownCablesetVisibleCount();
        int upcsvis = node.getUpCablesetVisibleCount();
        System.out.println("UpCSVis: " + upcsvis + " DnCSVis " + dncsvis);

        if (upcsvis < node.getInEdges().size()) {
          popup.add(
              new AbstractAction(
                  "Show All In Edges (" + (node.getInEdges().size() - upcsvis) + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().showInEdges(node);
                  vv.repaint();
                  // JungGraphPanel.instance.showUpCableset(node);
                }
              });

          popup.add(
              new AbstractAction("Show In Edges By Relation") {

                public void actionPerformed(ActionEvent e) {
                  CaseframeBasedShowHideDialog cd =
                      new CaseframeBasedShowHideDialog(
                          GUI2.getInstance(),
                          new ArrayList<String>(
                              GUI2.getInstance().getGraph().getInHiddenFSymbols(node)));

                  cd.setHelpText("   Select the relations you        wish to show in the graph.");
                  cd.setVisible(true);

                  for (String fsym : cd.getResult()) {
                    GUI2.getInstance().getGraph().showInEdges(node, fsym);
                  }

                  vv.repaint();
                }
              });

          /*JMenu submenu = new JMenu("Show In Relations");
          final HashMap<Caseframe, ArrayList<Edge>> hm = JungGraphPanel.instance.getHiddenUpCablesetCfs(node);
          for(final Caseframe cf : hm.keySet()){
          submenu.add(new AbstractAction(cf.toString()) {
          public void actionPerformed(ActionEvent e) {
          for(Edge je : hm.get(cf))
          JungGraphPanel.instance.showNode(je.from);
          }
          });
          }
          popup.add(submenu);*/
        }
        if (dncsvis < node.getOutEdges().size()) {
          popup.add(
              new AbstractAction(
                  "Show All Out Edges (" + (node.getOutEdges().size() - dncsvis) + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().showOutEdges(node);
                  vv.repaint();
                }
              });
        }

        if (upcsvis > 0) {
          popup.add(
              new AbstractAction("Hide All In Edges (" + upcsvis + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().hideInEdges(node);
                  vv.repaint();
                }
              });

          popup.add(
              new AbstractAction("Hide In Edges By Relation") {

                public void actionPerformed(ActionEvent e) {
                  CaseframeBasedShowHideDialog cd =
                      new CaseframeBasedShowHideDialog(
                          GUI2.getInstance(),
                          new ArrayList<String>(
                              GUI2.getInstance().getGraph().getInShownFSymbols(node)));

                  cd.setHelpText("   Select the relations you       wish to hide from the graph.");
                  cd.setVisible(true);

                  for (String fsym : cd.getResult()) {
                    GUI2.getInstance().getGraph().hideInEdges(node, fsym);
                  }

                  vv.repaint();
                }
              });
        }
        if (dncsvis > 0 && !node.getTerm().isMolecular()) {
          popup.add(
              new AbstractAction("Hide All Out Edges (" + dncsvis + " edges)") {

                public void actionPerformed(ActionEvent e) {
                  GUI2.getInstance().getGraph().hideOutEdges(node);
                  // JungGraphPanel.instance.hideDownCableset(node);
                  vv.repaint();
                }
              });
        }

        popup.add(
            new AbstractAction("Hide Node") {

              public void actionPerformed(ActionEvent e) {
                GUI2.getInstance().getGraph().hideVertex(node);
                vv.repaint();
                // JungGraphPanel.instance.hideNode(node);
              }
            });
        if (!node.getTerm().isAsserted()) {
          popup.add(
              new AbstractAction("Assert") {

                public void actionPerformed(ActionEvent e) {
                  FnInterop.addToContext(node.getTerm(), Context.getCurrentContext());
                  GUI2.getInstance().getGraphPanel().getVV().repaint();
                }
              });
        }
        if (node.getTerm().isAsserted()) {
          popup.add(
              new AbstractAction("Unassert") {

                public void actionPerformed(ActionEvent e) {
                  FnInterop.unassertTerm(node.getTerm());
                  GUI2.getInstance().getGraphPanel().getVV().repaint();
                }
              });
        }

        popup.show(vv, e.getX(), e.getY());
      }
    }
  }
  @SuppressWarnings("serial")
  protected void handlePopup(MouseEvent e) {
    @SuppressWarnings("unchecked")
    final VisualizationViewer<Node, Link> vv = (VisualizationViewer<Node, Link>) e.getSource();
    final Point2D p = e.getPoint();
    JPopupMenu popup = new JPopupMenu();

    GraphElementAccessor<Node, Link> pickSupport = vv.getPickSupport();

    if (pickSupport != null) {
      final Node pickedNode = pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());
      final Link pickedLink = pickSupport.getEdge(vv.getGraphLayout(), p.getX(), p.getY());
      if (pickedNode != null) {

        popup.add(
            new AbstractAction("Rename node") {
              public void actionPerformed(ActionEvent e) {
                String newNodeName =
                    JOptionPane.showInputDialog(null, "New name : ", "Rename Node", 1);
                String previousNodeName = pickedNode.name;
                if (newNodeName != null) {
                  if (newNodeName.isEmpty() == false) {
                    pickedNode.name = newNodeName;
                    vv.updateUI();
                    JOptionPane.showMessageDialog(
                        null,
                        "Node \""
                            + previousNodeName
                            + "\" has been renamed to \""
                            + newNodeName
                            + "\"",
                        "Rename Node",
                        1);
                  }
                  if (newNodeName.isEmpty()) {
                    JOptionPane.showMessageDialog(
                        null, "You can not leave the name empty", "Rename Link", 1);
                  }
                }
              }
            });
        popup.add(
            new AbstractAction("Delete node") {
              public void actionPerformed(ActionEvent e) {
                int confirmDelete =
                    JOptionPane.showConfirmDialog(
                        null,
                        "You are about to delete \"" + pickedNode.name + "\", are you sure?",
                        "Confirm",
                        JOptionPane.OK_CANCEL_OPTION);
                if (confirmDelete == 0) g.removeVertex(pickedNode);
                vv.updateUI();
              }
            });

        popup.show(vv, e.getX(), e.getY());
      }
      if (pickedLink != null) {
        popup.add(
            new AbstractAction("Set cost") {
              public void actionPerformed(ActionEvent e) {
                String newLinkCost =
                    JOptionPane.showInputDialog(null, "Set cost : ", "Set link cost", 1);
                @SuppressWarnings("unused")
                double previousLinkName = pickedLink.cost;
                if (newLinkCost != null) {
                  if (newLinkCost.isEmpty() == false) {
                    pickedLink.cost = Integer.parseInt((newLinkCost));

                    vv.updateUI();
                    //	                     		     JOptionPane.showMessageDialog(null, "Link cost
                    // has been set to \"" + newLinkCost + "\"", "Change link cost", 1);
                  }
                  if (newLinkCost.isEmpty()) {
                    JOptionPane.showMessageDialog(
                        null, "You can not leave the cost empty", "Change link cost", 1);
                  }
                }
              }
            });
        popup.add(
            new AbstractAction("Rename link") {
              public void actionPerformed(ActionEvent e) {
                String newLinkName =
                    JOptionPane.showInputDialog(null, "New name : ", "Rename link", 1);
                String previousLinkName = pickedLink.name;
                if (newLinkName != null) {
                  if (newLinkName.isEmpty() == false) {
                    pickedLink.name = newLinkName;
                    vv.updateUI();
                    JOptionPane.showMessageDialog(
                        null,
                        "Link \""
                            + previousLinkName
                            + "\" has been renamed to \""
                            + newLinkName
                            + "\"",
                        "Rename Link",
                        1);
                  }
                  if (newLinkName.isEmpty()) {
                    JOptionPane.showMessageDialog(
                        null, "You can not leave the name empty", "Rename Link", 1);
                  }
                }
              }
            });
        if (g.getEndpoints(pickedLink).getSecond() != g.getEndpoints(pickedLink).getFirst()) {
          popup.add(
              new AbstractAction("Reverse link") {
                public void actionPerformed(ActionEvent e) {
                  // Confirm before reverse
                  //                		int confirmDelete = JOptionPane.showConfirmDialog(null, "Are
                  // you sure?", "Reverse \"" +pickedLink.name + "\" direction",
                  // JOptionPane.OK_CANCEL_OPTION);
                  //                		if(confirmDelete == 0)

                  Link reversedLink =
                      new Link(
                          pickedLink.id,
                          g,
                          g.getEdgeType(pickedLink),
                          g.getEndpoints(pickedLink).getSecond(),
                          g.getEndpoints(pickedLink).getFirst());
                  reversedLink.cost = pickedLink.cost;
                  g.removeEdge(pickedLink);
                  vv.updateUI();
                }
              });
        }
        popup.add(
            new AbstractAction("Delete link") {
              public void actionPerformed(ActionEvent e) {
                int confirmDelete =
                    JOptionPane.showConfirmDialog(
                        null,
                        "You are about to delete \"" + pickedLink.name + "\", are you sure?",
                        "Confirm",
                        JOptionPane.OK_CANCEL_OPTION);
                if (confirmDelete == 0) g.removeEdge(pickedLink);
                vv.updateUI();
              }
            });
        popup.show(vv, e.getX(), e.getY());
      }
      //            if(pickedLink == null && pickedNode == null) {
      //                popup.add(new AbstractAction("Test Canvas") {
      //                 public void actionPerformed(ActionEvent e) {
      //                    System.out.println("Action Performed");
      //                    }
      //                });
      //                popup.show(vv, e.getX(), e.getY());
      //
      //             }
    }
  }