示例#1
0
 /**
  * @param e parameter
  * @see
  *     org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
  */
 @Override
 public void actionPerformed(ActionEvent e) {
   final XcosDiagram graph = (XcosDiagram) getGraph(e);
   final SwingScilabDockablePanel tab =
       ScilabTabFactory.getInstance().getFromCache(graph.getGraphTab());
   ClosingOperationsManager.startClosingOperation(tab);
 }
  /**
   * Add I/O blocks and reconnect them
   *
   * @param childGraph the child graph
   * @param childModel the child graph model
   * @param broken the broken entry
   */
  private void connectChild(
      final XcosDiagram childGraph, final mxGraphModel childModel, Broken broken) {
    childGraph.addCell(broken.getChildBlock());
    childGraph.addCell(broken.getChildLink());

    final mxICell source = broken.getChildTerminal(true);
    final mxICell target = broken.getChildTerminal(false);

    // then connect the link
    mxGraphModel.setTerminals(childModel, broken.getChildLink(), source, target);
  }
  /**
   * Create child cells and add them to the parent diagram. All links are also reconnected
   *
   * @param parentGraph the parent diagram
   * @param superBlock the superblock
   * @param inSelectionCells the cells in the selection
   * @return the broken descriptor set
   */
  private Collection<Broken> updateParent(
      final XcosDiagram parentGraph,
      final SuperBlock superBlock,
      final Set<Object> inSelectionCells) {
    final Collection<Broken> brokenLinks;
    final mxGraphModel parentModel = (mxGraphModel) parentGraph.getModel();

    parentModel.beginUpdate();
    try {
      /*
       * Add the internal links and fill border links Sort the broken
       * links by position (to perform a good numbering order) and keep
       * only one occurrence of a broken link.
       */
      brokenLinks = new TreeSet<Broken>();
      fillLinks(parentModel, inSelectionCells, brokenLinks);

      /*
       * Disconnect the broken links
       */
      for (Broken broken : brokenLinks) {
        mxGraphModel.setTerminals(parentModel, broken.getParentLink(), null, null);
      }

      /*
       * Add the super block
       */
      parentGraph.addCell(superBlock);

      /*
       * Main broken loop
       */
      // ordering access is : IN, OUT, e_IN, e_OUT
      final int[] ordering = {0, 0, 0, 0};
      for (Broken broken : brokenLinks) {

        // set the ordering
        incrementOrdering(ordering, broken);

        connectParent(parentGraph, parentModel, superBlock, broken);
        connectChild(parentGraph, parentModel, broken);

        /*
         * Update the view
         */
        BlockPositioning.updateBlockView(broken.getChildBlock());
      }
    } finally {
      parentModel.endUpdate();
    }

    return brokenLinks;
  }
  /**
   * Add I/O port and reconnect them
   *
   * @param parentGraph the parent graph
   * @param parentModel the parent graph model
   * @param superBlock the super block
   * @param broken the broken entry
   */
  private void connectParent(
      final XcosDiagram parentGraph,
      final mxGraphModel parentModel,
      final SuperBlock superBlock,
      Broken broken) {
    parentGraph.addCell(broken.getParentPort(), superBlock);
    parentGraph.addCell(broken.getParentLink());

    final mxICell source = broken.getParentTerminal(true);
    final mxICell target = broken.getParentTerminal(false);

    // then connect the link
    mxGraphModel.setTerminals(parentModel, broken.getParentLink(), source, target);
  }
  /**
   * Action !
   *
   * @param e the event
   * @see
   *     org.scilab.modules.gui.events.callback.CallBack#actionPerformed(java.awt.event.ActionEvent)
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    final XcosDiagram graph = (XcosDiagram) getGraph(e);

    // action disabled when the cell is edited
    final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
    if (comp.isEditing()) {
      return;
    }

    final SetContextDialog dialog = new SetContextDialog(comp, graph.getScicosParameters());

    dialog.pack();
    dialog.setVisible(true);
  }
  /** {@inheritDoc} */
  @Override
  public void actionPerformed(ActionEvent e) {
    final XcosDiagram parentGraph = (XcosDiagram) getGraph(e);

    // action disabled when the cell is edited
    final ScilabComponent comp = ((ScilabComponent) parentGraph.getAsComponent());
    if (comp.isEditing()) {
      return;
    }

    parentGraph.info(XcosMessages.GENERATE_SUPERBLOCK);
    parentGraph.getModel().beginUpdate();
    try {

      final SuperBlock superBlock;
      final Collection<Broken> brokenLinks;
      final Set<Object> inSelectionCells;

      /*
       * Allocate superBlock
       */
      final Object[] selection = parentGraph.getSelectionCells();
      final Object[] blocks = XcosDiagram.filterByClass(selection, BasicBlock.class);
      inSelectionCells = new LinkedHashSet<Object>(Arrays.asList(blocks));

      superBlock = allocateSuperBlock(parentGraph, selection);

      /*
       * First perform all modification on the parent diagram to handle
       * well undo/redo operations.
       */
      brokenLinks = updateParent(parentGraph, superBlock, inSelectionCells);

      /*
       * Then move some cells to the child diagram
       */
      final SuperBlockDiagram childGraph =
          moveToChild(parentGraph, superBlock, brokenLinks, inSelectionCells);

      /*
       * Finish the install on the child and sync it.
       */
      childGraph.installListeners();
      childGraph.installSuperBlockListeners();
      superBlock.invalidateRpar();

      Xcos.getInstance().addDiagram(parentGraph.getSavedFile(), childGraph);
    } finally {
      parentGraph.getModel().endUpdate();
      parentGraph.info(XcosMessages.EMPTY_INFO);
    }
  }
  /**
   * Allocate a superBlock
   *
   * @param parentGraph the base graph
   * @param selection the selected blocks
   * @return the allocated super block (without specific listeners)
   */
  private SuperBlock allocateSuperBlock(final XcosDiagram parentGraph, final Object[] selection) {
    final SuperBlock superBlock = (SuperBlock) BlockFactory.createBlock(INTERFUNCTION_NAME);
    superBlock.setStyle(INTERFUNCTION_NAME);

    /*
     * Allocate the diagram
     */
    final SuperBlockDiagram diag = new SuperBlockDiagram(superBlock);
    superBlock.setChild(diag);
    superBlock.setParentDiagram(parentGraph);

    /*
     * Place the super block
     */
    final mxRectangle dims = parentGraph.getBoundingBoxFromGeometry(selection);
    final double minX = dims.getX();
    final double maxX = minX + dims.getWidth();

    final double minY = dims.getY();
    final double maxY = minY + dims.getHeight();

    superBlock.getGeometry().setX((maxX + minX - superBlock.getGeometry().getWidth()) / 2.0);
    superBlock.getGeometry().setY((maxY + minY - superBlock.getGeometry().getHeight()) / 2.0);

    /*
     * get statistics
     */
    int angleCounter = 0;
    int flipCounter = 0;
    int mirrorCounter = 0;
    for (Object object : selection) {
      if (object instanceof BasicBlock) {
        final BasicBlock b = (BasicBlock) object;

        angleCounter += b.getAngle();
        if (b.getFlip()) {
          flipCounter++;
        }
        if (b.getMirror()) {
          mirrorCounter++;
        }
      }
    }

    /*
     * apply statistics
     */
    final int halfSize = selection.length / 2;
    superBlock.setAngle(BlockPositioning.roundAngle(angleCounter / selection.length));
    superBlock.setFlip(flipCounter > halfSize);
    superBlock.setMirror(mirrorCounter > halfSize);

    return superBlock;
  }
  /**
   * Move the cells to the child graph
   *
   * @param parentGraph the parent graph
   * @param superBlock the superBlock
   * @param brokenLinks the broken links set
   * @param inSelectionCells the cells in selection
   * @return the superblock child diagram
   */
  private SuperBlockDiagram moveToChild(
      final XcosDiagram parentGraph,
      final SuperBlock superBlock,
      final Collection<Broken> brokenLinks,
      final Set<Object> inSelectionCells) {
    final SuperBlockDiagram childGraph = superBlock.getChild();
    final mxGraphModel childModel = (mxGraphModel) childGraph.getModel();

    childModel.beginUpdate();
    try {
      final Collection<Object> cellsToCopy = new ArrayList<Object>();

      /*
       * create a collection with all the cells to move
       */
      cellsToCopy.addAll(inSelectionCells);
      for (Broken b : brokenLinks) {
        cellsToCopy.add(b.getChildBlock());
        cellsToCopy.add(b.getChildLink());
      }

      /*
       * Really copy the cells
       */
      final Object[] cells = cellsToCopy.toArray();
      parentGraph.removeCells(cells, false);
      childGraph.addCells(cells);

      childGraph.setChildrenParentDiagram();
      BlockPositioning.updateBlockView(superBlock);
    } finally {
      childModel.endUpdate();
    }

    return childGraph;
  }
