Skip to content
Permalink
Browse files

#6745 FDW scritp copy/save

  • Loading branch information...
serge-rider committed Oct 8, 2019
1 parent 6495f5c commit 2e60766a71b4664ca16725698b060887dd43836b
@@ -17,15 +17,12 @@

package org.jkiss.utils;

import org.jkiss.code.NotNull;

import java.io.*;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Calendar;
import java.util.Date;

/**
@@ -180,6 +177,18 @@ public static void copyText(
return buffer;
}

public static void writeFileFromBuffer(File file, byte[] buffer) throws IOException {
try (OutputStream os = new FileOutputStream(file)) {
os.write(buffer);
}
}

public static void writeFileFromString(File file, String str) throws IOException {
try (Writer os = new FileWriter(file)) {
os.write(str);
}
}

public static int readStreamToBuffer(
java.io.InputStream inputStream,
byte[] buffer)
@@ -32,7 +32,6 @@
import org.jkiss.dbeaver.ui.dialogs.ActiveWizardDialog;
import org.jkiss.dbeaver.ui.dialogs.BaseDialog;
import org.jkiss.dbeaver.ui.dialogs.connection.ClientHomesSelector;
import org.jkiss.dbeaver.ui.internal.UIMessages;

/**
* Tool wizard dialog
@@ -69,10 +68,6 @@ protected void createButtonsForButtonBar(Composite parent)
}

super.createButtonsForButtonBar(parent);
Button cancelButton = getButton(IDialogConstants.CANCEL_ID);
cancelButton.setText(IDialogConstants.CLOSE_LABEL);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText(UIMessages.button_start);
}

@Override
@@ -21,7 +21,6 @@
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreDatabase;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreObject;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.ui.dialogs.tools.ToolWizardDialog;
import org.jkiss.dbeaver.ui.tools.IUserInterfaceTool;
@@ -46,6 +45,7 @@ public void execute(IWorkbenchWindow window, IWorkbenchPart activePart, Collecti
ToolWizardDialog dialog = new ToolWizardDialog(
window,
new PostgreFDWConfigWizard(database));
dialog.setFinishButtonLabel("Install");
dialog.open();
}
}
@@ -16,6 +16,10 @@
*/
package org.jkiss.dbeaver.ext.postgresql.tools.fdw;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
@@ -27,6 +31,7 @@
import org.jkiss.dbeaver.ext.postgresql.model.*;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction;
import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment;
import org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor;
@@ -40,9 +45,13 @@
import org.jkiss.dbeaver.runtime.ui.UIServiceSQL;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage;
import org.jkiss.dbeaver.ui.dialogs.DialogUtils;
import org.jkiss.dbeaver.ui.editors.SimpleCommandContext;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@@ -52,6 +61,7 @@
private static final Log log = Log.getLog(PostgreFDWConfigWizardPageFinal.class);
private boolean activated;
private Object sqlPanel;
private String scriptText;

protected PostgreFDWConfigWizardPageFinal(PostgreFDWConfigWizard wizard)
{
@@ -73,14 +83,17 @@ public void createControl(Composite parent)
Composite composite = UIUtils.createComposite(parent, 1);

{
Group settingsGroup = UIUtils.createControlGroup(composite, "Script", 2, GridData.FILL_BOTH, 0);
settingsGroup.setLayout(new FillLayout());
Group settingsGroup = UIUtils.createControlGroup(composite, "Script", 1, GridData.FILL_BOTH, 0);

Composite sqlPanelPH = new Composite(settingsGroup, SWT.NONE);
sqlPanelPH.setLayoutData(new GridData(GridData.FILL_BOTH));
sqlPanelPH.setLayout(new FillLayout());
UIServiceSQL service = DBWorkbench.getService(UIServiceSQL.class);
if (service != null) {
try {
sqlPanel = service.createSQLPanel(
UIUtils.getActiveWorkbenchWindow().getActivePage().getActivePart().getSite(),
settingsGroup,
sqlPanelPH,
getWizard(),
"FDW Script",
true,
@@ -90,6 +103,28 @@ public void createControl(Composite parent)
setErrorMessage(e.getMessage());
}
}
Composite buttonsPanel = UIUtils.createComposite(settingsGroup, 2);
buttonsPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
UIUtils.createDialogButton(buttonsPanel, "Copy", new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
UIUtils.setClipboardContents(buttonsPanel.getDisplay(), TextTransfer.getInstance(), scriptText);
}
});
UIUtils.createDialogButton(buttonsPanel, "Save ...", new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
final File saveFile = DialogUtils.selectFileForSave(
buttonsPanel.getShell(), "Save SQL script", new String[]{"*.sql", "*.txt", "*", "*.*"}, null);
if (saveFile != null) {
try {
IOUtils.writeFileFromString(saveFile, scriptText);
} catch (IOException e1) {
DBWorkbench.getPlatformUI().showError("Save scritp to file", "Error saving script to file " + saveFile.getAbsolutePath(), e1);
}
}
}
});
}


@@ -108,18 +143,25 @@ public void activatePage() {
private void generateScript() {
StringBuilder script = new StringBuilder();
try {
PostgreDataSource dataSource = getWizard().getDatabase().getDataSource();
getWizard().getRunnableContext().run(true, true, monitor -> {
try {
monitor.beginTask("Generate FDW script", 2);
monitor.subTask("Read actions");
List<DBEPersistAction> actions = generateScript(monitor);
monitor.subTask("Generate script");
script.append(
SQLUtils.generateScript(
getWizard().getDatabase().getDataSource(),
actions.toArray(new DBEPersistAction[0]),
false));
monitor.done();
DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
try {
monitor.beginTask("Generate FDW script", 2);
monitor.subTask("Read actions");
List<DBEPersistAction> actions = generateScript(monitor);
monitor.subTask("Generate script");
script.append(
SQLUtils.generateScript(
dataSource,
actions.toArray(new DBEPersistAction[0]),
false));
monitor.done();
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (DBException e) {
throw new InvocationTargetException(e);
}
@@ -133,11 +175,11 @@ private void generateScript() {
}
setErrorMessage(null);

String sql = script.toString();
scriptText = script.toString();

UIServiceSQL service = DBWorkbench.getService(UIServiceSQL.class);
if (service != null) {
service.setSQLPanelText(sqlPanel, sql);
service.setSQLPanelText(sqlPanel, scriptText);
}
}

@@ -16,15 +16,19 @@
*/
package org.jkiss.dbeaver.ui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWizard;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.internal.UIMessages;

import java.util.HashSet;
import java.util.Set;
@@ -38,6 +42,9 @@
private Set<String> resizedShells = new HashSet<>();
private boolean adaptContainerSizeToPages = false;

private String finishButtonLabel = UIMessages.button_start;
private String cancelButtonLabel = IDialogConstants.CLOSE_LABEL;

public ActiveWizardDialog(IWorkbenchWindow window, IWizard wizard)
{
this(window, wizard, null);
@@ -86,4 +93,21 @@ public void showPage(IWizardPage page) {
}
}

@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);

Button cancelButton = getButton(IDialogConstants.CANCEL_ID);
cancelButton.setText(cancelButtonLabel);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText(finishButtonLabel);
}

public void setFinishButtonLabel(String finishButtonLabel) {
this.finishButtonLabel = finishButtonLabel;
}

public void setCancelButtonLabel(String cancelButtonLabel) {
this.cancelButtonLabel = cancelButtonLabel;
}
}

0 comments on commit 2e60766

Please sign in to comment.
You can’t perform that action at this time.