removed unneeded warning, updated editor.
[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")
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         public dragdrop_list_test(String startPath)
32     {
33         super("dragdrop_test");
34
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);
43
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())
51                 continue;
52             // skip dot files.
53             if (filelist[i].startsWith("."))
54                 continue;
55             try {
56                 listModel.addElement(makeNode(thisFile.getName(), thisFile.toURI().toURL()
57                         .toString(), thisFile.getAbsolutePath()));
58             } catch (java.net.MalformedURLException e) {
59             }
60         }
61
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);
68
69         // Create a panel that uses FlowLayout (the default).
70         JPanel buttonPane = new JPanel();
71         buttonPane.add(fileName);
72
73         Container contentPane = getContentPane();
74         contentPane.add(listScrollPane, BorderLayout.CENTER);
75         contentPane.add(buttonPane, BorderLayout.NORTH);
76     }
77
78     public void valueChanged(ListSelectionEvent e)
79     {
80         if (e.getValueIsAdjusting() == false) {
81             fileName.setText("");
82             if (list.getSelectedIndex() != -1) {
83                 String name = list.getSelectedValue().toString();
84                 fileName.setText(name);
85             }
86         }
87     }
88
89     private static Hashtable<String, String> makeNode(String name, String url, String strPath)
90     {
91         Hashtable<String, String> hashtable = new Hashtable<String, String>();
92         hashtable.put("name", name);
93         hashtable.put("url", url);
94         hashtable.put("path", strPath);
95         return hashtable;
96     }
97
98     public class DraggableDroppableList extends JList implements IDragonDropDataProvider
99     {
100                 public DraggableDroppableList()
101         {
102             setModel(new DefaultListModel());
103             setTransferHandler(new DragonTransferHandler(this));
104             setDragEnabled(true);
105         }
106
107         @Override
108         public boolean consumeDropList(List<Object> fileSet, Point location)
109         {
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());
113             }
114             return true;
115         }
116
117         @Override
118         public List<Object> provideDragList()
119         {
120             ArrayList<Object> toReturn = new ArrayList<Object>();
121             if (getSelectedIndex() == -1)
122                 return toReturn;
123             Object obj = getSelectedValue();
124             if (obj != null) {
125                 @SuppressWarnings("unchecked")
126                 Hashtable<String, String> table = (Hashtable<String, String>) obj;
127                 toReturn.add(new File((String) table.get("path")));
128             }
129             return toReturn;
130         }
131     }
132
133     public class CustomCellRenderer implements ListCellRenderer
134     {
135         DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
136
137         public Component getListCellRendererComponent(JList list, Object value, int index,
138                 boolean selected, boolean hasFocus)
139         {
140             listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
141             listCellRenderer.setText(getValueString(value));
142             return listCellRenderer;
143         }
144
145         private String getValueString(Object value)
146         {
147             String returnString = "null";
148             if (value != 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;
155                 } else {
156                     returnString = "X: " + value.toString();
157                 }
158             }
159             return returnString;
160         }
161     }
162
163     public static void main(String s[])
164     {
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.
170             homedir = "/";
171         }
172         JFrame frame = new dragdrop_list_test(homedir);
173         frame.addWindowListener(new WindowAdapter()
174         {
175             public void windowClosing(WindowEvent e)
176             {
177                 System.exit(0);
178             }
179         });
180         frame.pack();
181         frame.setVisible(true);
182     }
183 }