Esempio n. 1
0
  public void selectTrack(
      final Collection<mxCell> vertices, final Collection<mxCell> edges, final int direction) {

    // Look for spot and edges matching given mxCells
    HashSet<Spot> inspectionSpots = new HashSet<Spot>(vertices.size());
    for (mxCell cell : vertices) {
      Spot spot = graph.getSpotFor(cell);
      if (null == spot) {
        if (DEBUG) {
          System.out.println(
              "[TrackScheme] selectWholeTrack: tried to retrieve cell "
                  + cell
                  + ", unknown to spot map.");
        }
        continue;
      }
      inspectionSpots.add(spot);
    }
    HashSet<DefaultWeightedEdge> inspectionEdges = new HashSet<DefaultWeightedEdge>(edges.size());
    for (mxCell cell : edges) {
      DefaultWeightedEdge dwe = graph.getEdgeFor(cell);
      if (null == dwe) {
        if (DEBUG) {
          System.out.println(
              "[TrackScheme] select whole track: tried to retrieve cell "
                  + cell
                  + ", unknown to edge map.");
        }
        continue;
      }
      inspectionEdges.add(dwe);
    }
    // Forward to selection model
    selectionModel.selectTrack(inspectionSpots, inspectionEdges, direction);
  }
Esempio n. 2
0
  /**
   * Called when the user makes a selection change in the graph. Used to forward this event to the
   * {@link InfoPane} and to other {@link SelectionChangeListener}s.
   *
   * @param model the selection model
   * @param added the cells <b>removed</b> from selection (careful, inverted)
   * @param removed the cells <b>added</b> to selection (careful, inverted)
   */
  private void userChangedSelection(
      mxGraphSelectionModel mxGSmodel,
      Collection<Object> added,
      Collection<Object> removed) { // Seems to be inverted
    if (!doFireSelectionChangeEvent) return;
    Collection<Spot> spotsToAdd = new ArrayList<Spot>();
    Collection<Spot> spotsToRemove = new ArrayList<Spot>();
    Collection<DefaultWeightedEdge> edgesToAdd = new ArrayList<DefaultWeightedEdge>();
    Collection<DefaultWeightedEdge> edgesToRemove = new ArrayList<DefaultWeightedEdge>();

    if (null != added) {
      for (Object obj : added) {
        mxCell cell = (mxCell) obj;

        if (cell.getChildCount() > 0) {

          for (int i = 0; i < cell.getChildCount(); i++) {
            mxICell child = cell.getChildAt(i);
            if (child.isVertex()) {
              Spot spot = graph.getSpotFor(child);
              spotsToRemove.add(spot);
            } else {
              DefaultWeightedEdge edge = graph.getEdgeFor(child);
              edgesToRemove.add(edge);
            }
          }

        } else {

          if (cell.isVertex()) {
            Spot spot = graph.getSpotFor(cell);
            spotsToRemove.add(spot);
          } else {
            DefaultWeightedEdge edge = graph.getEdgeFor(cell);
            edgesToRemove.add(edge);
          }
        }
      }
    }

    if (null != removed) {
      for (Object obj : removed) {
        mxCell cell = (mxCell) obj;

        if (cell.getChildCount() > 0) {

          for (int i = 0; i < cell.getChildCount(); i++) {
            mxICell child = cell.getChildAt(i);
            if (child.isVertex()) {
              Spot spot = graph.getSpotFor(child);
              spotsToAdd.add(spot);
            } else {
              DefaultWeightedEdge edge = graph.getEdgeFor(child);
              edgesToAdd.add(edge);
            }
          }

        } else {

          if (cell.isVertex()) {
            Spot spot = graph.getSpotFor(cell);
            spotsToAdd.add(spot);
          } else {
            DefaultWeightedEdge edge = graph.getEdgeFor(cell);
            edgesToAdd.add(edge);
          }
        }
      }
    }
    if (DEBUG_SELECTION)
      System.out.println("[TrackScheme] userChangeSelection: sending selection change to model.");
    doFireSelectionChangeEvent = false;
    if (!edgesToAdd.isEmpty()) selectionModel.addEdgeToSelection(edgesToAdd);
    if (!spotsToAdd.isEmpty()) selectionModel.addSpotToSelection(spotsToAdd);
    if (!edgesToRemove.isEmpty()) selectionModel.removeEdgeFromSelection(edgesToRemove);
    if (!spotsToRemove.isEmpty()) selectionModel.removeSpotFromSelection(spotsToRemove);
    doFireSelectionChangeEvent = true;
  }
