001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui.action;
024:
025: import java.awt.event.ActionEvent;
026: import java.io.ByteArrayOutputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.IOException;
030: import javax.swing.AbstractAction;
031: import javax.swing.JFileChooser;
032: import javax.swing.JOptionPane;
033: import org.skunk.dav.client.DAVFile;
034: import org.skunk.dav.client.gui.DAVFileChooser;
035: import org.skunk.dav.client.gui.Explorer;
036: import org.skunk.dav.client.gui.ResourceManager;
037: import org.skunk.trace.Debug;
038:
039: public class UploadAction extends AbstractAction {
040: private Explorer ex;
041: private DAVFile file;
042:
043: public UploadAction(Explorer ex) {
044: this (ex, null);
045: }
046:
047: public UploadAction(Explorer ex, DAVFile file) {
048: this .ex = ex;
049: this .file = file;
050: }
051:
052: public void actionPerformed(ActionEvent ae) {
053: //obtain file to upload from a JFileChooser
054: JFileChooser chooser = new JFileChooser();
055: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
056: File f = null;
057: byte[] fileBody = null;
058: if (chooser.showOpenDialog(ex) == JFileChooser.APPROVE_OPTION
059: && (f = chooser.getSelectedFile()) != null) {
060: try {
061: FileInputStream fizz = new FileInputStream(f);
062: int initialSize = (int) Math.min((long) f.length(),
063: Integer.MAX_VALUE);
064: ByteArrayOutputStream baos = new ByteArrayOutputStream(
065: initialSize);
066: byte[] buffer = new byte[2048];
067: int bytesRead;
068: while ((bytesRead = fizz.read(buffer)) != -1) {
069: baos.write(buffer, 0, bytesRead);
070: }
071: fileBody = baos.toByteArray();
072: } catch (IOException oyVeh) {
073: Debug.trace(this , Debug.DP2, oyVeh);
074: return;
075: }
076: } else
077: return;
078: String fileName = null;
079: if (file == null) {
080: String title = ResourceManager
081: .getMessage(ResourceManager.UPLOAD_CHOOSER_DIALOG_TITLE);
082: DAVFileChooser.ChoiceStruct choice = ex.chooseFile(null,
083: title, true, f.getName());
084: if (choice == null) {
085: Debug.trace(this , Debug.DP3, "user cancelled upload");
086: return;
087: }
088: fileName = choice.getFilePath();
089: if (fileName == null) {
090: Debug.trace(this , Debug.DP3, "user cancelled upload");
091: return;
092: }
093: } else {
094: fileName = file.getFullName();
095: }
096: ex.createFile(fileName, fileBody);
097: }
098: }
099:
100: /* $Log: UploadAction.java,v $
101: /* Revision 1.9 2001/01/03 20:11:31 smulloni
102: /* the DAVFileChooser now replaces JFileChooser for remote file access.
103: /* DAVMethod now has a protocol property.
104: /*
105: /* Revision 1.8 2000/12/19 22:36:05 smulloni
106: /* adjustments to preamble.
107: /*
108: /* Revision 1.7 2000/12/03 23:53:26 smulloni
109: /* added license and copyright preamble to java files.
110: /*
111: /* Revision 1.6 2000/11/09 23:35:06 smullyan
112: /* log added to every Java file, with the help of python. Lock stealing
113: /* implemented, and treatment of locks made more robust.
114: /* */
|