1 package org.feistymeow.dragdrop;
7 import java.awt.event.*;
9 import javax.swing.event.*;
10 import javax.swing.tree.DefaultMutableTreeNode;
11 import javax.swing.tree.DefaultTreeCellRenderer;
12 import javax.swing.tree.DefaultTreeModel;
13 import javax.swing.tree.DefaultTreeSelectionModel;
14 import javax.swing.tree.MutableTreeNode;
15 import javax.swing.tree.TreeCellRenderer;
16 import javax.swing.tree.TreePath;
17 import javax.swing.tree.TreeSelectionModel;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.log4j.PropertyConfigurator;
31 @SuppressWarnings(
"serial")
35 private JTextField fileName;
40 super(
"dragdrop_test");
45 DefaultTreeModel treeModel = (DefaultTreeModel)
larch.getModel();
47 TreeSelectionModel selmod =
new DefaultTreeSelectionModel();
48 selmod.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
49 larch.setSelectionModel(selmod);
50 larch.addTreeSelectionListener(
this);
51 JScrollPane listScrollPane =
new JScrollPane(
larch);
53 String dirName = startPath +
"/";
55 String
filelist[] =
new File(dirName).list();
56 MutableTreeNode root_node = (MutableTreeNode) treeModel.getRoot();
57 if (root_node ==
null) {
58 logger.error(
"something is not right about tree. has null root.");
62 for (
int i = 0; i <
filelist.length; i++) {
63 String thisFileSt = dirName +
filelist[i];
64 File thisFile =
new File(thisFileSt);
66 if (thisFile.isDirectory())
73 DefaultMutableTreeNode newNode =
new DefaultMutableTreeNode(makeNode(
74 thisFile.getName(), thisFile.toURI().toURL().toString(),
75 thisFile.getAbsolutePath()));
76 treeModel.insertNodeInto(newNode, root_node, root_node.getChildCount());
77 }
catch (java.net.MalformedURLException e) {
78 logger.warn(
"caught an exception while trying to process path: "
79 + thisFile.getAbsolutePath());
84 fileName =
new JTextField(50);
86 larch.setSelectionPath(
larch.getPathForRow(0));
92 JPanel buttonPane =
new JPanel();
93 buttonPane.add(fileName);
95 Container contentPane = getContentPane();
96 contentPane.add(listScrollPane, BorderLayout.CENTER);
97 contentPane.add(buttonPane, BorderLayout.NORTH);
100 @SuppressWarnings(
"unchecked")
102 Hashtable<String, String> NodeToTable(Object node)
104 if (!(node instanceof DefaultMutableTreeNode))
106 Object content = ((DefaultMutableTreeNode) node).getUserObject();
107 if (content !=
null) {
108 if (content instanceof Hashtable<?, ?>) {
110 return (Hashtable<String, String>) content;
111 }
catch (Throwable cause) {
112 logger.error(
"failed to cast our tree node to a hashtable.");
121 fileName.setText(
"");
122 TreePath sel_path =
larch.getSelectionPath();
123 if (sel_path !=
null) {
124 Hashtable<String, String> table = NodeToTable(sel_path.getLastPathComponent());
126 String name = (String) table.get(
"name");
127 fileName.setText(name);
132 private static Hashtable<String, String> makeNode(String name, String url, String strPath)
134 Hashtable<String, String> hashtable =
new Hashtable<String, String>();
135 hashtable.put(
"name", name);
136 hashtable.put(
"url", url);
137 hashtable.put(
"path", strPath);
147 url =
new File(startPath).toURI().toURL().toString();
148 }
catch (Throwable cause) {
149 logger.warn(
"failed to calculate URL for " + startPath);
151 setModel(
new DefaultTreeModel(
new DefaultMutableTreeNode(
152 makeNode(
"top", url, startPath))));
154 setDragEnabled(
true);
160 logger.debug(
"into consume dropped files, file set is:");
161 for (
int i = 0; i < fileSet.size(); i++) {
162 logger.debug(
" " + ((File) fileSet.get(i)).getPath());
170 ArrayList<Object> toReturn =
new ArrayList<Object>();
171 TreePath tsp = getSelectionPath();
174 logger.debug(
"got the path...");
175 Hashtable<String, String> table = NodeToTable(tsp.getLastPathComponent());
177 toReturn.add(
new File(table.get(
"path")));
185 while (row < getRowCount()) {
195 DefaultTreeCellRenderer defRend =
new DefaultTreeCellRenderer();
197 private String getValueString(Object value)
199 String returnString =
"empty";
200 Hashtable<String, String> table = NodeToTable(value);
202 returnString = table.get(
"name") +
" -> " + table.get(
"url");
204 returnString =
"??: " + value.toString();
211 boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
213 defRend.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
215 defRend.setText(getValueString(value));
220 public static void main(String s[])
222 PropertyConfigurator.configure(
"log4j.properties");
224 String homedir = System.getenv(
"HOME");
225 if ((homedir ==
null) || (homedir.length() == 0)) {
230 frame.addWindowListener(
new WindowAdapter()
232 public void windowClosing(WindowEvent e)
238 frame.setVisible(
true);
Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
List< Object > provideDragList()
DraggableDroppableTree(String startPath)
boolean consumeDropList(List< Object > fileSet, Point location)
void valueChanged(TreeSelectionEvent e)
dragdrop_tree_test(String startPath)
static void main(String s[])
char * filelist[MAXFILES]