From 5a00c032d8c7b9617777db83b0a6d60ab6b74d18 Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Fri, 4 Mar 2011 22:40:56 +0000 Subject: [PATCH] Implementation of file handlers for both RCP & RAP git-svn-id: https://svn.argeo.org/commons/trunk@4271 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../org.argeo.eclipse.dep.common/pom.xml | 5 +- .../org.argeo.eclipse.dep.rap/pom.xml | 29 ++- .../org/argeo/eclipse/ui/jcr/JcrUiPlugin.java | 12 + .../eclipse/ui/jcr/commands/DeleteNode.java | 15 +- .../ui/jcr/views/GenericJcrBrowser.java | 85 ++++++- .../jcr/wizards/ImportFileSystemWizard.java | 232 ++++++++++-------- .../runtime/org.argeo.eclipse.ui.rap/pom.xml | 20 +- .../eclipse/ui/specific/FileHandler.java | 111 ++++++++- .../ui/specific/ImportFileSystemHandler.java | 7 + .../specific/ImportFileSystemWizardPage.java | 106 ++++++++ .../runtime/org.argeo.eclipse.ui.rcp/pom.xml | 10 + .../ui/specific/ImportFileSystemHandler.java | 10 + .../specific/ImportFileSystemWizardPage.java | 46 ++++ 13 files changed, 556 insertions(+), 132 deletions(-) create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java diff --git a/eclipse/features/org.argeo.eclipse.dep.common/pom.xml b/eclipse/features/org.argeo.eclipse.dep.common/pom.xml index 8846063f4..2fd61558b 100644 --- a/eclipse/features/org.argeo.eclipse.dep.common/pom.xml +++ b/eclipse/features/org.argeo.eclipse.dep.common/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.argeo.commons.eclipse @@ -46,5 +47,7 @@ org.eclipse.equinox org.eclipse.equinox.security + + diff --git a/eclipse/features/org.argeo.eclipse.dep.rap/pom.xml b/eclipse/features/org.argeo.eclipse.dep.rap/pom.xml index d8c92b521..328856e3e 100644 --- a/eclipse/features/org.argeo.eclipse.dep.rap/pom.xml +++ b/eclipse/features/org.argeo.eclipse.dep.rap/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.argeo.commons.eclipse @@ -24,6 +25,19 @@ org.argeo.eclipse.dep.common 0.2.3-SNAPSHOT + + + + org.argeo.dep.rap + org.eclipse.rwt.widgets.upload + + + + org.apache.commons + com.springsource.org.apache.commons.io + + org.eclipse.rap @@ -33,10 +47,6 @@ javax.servlet com.springsource.javax.servlet - - - - org.eclipse.equinox org.eclipse.equinox.http.registry @@ -65,14 +75,7 @@ org.eclipse.rap org.eclipse.rap.ui.views - - - - - - - - + org.eclipse.core org.eclipse.core.databinding diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrUiPlugin.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrUiPlugin.java index bc409830e..a53368296 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrUiPlugin.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/JcrUiPlugin.java @@ -2,10 +2,22 @@ package org.argeo.eclipse.ui.jcr; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; public class JcrUiPlugin extends AbstractUIPlugin { public final static String ID = "org.argeo.eclipse.ui.jcr"; + private static JcrUiPlugin plugin; + + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + public static JcrUiPlugin getDefault() { + return plugin; + } + public static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(ID, path); } diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/commands/DeleteNode.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/commands/DeleteNode.java index 93304fd70..5133b04b6 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/commands/DeleteNode.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/commands/DeleteNode.java @@ -4,6 +4,8 @@ import java.util.Iterator; import javax.jcr.Node; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.eclipse.ui.dialogs.Error; import org.argeo.eclipse.ui.jcr.views.GenericJcrBrowser; import org.eclipse.core.commands.AbstractHandler; @@ -15,6 +17,7 @@ import org.eclipse.ui.handlers.HandlerUtil; /** Deletes the selected nodes */ public class DeleteNode extends AbstractHandler { + private static Log log = LogFactory.getLog(DeleteNode.class); public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event) @@ -24,7 +27,6 @@ public class DeleteNode extends AbstractHandler { .findView(HandlerUtil.getActivePartId(event)); if (selection != null && selection instanceof IStructuredSelection) { Iterator it = ((IStructuredSelection) selection).iterator(); - Object obj = null; try { while (it.hasNext()) { @@ -32,9 +34,17 @@ public class DeleteNode extends AbstractHandler { if (obj instanceof Node) { Node node = (Node) obj; Node parentNode = node.getParent(); + log.debug("Node ids : node :" + node.getIdentifier() + + " - pNode : " + parentNode.getIdentifier()); node.remove(); - view.nodeRemoved(parentNode); + + // Postpone the refresh after the session.save + // view.nodeRemoved(parentNode); + node.getSession().save(); + if (log.isDebugEnabled()) + log.debug("session saved"); + view.nodeRemoved(parentNode); } } } catch (Exception e) { @@ -43,5 +53,4 @@ public class DeleteNode extends AbstractHandler { } return null; } - } diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java index 249617782..414cb010a 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java @@ -1,8 +1,9 @@ package org.argeo.eclipse.ui.jcr.views; -import java.io.BufferedInputStream; -import java.io.File; +import java.io.InputStream; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import javax.jcr.Node; import javax.jcr.Property; @@ -10,6 +11,8 @@ import javax.jcr.PropertyType; import javax.jcr.RepositoryException; import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; import org.argeo.eclipse.ui.dialogs.Error; import org.argeo.eclipse.ui.jcr.browser.NodeContentProvider; @@ -26,6 +29,7 @@ import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; import org.eclipse.jface.viewers.TreeViewer; @@ -39,6 +43,8 @@ import org.eclipse.swt.widgets.Menu; import org.eclipse.ui.part.ViewPart; public class GenericJcrBrowser extends ViewPart { + private static Log log = LogFactory.getLog(GenericJcrBrowser.class); + private TreeViewer nodesViewer; private TableViewer propertiesViewer; @@ -99,22 +105,21 @@ public class GenericJcrBrowser extends ViewPart { Error.show("Cannot open file children Node that are not of 'nt:resource' type."); return; } - BufferedInputStream fis = null; + InputStream fis = null; try { - fis = (BufferedInputStream) child + fis = (InputStream) child .getProperty("jcr:data").getBinary() .getStream(); String name = node.getName(); - + // Instantiate the generic object that fits for // both // RCP & RAP. FileHandler fh = new FileHandler(); - fh.openFile(name, - fis); - //fh.openFile(file); + fh.openFile(name, fis); + // fh.openFile(file); } catch (Exception e) { throw new ArgeoException( "Stream error while opening file", e); @@ -229,7 +234,69 @@ public class GenericJcrBrowser extends ViewPart { } public void nodeRemoved(Node parentNode) { - nodesViewer.refresh(parentNode); + + List al = new ArrayList(); + al.add(parentNode); + + IStructuredSelection newSel = new StructuredSelection(al); + // IStructuredSelection newSel = new StructuredSelection(parentNode); + + if (log.isDebugEnabled()) + log.debug("new selection size = " + newSel.size()); + + nodesViewer.setSelection(newSel, true); + IStructuredSelection tmpSel = (IStructuredSelection) nodesViewer + .getSelection(); + + if (log.isDebugEnabled()) + log.debug("set selection size = " + tmpSel.size()); + + nodesViewer.refresh(); + + // + // log.debug(" Class selected (Parent 1ST element) : " + // + tmpSel.getFirstElement().getClass()); + // setFocus(); + // + // nodesViewer.refresh(parentNode); + + // // Call the refresh node command + // try { + // IWorkbench iw = JcrUiPlugin.getDefault().getWorkbench(); + // IHandlerService handlerService = (IHandlerService) iw + // .getService(IHandlerService.class); + // + // // get the command from plugin.xml + // IWorkbenchWindow window = iw.getActiveWorkbenchWindow(); + // ICommandService cmdService = (ICommandService) window + // .getService(ICommandService.class); + // Command cmd = cmdService + // .getCommand(OpenEbiDetailsEditor.COMMAND_NAME); + // + // // log.debug("cmd : " + cmd); + // ArrayList parameters = new + // ArrayList(); + // + // // get the parameter + // IParameter iparam = cmd + // .getParameter(OpenEbiDetailsEditor.PARAM_UUID); + // + // Parameterization params = new Parameterization(iparam, + // ((String[]) obj)[0]); + // parameters.add(params); + // + // // build the parameterized command + // ParameterizedCommand pc = new ParameterizedCommand(cmd, + // parameters.toArray(new Parameterization[parameters.size()])); + // + // // execute the command + // handlerService = (IHandlerService) window + // .getService(IHandlerService.class); + // handlerService.executeCommand(pc, null); + // + // } catch (Exception e) { + // throw new ArgeoException("Error opening EBI", e); + // } } public void setRepositoryRegister(RepositoryRegister repositoryRegister) { diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/wizards/ImportFileSystemWizard.java b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/wizards/ImportFileSystemWizard.java index b10b066d4..14a301749 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/wizards/ImportFileSystemWizard.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/wizards/ImportFileSystemWizard.java @@ -9,17 +9,17 @@ import javax.jcr.Property; import javax.jcr.nodetype.NodeType; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; import org.argeo.eclipse.ui.dialogs.Error; +import org.argeo.eclipse.ui.specific.ImportFileSystemHandler; +import org.argeo.eclipse.ui.specific.ImportFileSystemWizardPage; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; -import org.eclipse.jface.preference.DirectoryFieldEditor; import org.eclipse.jface.wizard.Wizard; -import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.swt.widgets.Composite; public class ImportFileSystemWizard extends Wizard { private final static Log log = LogFactory @@ -28,9 +28,11 @@ public class ImportFileSystemWizard extends Wizard { private ImportFileSystemWizardPage page1; private final Node folder; + private ImportFileSystemHandler ifsh = new ImportFileSystemHandler(); + public ImportFileSystemWizard(Node folder) { this.folder = folder; - setNeedsProgressMonitor(true); + setNeedsProgressMonitor(ifsh.getNeedsProgressMonitor()); setWindowTitle("Import from file system"); } @@ -40,62 +42,99 @@ public class ImportFileSystemWizard extends Wizard { addPage(page1); } + /** + * Called when the user click on 'Finish' in the wizard. The real upload to + * the JCR repository is done here. + */ @Override public boolean performFinish() { - final String directory = page1.getDirectory(); - if (directory == null || !new File(directory).exists()) { - Error.show("Directory " + directory + " does not exist"); - return false; - } - Boolean failed = false; - final File dir = new File(directory).getAbsoluteFile(); - final Long sizeB = directorySize(dir, 0l); - final Stats stats = new Stats(); - Long begin = System.currentTimeMillis(); - try { - getContainer().run(true, true, new IRunnableWithProgress() { - public void run(IProgressMonitor monitor) { - try { - Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB); - monitor.beginTask("", sizeKB); - importDirectory(folder, dir, monitor, stats); - monitor.done(); - } catch (Exception e) { - if (e instanceof RuntimeException) - throw (RuntimeException) e; - else - throw new ArgeoException("Cannot import " - + directory, e); - } + // Initialization + final String objectType = page1.getObjectType(); + final String objectPath = page1.getObjectPath(); + + // We do not display a progress bar for one file only + if ("nt:file".equals(objectType)) { + // In Rap we must force the "real" upload of the file + page1.performFinish(); + try { + Node fileNode = folder.addNode(page1.getObjectName(), + NodeType.NT_FILE); + Node resNode = fileNode.addNode(Property.JCR_CONTENT, + NodeType.NT_RESOURCE); + Binary binary = null; + try { + binary = folder.getSession().getValueFactory() + .createBinary(page1.getFileInputStream()); + resNode.setProperty(Property.JCR_DATA, binary); + } finally { + if (binary != null) + binary.dispose(); + IOUtils.closeQuietly(page1.getFileInputStream()); } - }); - } catch (Exception e) { - Error.show("Cannot import " + directory, e); - failed = true; + folder.getSession().save(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } else if ("nt:folder".equals(objectType)) { + if (objectPath == null || !new File(objectPath).exists()) { + Error.show("Directory " + objectPath + " does not exist"); + return false; + } + + Boolean failed = false; + final File dir = new File(objectPath).getAbsoluteFile(); + final Long sizeB = directorySize(dir, 0l); + final Stats stats = new Stats(); + Long begin = System.currentTimeMillis(); + try { + getContainer().run(true, true, new IRunnableWithProgress() { + public void run(IProgressMonitor monitor) { + try { + Integer sizeKB = (int) (sizeB / FileUtils.ONE_KB); + monitor.beginTask("", sizeKB); + importDirectory(folder, dir, monitor, stats); + monitor.done(); + } catch (Exception e) { + if (e instanceof RuntimeException) + throw (RuntimeException) e; + else + throw new ArgeoException("Cannot import " + + objectPath, e); + } + } + }); + } catch (Exception e) { + Error.show("Cannot import " + objectPath, e); + failed = true; + } + + Long duration = System.currentTimeMillis() - begin; + Long durationS = duration / 1000l; + String durationStr = (durationS / 60) + " min " + (durationS % 60) + + " s"; + StringBuffer message = new StringBuffer("Imported\n"); + message.append(stats.fileCount).append(" files\n"); + message.append(stats.dirCount).append(" directories\n"); + message.append(FileUtils.byteCountToDisplaySize(stats.sizeB)); + if (failed) + message.append(" of planned ").append( + FileUtils.byteCountToDisplaySize(sizeB)); + message.append("\n"); + message.append("in ").append(durationStr).append("\n"); + if (failed) + MessageDialog.openError(getShell(), "Import failed", + message.toString()); + else + MessageDialog.openInformation(getShell(), "Import successful", + message.toString()); + + return true; } + return false; - Long duration = System.currentTimeMillis() - begin; - Long durationS = duration / 1000l; - String durationStr = (durationS / 60) + " min " + (durationS % 60) - + " s"; - StringBuffer message = new StringBuffer("Imported\n"); - message.append(stats.fileCount).append(" files\n"); - message.append(stats.dirCount).append(" directories\n"); - message.append(FileUtils.byteCountToDisplaySize(stats.sizeB)); - if (failed) - message.append(" of planned ").append( - FileUtils.byteCountToDisplaySize(sizeB)); - message.append("\n"); - message.append("in ").append(durationStr).append("\n"); - if (failed) - MessageDialog.openError(getShell(), "Import failed", - message.toString()); - else - MessageDialog.openInformation(getShell(), "Import successful", - message.toString()); - - return true; } /** Recursively computes the size of the directory in bytes. */ @@ -112,7 +151,9 @@ public class ImportFileSystemWizard extends Wizard { return size; } - /** Recursively computes the size of the directory in bytes. */ + /** + * Import recursively a directory and its content to the repository. + */ protected void importDirectory(Node folder, File dir, IProgressMonitor monitor, Stats stats) { try { @@ -126,33 +167,46 @@ public class ImportFileSystemWizard extends Wizard { stats.dirCount++; } else { Long fileSize = file.length(); - monitor.subTask(file.getName() + " (" - + FileUtils.byteCountToDisplaySize(fileSize) + ") " - + file.getCanonicalPath()); - try { - Node fileNode = folder.addNode(file.getName(), - NodeType.NT_FILE); - Node resNode = fileNode.addNode(Property.JCR_CONTENT, - NodeType.NT_RESOURCE); - Binary binary = null; + + // we skip tempory files that are created by apps when a + // file is being edited. + // TODO : make this configurable. + if (file.getName().lastIndexOf('~') != file.getName() + .length() - 1) { + + monitor.subTask(file.getName() + " (" + + FileUtils.byteCountToDisplaySize(fileSize) + + ") " + file.getCanonicalPath()); try { - binary = folder.getSession().getValueFactory() - .createBinary(new FileInputStream(file)); - resNode.setProperty(Property.JCR_DATA, binary); - } finally { - if (binary != null) - binary.dispose(); + Node fileNode = folder.addNode(file.getName(), + NodeType.NT_FILE); + Node resNode = fileNode.addNode( + Property.JCR_CONTENT, NodeType.NT_RESOURCE); + Binary binary = null; + try { + binary = folder + .getSession() + .getValueFactory() + .createBinary(new FileInputStream(file)); + resNode.setProperty(Property.JCR_DATA, binary); + } finally { + if (binary != null) + binary.dispose(); + } + folder.getSession().save(); + stats.fileCount++; + stats.sizeB = stats.sizeB + fileSize; + } catch (Exception e) { + log.warn("Import of " + + file + + " (" + + FileUtils + .byteCountToDisplaySize(fileSize) + + ") failed: " + e); + folder.getSession().refresh(false); } - folder.getSession().save(); - stats.fileCount++; - stats.sizeB = stats.sizeB + fileSize; - } catch (Exception e) { - log.warn("Import of " + file + " (" - + FileUtils.byteCountToDisplaySize(fileSize) - + ") failed: " + e); - folder.getSession().refresh(false); + monitor.worked((int) (fileSize / FileUtils.ONE_KB)); } - monitor.worked((int) (fileSize / FileUtils.ONE_KB)); } } } catch (Exception e) { @@ -161,26 +215,6 @@ public class ImportFileSystemWizard extends Wizard { } } - protected class ImportFileSystemWizardPage extends WizardPage { - private DirectoryFieldEditor dfe; - - public ImportFileSystemWizardPage() { - super("Import from file system"); - setDescription("Import files from the local file system into the JCR repository"); - } - - public void createControl(Composite parent) { - dfe = new DirectoryFieldEditor("directory", "From", - parent); - setControl(dfe.getTextControl(parent)); - } - - public String getDirectory() { - return dfe.getStringValue(); - } - - } - static class Stats { public Long fileCount = 0l; public Long dirCount = 0l; diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui.rap/pom.xml index a852e57d1..9a92853bd 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rap/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/pom.xml @@ -34,11 +34,10 @@ org.argeo.eclipse.ui.ArgeoUiPlugin org.eclipse.rap.ui,org.eclipse.core.runtime - org.springframework.beans.factory, - org.springframework.core.io.support, org.apache.commons.io, org.argeo, - org.argeo.eclipse.ui.dialogs, + org.springframework.beans.factory, + org.springframework.core.io.support, !org.eclipse.core.runtime, !org.eclipse.core.commands, !org.eclipse.ui.plugin, @@ -49,6 +48,9 @@ + + @@ -56,10 +58,18 @@ org.argeo.eclipse.ui 0.2.3-SNAPSHOT + + - org.apache.commons - com.springsource.org.apache.commons.io + org.argeo.commons.eclipse + org.argeo.eclipse.dep.rap + ${version.argeo-commons} + + + org.slf4j + com.springsource.slf4j.org.apache.commons.logging + diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java index 45905f6be..2058328a3 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java @@ -1,21 +1,128 @@ package org.argeo.eclipse.ui.specific; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; -//import org.argeo.eclipse.ui.dialogs.Error; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.ArgeoException; +import org.eclipse.rwt.RWT; public class FileHandler { + private static Log log = LogFactory.getLog(FileHandler.class); + public FileHandler() { } public void openFile(String fileName, InputStream is) { - // org.argeo.eclipse.ui.dialogs.Error.show("In Open File for RAP."); + + // // Which file to download? + // String fileName = RWT.getRequest().getParameter( "filename" ); + // // Get the file content + // byte[] download = MyDataStore.getByteArrayData( fileName ); + // // Send the file in the response + // HttpServletResponse response = RWT.getResponse(); + // response.setContentType( "application/octet-stream" ); + // response.setContentLength( download.length ); + // String contentDisposition = "attachment; filename=\"" + fileName + + // "\""; + // response.setHeader( "Content-Disposition", contentDisposition ); + // try { + // response.getOutputStream().write( download ); + // } catch( IOException e1 ) { + // e1.printStackTrace(); + // } + // + // + + try { + + // / workaround : create a tmp file. + String prefix = "", suffix = ""; + if (fileName != null) { + int ind = fileName.lastIndexOf('.'); + if (ind > 0) { + prefix = fileName.substring(0, ind); + suffix = fileName.substring(ind); + } + } + + File tmpFile = createTmpFile(prefix, suffix, is); + + // Send the file in the response + HttpServletResponse response = RWT.getResponse(); + byte[] ba = null; + ba = FileUtils.readFileToByteArray(tmpFile); + + response.setContentLength(ba.length); + + // String contentDisposition = "attachment; filename=\"" + fileName + // + "\""; + String contentDisposition = "attachment; filename=\"" + fileName + + "\""; + response.setContentType("application/force-download"); + response.setHeader("Content-Disposition", contentDisposition); + response.setHeader("Content-Transfer-Encoding", "binary"); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Cache-Control", "no-cache, must-revalidate"); + + // must-revalidate"); + + if (log.isDebugEnabled()) { + log.debug("Header Set "); + } + + // header("Content-Type: application/force-download; name=\"".$localName."\""); + // 852 header("Content-Transfer-Encoding: binary"); + // 853 if($gzip){ + // 854 header("Content-Encoding: gzip"); + // 855 // If gzip, recompute data size! + // 856 $gzippedData = + // ($data?gzencode($filePathOrData,9):gzencode(file_get_contents($filePathOrData), + // 9)); + // 857 $size = strlen($gzippedData); + // 858 } + // 859 header("Content-Length: ".$size); + // 860 if ($isFile && ($size != 0)) header("Content-Range: bytes 0-" + // . ($size - 1) . "/" . $size . ";"); + // 861 + // header("Content-Disposition: attachment; filename=\"".$localName."\""); + // 862 header("Expires: 0"); + // 863 header("Cache-Control: no-cache, must-revalidate"); + // 864 header("Pragma: no-cache"); + + // IOUtils.copy(is, response.getOutputStream()); + response.getOutputStream().write(ba); + // Error.show("In Open File for RAP."); + } catch (IOException ioe) { + + throw new ArgeoException("Cannot copy input stream from file " + + fileName + " to HttpServletResponse", ioe); + } + } private File createTmpFile(String prefix, String suffix, InputStream is) { File tmpFile = null; + OutputStream os = null; + try { + tmpFile = File.createTempFile(prefix, suffix); + os = new FileOutputStream(tmpFile); + IOUtils.copy(is, os); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + prefix + "." + + suffix, e); + } finally { + IOUtils.closeQuietly(os); + } return tmpFile; } diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java new file mode 100644 index 000000000..607d113d9 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java @@ -0,0 +1,7 @@ +package org.argeo.eclipse.ui.specific; + +public class ImportFileSystemHandler { + public boolean getNeedsProgressMonitor() { + return false; + } +} diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java new file mode 100644 index 000000000..829ceda6a --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java @@ -0,0 +1,106 @@ +package org.argeo.eclipse.ui.specific; + +import java.io.InputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.ArgeoException; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.rwt.widgets.Upload; +import org.eclipse.rwt.widgets.UploadAdapter; +import org.eclipse.rwt.widgets.UploadEvent; +import org.eclipse.rwt.widgets.UploadItem; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; + +public class ImportFileSystemWizardPage extends WizardPage { + private Log log = LogFactory.getLog(ImportFileSystemWizardPage.class); + + private Upload uploadFile; + + public ImportFileSystemWizardPage() { + super("Import from file system"); + setDescription("Import files from the local file system into the JCR repository"); + } + + public void createControl(Composite parent) { + + Composite composite = new Composite(parent, SWT.NONE); + composite.setLayout(new GridLayout(2, false)); + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); + + new Label(composite, SWT.NONE).setText("Pick up a file"); + + uploadFile = new Upload(composite, SWT.BORDER); + uploadFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); + uploadFile.setBrowseButtonText("Open..."); + uploadFile.addUploadListener(new UploadManager(uploadFile)); + + setControl(composite); + } + + public String getObjectPath() { + // NOTE Returns the full file name of the last uploaded file including + // the file path as selected by the user on his local machine. + // The full path including the directory and file drive are only + // returned, if the browser supports reading this properties. In Firefox + // 3, only the filename is returned. + return uploadFile.getPath(); + } + + public String getObjectName() { + return uploadFile.getUploadItem().getFileName(); + } + + public String getObjectType() { + return "nt:file"; + } + + public void performFinish() { + boolean success = uploadFile.performUpload(); + if (!success) + throw new ArgeoException("Cannot upload file named " + + uploadFile.getPath()); + } + + protected void handleUploadFinished(final Upload upload) { + if (log.isTraceEnabled()) { + UploadItem uploadItem = upload.getUploadItem(); + log.trace("filename : " + uploadItem.getFileName()); + log.trace("content-type : " + uploadItem.getContentType()); + } + } + + public InputStream getFileInputStream() { + return uploadFile.getUploadItem().getFileInputStream(); + } + + private class UploadManager extends UploadAdapter { + private Upload upload; + + public UploadManager(Upload upload) { + super(); + this.upload = upload; + } + + public void uploadFinished(UploadEvent uploadEvent) { + handleUploadFinished(upload); + } + + public void uploadInProgress(UploadEvent uploadEvent) { + } + + public void uploadException(UploadEvent uploadEvent) { + Exception exc = uploadEvent.getUploadException(); + if (exc != null) { + MessageDialog.openError(Display.getCurrent().getActiveShell(), + "Error", exc.getMessage()); + } + } + } +} diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml index ca9ad18ef..65f75a217 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml @@ -56,15 +56,25 @@ 0.2.3-SNAPSHOT + + + org.argeo.commons.eclipse + org.argeo.eclipse.dep.rcp + ${version.argeo-commons} + + + org.argeo.commons.basic org.argeo.basic.nodeps 0.2.3-SNAPSHOT + org.apache.commons com.springsource.org.apache.commons.io + \ No newline at end of file diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java new file mode 100644 index 000000000..0316c3950 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemHandler.java @@ -0,0 +1,10 @@ +package org.argeo.eclipse.ui.specific; + +public class ImportFileSystemHandler { + public boolean getNeedsProgressMonitor() { + return true; + } + + + +} diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java new file mode 100644 index 000000000..27f081172 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java @@ -0,0 +1,46 @@ +package org.argeo.eclipse.ui.specific; + +import java.io.InputStream; + +import org.eclipse.jface.preference.DirectoryFieldEditor; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.widgets.Composite; + +public class ImportFileSystemWizardPage extends WizardPage { + private DirectoryFieldEditor dfe; + + public ImportFileSystemWizardPage() { + super("Import from file system"); + setDescription("Import files from the local file system into the JCR repository"); + } + + public void createControl(Composite parent) { + dfe = new DirectoryFieldEditor("directory", "From", parent); + setControl(dfe.getTextControl(parent)); + } + + public String getObjectPath() { + System.out.println("dfe.getStringValue() : " + dfe.getStringValue()); + return dfe.getStringValue(); + } + + public String getObjectType() { + return "nt:folder"; + } + + // Dummy methods : useless in RCP context but useful for RAP + /** WARNING : always return null in RCP context */ + public String getObjectName() { + return null; + } + + /** WARNING : di nothing in RCP context */ + public void performFinish() { + } + + /** WARNING : always return null in RCP context */ + public InputStream getFileInputStream() { + return null; + } + +} -- 2.30.2