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.BufferedOutputStream;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import javax.swing.AbstractAction;
031: import javax.swing.JFileChooser; //import javax.swing.JLabel;
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.DAVTreeNode;
036: import org.skunk.dav.client.gui.Explorer;
037: import org.skunk.dav.client.gui.ResourceManager;
038: import org.skunk.trace.Debug;
039:
040: public class DownloadAction extends AbstractAction {
041: private Explorer ex;
042: private DAVFile file;
043:
044: public DownloadAction(Explorer ex) {
045: this (ex, null);
046: }
047:
048: public DownloadAction(Explorer ex, DAVFile file) {
049: this .ex = ex;
050: this .file = file;
051: }
052:
053: public void actionPerformed(ActionEvent ae) {
054: DAVFile localDAVFile = (file != null) ? file : ex
055: .getSelectedTableFile();
056: if (localDAVFile == null || localDAVFile.isCollection()) {
057: String title = ResourceManager
058: .getMessage(ResourceManager.DOWNLOAD_CHOOSER_DIALOG_TITLE);
059: DAVFileChooser.ChoiceStruct choice = ex.chooseFile(null,
060: title, false, null);
061: if (choice == null) {
062: Debug.trace(this , Debug.DP3, "user aborted download");
063: return;
064: }
065: DAVTreeNode selectedNode = choice.getSelectedLeaf();
066: if (selectedNode != null) {
067: localDAVFile = selectedNode.getDAVFile();
068: } else {
069: Debug.trace(this , Debug.DP3, "no file selected");
070: return;
071: }
072:
073: }
074: //get the local target
075: File f = null;
076: JFileChooser chooser = new JFileChooser();
077: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
078: chooser.setSelectedFile(new File(chooser.getCurrentDirectory(),
079: localDAVFile.getFileName()));
080:
081: if (chooser.showSaveDialog(ex) == JFileChooser.APPROVE_OPTION
082: && (f = chooser.getSelectedFile()) != null) {
083: byte[] fileBytes = ex.get(localDAVFile);
084: try {
085: BufferedOutputStream boss = new BufferedOutputStream(
086: new FileOutputStream(f));
087: for (int i = 0; i < fileBytes.length; i += 2048)
088: boss.write(fileBytes, i, Math.min(2048,
089: fileBytes.length - i));
090: boss.flush();
091: boss.close();
092: // Elias Sinderson pointedly suggested that this dialog was unnecessary.
093: // String message=ResourceManager.getMessage(ResourceManager.DOWNLOAD_SUCCESS_MESSAGE,
094: // new Object[] {localDAVFile.getFullName(),
095: // f.getAbsolutePath()});
096: // String title=ResourceManager.getMessage(ResourceManager.DOWNLOAD_SUCCESS_DIALOG_TITLE);
097: // JOptionPane.showMessageDialog(ex, new JLabel(message),title, JOptionPane.INFORMATION_MESSAGE);
098: } catch (IOException oyVeh) {
099: // probably I should have a dialog here. TO BE DONE
100: Debug.trace(this , Debug.DP2, oyVeh);
101: }
102: }
103:
104: }
105: }
106:
107: /* $Log: DownloadAction.java,v $
108: /* Revision 1.8 2001/07/25 21:00:50 smulloni
109: /* removed file download confirmation dialog.
110: /*
111: /* Revision 1.7 2001/01/03 20:11:31 smulloni
112: /* the DAVFileChooser now replaces JFileChooser for remote file access.
113: /* DAVMethod now has a protocol property.
114: /*
115: /* Revision 1.6 2000/12/19 22:36:05 smulloni
116: /* adjustments to preamble.
117: /*
118: /* Revision 1.5 2000/12/03 23:53:26 smulloni
119: /* added license and copyright preamble to java files.
120: /*
121: /* Revision 1.4 2000/11/09 23:35:02 smullyan
122: /* log added to every Java file, with the help of python. Lock stealing
123: /* implemented, and treatment of locks made more robust.
124: /* */
|