Example #1
0
 @Override
 public void keyTyped(KeyEvent event) {
   char keyChar = event.getKeyChar();
   if (keyChar == KeyEvent.VK_DELETE) {
     Set<V> verticesToRemove = new HashSet<V>();
     Set<V> picked = pickedVertexState.getPicked();
     for (V vertex : picked) {
       if (vertex instanceof VertexGroup<?>) {
         verticesToRemove.addAll(((VertexGroup<V>) vertex).getVertexGroupMembers());
       } else {
         verticesToRemove.add(vertex);
       }
     }
     for (V v : verticesToRemove) {
       System.out.println("\nremove vertex: " + v);
       pickedVertexState.pick(v, false);
       if (v instanceof AbstractNubisaveComponent) {
         ((AbstractNubisaveComponent) v).deactivate();
         ((AbstractNubisaveComponent) v).remove();
       }
       graph.removeVertex(v);
     }
   }
 }
  /**
   * Connect vertices in the graph. Connects vertices if startVertex and endVertex may be connected
   * by a directed edge. Ensures that: * startVertex is a RequiredPort and endVertex is a
   * ProvidedPort * the required port has not yet reached the maximal number of connections {@link
   * RequiredPort.getMaxDegree() getMaxDegree} * the ports do not belong to the same component * the
   * ports are not yet connected
   *
   * @param startVertex
   * @param endVertex
   * @param graph
   * @return true if the two vertexes were connected
   */
  protected boolean connect(NubiSaveVertex startVertex, NubiSaveVertex endVertex) {
    boolean shouldNotConnect = shouldNotConnect(startVertex, endVertex);
    System.out.println("nubisavecomponentgraphmouseplugin: connect");
    if (shouldNotConnect) {
      System.out.println("nubisavecomponentgraphmouseplugin: returning false");
      return false;
    }
    AbstractNubisaveComponent start =
        (AbstractNubisaveComponent) ((RequiredPort) startVertex).getParentComponent();
    AbstractNubisaveComponent end =
        (AbstractNubisaveComponent) ((ProvidedPort) endVertex).getParentComponent();
    if (!isConnected(startVertex, endVertex)) {
      BufferedWriter writer = null;
      try {
        System.out.println("is not connected");
        start.connectToProvidedPort(end);
        WeightedNubisaveVertexEdge edge = edgeFactory.create();
        edge.setWeight(end.getNrOfFilePartsToStore());
        graph.addEdge(edge, startVertex, endVertex, EdgeType.DIRECTED);
        File file = new File(storage_directory + "/" + "connections.txt");
        if (!file.exists()) {
          file.createNewFile();
        }
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write(start.getUniqueName());
        writer.write(" ");
        writer.write(end.getUniqueName());
        writer.newLine();
        writer.close();
      } catch (IOException ex) {
        Logger.getLogger(AbstractNubisaveComponentEdgeCreator.class.getName())
            .log(Level.SEVERE, null, ex);
      } finally {
        try {
          writer.close();
        } catch (IOException ex) {
          Logger.getLogger(AbstractNubisaveComponentEdgeCreator.class.getName())
              .log(Level.SEVERE, null, ex);
        }
      }

    } else {
      System.out.println("is  connected --> increase weight");
      WeightedNubisaveVertexEdge edge =
          (WeightedNubisaveVertexEdge) graph.findEdge(startVertex, endVertex);
      System.out.println("edge weight: " + edge.getWeight());
      int before = end.getNrOfFilePartsToStore();
      System.out.println("nroffileparts1: " + before);
      end.setNrOfFilePartsToStore(end.getNrOfFilePartsToStore() + 1);
      System.out.println("nroffileparts2: " + end.getNrOfFilePartsToStore());
      assert (end.getNrOfFilePartsToStore() - 1) == before;
      edge.setWeight(end.getNrOfFilePartsToStore());
      assert (edge.getWeight() - 1) == before;
      System.out.println("incrreased edge weight: " + edge.getWeight());
    }
    System.out.println("nubisavecomponentgraphmouseplugin: returning true");
    return true;
  }