2775560e6d6045b531c60e07b579755b80facc82
[feisty_meow.git] / kona / src / org / feistymeow / dragdrop / dragdrop_list_test.java
1 package org.feistymeow.dragdrop;
2
3 import java.awt.*;
4 import java.io.*;
5 import java.util.*;
6 import java.util.List;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import javax.swing.event.*;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.log4j.PropertyConfigurator;
14
15 /**
16  * A demo of the DragonDropHandler being used with a JList. Much love to the internet for lots of
17  * examples.
18  * 
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
23  */
24 @SuppressWarnings({"serial", "rawtypes"})
25 public class dragdrop_list_test extends JFrame implements ListSelectionListener
26 {
27     private DraggableDroppableList list;
28     private JTextField fileName;
29     static private Log logger = LogFactory.getLog(dragdrop_list_test.DraggableDroppableList.class);
30
31     @SuppressWarnings("unchecked")
32         public dragdrop_list_test(String startPath)
33     {
34         super("dragdrop_test");
35
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);
44
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())
52                 continue;
53             // skip dot files.
54             if (filelist[i].startsWith("."))
55                 continue;
56             try {
57                 listModel.addElement(makeNode(thisFile.getName(), thisFile.toURI().toURL()
58                         .toString(), thisFile.getAbsolutePath()));
59             } catch (java.net.MalformedURLException e) {
60             }
61         }
62
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);
69
70         // Create a panel that uses FlowLayout (the default).
71         JPanel buttonPane = new JPanel();
72         buttonPane.add(fileName);
73
74         Container contentPane = getContentPane();
75         contentPane.add(listScrollPane, BorderLayout.CENTER);
76         contentPane.add(buttonPane, BorderLayout.NORTH);
77     }
78
79     public void valueChanged(ListSelectionEvent e)
80     {
81         if (e.getValueIsAdjusting() == false) {
82             fileName.setText("");
83             if (list.getSelectedIndex() != -1) {
84                 String name = list.getSelectedValue().toString();
85                 fileName.setText(name);
86             }
87         }
88     }
89
90     private static Hashtable<String, String> makeNode(String name, String url, String strPath)
91     {
92         Hashtable<String, String> hashtable = new Hashtable<String, String>();
93         hashtable.put("name", name);
94         hashtable.put("url", url);
95         hashtable.put("path", strPath);
96         return hashtable;
97     }
98
99     public class DraggableDroppableList extends JList implements IDragonDropDataProvider
100     {
101         @SuppressWarnings("unchecked")
102                 public DraggableDroppableList()
103         {
104             setModel(new DefaultListModel());
105             setTransferHandler(new DragonTransferHandler(this));
106             setDragEnabled(true);
107         }
108
109         @Override
110         public boolean consumeDropList(List<Object> fileSet, Point location)
111         {
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());
115             }
116             return true;
117         }
118
119         @Override
120         public List<Object> provideDragList()
121         {
122             ArrayList<Object> toReturn = new ArrayList<Object>();
123             if (getSelectedIndex() == -1)
124                 return toReturn;
125             Object obj = getSelectedValue();
126             if (obj != null) {
127                 @SuppressWarnings("unchecked")
128                 Hashtable<String, String> table = (Hashtable<String, String>) obj;
129                 toReturn.add(new File((String) table.get("path")));
130             }
131             return toReturn;
132         }
133     }
134
135     public class CustomCellRenderer implements ListCellRenderer
136     {
137         DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
138
139         public Component getListCellRendererComponent(JList list, Object value, int index,
140                 boolean selected, boolean hasFocus)
141         {
142             listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
143             listCellRenderer.setText(getValueString(value));
144             return listCellRenderer;
145         }
146
147         private String getValueString(Object value)
148         {
149             String returnString = "null";
150             if (value != 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;
157                 } else {
158                     returnString = "X: " + value.toString();
159                 }
160             }
161             return returnString;
162         }
163     }
164
165     public static void main(String s[])
166     {
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.
172             homedir = "/";
173         }
174         JFrame frame = new dragdrop_list_test(homedir);
175         frame.addWindowListener(new WindowAdapter()
176         {
177             public void windowClosing(WindowEvent e)
178             {
179                 System.exit(0);
180             }
181         });
182         frame.pack();
183         frame.setVisible(true);
184     }
185 }