コード例 #1
0
ファイル: TrackScheme.java プロジェクト: halirutan/fiji
 /** Removes the cell selected by the user in the GUI. */
 public void removeSelectedCells() {
   graph.getModel().beginUpdate();
   try {
     graph.removeCells(graph.getSelectionCells());
     // Will be caught by the graph listeners
   } finally {
     graph.getModel().endUpdate();
   }
 }
コード例 #2
0
ファイル: TrackScheme.java プロジェクト: halirutan/fiji
  /**
   * Used to catch spot creation events that occurred elsewhere, for instance by manual editing in
   * the {@link AbstractTrackMateModelView}.
   *
   * <p>We have to deal with the graph modification ourselves here, because the {@link Model} model
   * holds a non-listenable JGraphT instance. A modification made to the model would not be
   * reflected on the graph here.
   */
  @Override
  public void modelChanged(final ModelChangeEvent event) {

    // Only catch model changes
    if (event.getEventID() != ModelChangeEvent.MODEL_MODIFIED) return;

    graph.getModel().beginUpdate();
    try {
      ArrayList<mxICell> cellsToRemove = new ArrayList<mxICell>();

      final int targetColumn = getUnlaidSpotColumn();

      // Deal with spots
      if (!event.getSpots().isEmpty()) {

        Collection<mxCell> spotsWithStyleToUpdate = new HashSet<mxCell>();

        for (Spot spot : event.getSpots()) {

          if (event.getSpotFlag(spot) == ModelChangeEvent.FLAG_SPOT_ADDED) {

            int frame = spot.getFeature(Spot.FRAME).intValue();
            // Put in the graph
            int column = Math.max(targetColumn, getNextFreeColumn(frame));
            mxICell newCell = insertSpotInGraph(spot, column); // move in right+1 free column
            rowLengths.put(frame, column);
            spotsWithStyleToUpdate.add((mxCell) newCell);

          } else if (event.getSpotFlag(spot) == ModelChangeEvent.FLAG_SPOT_MODIFIED) {

            // Change the look of the cell
            mxICell cell = updateCellOf(spot);
            spotsWithStyleToUpdate.add((mxCell) cell);

          } else if (event.getSpotFlag(spot) == ModelChangeEvent.FLAG_SPOT_REMOVED) {

            mxICell cell = graph.getCellFor(spot);
            cellsToRemove.add(cell);
          }
        }
        graph.removeCells(cellsToRemove.toArray(), true);
        stylist.updateVertexStyle(spotsWithStyleToUpdate);
      }

    } finally {
      graph.getModel().endUpdate();
    }

    // Deal with edges
    if (!event.getEdges().isEmpty()) {

      graph.getModel().beginUpdate();
      try {

        if (event.getEdges().size() > 0) {

          Map<Integer, Set<mxCell>> edgesToUpdate = new HashMap<Integer, Set<mxCell>>();

          for (DefaultWeightedEdge edge : event.getEdges()) {

            if (event.getEdgeFlag(edge) == ModelChangeEvent.FLAG_EDGE_ADDED) {

              mxCell edgeCell = graph.getCellFor(edge);
              if (null == edgeCell) {

                // Make sure target & source cells exist

                Spot source = model.getTrackModel().getEdgeSource(edge);
                mxCell sourceCell = graph.getCellFor(source);
                if (sourceCell == null) {
                  int frame = source.getFeature(Spot.FRAME).intValue();
                  // Put in the graph
                  int targetColumn = getUnlaidSpotColumn();
                  int column = Math.max(targetColumn, getNextFreeColumn(frame));
                  insertSpotInGraph(source, column); // move in right+1 free column
                  rowLengths.put(frame, column);
                }

                Spot target = model.getTrackModel().getEdgeTarget(edge);
                mxCell targetCell = graph.getCellFor(target);
                if (targetCell == null) {
                  int frame = target.getFeature(Spot.FRAME).intValue();
                  // Put in the graph
                  int targetColumn = getUnlaidSpotColumn();
                  int column = Math.max(targetColumn, getNextFreeColumn(frame));
                  insertSpotInGraph(target, column); // move in right+1 free column
                  rowLengths.put(frame, column);
                }

                // And finally create the edge cell
                edgeCell = graph.addJGraphTEdge(edge);
              }

              graph.getModel().add(graph.getDefaultParent(), edgeCell, 0);

              // Add it to the map of cells to recolor
              Integer trackID = model.getTrackModel().trackIDOf(edge);
              Set<mxCell> edgeSet = edgesToUpdate.get(trackID);
              if (edgesToUpdate.get(trackID) == null) {
                edgeSet = new HashSet<mxCell>();
                edgesToUpdate.put(trackID, edgeSet);
              }
              edgeSet.add(edgeCell);

            } else if (event.getEdgeFlag(edge) == ModelChangeEvent.FLAG_EDGE_MODIFIED) {
              // Add it to the map of cells to recolor
              Integer trackID = model.getTrackModel().trackIDOf(edge);
              Set<mxCell> edgeSet = edgesToUpdate.get(trackID);
              if (edgesToUpdate.get(trackID) == null) {
                edgeSet = new HashSet<mxCell>();
                edgesToUpdate.put(trackID, edgeSet);
              }
              edgeSet.add(graph.getCellFor(edge));

            } else if (event.getEdgeFlag(edge) == ModelChangeEvent.FLAG_EDGE_REMOVED) {

              mxCell cell = graph.getCellFor(edge);
              graph.removeCells(new Object[] {cell});
            }
          }

          stylist.execute(edgesToUpdate);
          SwingUtilities.invokeLater(
              new Runnable() {
                public void run() {
                  gui.graphComponent.refresh();
                  gui.graphComponent.repaint();
                }
              });
        }
      } finally {
        graph.getModel().endUpdate();
      }
    }
  }
コード例 #3
0
ファイル: TrackScheme.java プロジェクト: halirutan/fiji
  /**
   * 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();
      }
    }
  }