You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hop.apache.org by ha...@apache.org on 2021/02/25 21:49:21 UTC

[incubator-hop] branch master updated: HOP-2204 export current project to zip, initial version

This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hop.git


The following commit(s) were added to refs/heads/master by this push:
     new 6e6d261  HOP-2204 export current project to zip, initial version
     new ba93a81  Merge pull request #645 from bamaer/HOP-2204
6e6d261 is described below

commit 6e6d261a1a4f0f85c5d9448e62ecec8da178ba14
Author: Bart Maertens <ba...@know.bi>
AuthorDate: Thu Feb 25 20:44:13 2021 +0100

    HOP-2204 export current project to zip, initial version
---
 .../apache/hop/projects/gui/ProjectsGuiPlugin.java | 106 +++++++++++++++++++++
 .../misc/projects/src/main/resources/export.svg    |  14 +++
 .../gui/messages/messages_en_US.properties         |   1 +
 3 files changed, 121 insertions(+)

diff --git a/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java b/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
index 873cd50..8d3bc1a6b 100644
--- a/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
+++ b/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
@@ -24,6 +24,7 @@ import org.apache.hop.core.exception.HopException;
 import org.apache.hop.core.extension.ExtensionPointHandler;
 import org.apache.hop.core.extension.HopExtensionPoint;
 import org.apache.hop.core.gui.plugin.GuiPlugin;
+import org.apache.hop.core.gui.plugin.menu.GuiMenuElement;
 import org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElement;
 import org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElementType;
 import org.apache.hop.core.logging.ILogChannel;
@@ -46,7 +47,9 @@ import org.apache.hop.projects.project.ProjectConfig;
 import org.apache.hop.projects.project.ProjectDialog;
 import org.apache.hop.projects.util.ProjectsUtil;
 import org.apache.hop.ui.core.bus.HopGuiEvents;
+import org.apache.hop.ui.core.dialog.BaseDialog;
 import org.apache.hop.ui.core.dialog.ErrorDialog;
+import org.apache.hop.ui.core.gui.GuiResource;
 import org.apache.hop.ui.core.gui.GuiToolbarWidgets;
 import org.apache.hop.ui.core.gui.HopNamespace;
 import org.apache.hop.ui.core.vfs.HopVfsFileDialog;
@@ -54,11 +57,19 @@ import org.apache.hop.ui.hopgui.HopGui;
 import org.apache.hop.ui.pipeline.dialog.PipelineExecutionConfigurationDialog;
 import org.apache.hop.workflow.config.WorkflowRunConfiguration;
 import org.apache.hop.workflow.engines.local.LocalWorkflowRunConfiguration;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.MessageBox;
+import org.eclipse.swt.widgets.Shell;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
@@ -68,6 +79,8 @@ import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 @GuiPlugin
 public class ProjectsGuiPlugin {
@@ -84,6 +97,8 @@ public class ProjectsGuiPlugin {
   public static final String ID_TOOLBAR_ENVIRONMENT_ADD = "toolbar-50030-environment-add";
   public static final String ID_TOOLBAR_ENVIRONMENT_DELETE = "toolbar-50040-environment-delete";
 
+  public static final String ID_MAIN_MENU_PROJECT_EXPORT = "10055-menu-file-export-to-svg";
+
   public static final String NAVIGATE_TOOLBAR_PARENT_ID = "HopVfsFileDialog-NavigateToolbar";
   private static final String NAVIGATE_ITEM_ID_NAVIGATE_PROJECT_HOME =
       "0005-navigate-project-home"; // right next to Home button
@@ -852,4 +867,95 @@ public class ProjectsGuiPlugin {
       }
     }
   }
+
+  @GuiMenuElement(
+          root = HopGui.ID_MAIN_MENU,
+          id = ID_MAIN_MENU_PROJECT_EXPORT,
+          label = "i18n::HopGui.FileMenu.Project.Export.Label",
+          image = "export.svg",
+          parentId = HopGui.ID_MAIN_MENU_FILE,
+          separator = false
+  )
+  public void menuProjectExport() {
+    HopGui hopGui = HopGui.getInstance();
+    Shell shell = hopGui.getShell();
+
+    String zipFilename = BaseDialog.presentFileDialog(true, shell, new String[]{"*.zip", "*.*"}, new String[]{"Zip files (*.zip)", "All Files (*.*)"}, true);
+    if (zipFilename == null) {
+      return;
+    }
+
+    Combo combo = getProjectsCombo();
+    if (combo == null) {
+      return;
+    }
+    String projectName = combo.getText();
+    if (StringUtils.isEmpty(projectName)) {
+      return;
+    }
+    ProjectsConfig config = ProjectsConfigSingleton.getConfig();
+    ProjectConfig projectConfig = config.findProjectConfig(projectName);
+    String projectHome = projectConfig.getProjectHome();
+
+    try {
+      IRunnableWithProgress op =
+              monitor -> {
+                try {
+                  monitor.setTaskName("Zipping project directory... ");
+                  FileOutputStream fos = new FileOutputStream(zipFilename);
+                  ZipOutputStream zos = new ZipOutputStream(fos);
+                  File projectDirectory = new File(projectHome);
+                  zipFile(projectDirectory, projectDirectory.getName(), zos);
+                  zos.close();
+                  fos.close();
+                } catch (Exception e) {
+                  throw new InvocationTargetException(e, "Error zipping project: " + e.getMessage());
+                }
+              };
+
+      ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell);
+      pmd.run(true, false, op);
+
+      GuiResource.getInstance().toClipboard(zipFilename);
+
+      MessageBox box = new MessageBox(shell, SWT.CLOSE | SWT.ICON_INFORMATION);
+      box.setText("Project zip file created");
+      box.setMessage("A zip file was successfully created: " +
+              zipFilename +
+              Const.CR +
+              "The filename was copied to the clipboard.");
+      box.open();
+    } catch (Exception e) {
+      new ErrorDialog(HopGui.getInstance().getShell(), "Error", "Error zipping project", e);
+    }
+  }
+
+  public void zipFile(File fileToZip, String filename, ZipOutputStream zipOutputStream) throws IOException {
+    if (fileToZip.isHidden()) {
+      return;
+    }
+    if (fileToZip.isDirectory()) {
+      if (filename.endsWith("/")) {
+        zipOutputStream.putNextEntry(new ZipEntry(filename));
+        zipOutputStream.closeEntry();
+      } else {
+        zipOutputStream.putNextEntry(new ZipEntry(filename + "/"));
+        zipOutputStream.closeEntry();
+      }
+      File[] children = fileToZip.listFiles();
+      for (File childFile : children) {
+        zipFile(childFile, filename + "/" + childFile.getName(), zipOutputStream);
+      }
+      return;
+    }
+    FileInputStream fis = new FileInputStream(fileToZip);
+    ZipEntry zipEntry = new ZipEntry(filename);
+    zipOutputStream.putNextEntry(zipEntry);
+    byte[] bytes = new byte[1024];
+    int length;
+    while ((length = fis.read(bytes)) >= 0) {
+      zipOutputStream.write(bytes, 0, length);
+    }
+    fis.close();
+  }
 }
diff --git a/plugins/misc/projects/src/main/resources/export.svg b/plugins/misc/projects/src/main/resources/export.svg
new file mode 100644
index 0000000..29be68b
--- /dev/null
+++ b/plugins/misc/projects/src/main/resources/export.svg
@@ -0,0 +1,14 @@
+<svg
+        xmlns="http://www.w3.org/2000/svg"
+        height="24"
+        viewBox="0 0 24 24"
+        width="24">
+    <g>
+        <rect fill="none" height="24" width="24"/>
+    </g>
+    <g>
+        <path
+            style="fill:#0e3a5a;fill-opacity:1"
+            d="M5,20h14v-2H5V20z M5,10h4v6h6v-6h4l-7-7L5,10z"/>
+    </g>
+</svg>
\ No newline at end of file
diff --git a/plugins/misc/projects/src/main/resources/org/apache/hop/projects/gui/messages/messages_en_US.properties b/plugins/misc/projects/src/main/resources/org/apache/hop/projects/gui/messages/messages_en_US.properties
index dc6f0b8..0fbe5e2 100644
--- a/plugins/misc/projects/src/main/resources/org/apache/hop/projects/gui/messages/messages_en_US.properties
+++ b/plugins/misc/projects/src/main/resources/org/apache/hop/projects/gui/messages/messages_en_US.properties
@@ -32,3 +32,4 @@ HopGui.Toolbar.Environment.Add.Tooltip = Add a new environment
 
 FileDialog.Browse.Project.Home=Navigate to the project home directory
 
+HopGui.FileMenu.Project.Export.Label=Export current project to zip
\ No newline at end of file