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;
24 @SuppressWarnings(
"serial")
28 private JTextField fileName;
31 @SuppressWarnings({
"rawtypes",
"unchecked" })
34 super(
"dragdrop_test");
38 DefaultListModel listModel = (DefaultListModel) list.getModel();
40 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
41 list.setSelectedIndex(0);
42 list.addListSelectionListener(
this);
43 JScrollPane listScrollPane =
new JScrollPane(list);
45 String dirName = startPath +
"/";
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);
51 if (thisFile.isDirectory())
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);
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);
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 @SuppressWarnings(
"rawtypes")
102 @SuppressWarnings(
"unchecked")
105 setModel(
new DefaultListModel());
107 setDragEnabled(
true);
113 logger.debug(
"into consume dropped files, file set is:");
114 for (
int i = 0; i < fileSet.size(); i++) {
115 logger.debug(
" " + ((File) fileSet.get(i)).getPath());
123 ArrayList<Object> toReturn =
new ArrayList<Object>();
124 if (getSelectedIndex() == -1)
126 Object obj = getSelectedValue();
128 @SuppressWarnings(
"unchecked")
129 Hashtable<String, String> table = (Hashtable<String, String>) obj;
130 toReturn.add(
new File((String) table.get(
"path")));
136 @SuppressWarnings(
"rawtypes")
139 DefaultListCellRenderer listCellRenderer =
new DefaultListCellRenderer();
142 boolean selected,
boolean hasFocus)
144 listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
145 listCellRenderer.setText(getValueString(value));
146 return listCellRenderer;
149 private String getValueString(Object value)
151 String returnString =
"null";
153 if (value instanceof Hashtable<?, ?>) {
154 @SuppressWarnings(
"unchecked")
155 Hashtable<String, String> h = (Hashtable<String, String>) value;
156 String name = (String) h.get("name");
157 String url = (String) h.get("url");
158 returnString = name + " ==> " + url;
160 returnString =
"X: " + value.toString();
167 public static void main(String s[])
169 PropertyConfigurator.configure(
"log4j.properties");
171 String homedir = System.getenv(
"HOME");
172 if ((homedir ==
null) || (homedir.length() == 0)) {
177 frame.addWindowListener(
new WindowAdapter()
179 public void windowClosing(WindowEvent e)
185 frame.setVisible(
true);
Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean hasFocus)
List< Object > provideDragList()
boolean consumeDropList(List< Object > fileSet, Point location)
static void main(String s[])
dragdrop_list_test(String startPath)
void valueChanged(ListSelectionEvent e)
char * filelist[MAXFILES]