JDK-4294394 : Netscape URL Link can not be drag and dropped into a Java app
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-11-24
  • Updated: 2000-08-03
  • Resolved: 2000-08-03
Related Reports
Duplicate :  
Description

Name: rlT66838			Date: 11/24/99


java -version
java version "1.3beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)



When running the following code, the line
   is = (InputStream)t.getTransferData(tdf); will cause the following exception:

java.io.IOException: class
java.awt.datatransfer.UnsupportedFlavorException:Plain Text caught while getting
Data
	at
java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source)
	at DropText.drop(TestText.java:58)
	at java.awt.dnd.DropTarget.drop(Unknown Source)
	at sun.awt.windows.WDropTargetContextPeer.processDropMessage(Unknown
Source)
	at sun.awt.windows.WDropTargetContextPeer.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


Here is the source code, which is take from Question Of The Week with a slight
modificatio to show this problem:

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;

import java.io.*;

class DropText extends TextField implements
                DropTargetListener {
  DropText(String text){
    super(text);
    setDropTarget(new DropTarget(this, this));
  }
  
  public void dragEnter(DropTargetDragEvent e) {
    e.acceptDrag(DnDConstants.ACTION_COPY);
  }
  
  public void dragOver(DropTargetDragEvent e) {
    e.acceptDrag(DnDConstants.ACTION_COPY);
  }
  
  public void dragExit(DropTargetEvent e) {
    repaint();
  }
  
  public void drop(DropTargetDropEvent dtde) {
    DropTargetContext dtc =
                dtde.getDropTargetContext();
    
    boolean outcome = false;
    if ((dtde.getSourceActions()
                & DnDConstants.ACTION_COPY) != 0)
      dtde.acceptDrop(DnDConstants.ACTION_COPY);
    else {
      dtde.rejectDrop();
      return;
    }
    
    DataFlavor[] dfs = dtde.getCurrentDataFlavors();
    DataFlavor   tdf = null;
    
    for (int i = 0; i < dfs.length; i++) {
      if (DataFlavor.plainTextFlavor.equals(dfs[i])) {
          if ( false ) // This works
              tdf = dfs[i];
          else // This should be equivalent but does not work
              tdf = DataFlavor.plainTextFlavor;
        break;
      }
    }
    
    if (tdf != null) {
      Transferable t  = dtde.getTransferable();
      InputStream  is = null;
      
      try {
        is = (InputStream)t.getTransferData(tdf);
      } catch (IOException ioe) {
        ioe.printStackTrace();
        dtc.dropComplete(false);

        return;
      } catch (UnsupportedFlavorException ufe) {
        ufe.printStackTrace();
        dtc.dropComplete(false);

        repaint();
        return;
      }
      
      if (is != null) {
        String s = getText();

        try {
          int len = is.available();
          
          System.err.println("len = " + len);
          
          byte[] string = new byte[len];
          
          is.read(string, 0, len);
          
          for (int i = 0; i < len; i++)
            if (string[i] == 0) {
              len = i;
              break;
            }
          
          s = new String(string, 0, len);
          
          outcome = true;
        } catch (Exception e) {
          e.printStackTrace();
          dtc.dropComplete(false);
          
          repaint();
          return;
        } finally {
          setText(s);
        }
      } else outcome = false;
    }
    
    repaint();
    
    dtc.dropComplete(outcome);
  }

  public void dragScroll(DropTargetDragEvent e) {
  }
  
  public void
        dropActionChanged(DropTargetDragEvent e) {
  }
  
}

class DragText extends TextField implements
        Transferable, DragGestureListener,
                 DragSourceListener {
  
 DragText(String s) {
    super(s);
    DragSource ds = DragSource.getDefaultDragSource();
    ds.createDefaultDragGestureRecognizer(this,
                        DnDConstants.ACTION_COPY, this);
  }

    public void dragGestureRecognized(
                                DragGestureEvent dge) {
        dge.startDrag(null, this, null);
    }

  public void dragEnter(DragSourceDragEvent dsde) {
    dsde.getDragSourceContext().setCursor(
                        DragSource.DefaultCopyDrop);
  }
  
  public void dragOver(DragSourceDragEvent dsde) {
  }
  
  public void dragGestureChanged(
                        DragSourceDragEvent dsde) {
  }
  
  public void dragExit(DragSourceEvent dse) {
    dse.getDragSourceContext().setCursor(null);
  }
  
  public void dragDropEnd(DragSourceDropEvent dsde) {
  }
  
  public void dropActionChanged(DragSourceDragEvent e) {
  }
  public DataFlavor[] getTransferDataFlavors() {
    return dfs;
  }

  public boolean isDataFlavorSupported(DataFlavor sdf) {
    for (int i = 0 ; i < dfs.length; i++)
      if (dfs[i].equals(sdf)) return true;
    return false;
  }
  
  public Object getTransferData(DataFlavor tdf) throws
                UnsupportedFlavorException ,
                                IOException {
    if (!isDataFlavorSupported(tdf))
                throw new UnsupportedFlavorException(tdf);
    
    String text = getText();
    
    if (DataFlavor.stringFlavor.equals(tdf)) {
      ByteArrayOutputStream baos =
                        new ByteArrayOutputStream();
      ObjectOutputStream oos =
                        new ObjectOutputStream(baos);
      
      try {
        oos.writeObject(text);
      } catch (Exception e) {
        throw new IOException();
      }
      
      return new ObjectInputStream(
        new ByteArrayInputStream(baos.toByteArray()));
    } else {
      StringBufferInputStream sbis =
                        new StringBufferInputStream(text);
      return sbis;
    }
  }
  
  private transient int dropAction;
  
  private static DataFlavor create() {
    try {
      return new DataFlavor(
                "text/plain; charset=iso8859-1", "String");
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  private static    DataFlavor dfs[] = new DataFlavor[] {
    create(),
      DataFlavor.plainTextFlavor,
      DataFlavor.stringFlavor
      };
}

public class TestText extends Frame {
  Panel  p;
  
  public TestText() {
    super();
  }
  
  public void init() {
    Component c1, c2;
    
    p = new Panel();
    p.setBackground(Color.white);
    
    c1 = new DropText("Drop this");
    c2 = new DragText("Drag this");
    
    p.add(c1);
        p.add(new Button("test"));
    p.add(c2);
    p.show();
    c1.show();
    c2.show();
        add(p);
    
    pack();
    show();
  }


  public static void main(String[] args) {
    TestText tt = new TestText();
    
    tt.init();
  }
}
(Review ID: 98247) 
======================================================================

Comments
WORK AROUND Name: rlT66838 Date: 11/24/99 If instead of using tdf = DataFlavor.plainTextFlavor, we use tdf = dfs[i]; Conceptually there should be no difference wether we use DataFlavor.plainTextFlavor or dfs[i] because DataFlavor.plainTextFlavor.equals(dfs[i]) then it works. ======================================================================
11-06-2004