Esempio n. 3
0
  /**
   * This method is called when the user has created manually an edge in the graph, by dragging a
   * link between two spot cells. It checks whether the matching edge in the model exists, and tune
   * what should be done accordingly.
   *
   * @param cell the mxCell of the edge that has been manually created.
   */
  protected void addEdgeManually(mxCell cell) {
    if (cell.isEdge()) {
      final mxIGraphModel graphModel = graph.getModel();
      cell.setValue("New");
      model.beginUpdate();
      graphModel.beginUpdate();
      try {

        Spot source = graph.getSpotFor(cell.getSource());
        Spot target = graph.getSpotFor(cell.getTarget());

        if (DEBUG) {
          System.out.println(
              "[TrackScheme] #addEdgeManually: edge is between 2 spots belonging to the same frame. Removing it.");
          System.out.println(
              "[TrackScheme] #addEdgeManually: adding edge between source "
                  + source
                  + " at frame "
                  + source.getFeature(Spot.FRAME).intValue()
                  + " and target "
                  + target
                  + " at frame "
                  + target.getFeature(Spot.FRAME).intValue());
        }

        if (Spot.frameComparator.compare(source, target) == 0) {
          // Prevent adding edges between spots that belong to the same frame

          if (DEBUG) {
            System.out.println(
                "[TrackScheme] addEdgeManually: edge is between 2 spots belonging to the same frame. Removing it.");
          }
          graph.removeCells(new Object[] {cell});

        } else {
          // We can add it to the model

          // Put them right in order: since we use a oriented graph,
          // we want the source spot to precede in time.
          if (Spot.frameComparator.compare(source, target) > 0) {

            if (DEBUG) {
              System.out.println(
                  "[TrackScheme] #addEdgeManually: Source "
                      + source
                      + " succeed target "
                      + target
                      + ". Inverting edge direction.");
            }

            Spot tmp = source;
            source = target;
            target = tmp;
          }
          // We add a new jGraphT edge to the underlying model, if it does not exist yet.
          DefaultWeightedEdge edge = model.getTrackModel().getEdge(source, target);
          if (null == edge) {
            edge = model.addEdge(source, target, -1);
            if (DEBUG) {
              System.out.println(
                  "[TrackScheme] #addEdgeManually: Creating new edge: " + edge + ".");
            }
          } else {
            // Ah. There was an existing edge in the model we were trying to re-add there, from the
            // graph.
            // We remove the graph edge we have added,
            if (DEBUG) {
              System.out.println("[TrackScheme] #addEdgeManually: Edge pre-existed. Retrieve it.");
            }
            graph.removeCells(new Object[] {cell});
            // And re-create a graph edge from the model edge.
            cell = graph.addJGraphTEdge(edge);
            cell.setValue(String.format("%.1f", model.getTrackModel().getEdgeWeight(edge)));
            // We also need now to check if the edge belonged to a visible track. If not,
            // we make it visible.
            int ID = model.getTrackModel().trackIDOf(edge);
            // This will work, because track indices will be reprocessed only after the
            // graphModel.endUpdate()
            // reaches 0. So now, it's like we are dealing with the track indices priori to
            // modification.
            if (model.getTrackModel().isVisible(ID)) {
              if (DEBUG) {
                System.out.println(
                    "[TrackScheme] #addEdgeManually: Track was visible. Do nothing.");
              }
            } else {
              if (DEBUG) {
                System.out.println(
                    "[TrackScheme] #addEdgeManually: Track was invisible. Make it visible.");
              }
              importTrack(ID);
            }
          }
          graph.mapEdgeToCell(edge, cell);
        }

      } finally {
        graphModel.endUpdate();
        model.endUpdate();
        selectionModel.clearEdgeSelection();
      }
    }
  }