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

Dragging is not appropriate in AWT List

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86, sparc
    • OS:
      linux, solaris_2.5.1

      Description

      OPERATING SYSTEM(S):
      Linux

      FULL JDK VERSION(S):
      java version "1.6.0_03"
      Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode)

      DESCRIPTION:
      The testcase is designed to check if dragging in AWT list works as per the expected behavior, when we drag an AWT list item onto another item and drop it onto a text area.

      STEPS TO REPRODUCE:

      1. Compile and run the testcase below
      2. Click on "AWT List item1" in "AWT List", drag it on to "AWT
         List item2" and drop it in "AWT TextAreas".

      Observed Result:
      While dragging it will select "AWT List item 2 " and drop "AWT List
      item2 " in "AWT TextAreas". However, the behavior is not the same each time. Sometimes it drops "AWT List item 1". Hence inconsistent behavior. The problem is not seen on Windows.

      Expected Result:
      "AWT List item2" should not get selected and at the same time should not get dropped.


      TESTCASE:

      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.dnd.DnDConstants;
      import java.awt.dnd.DragSource;
      import java.awt.dnd.DropTarget;
      import java.awt.datatransfer.*;
      import java.awt.dnd.*;
      import java.util.*;
      import java.io.*;

      public class DragDropTest {
              public static void main(String []str)
              {
                      JFrame jf=new JFrame("Drag And Drop Test");
                      JPanel jp=new JPanel();
                      JPanel listPanel=new JPanel();
                      java.awt.List list1;
                      
                      TextArea ta1=new TextArea("TextArea:1: hello",4,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
                      TextArea ta2=new TextArea("TextArea:2 Hi",4,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
                      
                      listPanel.setBorder(BorderFactory.createTitledBorder("AWT List"));
      list1 = new java.awt.List(8, false);
      for (int i = 1; i <= 15; i++) {
      list1.add("AWT List item " + i);
      }
      new DropTarget(list1, new TextDropTargetListener(list1));
      DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(
      list1, DnDConstants.ACTION_COPY_OR_MOVE,
      new MyDragSourceListener(list1));
      //listPanel.setAlignmentX(100.0f);
      jp.add(list1);
                      

                      new DropTarget(ta1, new TextDropTargetListener(ta1));
                      new DropTarget(ta2, new TextDropTargetListener(ta2));
                      DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(
                                      ta1, DnDConstants.ACTION_COPY_OR_MOVE,
                                      new MyDragSourceListener(ta1));
                      DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(
                                      ta2, DnDConstants.ACTION_COPY_OR_MOVE,
                                      new MyDragSourceListener(ta2));

                      jf.add(jp);
                      jp.add(ta1);
                      jp.add(ta2);
                      //jf.add(listPanel);
                      jf.getContentPane().add(jp);
                      jf.setSize(200,300);
                      jf.setVisible(true);
              }

      }

      class MyDragSourceListener implements DragSourceListener,
      DragGestureListener {
      private JList fileList;
      private DefaultListModel model;
      private Object[] draggedValues;
      private Component component;
      private TextArea textArea;
      private TextField textField;
      private java.awt.List list;
      private int selectionStart;
      private int selectionEnd;
      private int selectedIndex;
      private String selectedText;

      public MyDragSourceListener(Component c) {
      component = c;
      if (component.getClass().toString().equals("class java.awt.TextArea")) {
              textArea = (TextArea) c;
      }
      if (component.getClass().toString().equals("class java.awt.TextField")) {
              textField = (TextField) c;
      }
      if (component.getClass().toString().equals("class java.awt.List")) {
      list = (java.awt.List) c;
      }


      }

      // DragGestureListener method
      public void dragGestureRecognized(DragGestureEvent event) {
      if (event.getComponent().getClass().toString().equals(
                      "class java.awt.TextArea")) {
              selectionStart = textArea.getSelectionStart();
              selectionEnd = textArea.getSelectionEnd();
              // make certain that some text has been highlighted
              if (selectionStart != selectionEnd) {
                      String draggedValues = textArea.getSelectedText();
                      Transferable transferable = new TextTransferable(draggedValues);
                      event.startDrag(null, transferable, this);
              }
      }
      if (event.getComponent().getClass().toString().equals(
                      "class java.awt.TextField")) {
              selectionStart = textField.getSelectionStart();
              selectionEnd = textField.getSelectionEnd();

       selectedText = textField.getSelectedText();
              // make sure some text is highlighted
              if (selectionStart != selectionEnd) {
                      String draggedValues = selectedText;
                      Transferable transferable = new TextTransferable(draggedValues);
                      event.startDrag(null, transferable, this);
              }
      }

      if (event.getComponent().getClass().toString().equals(
      "class java.awt.List")) {
      selectedIndex = list.getSelectedIndex();
      String draggedValues = list.getSelectedItem();
      Transferable transferable = new TextTransferable(draggedValues);
      event.startDrag(null, transferable, this);
      }

      }

      // DragSourceListener methods

      public void dragEnter(DragSourceDragEvent event) {
      }

      public void dragOver(DragSourceDragEvent event) {
      }

      public void dragExit(DragSourceEvent event) {
      }

      public void dropActionChanged(DragSourceDragEvent event) {
      }

      public void dragDropEnd(DragSourceDropEvent event) {
      if (event.getDropSuccess()) {
              int action = event.getDropAction();

              if (action == DnDConstants.ACTION_MOVE) {
                      if (component.getClass().toString().equals(
                                      "class java.awt.TextArea")) {
                              textArea.replaceRange("", selectionStart, selectionEnd);
                      }
                      if (component.getClass().toString().equals(
                                      "class java.awt.TextField")) {
                              String temp = "";

                              System.out.println(selectionStart + " " + selectionEnd
                                              + " " + selectedText.length());
                              temp = textField.getText().substring(0, selectionStart)
                                              + textField.getText().substring(selectionEnd,
                                                              textField.getText().length());
                              textField.setText(temp);
                      }
                      if (component.getClass().toString().equals(
      "class java.awt.List")) {
      list.remove(selectedIndex);
      }
                      

      }
      }
      }
      }

      class TextTransferable implements Transferable {
              private static DataFlavor[] flavors = { DataFlavor.stringFlavor };// ,

              // DataFlavor.plainTextFlavor}

              private String text;

              public TextTransferable(String text) {
                      this.text = text;
              }

              public DataFlavor[] getTransferDataFlavors() {
                      return flavors;
              }

              public boolean isDataFlavorSupported(DataFlavor flavor) {
                      // determines if the flavor is contained wthin the flavors array
                      return Arrays.asList(flavors).contains(flavor);
              }

              public synchronized Object getTransferData(DataFlavor flavor)
                              throws UnsupportedFlavorException {
                      if (flavor.equals(DataFlavor.stringFlavor)) {
                              return text;
                      } else {
                              throw new UnsupportedFlavorException(flavor);
                      }
              }
      }

      class TextDropTargetListener implements DropTargetListener {
              private TextArea textArea;

              private TextField textField;

              private java.awt.List list;

              private Component component;

              public TextDropTargetListener(Component c) {
                      component = c;
                      if (c.getClass().toString().equals("class java.awt.TextArea")) {

       textArea = (TextArea) c;
                      }
                      if (c.getClass().toString().equals("class java.awt.TextField")) {
                              textField = (TextField) c;
                      }
                      
                      if (c.getClass().toString().equals("class java.awt.List")) {
      list = (java.awt.List) c;
      }

              }

              public void dragEnter(DropTargetDragEvent event) {

                      int a = event.getDropAction();
                      if (!isDragAcceptable(event)) {
                              event.rejectDrag();
                              return;
                      }

              }

              public void dragExit(DropTargetEvent event) {
              }

              public void dragOver(DropTargetDragEvent event) { // you can provide
                      // visual feedback here
                      // /////////////////////////////////////////////////////////////////////

                      Transferable transferable = event.getTransferable();

                      DataFlavor[] flavors = transferable.getTransferDataFlavors();

                      if (isDataFlavorSupported(flavors)) {
                              DataFlavor d = determineFlavor(flavors);
                              try {
                                      if (d.equals(DataFlavor.javaFileListFlavor)) {
                                              event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                                              // System.out.println("Accepted");

                                      } else if (d.equals(DataFlavor.stringFlavor)) {
                                              event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                                              // System.out.println("Accepted");
                                      } else if (d.isMimeTypeEqual("text/plain")) {
                                              event.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
                                              // System.out.println("Accepted");
                                      } else {
                                              event.rejectDrag();
                                              // System.out.println("Rejected");
                                              return;

                                      }
                              } catch (Exception e) {
                                      writeText("Error: " + e + "\n");
                              }
                      } else {
                              event.rejectDrag();
                              // System.out.println("Rejected");
                              return;
                      }

                      // ///////////////////////////////////////////////////////////////

              }

              public void dropActionChanged(DropTargetDragEvent event) {
                      if (!isDragAcceptable(event)) {
                              event.rejectDrag();
                              return;
                      }
              }

              public void drop(DropTargetDropEvent event) {

                      if (!isDropAcceptable(event)) {
                              event.rejectDrop();
                              return;
                      }

      else {
      event.acceptDrop(java.awt.dnd.DnDConstants.ACTION_COPY);
      }
                      // determine if action is copy or move
                      Transferable transferable = event.getTransferable();



                      if (true) {

                              DataFlavor[] flavors = transferable.getTransferDataFlavors();
                              // for (int i = 0; i < flavors.length; i++) {
                              if (isDataFlavorSupported(flavors)) {
                                      DataFlavor d = determineFlavor(flavors);
                                      try {
                                              if (d.equals(DataFlavor.javaFileListFlavor)) {
                                                      java.util.List fileList = (java.util.List) transferable
                                                                      .getTransferData(d);
                                                      Iterator iterator = fileList.iterator();
                                                      while (iterator.hasNext()) {
                                                              File f = (File) iterator.next();
       writeText(f + "\n");
                                                      }
                                              } else if (d.equals(DataFlavor.stringFlavor)) {
                                                      String s = (String) transferable.getTransferData(d);
                                                      writeText(s + "\n");
                                              } else if (d.isMimeTypeEqual("text/plain")) {
                                                      String charset = d.getParameter("charset");
                                                      InputStream in = (InputStream) transferable
                                                                      .getTransferData(d);

                                                      boolean more = true;
                                                      int ch;
                                                      if (charset.equals("ascii")) {
                                                              do {
                                                                      ch = in.read();
                                                                      if (ch != 0 && ch != -1)
                                                                              writeText("" + (char) ch);
                                                                      else
                                                                              more = false;
                                                              } while (more);
                                                      } else if (charset.equals("unicode")) {
                                                              boolean littleEndian = true;
                                                              // if no byte ordering mark, we assume
                                                              // Windows is the culprit
                                                              do {
                                                                      ch = in.read();
                                                                      int ch2 = in.read();
                                                                      if (ch != -1 && littleEndian)
                                                                              ch = (ch & 0xFF) | ((ch2 & 0xFF) << 8);
                                                                      if (ch == 0xFFFE)
                                                                              littleEndian = false;
                                                                      else if (ch != 0 && ch != -1)
                                                                              writeText("" + (char) ch);
                                                                      else
                                                                              more = false;
                                                              } while (more);
                                                      }
                                                      writeText("\n");
                                              }
                                      } catch (Exception e) {
                                              writeText("Error: " + e + "\n");
                                      }
                              }
                              event.dropComplete(true);
                      }
              }
       public boolean isDragAcceptable(DropTargetDragEvent event) { // usually,
                      // you check the available data flavors here
                      // in this program, we accept all flavors
                      return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
              }

              public boolean isDropAcceptable(DropTargetDropEvent event) { // usually,
                      // you check the available data flavors here
                      // in this program, we accept all flavors
                      return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
              }

              public DataFlavor determineFlavor(DataFlavor[] flavors) {
                      for (int i = 0; i < flavors.length; i++) {
                              if (flavors[i].equals(DataFlavor.javaFileListFlavor)) {

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              elarsen Erik Larsen (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: