@Test
  public void testSimple() throws Exception {
    List<String> list = new ArrayList<String>();
    list.add("hello");
    list.add("world");

    String uuid = Clipboard.put(list);

    List<String> serList = (List<String>) Clipboard.get(uuid);

    assertEquals(serList.get(0), list.get(0));
  }
  @Override
  protected void execute(ExecutionEvent event, SortedSet<IEntity> resources) {
    if (resources.size() == 0) {
      return;
    }

    if (!CommonIDEParameters.isRolesEnabled()) {
      // assume trial instance, hence disable this function
      MessageDialog.openInformation(null, PASTE_ERROR, PASTE_FUNCTION_IS_DISABLED_IN_THIS_INSTANCE);
      return;
    }

    IRepository repository = RepositoryFacade.getInstance().getRepository();

    String targetReposiotryPath = resources.first().getPath().toString();

    Clipboard clipboard = Clipboard.getInstance();

    String command = clipboard.getCommand();

    Throwable throwable = null;
    if (CUT.equals(command) || COPY.equals(command)) {

      for (Object name : clipboard) {
        IEntity resource = (IEntity) name;
        String sourceRepositoryPath = resource.getPath().toString();
        try {
          byte[] data = repository.exportZip(sourceRepositoryPath, true);
          repository.importZip(data, targetReposiotryPath);
        } catch (IOException e) {
          if (throwable == null) {
            throwable = e;
          }
        }
        if (CUT.equals(command)) {
          try {
            resource.delete();
          } catch (IOException e) {
            if (throwable == null) {
              throwable = e;
            }
          }
        }
      }
    }

    if (throwable != null) {
      MessageDialog.openWarning(null, PASTE_ERROR, SOME_OR_ALL_OF_THE_FILES_COULD_NOT_BE_PASTED);
    }

    RefreshHandler.refreshActivePart(event);
  }
Beispiel #3
0
 public CommandResult execute(CommandContext context, Selection selection) {
   Clipboard.get(context).set(context.getScreen(), selection, this);
   return new BatchResult(
       new UpdateCommandsResult(),
       new NotificationResult(context, this)
           .setArgs(selection.size())
           .setDefaultMessage("{0,choice,1#Item|1<{0} items} put into the clipboard"));
 }
  public String inject(Player sender, String command) {
    Clipboard clipboard = new Clipboard(sender, plugin);

    String[] parts = command.split(" ");
    for (int ii = 0; ii < parts.length; ii++) {
      if (ii == 0) {
        continue;
      }

      try {
        if (parts[ii].startsWith("uuid:")) {
          int index = Integer.parseInt(parts[ii].substring("uuid:".length()));

          String uuid = clipboard.getUUID(index);
          if (uuid != null) {
            parts[ii] = uuid;
          }
        } else if (parts[ii].startsWith("name:")) {
          int index = Integer.parseInt(parts[ii].substring("name:".length()));

          String name = clipboard.getName(index);
          if (name != null) {
            parts[ii] = name;
          }
        } else if (parts[ii].startsWith("ip:")) {
          int index = Integer.parseInt(parts[ii].substring("ip:".length()));

          String ip = clipboard.getIp(index);
          if (ip != null) {
            parts[ii] = ip;
          }
        }
      } catch (NumberFormatException e) {
        // ignore
      }
    }

    StringBuilder builder = new StringBuilder(command.length());
    for (int ii = 0; ii < parts.length; ii++) {
      builder.append(parts[ii]).append(" ");
    }

    builder.setLength(builder.length() - 1);
    return builder.toString();
  }
 public boolean canPaste(Clipboard clipboard) {
   TransferData[] types = clipboard.getAvailableTypes();
   Transfer[] transfers = new Transfer[] {TextTransfer.getInstance(), RTFTransfer.getInstance()};
   for (int i = 0; i < types.length; i++) {
     for (int j = 0; j < transfers.length; j++) {
       if (transfers[j].isSupportedType(types[i])) return true;
     }
   }
   return false;
 }
