Implementation of openfile handler for RCP
authorBruno Sinou <bsinou@argeo.org>
Wed, 2 Mar 2011 12:20:42 +0000 (12:20 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 2 Mar 2011 12:20:42 +0000 (12:20 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4264 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

12 files changed:
eclipse/features/org.argeo.eclipse.dep.rcp/pom.xml
eclipse/runtime/org.argeo.eclipse.ui.jcr/pom.xml
eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/views/GenericJcrBrowser.java
eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rcp/.classpath [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rcp/.project [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rcp/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rcp/pom.xml
eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui/pom.xml
server/plugins/org.argeo.jcr.ui.explorer/org.argeo.jcr.ui.explorer.product
server/plugins/org.argeo.jcr.ui.explorer/plugin.xml

index 19f23bb6c6088a64a9aac3e7ea69fdcaa4093990..645bc90055a8d6bcd1d24ff1dcce5f8453b681ff 100644 (file)
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.commons.eclipse</groupId>
@@ -25,7 +26,6 @@
                        <version>0.2.3-SNAPSHOT</version>
                </dependency>
 
-               <!-- RCP Only -->
                <dependency>
                        <groupId>org.eclipse.ui</groupId>
                        <artifactId>org.eclipse.ui</artifactId>
index 3508f175742438110d507273dd111cebd433522f..5e42d87adc7b34d7764b160fc6041e8652a38709 100644 (file)
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.commons.eclipse</groupId>
@@ -32,6 +33,7 @@
                                                <Bundle-ActivationPolicy>lazy</Bundle-ActivationPolicy>
                                                <Import-Package>
                                                        org.eclipse.swt,
+                                                       org.argeo.eclipse.ui.specific,
                                                        *
                                                </Import-Package>
                                                <Require-Bundle>org.eclipse.ui;resolution:=optional,org.eclipse.rap.ui;resolution:=optional,org.eclipse.core.runtime</Require-Bundle>
                        <version>0.2.3-SNAPSHOT</version>
                        <scope>provided</scope>
                </dependency>
+               <dependency>
+                       <groupId>org.argeo.commons.eclipse</groupId>
+                       <artifactId>org.argeo.eclipse.ui.rcp</artifactId>
+                       <version>0.2.3-SNAPSHOT</version>
+                       <scope>provided</scope>
+               </dependency>
 
                <!-- Argeo Commons -->
                <dependency>
@@ -86,6 +94,6 @@
                        <groupId>org.slf4j</groupId>
                        <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
                </dependency>
-               
+
        </dependencies>
 </project>
index eb23629673f4c367f7fef24847eabc2cdd555c13..2496177824088a4404888cbbd47aae8e82263620 100644 (file)
@@ -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 (file)
index 0000000..8948e73
--- /dev/null
@@ -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 (file)
index 0000000..8cf7f48
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src/main/java"/>
+       <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rcp/.project b/eclipse/runtime/org.argeo.eclipse.ui.rcp/.project
new file mode 100644 (file)
index 0000000..ef2dc2d
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.argeo.eclipse.ui.rcp</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
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 (file)
index 0000000..b06d92e
--- /dev/null
@@ -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
index bcaca053579ba2d56ddbba64912df56d3a835777..ca9ad18ef3b8d7121d781c6a243e11150efb7eac 100644 (file)
@@ -36,6 +36,8 @@
                                                <Import-Package>
                                                        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,
                        <artifactId>org.argeo.eclipse.ui</artifactId>
                        <version>0.2.3-SNAPSHOT</version>
                </dependency>
+
+               <dependency>
+                       <groupId>org.argeo.commons.basic</groupId>
+                       <artifactId>org.argeo.basic.nodeps</artifactId>
+                       <version>0.2.3-SNAPSHOT</version>
+               </dependency>
+
+               <dependency>
+                       <groupId>org.apache.commons</groupId>
+                       <artifactId>com.springsource.org.apache.commons.io</artifactId>
+               </dependency>
        </dependencies>
 </project>
\ 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 (file)
index 0000000..93e9d92
--- /dev/null
@@ -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;
+       }
+
+}
index a11e1b316b9e01bdc49523170634b6be00a8fb27..9a271f19aeb7c67aedc2267facfeb3da3a034e5d 100644 (file)
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.commons.eclipse</groupId>
index dad5f187399d614d8ec01dce80fa9ace281f7ce5..885f079c39b29192fd5436fa323a8d53c855da3c 100644 (file)
       <plugin id="org.argeo.dep.osgi.tika"/>
       <plugin id="org.argeo.eclipse.ui"/>
       <plugin id="org.argeo.eclipse.ui.jcr"/>
+      <plugin id="org.argeo.eclipse.ui.rcp"/>
       <plugin id="org.argeo.jackrabbit.webapp"/>
       <plugin id="org.argeo.jcr.ui.explorer"/>
       <plugin id="org.argeo.node.repo.jackrabbit"/>
index ffe7aaffa159291b52000285637cd798e81a8800..8288ab5d725223cd07fe18d85778579a839ad50c 100644 (file)
         id="org.argeo.jcr.ui.explorer.importFileSystem"
         name="Import files...">
   </command>
+  <!-- TEST --> 
+  <command
+        defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+        id="org.argeo.jcr.ui.explorer.openFile"
+        name="Open current file">
+  </command>
+  <!-- TEST END --> 
+  
     </extension>
        <extension point="org.eclipse.ui.menus">
                <menuContribution