001 package ideExtensions.dragDropSimple;
002
003 import com.bea.ide.core.MessageSvc;
004 import com.bea.ide.core.ResourceSvc;
005 import com.bea.ide.core.datatransfer.DefaultDragDropDriver;
006 import ideExtensions.dragDropSimple.SimpleTree.ItemInfo;
007
008 import java.awt.Component;
009 import java.awt.Image;
010 import java.awt.Point;
011 import java.awt.datatransfer.StringSelection;
012 import java.awt.datatransfer.Transferable;
013 import javax.swing.tree.DefaultMutableTreeNode;
014 import javax.swing.tree.TreeSelectionModel;
015
016 /**
017 * A drag/drop driver that supports copying data from the simple
018 * tree to a document source. This drag/drop driver is registered by
019 * the TreePanelView as the tree's driver for drag/drop operations.
020 * This driver knows how to extract data from the tree that will be
021 * copied to the drop source.
022 */
023 public class SimpleTreeDragDropDriver extends DefaultDragDropDriver
024 {
025 /**
026 * Called by the IDE to get the IDragSourceInfo instance through which
027 * the IDE can retrieve the data to copy from the drag source to
028 * the drop target.
029 *
030 * @param component The drag source.
031 * @param point The point representing the start of the drag.
032 * @return The drag source info containing transfer data.
033 */
034 public IDragSourceInfo getDragInfo(Component component, Point point)
035 {
036 IDragSourceInfo dragInfo = null;
037 try
038 {
039 /**
040 * If there's anything selected in the tree, construct a draginfo
041 * from the selected node. Note that this tree does not support
042 * multiple selections; see SimpleTree for more information.
043 */
044 SimpleTree simpleTree = (SimpleTree) component;
045 TreeSelectionModel selectionModel = simpleTree.getSelectionModel();
046 DefaultMutableTreeNode node =
047 (DefaultMutableTreeNode) (selectionModel.getSelectionPath().getLastPathComponent());
048 dragInfo = new TreeViewDragInfo(node);
049 return dragInfo;
050 } catch (Exception e)
051 {
052 MessageSvc.get().displayError("Error while dragging: " +
053 e.getMessage(), 1);
054 e.printStackTrace();
055 return null;
056 }
057 }
058
059 /**
060 * A drag info class to support copying data from the simple tree
061 * to a document.
062 */
063 class TreeViewDragInfo implements IDragSourceInfo
064 {
065 private String m_infoData = null;
066 StringBuffer m_infoBuffer = new StringBuffer();
067
068 /**
069 * Constructs an instance of this class with a node from the simple
070 * tree, extracting data from the node for copying to the drop
071 * target.
072 *
073 * @param selection The selected tree node.
074 */
075 public TreeViewDragInfo(DefaultMutableTreeNode selection)
076 {
077 // Retrieve the object that contains node data.
078 Object itemInfo = selection.getUserObject();
079 /**
080 * If the node is a leaf (that is, if it has no children) then simply
081 * get its data out as a string for copying to the drop target.
082 */
083 if (selection.isLeaf())
084 {
085 m_infoData = ((ItemInfo) itemInfo).getItemData();
086 /**
087 * If the node is not a leaf (it has children) then collect the
088 * data from each of its children for copying to the drop target.
089 */
090 } else
091 {
092 DefaultMutableTreeNode node = (DefaultMutableTreeNode) selection;
093 ItemInfo itemUserObject = (ItemInfo) node.getUserObject();
094 String infoData = itemUserObject.getItemData();
095 m_infoBuffer.append(infoData + "\n\n");
096
097 int count = selection.getChildCount();
098 for (int i = 0; i < count; i++)
099 {
100 node = (DefaultMutableTreeNode) selection.getChildAt(i);
101 extractInfoFromNode(node);
102 }
103 m_infoData = m_infoBuffer.toString();
104 }
105 }
106
107 /**
108 * Extracts info from a child node, calling itself for any children
109 * the child has.
110 */
111 private void extractInfoFromNode(DefaultMutableTreeNode node)
112 {
113 ItemInfo itemUserObject = (ItemInfo) node.getUserObject();
114 String infoData = itemUserObject.getItemData();
115 m_infoBuffer.append(infoData + "\n\n");
116 for (int i = 0; i < node.getChildCount(); i++)
117 {
118 extractInfoFromNode((DefaultMutableTreeNode)node.getChildAt(i));
119 }
120 }
121
122 /**
123 * Called by the IDE to retrieve the image that should be displayed
124 * with the pointer during dragging.
125 *
126 * @return The image to display.
127 */
128 public Image getDragImage()
129 {
130 return ResourceSvc.get().getImage("images/txt.gif");
131 }
132
133 /**
134 * Called by the IDE to retrieve the icon image's offset from the
135 * mouse pointer.
136 *
137 * @return The offset point.
138 */
139 public Point getIconOffset()
140 {
141 return new Point(0, 0);
142 }
143
144 /**
145 * Called by the IDE to retrieve the Transferable instance
146 * that contains data to be copied (transferred) from the
147 * drag source to the drop target. This method uses the string
148 * extracted from the selected simple tree node to construct
149 * an instance of StringSelection, which implements Transferable.
150 *
151 * @return The Transferable instance.
152 */
153 public Transferable getTransferable()
154 {
155 Transferable dropData = new StringSelection(m_infoData);
156 return dropData;
157 }
158 }
159 }
|