예제 #1
0
  /**
   * This method has the same behavior as {@link #equals(Object)}. The only difference being that it
   * takes a {@code DataFlavor} instance as a parameter.
   *
   * @param that the <code>DataFlavor</code> to compare with <code>this</code>
   * @return <code>true</code> if <code>that</code> is equivalent to this <code>DataFlavor</code>;
   *     <code>false</code> otherwise
   * @see #selectBestTextFlavor
   */
  public boolean equals(DataFlavor that) {
    if (that == null) {
      return false;
    }
    if (this == that) {
      return true;
    }

    if (representationClass == null) {
      if (that.getRepresentationClass() != null) {
        return false;
      }
    } else {
      if (!representationClass.equals(that.getRepresentationClass())) {
        return false;
      }
    }

    if (mimeType == null) {
      if (that.mimeType != null) {
        return false;
      }
    } else {
      if (!mimeType.match(that.mimeType)) {
        return false;
      }

      if ("text".equals(getPrimaryType())
          && DataTransferer.doesSubtypeSupportCharset(this)
          && representationClass != null
          && !(isRepresentationClassReader()
              || String.class.equals(representationClass)
              || isRepresentationClassCharBuffer()
              || DataTransferer.charArrayClass.equals(representationClass))) {
        String thisCharset = DataTransferer.canonicalName(getParameter("charset"));
        String thatCharset = DataTransferer.canonicalName(that.getParameter("charset"));
        if (thisCharset == null) {
          if (thatCharset != null) {
            return false;
          }
        } else {
          if (!thisCharset.equals(thatCharset)) {
            return false;
          }
        }
      }
    }

    return true;
  }
 /**
  * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
  */
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   /*
    * System.out.println("isDataFlavorSupported: " + flavor.getMimeType()
    * + " " + flavor.getHumanPresentableName());
    */
   for (Iterator<DataFlavor> i = exportFlavors.iterator(); i.hasNext(); ) {
     DataFlavor supportedFlavor = i.next();
     if (supportedFlavor.getPrimaryType().equals(flavor.getPrimaryType())
         && supportedFlavor.getSubType().equals(flavor.getSubType())
         && flavor
             .getRepresentationClass()
             .isAssignableFrom(supportedFlavor.getRepresentationClass())) {
       return true;
     }
   }
   return false;
 }
예제 #3
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);
 }
 /**
  * @param flavor
  * @param supportedFlavors
  * @return
  */
 public static boolean isFlavorSupported(DataFlavor flavor, DataFlavor[] supportedFlavors) {
   for (int i = 0; i < supportedFlavors.length; i++) {
     if (supportedFlavors[i].getPrimaryType().equals(flavor.getPrimaryType())
         && supportedFlavors[i].getSubType().equals(flavor.getSubType())
         && flavor
             .getRepresentationClass()
             .isAssignableFrom(supportedFlavors[i].getRepresentationClass())) {
       return true;
     }
   }
   return false;
 }
예제 #5
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
   * @throws IOException if the data is no longer available in the requested flavor.
   * @throws UnsupportedFlavorException if the requested data flavor is not supported.
   * @see DataFlavor#getRepresentationClass
   */
  public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (isRicherFlavor(flavor)) {
      return getRicherData(flavor);
    } else if (isImageFlavor(flavor)) {
      if (image != null && image.getImage() instanceof RenderedImage) {
        if (flavor.equals(DataFlavor.imageFlavor)) {
          return image.getImage();
        } else {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          ImageIO.write((RenderedImage) image.getImage(), "bmp", stream);

          return new ByteArrayInputStream(stream.toByteArray());
        }
      }
    } else if (isHtmlFlavor(flavor)) {
      String data = getHtmlData();
      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 ByteArrayInputStream(data.getBytes());
      }
      // fall through to unsupported
    } else 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 ByteArrayInputStream(data.getBytes());
      }
      // fall through to unsupported

    } else if (isStringFlavor(flavor)) {
      String data = getPlainData();
      data = (data == null) ? "" : data;

      return data;
    }

    throw new UnsupportedFlavorException(flavor);
  }
예제 #6
0
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   return flavor.equals(DataFlavor.stringFlavor)
       || flavor.getPrimaryType().equals("application")
           && flavor.getSubType().equals("x-java-serialized-object")
           && flavor.getRepresentationClass().isAssignableFrom(obj.getClass());
 }
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   return (flavor.getRepresentationClass() == TreePath.class);
 }
