Skip to content

Commit

Permalink
0004943: Fixed Export/Download buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-miller-jumpmind committed Aug 11, 2021
1 parent 62160fb commit f10c628
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
Expand Up @@ -81,6 +81,30 @@ public void export() {
convertToCsv();
sendCsvToUser();
}

public ExportFileDownloader getFileDownloader() {
convertToCsv();
FileOutputStream outStream = null;
File file = null;
try {
String prefix = fileName.substring(0, fileName.length() - 4);
file = File.createTempFile(prefix, ".csv");
outStream = new FileOutputStream(file);
outStream.write(cellData.toString().getBytes());

return new ExportFileDownloader(fileName, csvMimeContentType, file);
} catch (Exception e) {
log.error("", e);
return null;
} finally {
try {
file.deleteOnExit();
outStream.close();
} catch (IOException e) {
log.error("Problem closing File Stream", e);
}
}
}

public void convertToCsv() {
addTitle();
Expand Down
Expand Up @@ -51,6 +51,7 @@
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.textfield.TextArea;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

public class DbExportDialog extends ResizableDialog {
Expand Down Expand Up @@ -108,6 +109,8 @@ enum DbExportFormat {
private TableSelectionLayout tableSelectionLayout;

private VerticalLayout optionLayout;

private HorizontalLayout buttonFooter;

private Anchor fileDownloader;

Expand Down Expand Up @@ -159,15 +162,6 @@ protected void addButtons() {
previousButton = new Button("Previous", event -> previous());
previousButton.setVisible(false);

exportFileButton = CommonUiUtils.createPrimaryButton("Export", event -> {
exportFileShortcutRegistration.remove();
fileDownloader.setHref(createResource());
doneShortcutRegistration = doneButton.addClickShortcut(Key.ENTER);
doneButton.focus();
});
buildFileDownloader();
fileDownloader.setVisible(false);

exportEditorButton = CommonUiUtils.createPrimaryButton("Export", event -> {
exportToEditor();
close();
Expand All @@ -176,9 +170,18 @@ protected void addButtons() {

doneButton = new Button("Close", event -> close());
doneButton.setVisible(false);

exportFileButton = CommonUiUtils.createPrimaryButton("Export", event -> {
exportFileShortcutRegistration.remove();
doneShortcutRegistration = doneButton.addClickShortcut(Key.ENTER);
doneButton.focus();
});
buildFileDownloader();
fileDownloader.setVisible(false);

add(buildButtonFooter(new Button[] {},
cancelButton, previousButton, nextButton, fileDownloader, exportEditorButton, doneButton));
buttonFooter = buildButtonFooter(new Button[] {}, cancelButton, previousButton, nextButton, fileDownloader,
exportEditorButton, doneButton);
add(buttonFooter);

}

Expand Down Expand Up @@ -224,6 +227,7 @@ protected void createOptionLayout() {
indices.setEnabled(false);
quotedIdentifiers.setEnabled(false);
}
buildFileDownloader();
});
formatSelect.setValue(DbExportFormat.SQL);
formLayout.add(formatSelect);
Expand Down Expand Up @@ -372,10 +376,15 @@ protected void exportToEditor() {
protected void buildFileDownloader() {
if (fileDownloader != null) {
fileDownloader.remove();
content.remove(buttonFooter);
}
fileDownloader = new Anchor(createResource(), null);
fileDownloader.getElement().setAttribute("download", true);
fileDownloader.add(exportFileButton);

buttonFooter = buildButtonFooter(new Button[] {}, cancelButton, previousButton, nextButton, fileDownloader,
exportEditorButton, doneButton);
add(buttonFooter);
}

private StreamResource createResource() {
Expand Down

0 comments on commit f10c628

Please sign in to comment.