1 package org.feistymeow.dragdrop;
7 import java.awt.event.*;
9 import javax.swing.event.*;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.log4j.PropertyConfigurator;
16 * A demo of the DragonDropHandler being used with a JList. Much love to the internet for lots of
19 * @author Chris Koeritz
20 * @copyright Copyright (c) 2012-$now By University of Virginia
21 * @license This file is free software; you can modify and redistribute it under the terms of the
22 * Apache License v2.0: http://www.apache.org/licenses/LICENSE-2.0
24 @SuppressWarnings("serial")
25 public class dragdrop_list_test extends JFrame implements ListSelectionListener
27 private DraggableDroppableList list;
28 private JTextField fileName;
29 static private Log logger = LogFactory.getLog(dragdrop_list_test.DraggableDroppableList.class);
31 public dragdrop_list_test(String startPath)
33 super("dragdrop_test");
35 // Create the list and put it in a scroll pane
36 list = new DraggableDroppableList();
37 DefaultListModel listModel = (DefaultListModel) list.getModel();
38 list.setCellRenderer(new CustomCellRenderer());
39 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
40 list.setSelectedIndex(0);
41 list.addListSelectionListener(this);
42 JScrollPane listScrollPane = new JScrollPane(list);
44 String dirName = startPath + "/"; // make sure we think of it as a directory.
45 String filelist[] = new File(dirName).list();
46 for (int i = 0; i < filelist.length; i++) {
47 String thisFileSt = dirName + filelist[i];
48 File thisFile = new File(thisFileSt);
49 // skip directories for now.
50 if (thisFile.isDirectory())
53 if (filelist[i].startsWith("."))
56 listModel.addElement(makeNode(thisFile.getName(), thisFile.toURI().toURL()
57 .toString(), thisFile.getAbsolutePath()));
58 } catch (java.net.MalformedURLException e) {
62 fileName = new JTextField(50);
63 list.setSelectedIndex(0);
64 int sel_index = list.getSelectedIndex();
65 Object obj_at_index = listModel.getElementAt(sel_index);
66 String name = obj_at_index.toString();
67 fileName.setText(name);
69 // Create a panel that uses FlowLayout (the default).
70 JPanel buttonPane = new JPanel();
71 buttonPane.add(fileName);
73 Container contentPane = getContentPane();
74 contentPane.add(listScrollPane, BorderLayout.CENTER);
75 contentPane.add(buttonPane, BorderLayout.NORTH);
78 public void valueChanged(ListSelectionEvent e)
80 if (e.getValueIsAdjusting() == false) {
82 if (list.getSelectedIndex() != -1) {
83 String name = list.getSelectedValue().toString();
84 fileName.setText(name);
89 private static Hashtable<String, String> makeNode(String name, String url, String strPath)
91 Hashtable<String, String> hashtable = new Hashtable<String, String>();
92 hashtable.put("name", name);
93 hashtable.put("url", url);
94 hashtable.put("path", strPath);
98 public class DraggableDroppableList extends JList implements IDragonDropDataProvider
100 public DraggableDroppableList()
102 setModel(new DefaultListModel());
103 setTransferHandler(new DragonTransferHandler(this));
104 setDragEnabled(true);
108 public boolean consumeDropList(List<Object> fileSet, Point location)
110 logger.debug("into consume dropped files, file set is:");
111 for (int i = 0; i < fileSet.size(); i++) {
112 logger.debug(" " + ((File) fileSet.get(i)).getPath());
118 public List<Object> provideDragList()
120 ArrayList<Object> toReturn = new ArrayList<Object>();
121 if (getSelectedIndex() == -1)
123 Object obj = getSelectedValue();
125 @SuppressWarnings("unchecked")
126 Hashtable<String, String> table = (Hashtable<String, String>) obj;
127 toReturn.add(new File((String) table.get("path")));
133 public class CustomCellRenderer implements ListCellRenderer
135 DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
137 public Component getListCellRendererComponent(JList list, Object value, int index,
138 boolean selected, boolean hasFocus)
140 listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
141 listCellRenderer.setText(getValueString(value));
142 return listCellRenderer;
145 private String getValueString(Object value)
147 String returnString = "null";
149 if (value instanceof Hashtable<?, ?>) {
150 @SuppressWarnings("unchecked")
151 Hashtable<String, String> h = (Hashtable<String, String>) value;
152 String name = (String) h.get("name");
153 String url = (String) h.get("url");
154 returnString = name + " ==> " + url;
156 returnString = "X: " + value.toString();
163 public static void main(String s[])
165 PropertyConfigurator.configure("log4j.properties");
166 // starting with user's personal area.
167 String homedir = System.getenv("HOME");
168 if ((homedir == null) || (homedir.length() == 0)) {
169 // fall back to the root if no home directory.
172 JFrame frame = new dragdrop_list_test(homedir);
173 frame.addWindowListener(new WindowAdapter()
175 public void windowClosing(WindowEvent e)
181 frame.setVisible(true);