示例#9
0
  @Override
  public void actionPerformed(ActionEvent e) {
    final XcosDiagram graph = (XcosDiagram) getGraph(e);
    final ScilabDirectHandler handler = ScilabDirectHandler.acquire();
    if (handler == null) {
      return;
    }

    final BasicBlock block;
    final ActionListener callback;
    try {
      /*
       * Export the whole diagram, to update all the sub-diagrams on demand.
       */
      handler.writeDiagram(graph.getRootDiagram());

      /*
       * Then export the selected block
       */
      Object cell = graph.getSelectionCell();
      if (cell instanceof BasicBlock) {
        block = (BasicBlock) cell;
        handler.writeBlock(block);
      } else {
        block = null;
      }

      /*
       * Import the updated block
       */
      if (block != null) {
        callback =
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                try {

                  final BasicBlock modifiedBlock = handler.readBlock();
                  block.updateBlockSettings(modifiedBlock);

                  graph.fireEvent(
                      new mxEventObject(
                          XcosEvent.ADD_PORTS, XcosConstants.EVENT_BLOCK_UPDATED, block));
                } catch (ScicosFormatException e1) {
                  LOG.severe(e1.getMessage());
                } finally {
                  handler.release();
                }
              }
            };
      } else {
        callback =
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                handler.release();
              }
            };
      }

      ScilabInterpreterManagement.asynchronousScilabExec(callback, localCommand);
    } catch (InterpreterException e2) {
      LOG.warning(e2.toString());

      handler.release();
    }
  }
示例#10
0
  /**
   * @param e parameter
   * @see
   *     org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    final XcosDiagram graph = (XcosDiagram) getGraph(e);

    // action disabled when the cell is edited
    final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
    if (comp.isEditing()) {
      return;
    }

    graph.info(XcosMessages.EXPORT_IN_PROGRESS);

    final ScilabDirectHandler handler = ScilabDirectHandler.acquire();
    if (handler == null) {
      return;
    }
    (new SwingWorker<Void, Void>() {

          @Override
          protected Void doInBackground() {
            try {
              handler.writeDiagram(((XcosDiagram) getGraph(null)));
              ((XcosDiagram) getGraph(null)).setReadOnly(true);
            } catch (Exception e) {
              cancel(true);
            }
            return null;
          }

          @Override
          protected void done() {
            if (isCancelled()) {
              graph.info(XcosMessages.EMPTY_INFO);

              handler.release();
              return;
            }

            graph.info(XcosMessages.COMPILATION_IN_PROGRESS);
            String cmd = "cpr = xcos_compile(scs_m);";

            final ActionListener action =
                new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                    graph.setReadOnly(false);
                    graph.info(XcosMessages.EMPTY_INFO);

                    handler.release();
                  }
                };

            try {
              ScilabInterpreterManagement.asynchronousScilabExec(action, cmd);
            } catch (InterpreterException e) {
              Logger.getLogger(CompileAction.class.getName()).severe(e.toString());

              handler.release();
            }
          }
        })
        .execute();
  }