예제 #8
0
  @SuppressWarnings("unchecked")
  public void drop(DropTargetDropEvent dtde) {
    Logger.debug("Drop detected...");
    Transferable t = dtde.getTransferable();
    boolean isAccepted = false;
    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      while (true) {
        Object o = null;
        try {
          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          o = t.getTransferData(DataFlavor.javaFileListFlavor);
          isAccepted = true;
        } catch (Exception e) {
          Logger.error("transfer failed");
        }
        // if o is still null we had an exception
        if (o instanceof List) {
          List<File> fileList = (List<File>) o;
          final int length = fileList.size();
          if (length == 1) {
            String fileName = fileList.get(0).getAbsolutePath().trim();
            if (fileName.endsWith(".bmp")) break; // try another flavor -- Mozilla bug
            dtde.getDropTargetContext().dropComplete(true);
            loadFile(fileName);
            return;
          }
          dtde.getDropTargetContext().dropComplete(true);
          loadFiles(fileList);
          return;
        }
        break;
      }
    }

    Logger.debug("browsing supported flavours to find something useful...");
    DataFlavor[] df = t.getTransferDataFlavors();

    if (df == null || df.length == 0) return;
    for (int i = 0; i < df.length; ++i) {
      DataFlavor flavor = df[i];
      Object o = null;
      if (true) {
        Logger.info("df " + i + " flavor " + flavor);
        Logger.info("  class: " + flavor.getRepresentationClass().getName());
        Logger.info("  mime : " + flavor.getMimeType());
      }

      if (flavor.getMimeType().startsWith("text/uri-list")
          && flavor.getRepresentationClass().getName().equals("java.lang.String")) {

        /*
         * This is one of the (many) flavors that KDE provides: df 2 flavour
         * java.awt.datatransfer.DataFlavor[mimetype=text/uri-list;
         * representationclass=java.lang.String] java.lang.String String: file
         * :/home/egonw/data/Projects/SourceForge/Jmol/Jmol-HEAD/samples/
         * cml/methanol2.cml
         *
         * A later KDE version gave me the following. Note the mime!! hence the
         * startsWith above
         *
         * df 3 flavor java.awt.datatransfer.DataFlavor[mimetype=text/uri-list
         * ;representationclass=java.lang.String] class: java.lang.String mime :
         * text/uri-list; class=java.lang.String; charset=Unicode
         */

        try {
          o = null;
          if (!isAccepted) dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          isAccepted = true;
          o = t.getTransferData(flavor);
        } catch (Exception e) {
          Logger.error(null, e);
        }

        if (o instanceof String) {
          if (Logger.debugging) {
            Logger.debug("  String: " + o.toString());
          }
          loadFile(o.toString());
          dtde.getDropTargetContext().dropComplete(true);
          return;
        }
      } else if (flavor
          .getMimeType()
          .equals("application/x-java-serialized-object; class=java.lang.String")) {

        /*
         * This is one of the flavors that jEdit provides:
         *
         * df 0 flavor java.awt.datatransfer.DataFlavor[mimetype=application/
         * x-java-serialized-object;representationclass=java.lang.String] class:
         * java.lang.String mime : application/x-java-serialized-object;
         * class=java.lang.String String: <molecule title="benzene.mol"
         * xmlns="http://www.xml-cml.org/schema/cml2/core"
         *
         * But KDE also provides:
         *
         * df 24 flavor java.awt.datatransfer.DataFlavor[mimetype=application
         * /x-java-serialized-object;representationclass=java.lang.String]
         * class: java.lang.String mime : application/x-java-serialized-object;
         * class=java.lang.String String: file:/home/egonw/Desktop/1PN8.pdb
         */

        try {
          o = null;
          if (!isAccepted) dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
          isAccepted = true;
          o = t.getTransferData(df[i]);
        } catch (Exception e) {
          Logger.error(null, e);
        }

        if (o instanceof String) {
          String content = (String) o;
          if (Logger.debugging) {
            Logger.debug("  String: " + content);
          }
          if (content.startsWith("file:/")) {
            loadFile(content);
          } else {
            PropertyChangeEvent pce =
                new PropertyChangeEvent(this, FD_PROPERTY_INLINE, fd_oldFileName, content);
            fd_propSupport.firePropertyChange(pce);
          }
          dtde.getDropTargetContext().dropComplete(true);
          return;
        }
      }
    }
    if (!isAccepted) dtde.rejectDrop();
  }