jdk/test/javax/swing/JTree/DnD/LastNodeLowerHalfDrop.java
author martin
Wed, 02 Sep 2015 14:11:50 -0700
changeset 32427 c22b7e41adf3
parent 31657 6b5f36a9a3c0
child 39021 9f829318bfbd
permissions -rw-r--r--
8134984: Text files should end in exactly one newline Summary: automated fixup of newlines at end-of-file via the usual perl one-liner Reviewed-by: chegar, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31657
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     1
/*
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     4
 *
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     8
 *
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    13
 * accompanied this code).
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    14
 *
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    18
 *
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    21
 * questions.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    22
 */
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    23
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    24
/* @test
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    25
   @bug 8129830
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    26
   @summary JTree drag/drop on lower half of last child of container incorrect
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    27
   @author Semyon Sadetsky
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    28
  */
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    29
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    30
import java.awt.*;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    31
import java.awt.datatransfer.DataFlavor;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    32
import java.awt.datatransfer.Transferable;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    33
import java.awt.datatransfer.UnsupportedFlavorException;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    34
import java.awt.event.InputEvent;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    35
import java.util.ArrayList;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    36
import java.util.Enumeration;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    37
import java.util.List;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    38
import javax.swing.*;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    39
import javax.swing.tree.DefaultMutableTreeNode;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    40
import javax.swing.tree.DefaultTreeModel;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    41
import javax.swing.tree.TreeModel;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    42
import javax.swing.tree.TreeNode;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    43
import javax.swing.tree.TreePath;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    44
import javax.swing.tree.TreeSelectionModel;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    45
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    46
public class LastNodeLowerHalfDrop {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    47
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    48
    private static DefaultMutableTreeNode b1;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    49
    private static DefaultMutableTreeNode b2;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    50
    private static DefaultMutableTreeNode c;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    51
    private static JTree jTree;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    52
    private static DefaultMutableTreeNode a;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    53
    private static DefaultMutableTreeNode b;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    54
    private static DefaultMutableTreeNode a1;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    55
    private static Point dragPoint;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    56
    private static Point dropPoint;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    57
    private static JFrame f;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    58
    private static DefaultMutableTreeNode c1;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    59
    private static DefaultMutableTreeNode root;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    60
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    61
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    63
        SwingUtilities.invokeAndWait(new Runnable() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    64
            @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    65
            public void run() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    66
                f = new JFrame();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    67
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    68
                f.add(new LastNodeLowerHalfDrop().getContent());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    69
                f.setSize(400, 400);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    70
                f.setLocationRelativeTo(null);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    71
                f.setVisible(true);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    72
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    73
        });
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    74
        testCase(b2, a1, +0.4f);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    75
        if (!"b2".equals(jTree.getModel().
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    76
                getChild(a, a.getChildCount() - 1).toString())) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    77
            throw new RuntimeException("b1 was not inserted in the last position in a");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    78
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    79
        testCase(c1, c, -0.4f);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    80
        if (!"c1".equals(jTree.getModel().getChild(root, 2).toString())) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    81
            throw new RuntimeException("c1 was not inserted beetween c and b nodes");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    82
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    83
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    84
        SwingUtilities.invokeLater(new Runnable() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    85
            @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    86
            public void run() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    87
                f.dispose();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    88
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    89
        });
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    90
        System.out.printf("ok");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    91
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    92
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    93
    static void testCase(DefaultMutableTreeNode drag,
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    94
                         DefaultMutableTreeNode drop, float shift) throws Exception {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    95
        Robot robot = new Robot();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    96
        robot.waitForIdle();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    97
        SwingUtilities.invokeAndWait(new Runnable() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    98
            @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
    99
            public void run() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   100
                Rectangle rectDrag =
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   101
                        jTree.getPathBounds(new TreePath(drag.getPath()));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   102
                dragPoint = new Point((int)rectDrag.getCenterX(),
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   103
                        (int) rectDrag.getCenterY());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   104
                SwingUtilities.convertPointToScreen(dragPoint, jTree);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   105
                Rectangle rectDrop =
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   106
                        jTree.getPathBounds(new TreePath(drop.getPath()));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   107
                dropPoint = new Point(rectDrop.x + 5,
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   108
                        (int) (rectDrop.getCenterY() + shift * rectDrop.height));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   109
                SwingUtilities.convertPointToScreen(dropPoint, jTree);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   110
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   111
        });
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   112
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   113
        robot.mouseMove(dragPoint.x, dragPoint.y);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   114
        robot.mousePress(InputEvent.BUTTON1_MASK);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   115
        robot.delay(400);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   116
        robot.mouseMove(dropPoint.x, dropPoint.y);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   117
        robot.delay(400);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   118
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   119
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   120
        robot.waitForIdle();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   121
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   122
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   123
    private JScrollPane getContent() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   124
        jTree = new JTree(getTreeModel());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   125
        jTree.setRootVisible(false);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   126
        jTree.setDragEnabled(true);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   127
        jTree.setDropMode(DropMode.INSERT);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   128
        jTree.setTransferHandler(new TreeTransferHandler());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   129
        jTree.getSelectionModel().setSelectionMode(
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   130
                TreeSelectionModel.SINGLE_TREE_SELECTION);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   131
        expandTree(jTree);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   132
        return new JScrollPane(jTree);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   133
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   134
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   135
    protected static TreeModel getTreeModel() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   136
        root = new DefaultMutableTreeNode("Root");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   137
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   138
        a = new DefaultMutableTreeNode("A");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   139
        root.add(a);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   140
        a1 = new DefaultMutableTreeNode("a1");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   141
        a.add(a1);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   142
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   143
        b = new DefaultMutableTreeNode("B");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   144
        root.add(b);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   145
        b1 = new DefaultMutableTreeNode("b1");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   146
        b.add(b1);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   147
        b2 = new DefaultMutableTreeNode("b2");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   148
        b.add(b2);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   149
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   150
        c = new DefaultMutableTreeNode("C");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   151
        root.add(c);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   152
        c1 = new DefaultMutableTreeNode("c1");
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   153
        c.add(c1);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   154
        return new DefaultTreeModel(root);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   155
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   156
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   157
    private void expandTree(JTree tree) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   158
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel()
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   159
                .getRoot();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   160
        Enumeration e = root.breadthFirstEnumeration();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   161
        while (e.hasMoreElements()) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   162
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   163
            if (node.isLeaf()) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   164
                continue;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   165
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   166
            int row = tree.getRowForPath(new TreePath(node.getPath()));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   167
            tree.expandRow(row);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   168
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   169
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   170
}
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   171
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   172
class TreeTransferHandler extends TransferHandler {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   173
    DataFlavor nodesFlavor;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   174
    DataFlavor[] flavors = new DataFlavor[1];
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   175
    DefaultMutableTreeNode[] nodesToRemove;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   176
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   177
    public TreeTransferHandler() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   178
        try {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   179
            String mimeType = DataFlavor.javaJVMLocalObjectMimeType
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   180
                    + ";class=\""
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   181
                    + javax.swing.tree.DefaultMutableTreeNode[].class.getName()
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   182
                    + "\"";
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   183
            nodesFlavor = new DataFlavor(mimeType);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   184
            flavors[0] = nodesFlavor;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   185
        } catch (ClassNotFoundException e) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   186
            System.out.println("ClassNotFound: " + e.getMessage());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   187
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   188
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   189
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   190
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   191
    public boolean canImport(TransferHandler.TransferSupport support) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   192
        if (!support.isDrop()) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   193
            return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   194
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   195
        support.setShowDropLocation(true);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   196
        if (!support.isDataFlavorSupported(nodesFlavor)) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   197
            return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   198
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   199
        // Do not allow a drop on the drag source selections.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   200
        JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   201
        JTree tree = (JTree) support.getComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   202
        int dropRow = tree.getRowForPath(dl.getPath());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   203
        int[] selRows = tree.getSelectionRows();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   204
        for (int i = 0; i < selRows.length; i++) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   205
            if (selRows[i] == dropRow) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   206
                return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   207
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   208
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   209
        // Do not allow MOVE-action drops if a non-leaf node is
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   210
        // selected unless all of its children are also selected.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   211
        int action = support.getDropAction();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   212
        if (action == MOVE) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   213
            return haveCompleteNode(tree);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   214
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   215
        // Do not allow a non-leaf node to be copied to a level
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   216
        // which is less than its source level.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   217
        TreePath dest = dl.getPath();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   218
        DefaultMutableTreeNode target = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   219
                dest.getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   220
        TreePath path = tree.getPathForRow(selRows[0]);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   221
        DefaultMutableTreeNode firstNode = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   222
                path.getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   223
        if (firstNode.getChildCount() > 0
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   224
                && target.getLevel() < firstNode.getLevel()) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   225
            return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   226
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   227
        return true;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   228
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   229
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   230
    private boolean haveCompleteNode(JTree tree) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   231
        int[] selRows = tree.getSelectionRows();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   232
        TreePath path = tree.getPathForRow(selRows[0]);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   233
        DefaultMutableTreeNode first = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   234
                path.getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   235
        int childCount = first.getChildCount();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   236
        // first has children and no children are selected.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   237
        if (childCount > 0 && selRows.length == 1) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   238
            return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   239
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   240
        // first may have children.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   241
        for (int i = 1; i < selRows.length; i++) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   242
            path = tree.getPathForRow(selRows[i]);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   243
            DefaultMutableTreeNode next = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   244
                    path.getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   245
            if (first.isNodeChild(next)) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   246
                // Found a child of first.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   247
                if (childCount > selRows.length - 1) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   248
                    // Not all children of first are selected.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   249
                    return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   250
                }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   251
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   252
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   253
        return true;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   254
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   255
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   256
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   257
    protected Transferable createTransferable(JComponent c) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   258
        JTree tree = (JTree) c;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   259
        TreePath[] paths = tree.getSelectionPaths();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   260
        if (paths != null) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   261
            // Make up a node array of copies for transfer and
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   262
            // another for/of the nodes that will be removed in
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   263
            // exportDone after a successful drop.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   264
            List<DefaultMutableTreeNode> copies = new ArrayList<>();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   265
            List<DefaultMutableTreeNode> toRemove = new ArrayList<>();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   266
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   267
                    paths[0].getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   268
            DefaultMutableTreeNode copy = copy(node);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   269
            copies.add(copy);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   270
            toRemove.add(node);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   271
            for (int i = 1; i < paths.length; i++) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   272
                DefaultMutableTreeNode next = (DefaultMutableTreeNode) paths[i]
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   273
                        .getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   274
                // Do not allow higher level nodes to be added to list.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   275
                if (next.getLevel() < node.getLevel()) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   276
                    break;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   277
                } else if (next.getLevel() > node.getLevel()) {  // child node
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   278
                    copy.add(copy(next));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   279
                    // node already contains child
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   280
                } else {                                        // sibling
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   281
                    copies.add(copy(next));
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   282
                    toRemove.add(next);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   283
                }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   284
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   285
            DefaultMutableTreeNode[] nodes = copies
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   286
                    .toArray(new DefaultMutableTreeNode[copies.size()]);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   287
            nodesToRemove = toRemove.toArray(
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   288
                    new DefaultMutableTreeNode[toRemove.size()]);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   289
            return new NodesTransferable(nodes);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   290
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   291
        return null;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   292
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   293
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   294
    /**
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   295
     * Defensive copy used in createTransferable.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   296
     */
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   297
    private DefaultMutableTreeNode copy(TreeNode node) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   298
        return new DefaultMutableTreeNode(node);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   299
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   300
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   301
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   302
    protected void exportDone(JComponent source, Transferable data, int action) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   303
        if ((action & MOVE) == MOVE) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   304
            JTree tree = (JTree) source;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   305
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   306
            // Remove nodes saved in nodesToRemove in createTransferable.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   307
            for (DefaultMutableTreeNode nodesToRemove1 : nodesToRemove) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   308
                model.removeNodeFromParent(nodesToRemove1);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   309
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   310
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   311
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   312
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   313
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   314
    public int getSourceActions(JComponent c) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   315
        return COPY_OR_MOVE;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   316
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   317
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   318
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   319
    public boolean importData(TransferHandler.TransferSupport support) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   320
        if (!canImport(support)) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   321
            return false;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   322
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   323
        // Extract transfer data.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   324
        DefaultMutableTreeNode[] nodes = null;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   325
        try {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   326
            Transferable t = support.getTransferable();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   327
            nodes = (DefaultMutableTreeNode[]) t.getTransferData(nodesFlavor);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   328
        } catch (UnsupportedFlavorException ufe) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   329
            System.out.println("UnsupportedFlavor: " + ufe.getMessage());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   330
        } catch (java.io.IOException ioe) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   331
            System.out.println("I/O error: " + ioe.getMessage());
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   332
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   333
        // Get drop location info.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   334
        JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   335
        int childIndex = dl.getChildIndex();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   336
        TreePath dest = dl.getPath();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   337
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   338
                dest.getLastPathComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   339
        JTree tree = (JTree) support.getComponent();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   340
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   341
        // Configure for drop mode.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   342
        int index = childIndex;    // DropMode.INSERT
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   343
        if (childIndex == -1) {     // DropMode.ON
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   344
            index = parent.getChildCount();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   345
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   346
        // Add data to model.
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   347
        for (DefaultMutableTreeNode node : nodes) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   348
            model.insertNodeInto(node, parent, index++);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   349
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   350
        return true;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   351
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   352
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   353
    @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   354
    public String toString() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   355
        return getClass().getName();
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   356
    }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   357
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   358
    public class NodesTransferable implements Transferable {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   359
        DefaultMutableTreeNode[] nodes;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   360
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   361
        public NodesTransferable(DefaultMutableTreeNode[] nodes) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   362
            this.nodes = nodes;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   363
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   364
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   365
        @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   366
        public Object getTransferData(DataFlavor flavor)
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   367
                throws UnsupportedFlavorException {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   368
            if (!isDataFlavorSupported(flavor)) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   369
                throw new UnsupportedFlavorException(flavor);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   370
            }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   371
            return nodes;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   372
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   373
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   374
        @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   375
        public DataFlavor[] getTransferDataFlavors() {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   376
            return flavors;
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   377
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   378
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   379
        @Override
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   380
        public boolean isDataFlavorSupported(DataFlavor flavor) {
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   381
            return nodesFlavor.equals(flavor);
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   382
        }
6b5f36a9a3c0 8129830: JTree drag/drop on lower half of last child of container incorrect.
ssadetsky
parents:
diff changeset
   383
    }
32427
c22b7e41adf3 8134984: Text files should end in exactly one newline
martin
parents: 31657
diff changeset
   384
}