-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from MicroFocus/dmoldoveanu_story_1063483_bdd_…
…scenario user story #1063483: Make BDD Scenario (and gherkin test) downloadable in the search tree
- Loading branch information
Showing
3 changed files
with
170 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
octane-eclipse-plugin/src/com/hpe/octane/ideplugins/eclipse/ui/util/DownloadScriptUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package com.hpe.octane.ideplugins.eclipse.ui.util; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.eclipse.core.filesystem.EFS; | ||
import org.eclipse.core.filesystem.IFileStore; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IResource; | ||
import org.eclipse.core.resources.ResourcesPlugin; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.core.runtime.NullProgressMonitor; | ||
import org.eclipse.core.runtime.Path; | ||
import org.eclipse.core.runtime.Status; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.custom.BusyIndicator; | ||
import org.eclipse.swt.widgets.DirectoryDialog; | ||
import org.eclipse.swt.widgets.Display; | ||
import org.eclipse.swt.widgets.Menu; | ||
import org.eclipse.swt.widgets.MessageBox; | ||
import org.eclipse.ui.IEditorDescriptor; | ||
import org.eclipse.ui.IFileEditorMapping; | ||
import org.eclipse.ui.IWorkbenchPage; | ||
import org.eclipse.ui.PartInitException; | ||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.ui.editors.text.EditorsUI; | ||
import org.eclipse.ui.ide.IDE; | ||
import org.eclipse.ui.internal.registry.EditorDescriptor; | ||
import org.eclipse.ui.internal.registry.EditorRegistry; | ||
import org.eclipse.ui.internal.registry.FileEditorMapping; | ||
|
||
import com.hpe.adm.nga.sdk.model.EntityModel; | ||
import com.hpe.adm.octane.ideplugins.services.nonentity.DownloadScriptService; | ||
import com.hpe.octane.ideplugins.eclipse.Activator; | ||
|
||
public class DownloadScriptUtil { | ||
|
||
private static DownloadScriptService scriptService = Activator.getInstance(DownloadScriptService.class); | ||
|
||
public void downloadScriptForTest(EntityModel entityModel, Menu menu) { | ||
File parentFolder = chooseParentFolder(); | ||
|
||
if (parentFolder != null) { | ||
long testId = Long.parseLong(entityModel.getValue("id").getValue().toString()); | ||
String testName = entityModel.getValue("name").getValue().toString(); | ||
String scriptFileName = testName + "-" + | ||
testId + ".feature"; | ||
File scriptFile = new File(parentFolder.getPath() + File.separator + | ||
scriptFileName); | ||
boolean shouldDownloadScript = true; | ||
|
||
if (scriptFile.exists()) { | ||
MessageBox messageBox = new MessageBox(menu.getShell(), SWT.ICON_QUESTION | | ||
SWT.YES | SWT.NO); | ||
messageBox.setMessage("Selected destination folder already contains a file named \"" + | ||
scriptFileName + "\". Do you want to overwrite this file?"); | ||
messageBox.setText("Confirm file overwrite"); | ||
shouldDownloadScript = messageBox.open() == SWT.YES; | ||
} | ||
|
||
if (shouldDownloadScript) { | ||
BusyIndicator.showWhile(Display.getCurrent(), () -> { | ||
String content = scriptService.getTestScriptContent(testId); | ||
createTestScriptFile(parentFolder.getPath(), scriptFileName, | ||
content); | ||
|
||
associateTextEditorToScriptFile(scriptFile); | ||
openInEditor(scriptFile); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
private void associateTextEditorToScriptFile(File file) { | ||
EditorRegistry editorRegistry = (EditorRegistry) PlatformUI.getWorkbench().getEditorRegistry(); | ||
IEditorDescriptor editorDescriptor = editorRegistry.getDefaultEditor(file.getName()); | ||
if (editorDescriptor == null) { | ||
String extension = "feature"; | ||
String editorId = EditorsUI.DEFAULT_TEXT_EDITOR_ID; | ||
|
||
EditorDescriptor editor = (EditorDescriptor) editorRegistry.findEditor(editorId); | ||
FileEditorMapping mapping = new FileEditorMapping(extension); | ||
mapping.addEditor(editor); | ||
mapping.setDefaultEditor(editor); | ||
|
||
IFileEditorMapping[] mappings = editorRegistry.getFileEditorMappings(); | ||
FileEditorMapping[] newMappings = new FileEditorMapping[mappings.length + 1]; | ||
for (int i = 0; i < mappings.length; i++) { | ||
newMappings[i] = (FileEditorMapping) mappings[i]; | ||
} | ||
newMappings[mappings.length] = mapping; | ||
editorRegistry.setFileEditorMappings(newMappings); | ||
} | ||
} | ||
|
||
private void openInEditor(File file) { | ||
IPath path = new Path(file.getPath()); | ||
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | ||
|
||
IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI()); | ||
try { | ||
// open as external file | ||
IDE.openEditorOnFileStore(page, fileStore); | ||
refreshFile(file); | ||
} catch (PartInitException e) { | ||
Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, | ||
Status.ERROR, "Script file could not be opened in the editor", e)); | ||
} | ||
} | ||
|
||
private void refreshFile(File file) { | ||
IPath path = new Path(file.getPath()); | ||
IFile eclipseFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); | ||
if (eclipseFile != null) { | ||
try { | ||
eclipseFile.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); | ||
} catch (CoreException e) { | ||
Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, | ||
Status.ERROR, "Script file could not be refreshed", e)); | ||
} | ||
} | ||
} | ||
|
||
private File chooseParentFolder() { | ||
DirectoryDialog dialog = new DirectoryDialog(Display.getDefault().getActiveShell(), SWT.OPEN); | ||
dialog.setText("Parent folder selection"); | ||
dialog.setMessage("Select the folder where the script file should be downloaded"); | ||
dialog.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()); | ||
String result = dialog.open(); | ||
return result == null ? null : new File(result); | ||
} | ||
|
||
private File createTestScriptFile(String path, String fileName, String script) { | ||
File f = new File(path + "/" + fileName.replaceAll("[\\\\/:?*\"<>|]", "")); | ||
try { | ||
f.createNewFile(); | ||
if (script != null) { | ||
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8)); | ||
out.append(script); | ||
out.flush(); | ||
out.close(); | ||
} | ||
} catch (IOException e) { | ||
Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, | ||
Status.ERROR, "Could not create or write script file in " + path, e)); | ||
} | ||
return f; | ||
} | ||
} |