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

setClipboardContents doesnt work if source doesnt extend

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.3
    • client-libs
    • x86
    • windows_nt



      Name: diC59631 Date: 09/26/97


      When using setContents to set the clipboard contents, nothing
      happens unless the Transferable object is either a StringSelection
      or extends StringSelection. In the example code the class
      YourStringSelection is an exact copy of the StringSelection class,
      unyet nothing gets stored on the clipboard. The same program when
      run under Solaris works as expected.

      /*
       * JDK 1.1 Clipboard Bug Test
       *
       */
       import java.awt.*;
       import java.applet.*;
       import java.awt.event.*;
       import java.io.*;
       import java.awt.datatransfer.*;

       /**
       JDK 1.1 AWT Testing Lab
       */
       public class ClipboardBug extends Applet
       implements WindowListener, ActionListener, ClipboardOwner
       {
       public static void main(String argv[])
       {
       Frame frame = new Frame("JDK 1.1 Clipboard Test");
       ClipboardBug c = new ClipboardBug();

       // Display the Description
       for(int i=0; i<desc.length; i++) System.out.println(desc[i]);

       // Init as a main program
       c.init(frame);
       }

       private static final String desc[] =
       {
       "java ClipboardBug",
       "",
       "Each time you press the \"Copy\" button, a new StringSelection is",
       "put on the system clipboard, and the time to perform the setContents",
       "method is displayed on the console.",
       };

       // Initialization when run as an applet
       public void init()
       {
       init(null);
       }

       // Common Initialization - applet or com.sssw.rt.application
       void init(Frame frame)
       {
       if(null != frame)
       {
       frame.setLayout(new BorderLayout());
       frame.add(this, BorderLayout.CENTER);
       frame.setSize(400, 400);
       frame.addWindowListener(this);
       }
       setLayout(null);
       Button button = new Button("Copy");
       button.setBounds(10, 10, 100, 24);
       button.addActionListener(this);
       add(button);

       if(null != frame)
       {
       frame.show();
       }
       enableEvents(AWTEvent.MOUSE_EVENT_MASK |
       AWTEvent.MOUSE_MOTION_EVENT_MASK);
       }

       // Action Listener
       private Transferable m_lastcontent;

       public void actionPerformed(ActionEvent e)
       {
       long start = System.currentTimeMillis();

       // copy something to the clipboard
       //m_lastcontent = new StringSelection("Hi"); // Works
       //m_lastcontent = new MyStringSelection("Hi"); // Works
       m_lastcontent = new YourStringSelection("Hi"); // Doesn't work!!

       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(m_lastcontent,

       this);
       long end = System.currentTimeMillis();

       System.out.println("Copy took "+(end-start)+" milliseconds, data:
      "+m_lastcontent);
       }

       public void lostOwnership(Clipboard clipboard, Transferable contents)
       {
       System.out.println("Lost Ownership. Clipboard: "+clipboard);
       System.out.println(" Data: "+contents);
       }

       public void paint(Graphics g)
       {
       g.setColor(Color.red);
       Rectangle b = getBounds();
       g.fillRect(0, 0, b.width, b.height);
       super.paint(g);
       }

       // Window Listener Methods --- So we can close when the frame's close-box is
      clicked
       public void windowOpened(WindowEvent e) {}
       public void windowClosing(WindowEvent e)
       {
       System.exit(0);
       }
       public void windowClosed(WindowEvent e) {}
       public void windowIconified(WindowEvent e) {}
       public void windowDeiconified(WindowEvent e) {}
       public void windowActivated(WindowEvent e) {}
       public void windowDeactivated(WindowEvent e) {}
      }
      class MyStringSelection extends StringSelection
      {
              MyStringSelection(String s)
              {
                      super(s);
              }
      }
      class YourStringSelection implements Transferable, ClipboardOwner {

          final static int STRING = 0;
          final static int PLAIN_TEXT = 1;

          DataFlavor flavors[] = {DataFlavor.stringFlavor,
      DataFlavor.plainTextFlavor};

          private String data;
                                                         
          /**
           * Creates a transferable object capable of transferring the
           * specified string in plain text format.
           */
          public YourStringSelection(String data) {
              this.data = data;
          }

          /**
           * Returns the array of flavors in which it can provide the data.
           */
          public synchronized DataFlavor[] getTransferDataFlavors() {
              return flavors;
          }

          /**
           * Returns whether the requested flavor is supported by this object.
           * @param flavor the requested flavor for the data
           */
          public boolean isDataFlavorSupported(DataFlavor flavor) {
              return (flavor.equals(flavors[STRING]) ||
      flavor.equals(flavors[PLAIN_TEXT]));
          }

          /**
           * If the data was requested in the "java.lang.String" flavor, return the
           * String representing the selection.
           *
           * @param flavor the requested flavor for the data
           * @exception UnsupportedFlavorException if the requested data flavor is
           * not supported in the "<code>java.lang.String</code>" flavor.
           */
          public synchronized Object getTransferData(DataFlavor flavor)
                              throws UnsupportedFlavorException, IOException {
              if (flavor.equals(flavors[STRING])) {
                  return (Object)data;
              } else if (flavor.equals(flavors[PLAIN_TEXT])) {
                  return new StringReader(data);
              } else {
                  throw new UnsupportedFlavorException(flavor);
              }
          }

          public void lostOwnership(Clipboard clipboard, Transferable contents) {
          }
      }
      company - Stanford Linear Accelerator Center , email -
      ###@###.###
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: