提交 b6ea3d7e 编写于 作者: J jurgen

Driver files download wizard

Former-commit-id: 963b351d
上级 68cbad6d
......@@ -17,7 +17,9 @@
*/
package org.jkiss.dbeaver.ui.dialogs.driver;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
......@@ -25,19 +27,27 @@ import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDriverFile;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.registry.DriverDescriptor;
import org.jkiss.dbeaver.registry.DriverFileDescriptor;
import org.jkiss.dbeaver.runtime.RunnableContextDelegate;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.preferences.PrefPageDrivers;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
class DriverDownloadAutoPage extends WizardPage {
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
static final Log log = Log.getLog(DriverDownloadAutoPage.class);
class DriverDownloadAutoPage extends DriverDownloadPage {
DriverDownloadAutoPage() {
super("Automatic download", "Download driver files", null);
......@@ -46,7 +56,7 @@ class DriverDownloadAutoPage extends WizardPage {
@Override
public void createControl(Composite parent) {
DriverDownloadWizard wizard = (DriverDownloadWizard) getWizard();
DriverDownloadWizard wizard = getWizard();
final DriverDescriptor driver = wizard.getDriver();
setMessage("Download " + driver.getFullName() + " driver files");
......@@ -110,5 +120,126 @@ class DriverDownloadAutoPage extends WizardPage {
setControl(composite);
}
@Override
void performFinish() {
downloadLibraryFiles(new RunnableContextDelegate(getContainer()), getWizard().getFiles());
}
private void downloadLibraryFiles(DBRRunnableContext runnableContext, final List<DriverFileDescriptor> files)
{
if (!getWizard().getDriver().acceptDriverLicenses(runnableContext)) {
return;
}
for (int i = 0, filesSize = files.size(); i < filesSize; ) {
DriverFileDescriptor lib = files.get(i);
int result = downloadLibraryFile(runnableContext, lib);
switch (result) {
case IDialogConstants.CANCEL_ID:
case IDialogConstants.ABORT_ID:
return;
case IDialogConstants.RETRY_ID:
continue;
case IDialogConstants.OK_ID:
case IDialogConstants.IGNORE_ID:
i++;
break;
}
}
}
private int downloadLibraryFile(DBRRunnableContext runnableContext, final DriverFileDescriptor file)
{
try {
runnableContext.run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
file.downloadLibraryFile(monitor);
} catch (IOException e) {
throw new InvocationTargetException(e);
}
}
});
return IDialogConstants.OK_ID;
} catch (InterruptedException e) {
// User just canceled download
return IDialogConstants.CANCEL_ID;
} catch (InvocationTargetException e) {
if (file.getType() == DBPDriverFile.FileType.license) {
return IDialogConstants.OK_ID;
}
DownloadRetry retryConfirm = new DownloadRetry(file, e.getTargetException());
UIUtils.runInUI(null, retryConfirm);
return retryConfirm.result;
}
}
private class DownloadRetry implements Runnable {
private final DriverFileDescriptor file;
private final Throwable error;
private int result;
public DownloadRetry(DriverFileDescriptor file, Throwable error)
{
this.file = file;
this.error = error;
}
@Override
public void run()
{
DownloadErrorDialog dialog = new DownloadErrorDialog(
null,
file.getPath(),
"Driver file download failed.\nDo you want to retry?",
error);
result = dialog.open();
}
}
public static class DownloadErrorDialog extends ErrorDialog {
public DownloadErrorDialog(
Shell parentShell,
String dialogTitle,
String message,
Throwable error)
{
super(parentShell, dialogTitle, message,
GeneralUtils.makeExceptionStatus(error),
IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(
parent,
IDialogConstants.ABORT_ID,
IDialogConstants.ABORT_LABEL,
true);
createButton(
parent,
IDialogConstants.RETRY_ID,
IDialogConstants.RETRY_LABEL,
false);
createButton(
parent,
IDialogConstants.IGNORE_ID,
IDialogConstants.IGNORE_LABEL,
false);
createDetailsButton(parent);
}
@Override
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.DETAILS_ID) {
super.buttonPressed(buttonId);
} else {
setReturnCode(buttonId);
close();
}
}
}
}
\ No newline at end of file
}
......@@ -17,31 +17,17 @@
*/
package org.jkiss.dbeaver.ui.dialogs.driver;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDriverFile;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.registry.DriverDescriptor;
import org.jkiss.dbeaver.registry.DriverFileDescriptor;
import org.jkiss.dbeaver.runtime.RunnableContextDelegate;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
/**
......@@ -59,11 +45,12 @@ public class DriverDownloadDialog extends WizardDialog
}
DriverDescriptor getDriver() {
return ((DriverDownloadWizard)getWizard()).getDriver();
return getWizard().getDriver();
}
List<DriverFileDescriptor> getDriverFiles() {
return ((DriverDownloadWizard)getWizard()).getFiles();
@Override
public DriverDownloadWizard getWizard() {
return (DriverDownloadWizard)super.getWizard();
}
@Override
......@@ -77,13 +64,7 @@ public class DriverDownloadDialog extends WizardDialog
@Override
protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
if (id == IDialogConstants.FINISH_ID) {
String finishText;
if (CommonUtils.isEmpty(getDriver().getDriverFileSources())) {
finishText = "Download";
} else {
finishText = "Open Download Page";
}
Button button = super.createButton(parent, id, finishText, defaultButton);
Button button = super.createButton(parent, id, getWizard().getFinishText(), defaultButton);
button.setImage(DBeaverIcons.getImage(UIIcon.BROWSER));
setButtonLayoutData(button);
return button;
......@@ -103,7 +84,7 @@ public class DriverDownloadDialog extends WizardDialog
@Override
protected void finishPressed() {
downloadLibraryFiles(new RunnableContextDelegate(this), getDriverFiles());
getButton(EDIT_DRIVER_BUTTON_ID).setEnabled(false);
doDownload = true;
super.finishPressed();
}
......@@ -114,121 +95,4 @@ public class DriverDownloadDialog extends WizardDialog
return dialog.doDownload;
}
private void downloadLibraryFiles(DBRRunnableContext runnableContext, final List<DriverFileDescriptor> files)
{
if (!getDriver().acceptDriverLicenses(runnableContext)) {
return;
}
for (int i = 0, filesSize = files.size(); i < filesSize; ) {
DriverFileDescriptor lib = files.get(i);
int result = downloadLibraryFile(runnableContext, lib);
switch (result) {
case IDialogConstants.CANCEL_ID:
case IDialogConstants.ABORT_ID:
return;
case IDialogConstants.RETRY_ID:
continue;
case IDialogConstants.OK_ID:
case IDialogConstants.IGNORE_ID:
i++;
break;
}
}
}
private int downloadLibraryFile(DBRRunnableContext runnableContext, final DriverFileDescriptor file)
{
try {
runnableContext.run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
file.downloadLibraryFile(monitor);
} catch (IOException e) {
throw new InvocationTargetException(e);
}
}
});
return IDialogConstants.OK_ID;
} catch (InterruptedException e) {
// User just canceled download
return IDialogConstants.CANCEL_ID;
} catch (InvocationTargetException e) {
if (file.getType() == DBPDriverFile.FileType.license) {
return IDialogConstants.OK_ID;
}
DownloadRetry retryConfirm = new DownloadRetry(file, e.getTargetException());
UIUtils.runInUI(null, retryConfirm);
return retryConfirm.result;
}
}
private class DownloadRetry implements Runnable {
private final DriverFileDescriptor file;
private final Throwable error;
private int result;
public DownloadRetry(DriverFileDescriptor file, Throwable error)
{
this.file = file;
this.error = error;
}
@Override
public void run()
{
DownloadErrorDialog dialog = new DownloadErrorDialog(
null,
file.getPath(),
"Driver file download failed.\nDo you want to retry?",
error);
result = dialog.open();
}
}
public static class DownloadErrorDialog extends ErrorDialog {
public DownloadErrorDialog(
Shell parentShell,
String dialogTitle,
String message,
Throwable error)
{
super(parentShell, dialogTitle, message,
GeneralUtils.makeExceptionStatus(error),
IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(
parent,
IDialogConstants.ABORT_ID,
IDialogConstants.ABORT_LABEL,
true);
createButton(
parent,
IDialogConstants.RETRY_ID,
IDialogConstants.RETRY_LABEL,
false);
createButton(
parent,
IDialogConstants.IGNORE_ID,
IDialogConstants.IGNORE_LABEL,
false);
createDetailsButton(parent);
}
@Override
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.DETAILS_ID) {
super.buttonPressed(buttonId);
} else {
setReturnCode(buttonId);
close();
}
}
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2015 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.ui.dialogs.driver;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.registry.DriverDescriptor;
import org.jkiss.dbeaver.registry.DriverFileSource;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.preferences.PrefPageDrivers;
import org.jkiss.utils.CommonUtils;
class DriverDownloadManualPage extends DriverDownloadPage {
private DriverFileSource fileSource;
DriverDownloadManualPage() {
super("Configure driver files", "Download driver files", null);
setPageComplete(false);
}
@Override
public void createControl(Composite parent) {
final DriverDescriptor driver = getWizard().getDriver();
setMessage("Download & configure " + driver.getFullName() + " driver files");
StringBuilder message = new StringBuilder();
message.append("").append(driver.getFullName());
message.append("\n\nOr you can obtain driver files by yourself and add them in driver editor.");
initializeDialogUnits(parent);
Composite composite = UIUtils.createPlaceholder(parent, 1);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Text infoText = new Text(composite, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
infoText.setText(message.toString());
infoText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//UIUtils.createHorizontalLine(composite);
UIUtils.createPlaceholder(composite, 1).setLayoutData(new GridData(GridData.FILL_BOTH));
{
Composite linksGroup = UIUtils.createPlaceholder(composite, 2);
((GridLayout)linksGroup.getLayout()).makeColumnsEqualWidth = true;
linksGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// Vendor site
if (!CommonUtils.isEmpty(driver.getWebURL())) {
Link link = UIUtils.createLink(
linksGroup,
"<a>Vendor's website</a>",
new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
RuntimeUtils.openWebBrowser(driver.getWebURL());
}
});
link.setToolTipText(driver.getWebURL());
link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
} else {
UIUtils.createPlaceholder(linksGroup, 1).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
Link link = UIUtils.createLink(
linksGroup,
"<a>Download configuration</a>",
new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
UIUtils.showPreferencesFor(
DBeaverUI.getActiveWorkbenchShell(),
null,
PrefPageDrivers.PAGE_ID);
}
});
link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END));
}
setControl(composite);
fileSource = getWizard().getDriver().getDriverFileSources().get(0);
}
@Override
void performFinish() {
UIUtils.runInDetachedUI(getShell(), new Runnable() {
@Override
public void run() {
RuntimeUtils.openWebBrowser(fileSource.getUrl());
}
});
DriverEditDialog dialog = new DriverEditDialog(DBeaverUI.getActiveWorkbenchShell(), getWizard().getDriver());
dialog.open();
}
}
\ No newline at end of file
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2015 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.ui.dialogs.driver;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
abstract class DriverDownloadPage extends WizardPage {
DriverDownloadPage(String pageName, String title, ImageDescriptor titleImage) {
super(pageName, title, titleImage);
}
public DriverDownloadWizard getWizard() {
return (DriverDownloadWizard) super.getWizard();
}
abstract void performFinish();
}
\ No newline at end of file
......@@ -26,6 +26,7 @@ import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.registry.DriverDescriptor;
import org.jkiss.dbeaver.registry.DriverFileDescriptor;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.utils.CommonUtils;
import java.util.List;
......@@ -35,6 +36,7 @@ public class DriverDownloadWizard extends Wizard implements IExportWizard {
private DriverDescriptor driver;
private List<DriverFileDescriptor> files;
private DriverDownloadPage downloadPage;
public DriverDownloadWizard(@NotNull DriverDescriptor driver, List<DriverFileDescriptor> files) {
this.driver = driver;
......@@ -61,7 +63,12 @@ public class DriverDownloadWizard extends Wizard implements IExportWizard {
@Override
public void addPages() {
super.addPages();
addPage(new DriverDownloadAutoPage());
if (hasPredefinedFiles()) {
downloadPage = new DriverDownloadAutoPage();
} else {
downloadPage = new DriverDownloadManualPage();
}
addPage(downloadPage);
}
@Override
......@@ -79,9 +86,20 @@ public class DriverDownloadWizard extends Wizard implements IExportWizard {
@Override
public boolean performFinish() {
downloadPage.performFinish();
return true;
}
public String getFinishText() {
if (hasPredefinedFiles()) {
return "Download";
} else {
return "Open Download Page";
}
}
private boolean hasPredefinedFiles() {
return CommonUtils.isEmpty(getDriver().getDriverFileSources());
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册