feisty meow concerns codebase  2.140
dragdrop_list_test.java
Go to the documentation of this file.
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 
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  @SuppressWarnings({ "rawtypes", "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  @SuppressWarnings("rawtypes")
100  public class DraggableDroppableList extends JList implements IDragonDropDataProvider
101  {
102  @SuppressWarnings("unchecked")
104  {
105  setModel(new DefaultListModel());
106  setTransferHandler(new DragonTransferHandler(this));
107  setDragEnabled(true);
108  }
109 
110  @Override
111  public boolean consumeDropList(List<Object> fileSet, Point location)
112  {
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());
116  }
117  return true;
118  }
119 
120  @Override
121  public List<Object> provideDragList()
122  {
123  ArrayList<Object> toReturn = new ArrayList<Object>();
124  if (getSelectedIndex() == -1)
125  return toReturn;
126  Object obj = getSelectedValue();
127  if (obj != null) {
128  @SuppressWarnings("unchecked")
129  Hashtable<String, String> table = (Hashtable<String, String>) obj;
130  toReturn.add(new File((String) table.get("path")));
131  }
132  return toReturn;
133  }
134  }
135 
136  @SuppressWarnings("rawtypes")
137  public class CustomCellRenderer implements ListCellRenderer
138  {
139  DefaultListCellRenderer listCellRenderer = new DefaultListCellRenderer();
140 
141  public Component getListCellRendererComponent(JList list, Object value, int index,
142  boolean selected, boolean hasFocus)
143  {
144  listCellRenderer.getListCellRendererComponent(list, value, index, selected, hasFocus);
145  listCellRenderer.setText(getValueString(value));
146  return listCellRenderer;
147  }
148 
149  private String getValueString(Object value)
150  {
151  String returnString = "null";
152  if (value != 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;
159  } else {
160  returnString = "X: " + value.toString();
161  }
162  }
163  return returnString;
164  }
165  }
166 
167  public static void main(String s[])
168  {
169  PropertyConfigurator.configure("log4j.properties");
170  // starting with user's personal area.
171  String homedir = System.getenv("HOME");
172  if ((homedir == null) || (homedir.length() == 0)) {
173  // fall back to the root if no home directory.
174  homedir = "/";
175  }
176  JFrame frame = new dragdrop_list_test(homedir);
177  frame.addWindowListener(new WindowAdapter()
178  {
179  public void windowClosing(WindowEvent e)
180  {
181  System.exit(0);
182  }
183  });
184  frame.pack();
185  frame.setVisible(true);
186  }
187 }
Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean hasFocus)
boolean consumeDropList(List< Object > fileSet, Point location)
char * filelist[MAXFILES]
Definition: makedep.cpp:102