Exemplo n.º 1
0
  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    return watchedExecute(
        () -> {
          final EntryEditor editor = getActiveEntryEditor();
          if (editor == null) {
            return null;
          }

          String data = fromClipboard(editor);

          ISelectionProvider sp = editor.getSite().getSelectionProvider();
          IStructuredSelection sel = (IStructuredSelection) sp.getSelection();

          TreeNode selection = (TreeNode) sel.getFirstElement();
          ValueData selectionVD = ValueData.of(selection);
          Thype selThype = selectionVD.element().thype();

          try {
            if (selThype instanceof CollectionThypeLike) {
              selection = pasteCollection(editor, selection, data);
            } else if (selThype instanceof SimpleThypeLike) {
              selection = pasteSimple(editor, selection, data);
            } else {
              ElementHelper.unsupportedThype(selThype);
            }
          } catch (Exception e) {
            ElementHelper.panic(log, "pasting content", e);
            throw e;
          }

          editor.refresh(selection, false);
          editor.markDirty();

          return null;
        });
  }
Exemplo n.º 2
0
  private TreeNode pasteCollection(EntryEditor editor, TreeNode parent, String data) {
    Shell shell = editor.getSite().getShell();
    Thype parentThype = ValueData.of(parent).element().thype();
    SpreadSheetTable table = new SpreadSheetTable(data);
    if (parentThype instanceof ArrayThype) {
      if (table.numCols() != 1) {
        String title = "Invalid content";
        S msg = new S();
        msg.addf("You can only paste a single column table" + " onto an array node!");
        MessageDialog.openError(shell, title, msg.toString());
        return parent;
      }

      parent = ElementHelper.copyOnNeed(parent, editor);
      if (parent == null) {
        return null;
      }

      addArrayElements(parent, table, range(0, table.numRows()), log);
    } else if (parentThype instanceof MapThype) {
      if (table.numCols() != 2) {
        String title = "Invalid content";
        S msg = new S();
        msg.addf("You can only paste a two-column table" + " onto a map node!");
        MessageDialog.openError(shell, title, msg.toString());
        return null;
      }

      parent = ElementHelper.copyOnNeed(parent, editor);
      if (parent == null) {
        return null;
      }

      addMapElements(parent, table, range(0, table.numRows()), log);
    } else {
      ElementHelper.unsupportedThype(parentThype);
      return null;
    }

    return parent;
  }