Closed Thread Icon

Topic awaiting preservation: Java Guru searched - Drag/Drop-Upload (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=25397" title="Pages that link to Topic awaiting preservation: Java Guru searched - Drag/Drop-Upload (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Java Guru searched - Drag/Drop-Upload <span class="small">(Page 1 of 1)</span>\

 
Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 04-01-2005 13:35

Hello,

I'm looking for somebody who's knowledgable in Java, specifically java applets.

I'd like to build a drag-drop file upload functionality for a web application framework.
(http://www.radinks.com/upload/ has a pretty neat example).
Goal is to have all three, a java, an activeX and a simple-html-fallback solution.
The system will eventually choose the most appropriate one for the current use.

I could hack out the activeX control, and the html fallback is not that complicated,
but I'd spent an eternity on the java end.

I'm planning for a PHP4 backend for now, though we could easily keep this modular as well.

Would anybody around here be interested in working on such a thing?
Or can any body provide some helpful pointers to get me started on doing it on my own?

Thanks,
so long,

->Tyberius Prime

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-01-2005 17:00

http://forum.java.sun.com/thread.jspa?threadID=590259&messageID=3103291

I grabbed the code from here and cleaned it up a little bit. I have it launching but didn't actually try and get it to do anything. It is an application as opposed to an applet, but it shouldn't be too hard to convert it.

Hope this helps a little.

code:
//package org.ditchnet;

import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;


public class Uploader extends JFrame {

public static JFrame frame;
public JList list;
public DefaultListModel model;

public File currentDir;
public DropTarget target;

JButton addButton, removeButton, uploadButton;

public Uploader() {
setSize(600,400);
setResizable(true);
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

URL uploadImgURL = Uploader.class.getResource("upload.png");
URL browseImgURL = Uploader.class.getResource("browse.png");
URL removeImgURL = Uploader.class.getResource("remove.png");

Action uploadAction = new UploadAction("Upload",null);
Action browseAction = new BrowseAction("Browse...",null);
Action removeAction = new RemoveAction("Remove",null);
Action selectAllAction = new SelectAllAction("Select All");

JPanel cp = new JPanel();
cp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
cp.setLayout(new BorderLayout());

setContentPane(cp);

JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenuItem item;
int metaKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
fileMenu.add(item = new JMenuItem(browseAction));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, metaKey));
fileMenu.add(item = new JMenuItem(uploadAction));
editMenu.add(item = new JMenuItem(selectAllAction));
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, metaKey));
editMenu.add(item = new JMenuItem(removeAction));
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);

model = new DefaultListModel();

list = new JList(model);
list.getInputMap().put(KeyStroke.getKeyStroke(65,KeyEvent.META_MASK),"selectAll Action");
list.getActionMap().put("selectAllAction",selectAllAction);
list.getInputMap().put(KeyStroke.getKeyStroke(8,0),"removeAction");
list.getActionMap().put("removeAction",removeAction);
list.setVisibleRowCount(12);
list.setCellRenderer(new ImageCellRenderer());

target = new DropTarget(list,new FileDropTargetListener());

cp.add(new JScrollPane(list),BorderLayout.CENTER);

JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10));

JLabel uploadLabel = new JLabel();
uploadLabel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
uploadButton = new JButton(uploadAction);
uploadLabel.add(uploadButton);
buttons.add(uploadButton);

addButton = new JButton(browseAction);
removeButton = new JButton(removeAction);

buttons.add(removeButton);
buttons.add(addButton);

cp.add(buttons,BorderLayout.SOUTH);

currentDir = new File(System.getProperty("user.dir"));

}

public int addToListModel(ArrayList filenames) {
int count = 0;
for ( Iterator i = filenames.iterator(); i.hasNext(); ) {
String filename = (String)i.next();
if (!model.contains(filename)) {
model.addElement(filename);
count++;
}
}
//System.out.println(count + " added. Contains:\n" + model.toString());
return count;
}

public static void main(String [] args) {
frame = new Uploader();
frame.setVisible(true);
}

class BrowseAction extends AbstractAction {
public BrowseAction(String text,Icon icon) { super(text,icon); }

public void actionPerformed(ActionEvent evt) {
JFileChooser chooser = new JFileChooser(currentDir);
chooser.addChoosableFileFilter(new ImageFileFilter());
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(true);
chooser.setDragEnabled(true);
chooser.showOpenDialog(frame);

currentDir = chooser.getCurrentDirectory();

File [] files = chooser.getSelectedFiles();
ArrayList filenames = new ArrayList(files.length);
for (int i = 0; i < files.length; i++)
filenames.add(files[i].getName());
addToListModel(filenames);
}
}

class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory()) return false;
String name = file.getName().toLowerCase();
return (name.endsWith(".jpg") || name.endsWith(".png"));
}

public String getDescription() { return "Images ( *.jpg, *.png )"; }
}

class UploadAction extends AbstractAction {

public UploadAction(String text,Icon icon) { super(text,icon); }

public void actionPerformed(ActionEvent evt) {
System.out.println(evt.getActionCommand());
//HTTP Client Networking Code called here.
}
}

class RemoveAction extends AbstractAction {
public RemoveAction(String text,Icon icon) { super(text,icon); }

public void actionPerformed(ActionEvent evt) {
int selected [] = list.getSelectedIndices();
if (selected.length == 0) return;
for ( int i = selected.length-1; i >= 0; i-- )
model.removeElementAt(selected[i]);
}
}

class SelectAllAction extends AbstractAction {

public SelectAllAction(String text) { super(text); }

public void actionPerformed(ActionEvent evt) {
int num = model.getSize();
int [] intArray = new int [num];
for ( int i = 0; i < num; i++ )
intArray[i] = i;
list.setSelectedIndices(intArray);
}
}

class ImageCellRenderer extends JLabel implements ListCellRenderer {
public ImageCellRenderer() {
setOpaque(true);
setIconTextGap(12);
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus){

File f = new File(value.toString());

Border empty = BorderFactory.createEmptyBorder(3,3,3,3);
Border matte = BorderFactory.createMatteBorder(0,0,1,0,Color.white);
setBorder(BorderFactory.createCompoundBorder(matte,empty));

setText(f.getName());

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image icon = toolkit.getImage(f.getPath());
Image scaledIcon = icon.getScaledInstance(40,40,Image.SCALE_FAST);
setIcon(new ImageIcon(scaledIcon));

if (isSelected) {
setBackground(new Color(61,128,223));
setForeground(Color.white);
} else if (index % 2 == 0) {
setBackground(new Color(237,243,254));
setForeground(Color.darkGray);
} else {
setBackground(Color.white);
setForeground(Color.darkGray);
}
return this;
}
}

class FileDropTargetListener extends DropTargetAdapter {
public void dragEnter(DropTargetDragEvent evt) {
frame.requestFocusInWindow();
}

public void drop(DropTargetDropEvent evt) {
try {
Transferable tr = evt.getTransferable();
DataFlavor [] flavors = tr.getTransferDataFlavors();
for ( int i = 0; i < flavors.length; i++ ) {
if ( flavors[i].isFlavorJavaFileListType() ) {
evt.acceptDrop(DnDConstants.ACTION_COPY);
java.util.List list2 = (java.util.List)tr.getTransferData(flavors[i]);
ArrayList filenames = new ArrayList();
for ( int j = 0; j < list2.size(); j++ ) {
String path = list2.get(j).toString();
if ( ((new ImageFileFilter()).accept(new File(path))) ) {
filenames.add(path);
}
}
if (filenames.size() > 0 ) {
int numAdded = addToListModel(filenames);
evt.dropComplete(true);
return;
}
}
JOptionPane.showMessageDialog(frame,
"You may only drag and drop\nimage files of type JPEG or PNG\nto the Uploader for uploading.",
"Invalid File Type",JOptionPane.WARNING_MESSAGE);
evt.rejectDrop();
}
} catch (Exception e) {
e.printStackTrace();
evt.rejectDrop();
}
}
}

}



Dan @ Code Town

(Edited by WarMage on 04-01-2005 17:01)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 04-05-2005 08:55

Thanks WarMage,
looks exactly like what I need.
Now all I need is some time - this is going to be the year of forced marches resulting in major growth, I guess.

« BackwardsOnwards »

Show Forum Drop Down Menu