public static void main(String... aArguments) {
    TextTransfer textTransfer = new TextTransfer();

    // display what is currently on the clipboard
    System.out.println("Clipboard contains:" + textTransfer.getClipboardContents());

    // change the contents and then re-display
    textTransfer.setClipboardContents("blah, blah, blah");
    System.out.println("Clipboard contains:" + textTransfer.getClipboardContents());
  }
 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;
 }
 /** Returns the data to be transferred in a drag and drop operation. */
 @Override
 public void dragSetData(DragSourceEvent event) {
   if (event.doit == false) return;
   if (ModelDataTransfer.getInstance().isSupportedType(event.dataType)) {
     event.data = getSelectedModelObjects();
     fDragData = event.data;
     return;
   }
   if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
     event.data =
         createTextualRepresentation((IStructuredSelection) fSelectionProvider.getSelection());
     fDragData = null;
     return;
   }
 }
  public static void setDragDrop(final Text text) {
    Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;

    final DragSource source = new DragSource(text, operations);
    source.setTransfer(types);
    source.addDragListener(
        new DragSourceListener() {
          Point selection;

          @Override
          public void dragStart(DragSourceEvent e) {
            selection = text.getSelection();
            e.doit = selection.x != selection.y;
          }

          @Override
          public void dragSetData(DragSourceEvent e) {
            e.data = text.getText(selection.x, selection.y - 1);
          }

          @Override
          public void dragFinished(DragSourceEvent e) {
            if (e.detail == DND.DROP_MOVE) {
              text.setSelection(selection);
              text.insert("");
            }
            selection = null;
          }
        });

    DropTarget target = new DropTarget(text, operations);
    target.setTransfer(types);
    target.addDropListener(
        new DropTargetAdapter() {
          @Override
          public void drop(DropTargetEvent event) {
            if (event.data == null) {
              event.detail = DND.DROP_NONE;
              return;
            }
            text.append((String) event.data);
          }
        });
  }
  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();
  }
Example #6
0
  /**
   * Process the menu events.
   *
   * @param evt the event.
   * @param fff the frame in which the menu is present.
   */
  public void processMenuActions(ActionEvent evt, FidoFrame fff) {
    ExportTools et = fff.getExportTools();
    PrintTools pt = fff.getPrintTools();
    CircuitPanel cc = fff.cc;
    String arg = evt.getActionCommand();
    EditorActions edt = cc.getEditorActions();
    CopyPasteActions cpa = cc.getCopyPasteActions();
    ElementsEdtActions eea = cc.getContinuosMoveActions();
    SelectionActions sa = cc.getSelectionActions();
    ParserActions pa = cc.getParserActions();

    // Edit the FidoCadJ code of the drawing
    if (arg.equals(Globals.messages.getString("Define"))) {
      EnterCircuitFrame circuitDialog =
          new EnterCircuitFrame(fff, cc.getParserActions().getText(!cc.extStrict).toString());
      circuitDialog.setVisible(true);

      pa.parseString(new StringBuffer(circuitDialog.getStringCircuit()));
      cc.getUndoActions().saveUndoState();
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("LibraryUpdate"))) {
      // Update libraries
      fff.loadLibraries();
      fff.show();
    } else if (arg.equals(Globals.messages.getString("Circ_opt"))) {
      // Options for the current drawing
      fff.showPrefs();
    } else if (arg.equals(Globals.messages.getString("Layer_opt"))) {
      // Options for the layers
      DialogLayer layerDialog = new DialogLayer(fff, cc.dmp.getLayers());
      layerDialog.setVisible(true);

      // It is important that we force a complete recalculation of
      // all details in the drawing, otherwise the buffered setup
      // will not be responsive to the changes in the layer editing.

      cc.dmp.setChanged(true);
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Print"))) {
      // Print the current drawing
      pt.associateToCircuitPanel(cc);
      pt.printDrawing(fff);
    } else if (arg.equals(Globals.messages.getString("SaveName"))) {
      // Save with name
      fff.getFileTools().saveWithName(false);
    } else if (arg.equals(Globals.messages.getString("Save_split"))) {
      // Save with name, split non standard macros
      fff.getFileTools().saveWithName(true);
    } else if (arg.equals(Globals.messages.getString("Save"))) {
      // Save with the current name (if available)
      fff.getFileTools().save(false);
    } else if (arg.equals(Globals.messages.getString("New"))) {
      // New drawing
      fff.createNewInstance();
    } else if (arg.equals(Globals.messages.getString("Undo"))) {
      // Undo the last action
      cc.getUndoActions().undo();
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Redo"))) {
      // Redo the last action
      cc.getUndoActions().redo();
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("About_menu"))) {
      // Show the about menu
      DialogAbout d = new DialogAbout(fff);
      d.setVisible(true);
    } else if (arg.equals(Globals.messages.getString("Open"))) {
      // Open a file
      OpenFile openf = new OpenFile();
      openf.setParam(fff);
      SwingUtilities.invokeLater(openf);
      /*
          The following code would require a thread safe implementation
          of some of the inner classes (such as CircuitModel), which was
          indeed not the case... Now, yes!
      */
      /*Thread thread = new Thread(openf);
      thread.setDaemon(true);
      // Start the thread
      thread.start();*/

    } else if (arg.equals(Globals.messages.getString("Export"))) {
      // Export the current drawing
      et.launchExport(fff, cc, fff.getFileTools().openFileDirectory);
    } else if (arg.equals(Globals.messages.getString("SelectAll"))) {
      // Select all elements in the current drawing
      sa.setSelectionAll(true);
      // Even if the drawing is not changed, a repaint operation is
      // needed since all selected elements are rendered in green.
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Copy"))) {
      // Copy all selected elements in the clipboard
      cpa.copySelected(!cc.extStrict, false);
    } else if (arg.equals(Globals.messages.getString("Copy_split"))) {
      // Copy elements, splitting non standard macros
      cpa.copySelected(!cc.extStrict, true);
    } else if (arg.equals(Globals.messages.getString("Cut"))) {
      // Cut all the selected elements
      cpa.copySelected(!cc.extStrict, false);
      edt.deleteAllSelected(true);
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Mirror_E"))) {
      // Mirror all the selected elements
      if (eea.isEnteringMacro()) eea.mirrorMacro();
      else edt.mirrorAllSelected();
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Rotate"))) {
      // 90 degrees rotation of all selected elements
      if (eea.isEnteringMacro()) eea.rotateMacro();
      else edt.rotateAllSelected();
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Duplicate"))) {
      // Duplicate
      cpa.copySelected(!cc.extStrict, false);
      cpa.paste(cc.getMapCoordinates().getXGridStep(), cc.getMapCoordinates().getYGridStep());
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("DefineClipboard"))) {
      // Paste as a new circuit
      TextTransfer textTransfer = new TextTransfer();
      FidoFrame popFrame;
      if (cc.getUndoActions().getModified()) {
        popFrame = fff.createNewInstance();
      } else {
        popFrame = fff;
      }
      pa.parseString(new StringBuffer(textTransfer.getClipboardContents()));
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Paste"))) {
      // Paste some graphical elements
      cpa.paste(cc.getMapCoordinates().getXGridStep(), cc.getMapCoordinates().getYGridStep());
      fff.repaint();
    } else if (arg.equals(Globals.messages.getString("Close"))) {
      // Close the current window
      if (!fff.getFileTools().checkIfToBeSaved()) {
        return;
      }
      fff.setVisible(false);
      cc.getUndoActions().doTheDishes();
      fff.dispose();
      Globals.openWindows.remove(fff);

      --Globals.openWindowsNumber;

      if (Globals.openWindowsNumber < 1) System.exit(0);
    }
  }