Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4119256

Win95: data doesn't transferred properly while dnd from native to java vm

    XMLWordPrintable

Details

    • 1.2fcs
    • generic
    • solaris_2.5, windows_95
    • Verified

    Description

      On Win95, drag-n-drop a string of text from native (such as Wordpad) to java vm (a dnd test with TargetListener) doesn't work. The transferred object (string) is damaged to be empty. For the same test, dnd within/between java vm is ok, dnd from native to java vm on Win NT is also fine. The problem is uncovered on jdk1.2 L build.

      The following test demonstrated the problem, the runtime output shows the transferred string is empty: len of string becomes 0.
       
      /*
       * This test is to demonstrate basic drag-n-drop funcationality
       * It drags-n-drops between an AWT Button and another AWT Button
       * It initiates dnd action by middle button - BUTTON2
       */


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

      import java.io.*;

      import java.util.mime.*;

      class MyDropButton extends Button implements DropTargetListener {
        MyDropButton(String text, Color bg, Color hg) {
          super(text);
          
          setBackground(back = bg);
          hlight = hg;
          setDropTarget(new DropTarget(this, this));
        }
        
        public void dragEnter(DropTargetDragEvent e) {
          System.err.println("[Target] dragEnter");
          
          e.acceptDrag(DnDConstants.ACTION_COPY);
          setBackground(hlight);
          repaint();
        }
        
        public void dragOver(DropTargetDragEvent e) {
          e.acceptDrag(DnDConstants.ACTION_COPY);
          System.err.println("[Target] dragOver");
          
        }
        
        public void dragExit(DropTargetEvent e) {
          System.err.println("[Target] dragExit");
          setBackground(back);
          repaint();
        }
        
        public void drop(DropTargetDropEvent dtde) {
          System.err.println("[Target] drop");
          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;
          
          System.err.println(DataFlavor.plainTextFlavor.getMimeType());
          for (int i = 0; i < dfs.length; i++) {
            System.err.println(dfs[i].getMimeType());
            if (DataFlavor.plainTextFlavor.equals(dfs[i])) {
      System.err.println("matched");
      tdf = dfs[i];
      break;
            }
          }
          
          if (tdf != null) {
            Transferable t = dtde.getTransferable();
            InputStream is = null;
            
            try {
      System.err.println("get stream");
      is = (InputStream)t.getTransferData(tdf);
            } catch (IOException ioe) {
      ioe.printStackTrace();
      System.err.println(ioe.getMessage());
      dtc.dropComplete(false);

      setBackground(back);
      repaint();
      return;
            } catch (UnsupportedFlavorException ufe) {
      ufe.printStackTrace();
      System.err.println(ufe.getMessage());
      dtc.dropComplete(false);

      setBackground(back);
      repaint();
      return;
            }
            
            if (is != null) {
      String s = getLabel();

      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();
      System.err.println(e.getMessage());
      dtc.dropComplete(false);

      setBackground(back);
      repaint();
      return;
      } finally {
      setLabel(s);
      }
            } else outcome = false;
          }
          
          setBackground(back);
          repaint();
          
          dtc.dropComplete(outcome);
        }

        public void dragScroll(DropTargetDragEvent e) {
        }
        
        public void dropActionChanged(DropTargetDragEvent e) {
          System.err.println("[Target] dropActionChanged");
        }
        
        Color back;
        Color hlight;
      }

      class MyDragButton extends Button implements Transferable, DragSourceListener {
        
        /**
         *
         */
        
        MyDragButton(String s) {
          super(s);
          
          addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
            }
            
            public void mousePressed(MouseEvent e) {
      if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
      DragSource ds = DragSource.getDefaultDragSource();


      System.err.println("starting drag");

      ds.startDrag(MyDragButton.this,
      e,
      DnDConstants.ACTION_COPY,
      (Cursor)DragSource.DefaultCopyNoDrop,
      (Image)null,
      (Point)null,
      (Transferable)MyDragButton.this,
      (DragSourceListener)MyDragButton.this
      );
      }
            }
            
            public void mouseReleased(MouseEvent e) {
            }
            
            public void mouseEntered(MouseEvent e) {
            }
            
            public void mouseExited(MouseEvent e) {
            }
          }
      );
          
          setBackground(Color.yellow);
          setForeground(Color.blue);
        }
        
          /**
           * as the hotspot enters a platform dependent drop site
           */

        public void dragEnter(DragSourceDragEvent dsde) {
          System.err.println("[Source] dragEnter");
          System.err.println(" actions = " + dsde.getTargetActions());
          
          dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
        }
        
        /**
         * as the hotspot moves over a platform dependent drop site
         */
        
        public void dragOver(DragSourceDragEvent dsde) {
          System.err.println("[Source] dragOver");
          System.err.println(" actions = " + dsde.getTargetActions());
        }
        
        /**
         * as the operation changes
         */
        
        public void dragGestureChanged(DragSourceDragEvent dsde) {
          System.err.println("[Source] dragChanged");
        }
        
        /**
         * as the hotspot exits a platform dependent drop site
         */
        
        public void dragExit(DragSourceEvent dse) {
          dse.getDragSourceContext().setCursor(null);
          System.err.println("[Source] dragExit");
        }
        
        /**
         * as the operation completes
           */
        
        public void dragDropEnd(DragSourceDropEvent dsde) {
          System.err.println("[Source] dragDropEnd");
        }
        
        public void dropActionChanged(DragSourceDragEvent e) {
          System.err.println("[Source] dropActionChanged");
        }
        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 = getLabel();
          
          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);
            System.err.println("[Source] buffer = " + sbis.available());
            
            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 TestButton extends Frame {
        Panel p;
        
        public TestButton() {
          super();
        }
        
        public void init() {
          Component c1, c2;
          
          setLayout(new BorderLayout());
          
          p = new Panel();
          p.setLayout(new GridBagLayout());
          
          
          p.setBackground(Color.white);
          
          add(p, BorderLayout.CENTER);
          
          c1 = new MyDropButton("Drop on me!", Color.red, Color.yellow);
          
          c2 = new MyDragButton("Drag ME!");
          
          GridBagConstraints gbc1 = new GridBagConstraints();
          
          gbc1.anchor = GridBagConstraints.SOUTHWEST;
          gbc1.fill = GridBagConstraints.BOTH;
          gbc1.insets = new Insets(2,2,2,2);
          
          p.add(c1, gbc1);
          
          GridBagConstraints gbc2 = new GridBagConstraints();
          
          gbc2.anchor = GridBagConstraints.SOUTHWEST;
          gbc2.fill = GridBagConstraints.BOTH;
          gbc2.insets = new Insets(2,2,2,2);
          
          p.add(c2, gbc2);
          p.show();
          
          pack();
          setSize(getPreferredSize());
          show();
          
          c1.show();
          c2.show();
        }


        public static void main(String[] args) {
          TestButton bt = new TestButton();
          
          bt.init();
        }
      }

      Attachments

        Issue Links

          Activity

            People

              lcablesunw Larry Cable (Inactive)
              tsusunw Tina Su (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: