Author: tchemit Date: 2008-02-08 14:19:02 +0000 (Fri, 08 Feb 2008) New Revision: 743 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAttachmentAction.java trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadLoggableElementAction.java trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/util/DownloadAbstractAction.java Removed: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAction.java Log: suppression ancienne action download ajout action abstraite + action downloadLE et downloadAttachment Deleted: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAction.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAction.java 2008-02-08 14:17:48 UTC (rev 742) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAction.java 2008-02-08 14:19:02 UTC (rev 743) @@ -1,142 +0,0 @@ -/* -* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, -* Tony Chemit, Gabriel Landais -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -* ##% */ -package fr.cemagref.simexplorer.is.ui.swing.actions; - -import com.healthmarketscience.rmiio.RemoteInputStream; -import com.healthmarketscience.rmiio.RemoteInputStreamClient; -import fr.cemagref.simexplorer.is.entities.data.LoggableElement; -import fr.cemagref.simexplorer.is.entities.metadata.Version; -import fr.cemagref.simexplorer.is.service.StorageService; -import fr.cemagref.simexplorer.is.ui.swing.actions.util.SimExplorerAbstractTabAction; -import fr.cemagref.simexplorer.is.ui.swing.model.DetailTabModel; -import fr.cemagref.simexplorer.is.ui.swing.model.LoggableElementTreeHelper; -import fr.cemagref.simexplorer.is.ui.swing.model.LoggableElementTreeNode; -import fr.cemagref.simexplorer.is.ui.swing.tab.JApplicationDetailTab; - -import javax.swing.JTree; -import java.awt.event.ActionEvent; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.text.MessageFormat; - -/** - * Action pour downloader un résultat ou une librairie - * - * @author chemit - */ - at jaxx.runtime.builder.ActionConfig( - actionCommand = "download", - name = "simexplorer.action.application.download", - shortDescription = "simexplorer.action.application.download.tooltip", - longDescription = "simexplorer.action.application.download.help", - smallIcon = "action/download.png", - mnemonic = 'x', - hideActionText = true -) -public class DownloadAction extends SimExplorerAbstractTabAction { - - private static final long serialVersionUID = 7535647131305304738L; - - protected static final MessageFormat FILENAME_PATTERN = new MessageFormat("{0}.xml"); - JTree tree; - LoggableElementTreeNode node; - File file; - LoggableElement element; - String uuid; - Version version; - private static final int BUFFER_SIZE = 8 * 1024; - - public DownloadAction(String name) { - super(name); - } - - @Override - protected boolean beforeAction(ActionEvent e) throws Exception { - if (!super.beforeAction(e)) { - return false; - } - JApplicationDetailTab ui = (JApplicationDetailTab) getUI(); - tree = ui.getNavigationTree(); - node = LoggableElementTreeHelper.getSelectedNodeOrRootNode(tree); - boolean result; - result = LoggableElementTreeHelper.canDownload(node); - if (result) { - // ask user path where to save data - String dir = org.codelutin.util.FileUtil.getDirectory(); - if (dir == null) { - // user cancel action - return false; - } - - //Object obj= node.getUserObject(); - //element = (LoggableElement) node.getUserObject(); - //uuid = element.getMetaData().getUuid(); - //version = element.getMetaData().getVersion(); - //file = new File(FILENAME_PATTERN.format(new Object[]{uuid + "-" + version})); - } - result=false; - return result; - } - - @Override - protected void doAction(ActionEvent e) throws Exception { - super.doAction(e); - //TODO push this in StorageHelper - StorageService service = getContext().getStorageService(((DetailTabModel) getModel()).isRemote()); - RemoteInputStream ris = service.retrieveElementXML(getContext().getToken(), uuid, version.toString()); - if (ris == null) { - // todo log error - log.warn("could not get incoming data for " + uuid); - return; - } - InputStream stream = RemoteInputStreamClient.wrap(ris); - FileOutputStream fos = new FileOutputStream(file); - BufferedInputStream bis = new BufferedInputStream(stream); - BufferedOutputStream bos = new BufferedOutputStream(fos); - try { - byte[] buffer = new byte[BUFFER_SIZE]; - while (bis.read(buffer, 0, BUFFER_SIZE) != -1) { - bos.write(buffer); - bos.flush(); - } - } finally { - if (bis != null) { - bis.close(); - } - if (bos != null) { - bos.flush(); - bos.close(); - } - } - } - - @Override - protected void clear() { - super.clear(); - tree = null; - node = null; - file = null; - element = null; - version = null; - uuid = null; - } -} \ No newline at end of file Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAttachmentAction.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAttachmentAction.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadAttachmentAction.java 2008-02-08 14:19:02 UTC (rev 743) @@ -0,0 +1,89 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.ui.swing.actions; + +import fr.cemagref.simexplorer.is.attachment.Attachment; +import fr.cemagref.simexplorer.is.ui.StorageServiceHelper; +import fr.cemagref.simexplorer.is.ui.swing.actions.util.DownloadAbstractAction; +import fr.cemagref.simexplorer.is.ui.swing.model.DetailTabModel; + +import java.io.BufferedInputStream; +import java.io.File; + +/** + * Action pour downloader un résultat ou une librairie + * + * @author chemit + */ + at jaxx.runtime.builder.ActionConfig( + actionCommand = "downloadAttachment", + name = "simexplorer.action.application.downloadAttachment", + shortDescription = "simexplorer.action.application.downloadAttachment.tooltip", + longDescription = "simexplorer.action.application.downloadAttachment.help", + smallIcon = "action/download.png", + mnemonic = 'x', + hideActionText = true +) +public class DownloadAttachmentAction extends DownloadAbstractAction { + + protected Attachment attachment; + private static final long serialVersionUID = 595021227246573485L; + + public DownloadAttachmentAction(String name) { + super(name); + } + + @Override + protected void initModel(DetailTabModel model) { + super.initModel(model); + int selectedAttachment = model.getSelectedAttachmentIndex(); + attachment = getElement().getAttachment(selectedAttachment); + } + + protected File getFile() { + // ask user path where to save data + String dir = org.codelutin.util.FileUtil.getDirectory("select directory where todownload attachment '" + attachment.getFileName() + "'"); + if (dir == null) { + // user cancel action + return null; + } + File parent = new File(dir); + if (!parent.exists()) { + parent.mkdirs(); + } + File file; + file = new File(parent, attachment.getFileName()); + return file; + } + + protected BufferedInputStream getInputStream() { + BufferedInputStream bis; + bis = StorageServiceHelper.downloadElementData(getContext(), ((DetailTabModel) getModel()).isRemote(), getUuid(), getVersion().toString(), attachment); + //TODO bis = StorageServiceHelper.retrieveElementData(getContext(), ((DetailTabModel) getModel()).isRemote(), getUuid(), getVersion().toString(), attachment); + return bis; + } + + @Override + protected void clear() { + super.clear(); + attachment = null; + } + + +} \ No newline at end of file Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadLoggableElementAction.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadLoggableElementAction.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/DownloadLoggableElementAction.java 2008-02-08 14:19:02 UTC (rev 743) @@ -0,0 +1,74 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.ui.swing.actions; + +import fr.cemagref.simexplorer.is.ui.StorageServiceHelper; +import fr.cemagref.simexplorer.is.ui.swing.model.DetailTabModel; +import fr.cemagref.simexplorer.is.ui.swing.actions.util.DownloadAbstractAction; + +import java.io.BufferedInputStream; +import java.io.File; +import java.text.MessageFormat; + +/** + * Action pour downloader un Loggable Element + * + * @author chemit + */ + at jaxx.runtime.builder.ActionConfig( + actionCommand = "downloadLoggableElement", + name = "simexplorer.action.application.downloadLoggableElement", + shortDescription = "simexplorer.action.application.downloadLoggableElement.tooltip", + longDescription = "simexplorer.action.application.downloadLoggableElement.help", + smallIcon = "action/download.png", + mnemonic = 'x', + hideActionText = true +) +public class DownloadLoggableElementAction extends DownloadAbstractAction { + + protected static final MessageFormat FILENAME_PATTERN = new MessageFormat("{0}.zip"); + private static final long serialVersionUID = 5141501117088118393L; + + public DownloadLoggableElementAction(String name) { + super(name); + } + + protected File getFile() { + String dir = org.codelutin.util.FileUtil.getDirectory("select directory where to download a Zip of '" + getElement().getName() + "'"); + if (dir == null) { + // user cancel action + return null; + } + File parent = new File(dir); + if (!parent.exists()) { + parent.mkdirs(); + } + File file; + file = new File(parent, FILENAME_PATTERN.format(new Object[]{getElement().getName() + "-" + getVersion()})); + return file; + } + + protected BufferedInputStream getInputStream() { + BufferedInputStream bis; + bis = StorageServiceHelper.downloadElementFull(getContext(), ((DetailTabModel) getModel()).isRemote(), getUuid(), getVersion().toString()); + //TODO bis = StorageServiceHelper.retrieveElementFull(getContext(), ((DetailTabModel) getModel()).isRemote(), getUuid(), getVersion().toString()); + return bis; + } + +} \ No newline at end of file Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/util/DownloadAbstractAction.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/util/DownloadAbstractAction.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/actions/util/DownloadAbstractAction.java 2008-02-08 14:19:02 UTC (rev 743) @@ -0,0 +1,136 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.ui.swing.actions.util; + +import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; +import fr.cemagref.simexplorer.is.ui.swing.model.DetailTabModel; + +import java.awt.event.ActionEvent; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Action pour downloader un résultat ou une librairie + * + * @author chemit + */ +public abstract class DownloadAbstractAction extends SimExplorerAbstractTabAction { + + public static final int BUFFER_SIZE = 8 * 1024; + private static final long serialVersionUID = 7535647131305304738L; + + File file; + MetaData element; + String uuid; + Version version; + + protected DownloadAbstractAction(String name) { + super(name); + } + + protected abstract File getFile(); + + protected abstract BufferedInputStream getInputStream(); + + protected void initModel(DetailTabModel model) { + element = model.getDetail(); + version = model.getSelectedVersion(); + uuid = element.getUuid(); + } + + protected MetaData getElement() { + return element; + } + + protected String getUuid() { + return uuid; + } + + protected Version getVersion() { + return version; + } + + @Override + protected boolean beforeAction(ActionEvent e) throws Exception { + if (!super.beforeAction(e)) { + return false; + } + DetailTabModel model = ((DetailTabModel) getModel()); + boolean result; + result = element != null && version != null; + if (result) { + // init from model + initModel(model); + + // ask user path where to save data + file = getFile(); + if (file == null) { + return false; + } + } + return result; + } + + @Override + protected void doAction(ActionEvent e) throws Exception { + super.doAction(e); + BufferedInputStream bis = null; + try { + bis = getInputStream(); + storeStream(bis); + } finally { + if (bis != null) { + bis.close(); + } + } + } + + @Override + protected void clear() { + super.clear(); + file = null; + element = null; + version = null; + uuid = null; + } + + private void storeStream(BufferedInputStream bis) throws IOException { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); + try { + if (bis == null) { + log.warn("could not get incoming data for " + uuid); + return; + } + byte[] buffer = new byte[BUFFER_SIZE]; + while (bis.read(buffer, 0, BUFFER_SIZE) != -1) { + bos.write(buffer); + bos.flush(); + } + } finally { + if (bos != null) { + bos.flush(); + bos.close(); + } + } + } +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org