Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/tree/dnd/TransferableTreeNode.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
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.
13 *
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.
17 *
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.
29 */
30 package org.pushingpixels.lafwidget.tree.dnd;
31
32 import java.awt.datatransfer.*;
33 import java.io.IOException;
34
35 import javax.swing.JTree;
36 import javax.swing.tree.MutableTreeNode;
37
38 /**
39 * TransferableTreeNode is a Transferable object used to transfer TreeNodes or
40 * Strings in drag and drop operations.
41 *
42 * @author Antonio Vieiro ([email protected]), $Author: kirillcool $
43 */
44 class TransferableTreeNode implements Transferable {
45 /**
46 * The local JVM DataFlavor.
47 */
48 private static DataFlavor javaJVMLocalObjectFlavor;
49 /**
50 * The supported data flavors.
51 */
52 private static DataFlavor[] supportedDataFlavors;
53
54 /**
55 * Returns the Java JVM LocalObject Flavor.
56 */
57 public static DataFlavor getJavaJVMLocalObjectFlavor() {
58 if (TransferableTreeNode.javaJVMLocalObjectFlavor == null) {
59 try {
60 TransferableTreeNode.javaJVMLocalObjectFlavor = new DataFlavor(
61 DataFlavor.javaJVMLocalObjectMimeType);
62 } catch (ClassNotFoundException cnfe) {
63 System.err.println("Cannot create JVM Local Object Flavor "
64 + cnfe.getMessage());
65 }
66 }
67 return TransferableTreeNode.javaJVMLocalObjectFlavor;
68 }
69
70 /**
71 * Returns the supported data flavors.
72 */
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 };
80 }
81 return TransferableTreeNode.supportedDataFlavors;
82 }
83
84 /**
85 * Creates a new instance of TransferableTreeNode.
86 *
87 * @param aTree
88 * the JTree that contains de dragged node.
89 * @param aNode
90 * the MutableTreeNode in JTree that is to be dragged.
91 * @param wasExpanded
92 * true if the source node was expanded, false otherwise.
93 */
94 public TransferableTreeNode(JTree aTree, MutableTreeNode aNode,
95 boolean wasExpanded) {
96 this.setSourceTree(aTree);
97 this.setSourceNode(aNode);
98 this.setNodeWasExpanded(wasExpanded);
99 }
100
101 @Override
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]))
106 return true;
107 }
108 return false;
109 }
110
111 @Override
112 public Object getTransferData(DataFlavor flavor)
113 throws UnsupportedFlavorException, IOException {
114 if (flavor.equals(TransferableTreeNode.javaJVMLocalObjectFlavor)) {
115 return this;
116 } else if (flavor.equals(DataFlavor.stringFlavor)) {
117 return this.getSourceNode().toString();
118 } else
119 throw new UnsupportedFlavorException(flavor);
120 }
121
122 @Override
123 public DataFlavor[] getTransferDataFlavors() {
124 return TransferableTreeNode.getSupportedDataFlavors();
125 }
126
127 /**
128 * Holds value of property sourceTree.
129 */
130 private JTree sourceTree;
131
132 /**
133 * Getter for property sourceTree.
134 *
135 * @return Value of property sourceTree.
136 */
137 public JTree getSourceTree() {
138 return this.sourceTree;
139 }
140
141 /**
142 * Setter for property sourceTree.
143 *
144 * @param sourceTree
145 * New value of property sourceTree.
146 */
147 public void setSourceTree(JTree sourceTree) {
148 this.sourceTree = sourceTree;
149 }
150
151 /**
152 * Holds value of property sourceNode.
153 */
154 private MutableTreeNode sourceNode;
155
156 /**
157 * Getter for property sourceNode.
158 *
159 * @return Value of property sourceNode.
160 */
161 public MutableTreeNode getSourceNode() {
162 return this.sourceNode;
163 }
164
165 /**
166 * Setter for property sourceNode.
167 *
168 * @param sourceNode
169 * New value of property sourceNode.
170 */
171 public void setSourceNode(MutableTreeNode sourceNode) {
172 this.sourceNode = sourceNode;
173 }
174
175 /**
176 * Holds value of property nodeWasExpanded.
177 */
178 private boolean nodeWasExpanded;
179
180 /**
181 * Getter for property nodeWasExpanded.
182 *
183 * @return Value of property nodeWasExpanded.
184 */
185 public boolean isNodeWasExpanded() {
186 return this.nodeWasExpanded;
187 }
188
189 /**
190 * Setter for property nodeWasExpanded.
191 *
192 * @param nodeWasExpanded
193 * New value of property nodeWasExpanded.
194 */
195 public void setNodeWasExpanded(boolean nodeWasExpanded) {
196 this.nodeWasExpanded = nodeWasExpanded;
197 }
198
199 }