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

Drag & Drop does not work when Java Bean is packaged in an ActiveX Control

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • deploy
    • x86
    • windows_nt



      Name: boT120536 Date: 01/23/2001


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

      I have implemented a panel that contains two buttons. The buttons inside the
      panel supports drag and drop by using drag and drop API functions. Therefore
      drag and drop between the buttons is possible. If I use this panel in a java
      application drag and drop works fine. But I want to use this panel in an
      ActiveX control by using the JavaBean ActiveX Bridge Packager. I put the
      created ActiveX control that contains the panel in an MFC Windows Application.
      Drag and drop does not work!

      Result:

      No drag and drop inside ActiveX controls that consists of Java Beans!

      Maybe a problem of JDK drag and drop API ?

      SOURCES:

      package com.bosch.de.uc.apps.java_activex;

      import java.awt.*;
      import javax.swing.JButton;
      import java.awt.dnd.*;
      import java.awt.datatransfer.*;
      import java.io.*;
      import java.util.List;
      import java.util.*;
      import java.awt.event.*;
      import javax.swing.JOptionPane;


      public class DnDButtonBean extends JButton implements DropTargetListener,
                                                            DragGestureListener
      {
        private transient DropTarget dropTarget;
        private transient DragSource dragSource = DragSource.getDefaultDragSource();
        private transient final static DragSourceListener dragSourceListener = new
      MyDragSourceListener();
        private transient DragGestureRecognizer dragGestureRecognizer;

        static class MyDragSourceListener implements DragSourceListener {
          public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
            System.out.println ("Drag Drop End");
          }
          public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
            System.out.println ("Drag Enter");
          }
          public void dragExit(DragSourceEvent DragSourceEvent) {
            System.out.println ("Drag Exit");
          }
          public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
            System.out.println ("Drag Over");
          }
          public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
            System.out.println ("Drag Action Changed");
          }
        }


        public DnDButtonBean()
        {
          try
          {
            jbInit();
          }
          catch(Exception ex)
          {
            ex.printStackTrace();
          }
        }

        private void jbInit() throws Exception
        {

          this.dragGestureRecognizer = dragSource.createDefaultDragGestureRecognizer
      (this, DnDConstants.ACTION_COPY_OR_MOVE, this);
          dropTarget = new DropTarget (this, this);
          this.setBackground(Color.orange);
          this.setEnabled(true);
          this.setText("Drag & Drop Java Button Bean");
        }

        public void dragGestureRecognized(DragGestureEvent dragGestureEvent)
        {
           StringTransferable trans = new StringTransferable(this.getText() + " >");
           dragSource.startDrag(dragGestureEvent, DragSource.DefaultCopyDrop, trans,
      dragSourceListener);
           Toolkit.getDefaultToolkit().beep();
        }

        public void higlight()
        {
          this.setBackground(Color.yellow);
        }

        public void dragEnter (DropTargetDragEvent dropTargetDragEvent)
        {
          dropTargetDragEvent.acceptDrag (DnDConstants.ACTION_COPY_OR_MOVE);
        }

        public void dragExit (DropTargetEvent dropTargetEvent)
        {
        }

        public void dragOver (DropTargetDragEvent dropTargetDragEvent)
        {
        }

        public synchronized void drop (DropTargetDropEvent dropTargetDropEvent)
        {
          try
          {
           Transferable tr = dropTargetDropEvent.getTransferable();
            if (tr.isDataFlavorSupported (DataFlavor.stringFlavor))
            {
              dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
              String string = (String)tr.getTransferData (DataFlavor.stringFlavor);
              this.setText(string);
              dropTargetDropEvent.getDropTargetContext().dropComplete(true);
            } else if (tr.isDataFlavorSupported (DataFlavor.plainTextFlavor)) {
              dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
              Object stream = tr.getTransferData(DataFlavor.plainTextFlavor);
              if (stream instanceof InputStream) {
                InputStreamReader isr = new InputStreamReader((InputStream)stream);
                BufferedReader reader = new BufferedReader(isr);
                String line;
                while ((line = reader.readLine()) != null) {
                  this.setText(line);
                }
                dropTargetDropEvent.getDropTargetContext().dropComplete(true);
              } else if (stream instanceof Reader) {
                BufferedReader reader = new BufferedReader((Reader)stream);
                String line;
                while ((line = reader.readLine()) != null) {
                  this.setText(line);
                }
                dropTargetDropEvent.getDropTargetContext().dropComplete(true);
              } else {
                System.err.println ("Unknown type: " + stream.getClass());
                dropTargetDropEvent.rejectDrop();
              }
            } else if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor)) {
              dropTargetDropEvent.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE);
              List fileList = (List)tr.getTransferData(DataFlavor.javaFileListFlavor);
              Iterator iterator = fileList.iterator();
              while (iterator.hasNext()) {
                File file = (File)iterator.next();
                Hashtable hashtable = new Hashtable();
                hashtable.put("name", file.getName());
                hashtable.put("url", file.toURL().toString());
                this.setText(hashtable.toString());
              }
              dropTargetDropEvent.getDropTargetContext().dropComplete(true);
            } else {
              System.err.println ("Rejected");
              dropTargetDropEvent.rejectDrop();
            }
          } catch (IOException io) {
            io.printStackTrace();
            dropTargetDropEvent.rejectDrop();
          } catch (UnsupportedFlavorException ufe) {
            ufe.printStackTrace();
            dropTargetDropEvent.rejectDrop();
          }
        }
        public void dropActionChanged (DropTargetDragEvent dropTargetDragEvent)
        {
        }
      }
      ---------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;


      import java.beans.*;

      public class DnDButtonBeanBeanInfo extends SimpleBeanInfo
      {
        Class beanClass = DnDButtonBean.class;
        String iconColor16x16Filename = "DnDButtonBean16.gif";
        String iconColor32x32Filename;
        String iconMono16x16Filename;
        String iconMono32x32Filename;

        public DnDButtonBeanBeanInfo()
        {
        }

        public PropertyDescriptor[] getPropertyDescriptors()
        {
          PropertyDescriptor[] pds = new PropertyDescriptor[] { };
          return pds;
        }

        public java.awt.Image getIcon(int iconKind)
        {
          switch (iconKind) {
      case BeanInfo.ICON_COLOR_16x16:
      return iconColor16x16Filename != null ? loadImage
      (iconColor16x16Filename) : null;
      case BeanInfo.ICON_COLOR_32x32:
      return iconColor32x32Filename != null ? loadImage
      (iconColor32x32Filename) : null;
      case BeanInfo.ICON_MONO_16x16:
      return iconMono16x16Filename != null ? loadImage
      (iconMono16x16Filename) : null;
      case BeanInfo.ICON_MONO_32x32:
      return iconMono32x32Filename != null ? loadImage
      (iconMono32x32Filename) : null;
      }
          return null;
        }
      }
       
      ---------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;
       
      import javax.swing.*;
      import java.awt.*;
      import java.awt.datatransfer.*;
      import java.io.*;
      import java.util.*;
      import java.util.List;


           
      /**
        * File: StringTransferable.java<br>
       */
           
      public class StringTransferable implements Transferable, ClipboardOwner,
      Serializable {

        // We don't really need these class variables since the array will contain
      them.
        // In a future article we will actually make our own flavors here.
        public static final DataFlavor plainTextFlavor = DataFlavor.plainTextFlavor;
        public static final DataFlavor localStringFlavor = DataFlavor.stringFlavor;
        
        public static final DataFlavor[] flavors = {
          StringTransferable.plainTextFlavor,
          StringTransferable.localStringFlavor
        };
        
        private static final List flavorList = Arrays.asList( flavors );
        private String string;

        /**
         * Constructor.
         * simply initializes instance variable
         */
        public StringTransferable(String string) {
          this.string = string;
        }
        
        private void dumpFlavor(DataFlavor flavor) {
          System.out.println( "getMimeType " +
            flavor.getMimeType() );
          System.out.println( "getHumanPresentableName " +
            flavor.getHumanPresentableName() );
          System.out.println( "getRepresentationClass " +
            flavor.getRepresentationClass().getName() );
          System.out.println( "isMimeTypeSerializedObject " +
            flavor.isMimeTypeSerializedObject() );
          System.out.println( "isRepresentationClassInputStream " +
            flavor.isRepresentationClassInputStream() );
          System.out.println( "isRepresentationClassSerializable " +
            flavor.isRepresentationClassSerializable() );
          System.out.println( "isRepresentationClassRemote " +
            flavor.isRepresentationClassRemote() );
          System.out.println( "isFlavorSerializedObjectType " +
            flavor.isFlavorSerializedObjectType() );
          System.out.println( "isFlavorRemoteObjectType " +
            flavor.isFlavorRemoteObjectType() );
          System.out.println( "isFlavorJavaFileListType " +
            flavor.isFlavorJavaFileListType() );
        }
           
        public synchronized DataFlavor[] getTransferDataFlavors() {
      // return (DataFlavor[]) flavorList.toArray();
          return flavors;
        }
           
        public boolean isDataFlavorSupported( DataFlavor flavor ) {
          return ( flavorList.contains( flavor ) );
        }
        
        public synchronized Object getTransferData(DataFlavor flavor)
          throws UnsupportedFlavorException, IOException {
          System.err.println( "getTransferData(): ");
          dumpFlavor(flavor);
          
          if (flavor.equals(StringTransferable.plainTextFlavor)) {
            return new ByteArrayInputStream(this.string.getBytes("Unicode"));
          } else if (StringTransferable.localStringFlavor.equals(flavor)) {
            return this.string;
          } else {
            throw new UnsupportedFlavorException (flavor);
          }
        }
        public String toString() {
          return "StringTransferable";
        }
        
        public void lostOwnership(Clipboard clipboard, Transferable contents) {
          System.out.println ("StringTransferable lost ownership of " +
            clipboard.getName());
          System.out.println ("data: " + contents);
        }
      }

      -----------------------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;

      import java.awt.*;
      import javax.swing.JPanel;

      public class ButtonBeanPanel extends JPanel {
        private transient DnDButtonBean dnDButtonBean1 = new DnDButtonBean();
        private transient DnDButtonBean dnDButtonBean2 = new DnDButtonBean();
        private transient BorderLayout borderLayout1 = new BorderLayout();

        public ButtonBeanPanel() {
          try {
            jbInit();
          }
          catch(Exception ex) {
            ex.printStackTrace();
          }
        }

        private void jbInit() throws Exception {
          this.setLayout(borderLayout1);
          dnDButtonBean2.setBackground(Color.pink);
          this.add(dnDButtonBean1, BorderLayout.NORTH);
          this.add(dnDButtonBean2, BorderLayout.SOUTH);
        }

      }
      -----------------------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;

      import java.beans.*;

      public class ButtonBeanPanelBeanInfo extends SimpleBeanInfo {
        Class beanClass = ButtonBeanPanel.class;
        String iconColor16x16Filename = "DnDButtonBean16.gif";
        String iconColor32x32Filename;
        String iconMono16x16Filename;
        String iconMono32x32Filename;

        public ButtonBeanPanelBeanInfo() {
        }

        public PropertyDescriptor[] getPropertyDescriptors() {
          PropertyDescriptor[] pds = new PropertyDescriptor[] { };
          return pds;
        }

        public java.awt.Image getIcon(int iconKind) {
          switch (iconKind) {
      case BeanInfo.ICON_COLOR_16x16:
      return iconColor16x16Filename != null ? loadImage
      (iconColor16x16Filename) : null;
      case BeanInfo.ICON_COLOR_32x32:
      return iconColor32x32Filename != null ? loadImage
      (iconColor32x32Filename) : null;
      case BeanInfo.ICON_MONO_16x16:
      return iconMono16x16Filename != null ? loadImage
      (iconMono16x16Filename) : null;
      case BeanInfo.ICON_MONO_32x32:
      return iconMono32x32Filename != null ? loadImage
      (iconMono32x32Filename) : null;
      }
          return null;
        }
      }
      -----------------------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;


      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class TestFrame extends JFrame
      {
        private BorderLayout borderLayout1 = new BorderLayout();
        private ButtonBeanPanel buttonBeanPanel1 = new ButtonBeanPanel();

        //Construct the frame
        public TestFrame()
        {
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          try
          {
            jbInit();
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        }

        //Component initialization
        private void jbInit() throws Exception
        {
          this.getContentPane().setLayout(borderLayout1);
          this.setSize(new Dimension(400, 300));
          this.setTitle("Java / ActiveX Testapplication");
          this.getContentPane().add(buttonBeanPanel1, BorderLayout.NORTH);
        }

        //Overridden so we can exit on System Close
        protected void processWindowEvent(WindowEvent e)
        {
          super.processWindowEvent(e);
          if(e.getID() == WindowEvent.WINDOW_CLOSING)
          {
            System.exit(0);
          }
        }
      }
      -------------------------------------------------------------------------
      package com.bosch.de.uc.apps.java_activex;

      import javax.swing.UIManager;

      public class TestApp
      {
        boolean packFrame = false;

        //Construct the application
        public TestApp()
        {
          TestFrame frame = new TestFrame();
          //Validate frames that have preset sizes
          //Pack frames that have useful preferred size info, e.g. from their layout
          if (packFrame)
            frame.pack();
          else
            frame.validate();
          frame.setVisible(true);
        }

        //Main method
        public static void main(String[] args)
        {
          try
          {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          }
          catch(Exception e)
          {
          }
          new TestApp();
        }
      }

      FURTHER INFORMATION:

      For creation of the C++ Application that uses the Java Bean ActiveX Control I
      have used Microsoft Visual C++ 6.0 (operating system is Windows NT 4.0 with
      service pack 3)
      (Review ID: 107202)
      ======================================================================

            jmelvin James Melvin (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: