Exemplo n.º 1
0
  /** @return */
  private static Component getActiveArea() {
    if (activeArea == null) {
      activeArea =
          new BufferedImagePanel() {
            public void paintComponent(Graphics g) {
              super.paintComponent(g);
              if (onlyPDFs == null) {
                return;
              }

              // draw list of file names
              final Graphics2D g2d = (Graphics2D) g;
              g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
              g2d.setColor(TRANSLUCENT_GRAY);
              g2d.setFont(FONT);
              int stringY = (int) ((activeArea.getHeight() - onlyPDFs.size() * 40) / 2.0);
              for (File f : onlyPDFs) {
                g2d.drawString(f.getName(), 50, stringY);
                stringY += 50;
              }
            }
          };
      activeArea.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.LIGHT_GRAY));
      activeArea.setBackground(Color.DARK_GRAY);
      fileTransferHandler = new FileTransferHandler();
      activeArea.setTransferHandler(fileTransferHandler);

      try {
        dropTarget = new DropTarget();
        dropTarget.setComponent(activeArea);
        dropTarget.addDropTargetListener(getDropTargetAdapter());
      } catch (TooManyListenersException e) {
        e.printStackTrace();
      }

      pdfLogo = ImageCache.loadBufferedImage(PaperToolkit.getDataFile("icons/pdfIcon.png"));
    }
    return activeArea;
  }
  public DragDropTest_ModeMemoryStream() throws Exception {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(0, 0, 400, 300);
    this.setVisible(true);
    this.setTitle("Example for MODE_MEMORY_STREAM");

    JLabel txt = new JLabel("Drop here");
    this.getContentPane().add(txt);
    txt.setVisible(true);

    JButton button = new JButton("Paste from clipboard");
    this.getContentPane().add(button, "South");

    DropTarget dropTarget = new DropTarget();
    dropTarget.setComponent(this);
    dropTarget.addDropTargetListener(
        new DropTargetAdapter() {
          @Override
          public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde) {
            boolean dropFiles =
                dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor);
            dropFiles |= OutlookDD.isDataAvail(); // true, if this is a D&D operation from Outlook
            System.out.println("enter: file list avail=" + dropFiles);
            super.dragEnter(dtde);
          };

          @Override
          public void dragOver(java.awt.dnd.DropTargetDragEvent dtde) {
            boolean dropFiles =
                dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor);
            dropFiles |= OutlookDD.isDataAvail(); // true, if this is a D&D operation from Outlook
            System.out.println("over: file list avail=" + dropFiles);

            int action = dtde.getDropAction();
            if (!dropFiles) action = DnDConstants.ACTION_NONE;

            // Un-comment this line, if you do not want to remove the Email from Outlook:
            // if (action == DnDConstants.ACTION_MOVE) action = DnDConstants.ACTION_COPY;

            dtde.acceptDrag(action);

            super.dragOver(dtde);
          };

          public void drop(DropTargetDropEvent dtde) {
            try {

              int action = dtde.getDropAction();

              // Un-comment this line, if you do not want to remove the Email from Outlook:
              // if (action == DnDConstants.ACTION_MOVE) action = DnDConstants.ACTION_COPY;

              dtde.acceptDrop(action);

              Transferable t = dtde.getTransferable();
              if (OutlookDD.isDataAvail()) { // true, if this is a D&D operation from Outlook
                try {
                  OutlookData outlData = OutlookDD.getData();
                  handleOutlookData(outlData);
                } finally {
                  OutlookDD.release();
                }

                //          // Test: other heavyweight components do not disturb the D&D
                // functionality
                //          SwingUtilities.invokeLater(new Runnable() {
                //            public void run() {
                //              for (int i = 0; i < 3; i++) {
                //                 JDialog dlg = new JDialog();
                //                 dlg.setBounds(10,10, 300, 300);
                //                 dlg.setTitle("Dialog");
                //                 JTextField txt = new JTextField();
                //                 dlg.add(txt);
                //                 dlg.setVisible(true);
                //              }
                //            }
                //           });

              } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {

                List<?> list = (List<?>) t.getTransferData(DataFlavor.javaFileListFlavor);
                for (Object o : list) {
                  System.out.println("file=" + o);
                }
              }

            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              dtde.dropComplete(true);
            }
          }
        });

    // On-click-handler for button "Paste..."
    button.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {

            // First check for Outlook clipboard data
            if (OutlookDD.isClipboardDataAvail()) {
              try {
                OutlookData outlData = OutlookDD.getClipboardData();
                handleOutlookData(outlData);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
                OutlookDD.release();
              }
            }

            // Handle other clipboard formats using the
            // AWT clipboard functions.
            else {
              String text = getTextFromSystemClipboard();
              System.out.println("text from clipboard=" + text);
            }
          }
        });
  }