From 5f0b7679ef0cbcb1b101f71673f1e5e24e735b39 Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Wed, 2 Mar 2011 12:20:42 +0000 Subject: [PATCH] Implementation of openfile handler for RCP git-svn-id: https://svn.argeo.org/commons/trunk@4264 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../org.argeo.eclipse.dep.rcp/pom.xml | 4 +- .../runtime/org.argeo.eclipse.ui.jcr/pom.xml | 12 ++- .../ui/jcr/views/GenericJcrBrowser.java | 43 +++++++++++ .../eclipse/ui/specific/FileHandler.java | 75 +++++++++++++++++++ .../org.argeo.eclipse.ui.rcp/.classpath | 7 ++ .../runtime/org.argeo.eclipse.ui.rcp/.project | 28 +++++++ .../.settings/org.eclipse.jdt.core.prefs | 8 ++ .../runtime/org.argeo.eclipse.ui.rcp/pom.xml | 13 ++++ .../eclipse/ui/specific/FileHandler.java | 75 +++++++++++++++++++ eclipse/runtime/org.argeo.eclipse.ui/pom.xml | 3 +- .../org.argeo.jcr.ui.explorer.product | 1 + .../org.argeo.jcr.ui.explorer/plugin.xml | 8 ++ 12 files changed, 272 insertions(+), 5 deletions(-) create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/.classpath create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/.project create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/.settings/org.eclipse.jdt.core.prefs create mode 100644 eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java diff --git a/eclipse/features/org.argeo.eclipse.dep.rcp/pom.xml b/eclipse/features/org.argeo.eclipse.dep.rcp/pom.xml index 19f23bb6c..645bc9005 100644 --- a/eclipse/features/org.argeo.eclipse.dep.rcp/pom.xml +++ b/eclipse/features/org.argeo.eclipse.dep.rcp/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.argeo.commons.eclipse @@ -25,7 +26,6 @@ 0.2.3-SNAPSHOT - org.eclipse.ui org.eclipse.ui diff --git a/eclipse/runtime/org.argeo.eclipse.ui.jcr/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui.jcr/pom.xml index 3508f1757..5e42d87ad 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.jcr/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui.jcr/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.commons.eclipse @@ -32,6 +33,7 @@ lazy org.eclipse.swt, + org.argeo.eclipse.ui.specific, * org.eclipse.ui;resolution:=optional,org.eclipse.rap.ui;resolution:=optional,org.eclipse.core.runtime @@ -73,6 +75,12 @@ 0.2.3-SNAPSHOT provided + + org.argeo.commons.eclipse + org.argeo.eclipse.ui.rcp + 0.2.3-SNAPSHOT + provided + @@ -86,6 +94,6 @@ org.slf4j com.springsource.slf4j.org.apache.commons.logging - + 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 eb2362967..249617782 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,5 +1,7 @@ package org.argeo.eclipse.ui.jcr.views; +import java.io.BufferedInputStream; +import java.io.File; import java.util.Arrays; import javax.jcr.Node; @@ -7,12 +9,15 @@ import javax.jcr.Property; import javax.jcr.PropertyType; import javax.jcr.RepositoryException; +import org.apache.commons.io.IOUtils; import org.argeo.ArgeoException; +import org.argeo.eclipse.ui.dialogs.Error; import org.argeo.eclipse.ui.jcr.browser.NodeContentProvider; import org.argeo.eclipse.ui.jcr.browser.NodeLabelProvider; import org.argeo.eclipse.ui.jcr.browser.PropertiesContentProvider; import org.argeo.eclipse.ui.jcr.browser.RepositoryNode; import org.argeo.eclipse.ui.jcr.browser.WorkspaceNode; +import org.argeo.eclipse.ui.specific.FileHandler; import org.argeo.jcr.RepositoryRegister; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.viewers.ColumnLabelProvider; @@ -83,6 +88,44 @@ public class GenericJcrBrowser extends ViewPart { } else if (obj instanceof WorkspaceNode) { ((WorkspaceNode) obj).login(); nodesViewer.refresh(obj); + } // call the openFile commands on node + else if (obj instanceof Node) { + Node node = (Node) obj; + try { + if (node.isNodeType("nt:file")) { + + Node child = node.getNodes().nextNode(); + if (!child.isNodeType("nt:resource")) { + Error.show("Cannot open file children Node that are not of 'nt:resource' type."); + return; + } + BufferedInputStream fis = null; + + try { + fis = (BufferedInputStream) 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); + } catch (Exception e) { + throw new ArgeoException( + "Stream error while opening file", e); + } finally { + IOUtils.closeQuietly(fis); + } + } + } catch (RepositoryException re) { + re.printStackTrace(); + + } } } 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 new file mode 100644 index 000000000..8948e73c7 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java @@ -0,0 +1,75 @@ +package org.argeo.eclipse.ui.specific; + +import java.awt.Desktop; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; +import org.argeo.ArgeoException; + +//import org.apache.commons.io; + +public class FileHandler { + + private BufferedInputStream bis; + + public FileHandler() { + } + + public File createTmpFile(String fileName, String suffix, InputStream is) { + File tmpFile = null; + OutputStream os = null; + try { + tmpFile = File.createTempFile(fileName, suffix); + os = new FileOutputStream(tmpFile); + IOUtils.copy(is, os); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + fileName, e); + } finally { + IOUtils.closeQuietly(os); + } + return tmpFile; + } + + public void openFile(String fileName, InputStream is) { + + String prefix ="", extension = ""; + + if (fileName != null){ + int ind = fileName. + if (true){ + } + } + + prefix = .substring(0, + node.getName().lastIndexOf('.')); + extension = node.getName().substring( + node.getName().lastIndexOf('.')); + try { + Desktop desktop = null; + if (Desktop.isDesktopSupported()) { + desktop = Desktop.getDesktop(); + } + desktop.open(file); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + file.getName(), e); + } + } + + + public void openFile(File file) { + try { + Desktop desktop = null; + if (Desktop.isDesktopSupported()) { + desktop = Desktop.getDesktop(); + } + desktop.open(file); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + file.getName(), e); + } + } +} diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/.classpath b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.classpath new file mode 100644 index 000000000..8cf7f48a0 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/.project b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.project new file mode 100644 index 000000000..ef2dc2dbb --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.project @@ -0,0 +1,28 @@ + + + org.argeo.eclipse.ui.rcp + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/.settings/org.eclipse.jdt.core.prefs b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..b06d92e45 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Tue Mar 01 19:20:51 CET 2011 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml index bcaca0535..ca9ad18ef 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml @@ -36,6 +36,8 @@ org.springframework.beans.factory, org.springframework.core.io.support, + org.apache.commons.io, + org.argeo, !org.eclipse.core.runtime, !org.eclipse.core.commands, !org.eclipse.ui.plugin, @@ -53,5 +55,16 @@ org.argeo.eclipse.ui 0.2.3-SNAPSHOT + + + 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/FileHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java new file mode 100644 index 000000000..93e9d9213 --- /dev/null +++ b/eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java @@ -0,0 +1,75 @@ +package org.argeo.eclipse.ui.specific; + +import java.awt.Desktop; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.commons.io.IOUtils; +import org.argeo.ArgeoException; + +/** + * Abstraction that enable to implement runtime environment specific (typically + * RCP or RAP) methods while dealing with files in the UI. + * + */ +public class FileHandler { + + public FileHandler() { + } + + public void openFile(String fileName, InputStream is) { + + String prefix = "", extension = ""; + if (fileName != null) { + int ind = fileName.lastIndexOf('.'); + if (ind > 0) { + prefix = fileName.substring(0, ind); + extension = fileName.substring(ind); + } + } + + File file = createTmpFile(prefix, extension, is); + + try { + Desktop desktop = null; + if (Desktop.isDesktopSupported()) { + desktop = Desktop.getDesktop(); + } + desktop.open(file); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + file.getName(), e); + } + } + + public void openFile(File file) { + try { + Desktop desktop = null; + if (Desktop.isDesktopSupported()) { + desktop = Desktop.getDesktop(); + } + desktop.open(file); + } catch (IOException e) { + throw new ArgeoException("Cannot open file " + file.getName(), e); + } + } + + 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/pom.xml b/eclipse/runtime/org.argeo.eclipse.ui/pom.xml index a11e1b316..9a271f19a 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui/pom.xml +++ b/eclipse/runtime/org.argeo.eclipse.ui/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.commons.eclipse diff --git a/server/plugins/org.argeo.jcr.ui.explorer/org.argeo.jcr.ui.explorer.product b/server/plugins/org.argeo.jcr.ui.explorer/org.argeo.jcr.ui.explorer.product index dad5f1873..885f079c3 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/org.argeo.jcr.ui.explorer.product +++ b/server/plugins/org.argeo.jcr.ui.explorer/org.argeo.jcr.ui.explorer.product @@ -116,6 +116,7 @@ + diff --git a/server/plugins/org.argeo.jcr.ui.explorer/plugin.xml b/server/plugins/org.argeo.jcr.ui.explorer/plugin.xml index ffe7aaffa..8288ab5d7 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/plugin.xml +++ b/server/plugins/org.argeo.jcr.ui.explorer/plugin.xml @@ -61,6 +61,14 @@ id="org.argeo.jcr.ui.explorer.importFileSystem" name="Import files..."> + + + + +