Gridarta Editor
TreeDragSource.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.mapmenu;
21 
22 import java.awt.Cursor;
23 import java.awt.dnd.DnDConstants;
24 import java.awt.dnd.DragGestureListener;
25 import java.awt.dnd.DragSource;
26 import java.awt.dnd.DragSourceDragEvent;
27 import java.awt.dnd.DragSourceDropEvent;
28 import java.awt.dnd.DragSourceEvent;
29 import java.awt.dnd.DragSourceListener;
30 import javax.swing.JTree;
31 import javax.swing.tree.DefaultTreeModel;
32 import javax.swing.tree.MutableTreeNode;
33 import javax.swing.tree.TreePath;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class TreeDragSource {
42 
46  @NotNull
47  private final JTree tree;
48 
52  @NotNull
53  private final DragSource dragSource = new DragSource();
54 
58  @Nullable
59  private MutableTreeNode draggedTreeNode;
60 
64  @NotNull
65  private final DragSourceListener dragSourceListener = new DragSourceListener() {
66 
67  @Override
68  public void dragEnter(@NotNull final DragSourceDragEvent dsde) {
69  // ignore
70  }
71 
72  @Override
73  public void dragOver(@NotNull final DragSourceDragEvent dsde) {
74  // ignore
75  }
76 
77  @Override
78  public void dropActionChanged(@NotNull final DragSourceDragEvent dsde) {
79  // ignore
80  }
81 
82  @Override
83  public void dragExit(@NotNull final DragSourceEvent dse) {
84  // ignore
85  }
86 
87  @Override
88  public void dragDropEnd(@NotNull final DragSourceDropEvent dsde) {
89  if (draggedTreeNode == null) {
90  return;
91  }
92 
93  if (dsde.getDropSuccess()) {
94  switch (dsde.getDropAction()) {
95  case DnDConstants.ACTION_COPY:
96  break;
97 
98  case DnDConstants.ACTION_MOVE:
99  ((DefaultTreeModel) tree.getModel()).removeNodeFromParent(draggedTreeNode);
100  break;
101  }
102  }
103  draggedTreeNode = null;
104  }
105 
106  };
107 
113  public TreeDragSource(@NotNull final JTree tree, final int actions) {
114  this.tree = tree;
115 
116  final DragGestureListener dragGestureListener = dge -> {
117  final TreePath selectionPath = tree.getSelectionPath();
118  if (selectionPath == null || selectionPath.getPathCount() <= 1) {
119  return;
120  }
121 
122  draggedTreeNode = (MutableTreeNode) selectionPath.getLastPathComponent();
123  final Cursor cursor;
124  switch (dge.getDragAction()) {
125  case DnDConstants.ACTION_COPY:
126  cursor = DragSource.DefaultCopyDrop;
127  break;
128 
129  case DnDConstants.ACTION_MOVE:
130  cursor = DragSource.DefaultMoveDrop;
131  break;
132 
133  default:
134  return;
135  }
136  dragSource.startDrag(dge, cursor, new TransferableTreeNode(selectionPath), dragSourceListener);
137  };
138  dragSource.createDefaultDragGestureRecognizer(tree, actions, dragGestureListener);
139  }
140 
141 }
net.sf.gridarta.gui.mapmenu.TransferableTreeNode
A Transferable that transfers TreePath instances.
Definition: TransferableTreeNode.java:32
net.sf.gridarta.gui.mapmenu.TreeDragSource.tree
final JTree tree
The JTree being monitored.
Definition: TreeDragSource.java:47
net.sf.gridarta.gui.mapmenu.TreeDragSource
Implements a drag source for JTree instances.
Definition: TreeDragSource.java:41
net.sf.gridarta.gui.mapmenu.TreeDragSource.dragSourceListener
final DragSourceListener dragSourceListener
The DragSourceListener attached to dragSource.
Definition: TreeDragSource.java:65
net.sf.gridarta.gui.mapmenu.TreeDragSource.dragSource
final DragSource dragSource
The DragSource for tree.
Definition: TreeDragSource.java:53
net.sf.gridarta.gui.mapmenu.TreeDragSource.TreeDragSource
TreeDragSource(@NotNull final JTree tree, final int actions)
Creates a new instance.
Definition: TreeDragSource.java:113
net.sf.gridarta.gui.mapmenu.TreeDragSource.draggedTreeNode
MutableTreeNode draggedTreeNode
The MutableTreeNode being dragged.
Definition: TreeDragSource.java:59