Esempio n. 1
0
  private boolean canPasteBefore(EditorCell selectedCell, List<SNode> pasteNodes) {
    if (!selectedCell.isFirstPositionInBigCell()) return false;
    SNode anchor = selectedCell.getSNode();
    if (anchor.getParent() == null) return false;

    NodeAndRole nodeAndRole =
        new NodePaster(pasteNodes).getActualAnchorNode(anchor, anchor.getRoleInParent());
    if (nodeAndRole == null) return false;

    EditorCell targetCell = selectedCell.getEditor().findNodeCell(nodeAndRole.myNode);
    return targetCell != null
        && targetCell.getFirstLeaf(CellConditions.SELECTABLE) == selectedCell
        && new NodePaster(pasteNodes).canPasteRelative(nodeAndRole.myNode);
  }
Esempio n. 2
0
  public boolean canExecute(EditorContext context) {
    EditorCell selectedCell = getCellToPasteTo((EditorCell) context.getSelectedCell());
    if (selectedCell == null) {
      return false;
    }
    SNode selectedNode = selectedCell.getSNode();
    if (selectedNode == null || jetbrains.mps.util.SNodeOperations.isDisposed(selectedNode)) {
      return false;
    }
    List<SNode> pasteNodes = CopyPasteUtil.getNodesFromClipboard(selectedNode.getModel());
    if (pasteNodes == null || pasteNodes.isEmpty()) {
      return CopyPasteUtil.isConversionAvailable(selectedNode.getModel(), selectedNode);
    }

    if (!new NodePaster(pasteNodes).canPaste(selectedCell)) {
      LOG.debug("Couldn't paste node here");
      return false;
    }
    return true;
  }
Esempio n. 3
0
  public void execute(final EditorContext context) {
    LOG.assertInCommand();
    final EditorComponent editorComponent = (EditorComponent) context.getEditorComponent();
    EditorCell pasteTargetCell = getCellToPasteTo(editorComponent.getSelectedCell());
    final CellInfo pasteTargetCellInfo = pasteTargetCell.getCellInfo();
    final SNode nodeSelected = pasteTargetCell.getSNode();
    final SNodePointer selectedNodePointer = new SNodePointer(nodeSelected);
    final SModel model = nodeSelected.getModel();
    // sometimes model is not in repository (paste in merge dialog)
    final boolean inRepository = model.getModelDescriptor() == selectedNodePointer.getModel();

    PasteNodeData data = CopyPasteUtil.getPasteNodeDataFromClipboard(model);
    if (data == null || data.getNodes().isEmpty()) {
      data =
          CopyPasteUtil.getConvertedFromClipboard(
              model, context.getOperationContext().getProject());
      if (data == null || data.getNodes().isEmpty()) return;
    }
    final PasteNodeData pasteNodeData = data;

    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            final Runnable addImportsRunnable =
                CopyPasteUtil.addImportsWithDialog(
                    pasteNodeData, model, context.getOperationContext());
            ModelAccess.instance()
                .runCommandInEDT(
                    new Runnable() {
                      public void run() {
                        if (addImportsRunnable != null) {
                          addImportsRunnable.run();
                        }
                        SNode selectedNode =
                            inRepository ? selectedNodePointer.getNode() : nodeSelected;
                        if (jetbrains.mps.util.SNodeOperations.isDisposed(selectedNode)) {
                          StringBuilder errorText =
                              new StringBuilder(
                                  "Selected node is disposed: " + selectedNode.toString());
                          SModelReference modelReference = selectedNodePointer.getModelReference();
                          if (modelReference != null) {
                            SModelDescriptor modelDescriptor =
                                SModelRepository.getInstance().getModelDescriptor(modelReference);
                            if (modelDescriptor != null) {
                              SModel sModel = modelDescriptor.getSModel();
                              errorText.append(", sModel.isDisposed(): " + sModel.isDisposed());
                              SNode node = sModel.getNodeById(selectedNodePointer.getNodeId());
                              if (node != null) {
                                errorText.append(
                                    ", node != null, node.isDisposed(): "
                                        + jetbrains.mps.util.SNodeOperations.isDisposed(node));
                              } else {
                                errorText.append(", node == null");
                              }
                            }
                          }
                          LOG.error(errorText.toString());
                          return;
                        }
                        EditorCell selectedCell = pasteTargetCellInfo.findCell(editorComponent);
                        assert selectedCell != null;

                        List<SNode> pasteNodes = pasteNodeData.getNodes();

                        if (canPasteBefore(selectedCell, pasteNodes)) {
                          new NodePaster(pasteNodes)
                              .pasteRelative(selectedNode, PastePlaceHint.BEFORE_ANCHOR);
                        } else {
                          new NodePaster(pasteNodes).paste(selectedCell);
                        }

                        Set<SReference> requireResolveReferences = new HashSet<SReference>();
                        for (SReference ref : pasteNodeData.getRequireResolveReferences()) {
                          // ref can be detached from model while using copy/paste handlers
                          if (ref.getSourceNode() == null || ref.getSourceNode().getModel() == null)
                            continue;
                          requireResolveReferences.add(ref);
                        }

                        ResolverComponent.getInstance()
                            .resolveScopesOnly(
                                requireResolveReferences, context.getOperationContext());

                        // set selection
                        editorComponent.flushEvents();
                        EditorCell nodeCell = editorComponent.findNodeCell(pasteNodes.get(0));
                        if (nodeCell == null) return; // after 'set reference'?

                        EditorCell_Label labelCell =
                            nodeCell.findChild(CellFinders.byClass(EditorCell_Label.class, true));
                        if (labelCell != null) {
                          editorComponent.changeSelection(labelCell);
                        }

                        if (pasteNodes.size() == 1) {
                          editorComponent.pushSelection(nodeCell);
                        } else {
                          SNode firstNodeToSelect = pasteNodes.get(0);
                          SNode lastNodeToSelect = null;
                          for (int i = pasteNodes.size() - 1;
                              i > 0 && lastNodeToSelect == null;
                              i--) {
                            if (pasteNodes.get(i).getParent() == firstNodeToSelect.getParent()) {
                              lastNodeToSelect = pasteNodes.get(i);
                            }
                          }
                          if (lastNodeToSelect != null) {
                            SelectionManager selectionManager =
                                editorComponent.getSelectionManager();
                            selectionManager.pushSelection(
                                selectionManager.createRangeSelection(
                                    firstNodeToSelect, lastNodeToSelect));
                          }
                        }
                      }
                    },
                    context.getOperationContext().getProject());
          }
        });
  }