Beispiel #6
0
 public static String getClipboard() {
   Transferable content = Clipboard.getSystemClipboard().getContents(null);
   try {
     if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
       return (String) content.getTransferData(DataFlavor.stringFlavor);
     }
   } catch (UnsupportedFlavorException e) {
     Debug.error("Env.getClipboard: UnsupportedFlavorException: " + content);
   } catch (IOException e) {
     e.printStackTrace();
   }
   return "";
 }
  @Override
  public void paste() {
    ClipboardContents clip = Clipboard.get();
    Collection<CanvasObject> contents = clip.getElements();
    List<CanvasObject> add = new ArrayList<CanvasObject>(contents.size());
    for (CanvasObject o : contents) {
      add.add(o.clone());
    }
    if (add.isEmpty()) return;

    // find how far we have to translate shapes so that at least one of the
    // pasted shapes doesn't match what's already in the model
    Collection<CanvasObject> raw = canvas.getModel().getObjectsFromBottom();
    MatchingSet<CanvasObject> cur = new MatchingSet<CanvasObject>(raw);
    int dx = 0;
    while (true) {
      // if any shapes in "add" aren't in canvas, we are done
      boolean allMatch = true;
      for (CanvasObject o : add) {
        if (!cur.contains(o)) {
          allMatch = false;
          break;
        }
      }
      if (!allMatch) break;

      // otherwise translate everything by 10 pixels and repeat test
      for (CanvasObject o : add) {
        o.translate(10, 10);
      }
      dx += 10;
    }

    Location anchorLocation = clip.getAnchorLocation();
    if (anchorLocation != null && dx != 0) {
      anchorLocation = anchorLocation.translate(dx, dx);
    }

    canvas
        .getProject()
        .doAction(
            new SelectionAction(
                canvas,
                Strings.getter("pasteClipboardAction"),
                null,
                add,
                add,
                anchorLocation,
                clip.getAnchorFacing()));
  }
 public void execute() {
   Point lastClick = view().lastClick();
   FigureSelection selection = (FigureSelection) Clipboard.getClipboard().getContents();
   if (selection != null) {
     FigureEnumerator affectedFigures =
         (FigureEnumerator) selection.getData(StandardFigureSelection.TYPE);
     if (! /*@AJHD getUndoActivity().getAffectedFigures()*/affectedFigures.hasNextFigure()) {
       return;
     }
     Rectangle r = getBounds(affectedFigures);
     affectedFigures.reset();
     view().clearSelection();
     FigureEnumeration fe = insertFigures(affectedFigures, lastClick.x - r.x, lastClick.y - r.y);
   }
 }
 public boolean isExecutableWithView() {
   return Clipboard.getClipboard().getContents() != null;
 }
  @Override
  public void computeEnabled() {
    Project proj = canvas.getProject();
    Circuit circ = canvas.getCircuit();
    Selection sel = canvas.getSelection();
    boolean selEmpty = sel.isEmpty();
    boolean canChange = proj.getLogisimFile().contains(circ);
    boolean clipExists = !Clipboard.isEmpty();
    boolean selHasRemovable = false;
    for (CanvasObject o : sel.getSelected()) {
      if (!(o instanceof AppearanceElement)) {
        selHasRemovable = true;
      }
    }
    boolean canRaise;
    boolean canLower;
    if (!selEmpty && canChange) {
      Map<CanvasObject, Integer> zs = ZOrder.getZIndex(sel.getSelected(), canvas.getModel());
      int zmin = Integer.MAX_VALUE;
      int zmax = Integer.MIN_VALUE;
      int count = 0;
      for (Map.Entry<CanvasObject, Integer> entry : zs.entrySet()) {
        if (!(entry.getKey() instanceof AppearanceElement)) {
          count++;
          int z = entry.getValue().intValue();
          if (z < zmin) zmin = z;
          if (z > zmax) zmax = z;
        }
      }
      int maxPoss = AppearanceCanvas.getMaxIndex(canvas.getModel());
      if (count > 0 && count <= maxPoss) {
        canRaise = zmin <= maxPoss - count;
        canLower = zmax >= count;
      } else {
        canRaise = false;
        canLower = false;
      }
    } else {
      canRaise = false;
      canLower = false;
    }
    boolean canAddCtrl = false;
    boolean canRemCtrl = false;
    Handle handle = sel.getSelectedHandle();
    if (handle != null && canChange) {
      CanvasObject o = handle.getObject();
      canAddCtrl = o.canInsertHandle(handle.getLocation()) != null;
      canRemCtrl = o.canDeleteHandle(handle.getLocation()) != null;
    }

    setEnabled(LogisimMenuBar.CUT, selHasRemovable && canChange);
    setEnabled(LogisimMenuBar.COPY, !selEmpty);
    setEnabled(LogisimMenuBar.PASTE, canChange && clipExists);
    setEnabled(LogisimMenuBar.DELETE, selHasRemovable && canChange);
    setEnabled(LogisimMenuBar.DUPLICATE, !selEmpty && canChange);
    setEnabled(LogisimMenuBar.SELECT_ALL, true);
    setEnabled(LogisimMenuBar.RAISE, canRaise);
    setEnabled(LogisimMenuBar.LOWER, canLower);
    setEnabled(LogisimMenuBar.RAISE_TOP, canRaise);
    setEnabled(LogisimMenuBar.LOWER_BOTTOM, canLower);
    setEnabled(LogisimMenuBar.ADD_CONTROL, canAddCtrl);
    setEnabled(LogisimMenuBar.REMOVE_CONTROL, canRemCtrl);
  }
 /** Copies the selection to the clipboard. */
 protected void copySelection() {
   FigureSelection selection = fView.getFigureSelection();
   Clipboard.getClipboard().setContents(selection);
 }
 void dispose() {
   if (shell != null) shell.dispose();
   if (clipboard != null) clipboard.dispose();
 }
 /** Copies the FigureEnumeration to the clipboard. */
 protected void copyFigures(FigureEnumeration fe, int figureCount) {
   Clipboard.getClipboard().setContents(new StandardFigureSelection(fe, figureCount));
 }
Beispiel #14
0
 public static void setClipboard(String text) {
   Clipboard.putText(Clipboard.PLAIN, Clipboard.UTF8, Clipboard.BYTE_BUFFER, text);
 }
  public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(
        SWT.Selection,
        event -> {
          FileDialog dialog = new FileDialog(shell, SWT.OPEN);
          dialog.setText("Open an image file or cancel");
          String string = dialog.open();
          if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null) image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(
        SWT.Selection,
        event -> {
          Image image = imageButton.getImage();
          if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(
                new Object[] {image.getImageData(), imageText.getText()},
                new Transfer[] {imageTransfer, textTransfer});
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(
        SWT.Selection,
        event -> {
          ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
          if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null) image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
          } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
          }
          String text = (String) clipboard.getContents(TextTransfer.getInstance());
          if (text != null) {
            imageText.setText(text);
          } else {
            imageText.setText("");
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(
        SWT.Selection,
        event -> {
          imageButton.setText("");
          Image image = imageButton.getImage();
          if (image != null) image.dispose();
          imageButton.setImage(null);
          imageText.setText("");
        });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }