예제 #1
0
  /**
   * This method causes a transfer to a component from a clipboard or a DND drop operation. The
   * Transferable represents the data to be imported into the component.
   *
   * @param comp The component to receive the transfer. This argument is provided to enable sharing
   *     of TransferHandlers by multiple components.
   * @param t The data to import
   * @return <code>true</code> iff the data was inserted into the component.
   */
  public boolean importData(JComponent comp, Transferable t) {

    JTextComponent c = (JTextComponent) comp;
    withinSameComponent = c == exportComp;

    // if we are importing to the same component that we exported from
    // then don't actually do anything if the drop location is inside
    // the drag location and set shouldRemove to false so that exportDone
    // knows not to remove any data
    if (withinSameComponent && c.getCaretPosition() >= p0 && c.getCaretPosition() <= p1) {
      shouldRemove = false;
      return true;
    }

    boolean imported = false;
    DataFlavor importFlavor = getImportFlavor(t.getTransferDataFlavors(), c);
    if (importFlavor != null) {
      try {
        InputContext ic = c.getInputContext();
        if (ic != null) ic.endComposition();
        Reader r = importFlavor.getReaderForText(t);
        handleReaderImport(r, c);
        imported = true;
      } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
      } catch (BadLocationException ble) {
        ble.printStackTrace();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }

    return imported;
  }
예제 #2
0
 /**
  * Returns an object which represents the data to be transferred. The class of the object
  * returned is defined by the representation class of the flavor.
  *
  * @param flavor the requested flavor for the data
  * @see DataFlavor#getRepresentationClass
  * @exception IOException if the data is no longer available in the requested flavor.
  * @exception UnsupportedFlavorException if the requested data flavor is not supported.
  */
 public Object getTransferData(DataFlavor flavor)
     throws UnsupportedFlavorException, IOException {
   if (isPlainFlavor(flavor)) {
     String data = getPlainData();
     data = (data == null) ? "" : data;
     if (String.class.equals(flavor.getRepresentationClass())) {
       return data;
     } else if (Reader.class.equals(flavor.getRepresentationClass())) {
       return new StringReader(data);
     } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
       return new StringBufferInputStream(data);
     }
     // fall through to unsupported
   } else if (isStringFlavor(flavor)) {
     String data = getPlainData();
     data = (data == null) ? "" : data;
     return data;
   }
   throw new UnsupportedFlavorException(flavor);
 }