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", "rawtypes"})
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 @SuppressWarnings("unchecked")
32 public dragdrop_list_test(String startPath)
34 super("dragdrop_test");
36 // Create the list and put it in a scroll pane
37 list = new DraggableDroppableList();
38 DefaultListModel listModel = (DefaultListModel) list.getModel();
39 list.setCellRenderer(new CustomCellRenderer());
40 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
41 list.setSelectedIndex(0);
42 list.addListSelectionListener(this);
43 JScrollPane listScrollPane = new JScrollPane(list);
45 String dirName = startPath + "/"; // make sure we think of it as a directory.
46 String filelist[] = new File(dirName).list();
47 for (int i = 0; i < filelist.length; i++) {
48 String thisFileSt = dirName + filelist[i];
49 File thisFile = new File(thisFileSt);
50 // skip directories for now.
51 if (thisFile.isDirectory())
54 if (filelist[i].startsWith("."))
57 listModel.addElement(makeNode(thisFile.getName(), thisFile.toURI().toURL()
58 .toString(), thisFile.getAbsolutePath()));
59 } catch (java.net.MalformedURLException e) {
63 fileName = new JTextField(50);
64 list.setSelectedIndex(0);
65 int sel_index = list.getSelectedIndex();
66 Object obj_at_index = listModel.getElementAt(sel_index);
67 String name = obj_at_index.toString();
68 fileName.setText(name);
70 // Create a panel that uses FlowLayout (the default).
71 JPanel buttonPane = new JPanel();
72 buttonPane.add(fileName);
74 Container contentPane = getContentPane();
75 contentPane.add(listScrollPane, BorderLayout.CENTER);
76 contentPane.add(buttonPane, BorderLayout.NORTH);
79 public void valueChanged(ListSelectionEvent e)
81 if (e.getValueIsAdjusting() == false) {
83 if (list.getSelectedIndex() != -1) {
84 String name = list.getSelectedValue().toString();
85 fileName.setText(name);
90 private static Hashtable<String, String> makeNode(String name, String url, String strPath)
92 Hashtable<String, String> hashtable = new Hashtable<String, String>();
93 hashtable.put("name", name);
94 hashtable.put("url", url);
95 hashtable.put("path", strPath);
99 public class DraggableDroppableList extends JList implements IDragonDropDataProvider
101 @SuppressWarnings("unchecked")
102 public DraggableDroppableList()
104 setModel(new DefaultListModel());
105 setTransferHandler(new DragonTransferHandler(this));
106 setDragEnabled(true);
110 public boolean consumeDropList(List<Object> fileSet, Point location)
112 logger.debug("into consume dropped files, file set is:");
113 for (int i = 0; i < fileSet.size(); i++) {
114 logger.debug(" " + ((File) fileSet.get(i)).getPath());
120 public List<Object> provideDragList()
122 ArrayList<Object> toReturn = new ArrayList<Object>();
123 if (getSelectedIndex() == -1)
125 Object obj = getSelectedValue();
127 @SuppressWarnings("unchecked")
128 Hashtable<String, String> table = (Hashtable<String, String>) obj;
129 toReturn.add(new File((String) table.get("path")));
135 public class CustomCellRenderer implements ListCellRenderer
137 DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
139 public Component getListCellRendererComponent(JList list, Object value, int index,
140 boolean selected, boolean hasFocus)
142 listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
143 listCellRenderer.setText(getValueString(value));
144 return listCellRenderer;
147 private String getValueString(Object value)
149 String returnString = "null";
151 if (value instanceof Hashtable<?, ?>) {
152 @SuppressWarnings("unchecked")
153 Hashtable<String, String> h = (Hashtable<String, String>) value;
154 String name = (String) h.get("name");
155 String url = (String) h.get("url");
156 returnString = name + " ==> " + url;
158 returnString = "X: " + value.toString();
165 public static void main(String s[])
167 PropertyConfigurator.configure("log4j.properties");
168 // starting with user's personal area.
169 String homedir = System.getenv("HOME");
170 if ((homedir == null) || (homedir.length() == 0)) {
171 // fall back to the root if no home directory.
174 JFrame frame = new dragdrop_list_test(homedir);
175 frame.addWindowListener(new WindowAdapter()
177 public void windowClosing(WindowEvent e)
183 frame.setVisible(true);