2
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.lafwidget.tree.dnd;
32
import java.awt.datatransfer.*;
33
import java.io.IOException;
35
import javax.swing.JTree;
36
import javax.swing.tree.MutableTreeNode;
39
* TransferableTreeNode is a Transferable object used to transfer TreeNodes or
40
* Strings in drag and drop operations.
42
* @author Antonio Vieiro (antonio@antonioshome.net), $Author: kirillcool $
44
class TransferableTreeNode implements Transferable {
46
* The local JVM DataFlavor.
48
private static DataFlavor javaJVMLocalObjectFlavor;
50
* The supported data flavors.
52
private static DataFlavor[] supportedDataFlavors;
55
* Returns the Java JVM LocalObject Flavor.
57
public static DataFlavor getJavaJVMLocalObjectFlavor() {
58
if (TransferableTreeNode.javaJVMLocalObjectFlavor == null) {
60
TransferableTreeNode.javaJVMLocalObjectFlavor = new DataFlavor(
61
DataFlavor.javaJVMLocalObjectMimeType);
62
} catch (ClassNotFoundException cnfe) {
63
System.err.println("Cannot create JVM Local Object Flavor "
67
return TransferableTreeNode.javaJVMLocalObjectFlavor;
71
* Returns the supported data flavors.
73
private static DataFlavor[] getSupportedDataFlavors() {
74
if (TransferableTreeNode.supportedDataFlavors == null) {
75
DataFlavor localJVMFlavor = TransferableTreeNode
76
.getJavaJVMLocalObjectFlavor();
77
TransferableTreeNode.supportedDataFlavors = localJVMFlavor == null ? new DataFlavor[] { DataFlavor.stringFlavor }
78
: new DataFlavor[] { localJVMFlavor,
79
DataFlavor.stringFlavor };
81
return TransferableTreeNode.supportedDataFlavors;
85
* Creates a new instance of TransferableTreeNode.
88
* the JTree that contains de dragged node.
90
* the MutableTreeNode in JTree that is to be dragged.
92
* true if the source node was expanded, false otherwise.
94
public TransferableTreeNode(JTree aTree, MutableTreeNode aNode,
95
boolean wasExpanded) {
96
this.setSourceTree(aTree);
97
this.setSourceNode(aNode);
98
this.setNodeWasExpanded(wasExpanded);
102
public boolean isDataFlavorSupported(DataFlavor flavor) {
103
DataFlavor[] flavors = TransferableTreeNode.getSupportedDataFlavors();
104
for (int i = 0; i < flavors.length; i++) {
105
if (flavor.equals(flavors[i]))
112
public Object getTransferData(DataFlavor flavor)
113
throws UnsupportedFlavorException, IOException {
114
if (flavor.equals(TransferableTreeNode.javaJVMLocalObjectFlavor)) {
116
} else if (flavor.equals(DataFlavor.stringFlavor)) {
117
return this.getSourceNode().toString();
119
throw new UnsupportedFlavorException(flavor);
123
public DataFlavor[] getTransferDataFlavors() {
124
return TransferableTreeNode.getSupportedDataFlavors();
128
* Holds value of property sourceTree.
130
private JTree sourceTree;
133
* Getter for property sourceTree.
135
* @return Value of property sourceTree.
137
public JTree getSourceTree() {
138
return this.sourceTree;
142
* Setter for property sourceTree.
145
* New value of property sourceTree.
147
public void setSourceTree(JTree sourceTree) {
148
this.sourceTree = sourceTree;
152
* Holds value of property sourceNode.
154
private MutableTreeNode sourceNode;
157
* Getter for property sourceNode.
159
* @return Value of property sourceNode.
161
public MutableTreeNode getSourceNode() {
162
return this.sourceNode;
166
* Setter for property sourceNode.
169
* New value of property sourceNode.
171
public void setSourceNode(MutableTreeNode sourceNode) {
172
this.sourceNode = sourceNode;
176
* Holds value of property nodeWasExpanded.
178
private boolean nodeWasExpanded;
181
* Getter for property nodeWasExpanded.
183
* @return Value of property nodeWasExpanded.
185
public boolean isNodeWasExpanded() {
186
return this.nodeWasExpanded;
190
* Setter for property nodeWasExpanded.
192
* @param nodeWasExpanded
193
* New value of property nodeWasExpanded.
195
public void setNodeWasExpanded(boolean nodeWasExpanded) {
196
this.nodeWasExpanded = nodeWasExpanded;