提交 2beb8287 编写于 作者: J jurgen

UI/runtime model refactoring

上级 64371299
......@@ -23,19 +23,13 @@ import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.keys.IBindingService;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.ui.actions.DBeaverVersionChecker;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.registry.DataSourceRegistry;
import org.jkiss.dbeaver.ui.actions.DBeaverVersionChecker;
import org.jkiss.dbeaver.ui.actions.datasource.DataSourceHandler;
import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.Random;
......@@ -45,8 +39,6 @@ import java.util.Random;
*/
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor
{
static final Log log = Log.getLog(ApplicationWorkbenchAdvisor.class);
private static final String PERSPECTIVE_ID = "org.jkiss.dbeaver.core.perspective"; //$NON-NLS-1$
public static final String DBEAVER_SCHEME_NAME = "org.jkiss.dbeaver.defaultKeyScheme"; //$NON-NLS-1$
......@@ -70,18 +62,6 @@ public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor
configurer.setSaveAndRestore(true);
TrayDialog.setDialogHelpAvailable(true);
// Disable all schemas except our own
final IBindingService bindingService = (IBindingService)configurer.getWorkbench().getService(IBindingService.class);
// for (Binding binding : bindingService.getBindings()) {
// System.out.println("binding:" + binding);
// }
// for (Scheme scheme : bindingService.getDefinedSchemes()) {
// if (!scheme.getId().equals(DBEAVER_SCHEME_NAME)) {
// scheme.undefine();
// }
// }
}
@Override
......@@ -137,44 +117,25 @@ public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor
private boolean saveAndCleanup()
{
if (!closeActiveTransactions()) {
return false;
}
return true;
return closeActiveTransactions();
}
private boolean closeActiveTransactions()
{
TransactionCloser closer = new TransactionCloser();
try {
DBeaverUI.runInProgressService(closer);
} catch (InvocationTargetException e) {
log.error(e.getTargetException());
} catch (InterruptedException e) {
// ignore
}
return closer.getResult();
}
private static class TransactionCloser extends DBRRunnableWithResult<Boolean> {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
result = true;
DBeaverCore core = DBeaverCore.getInstance();
for (IProject project : core.getLiveProjects()) {
if (project.isOpen()) {
DataSourceRegistry registry = core.getProjectRegistry().getDataSourceRegistry(project);
if (registry != null) {
for (DataSourceDescriptor dataSourceDescriptor : registry.getDataSources()) {
if (!DataSourceHandler.checkAndCloseActiveTransaction(monitor, dataSourceDescriptor)) {
result = false;
return;
}
DBeaverCore core = DBeaverCore.getInstance();
for (IProject project : core.getLiveProjects()) {
if (project.isOpen()) {
DataSourceRegistry registry = core.getProjectRegistry().getDataSourceRegistry(project);
if (registry != null) {
for (DataSourceDescriptor dataSourceDescriptor : registry.getDataSources()) {
if (!DataSourceHandler.checkAndCloseActiveTransaction(dataSourceDescriptor)) {
return false;
}
}
}
}
}
return true;
}
}
......@@ -14,7 +14,7 @@ Export-Package: org.jkiss.dbeaver,
org.jkiss.dbeaver.registry.transfer,
org.jkiss.dbeaver.registry.updater,
org.jkiss.dbeaver.runtime,
org.jkiss.dbeaver.runtime.exec,
org.jkiss.dbeaver.ui.dialogs.exec,
org.jkiss.dbeaver.runtime.jobs,
org.jkiss.dbeaver.runtime.load,
org.jkiss.dbeaver.runtime.load.jobs,
......
......@@ -27,19 +27,15 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.*;
import org.eclipse.ui.services.IDisposable;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.access.DBAAuthInfo;
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.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.runtime.*;
import org.jkiss.dbeaver.runtime.RunnableContextDelegate;
import org.jkiss.dbeaver.runtime.RunnableWithResult;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
......@@ -50,6 +46,8 @@ import org.jkiss.dbeaver.ui.SharedTextColors;
import org.jkiss.dbeaver.ui.TrayIconHandler;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.connection.BaseAuthDialog;
import org.jkiss.dbeaver.ui.views.process.ProcessPropertyTester;
import org.jkiss.dbeaver.ui.views.process.ShellProcessView;
import org.jkiss.dbeaver.utils.GeneralUtils;
import java.lang.reflect.InvocationTargetException;
......@@ -282,6 +280,11 @@ public class DBeaverUI implements DBUICallback {
UIUtils.showErrorDialog(null, title, message, status);
}
@Override
public void showError(@NotNull String title, @Nullable String message, @NotNull Throwable e) {
UIUtils.showErrorDialog(null, title, message, e);
}
@Override
public DBAAuthInfo promptUserCredentials(String prompt, String userName, String userPassword) {
// Ask user
......@@ -303,4 +306,44 @@ public class DBeaverUI implements DBUICallback {
return null;
}
}
@Override
public void executeProcess(final DBRProcessDescriptor processDescriptor) {
processDescriptor.setProcessListener(new DBRProcessListener() {
@Override
public void onProcessStarted() {
ProcessPropertyTester.firePropertyChange(ProcessPropertyTester.PROP_RUNNING);
}
@Override
public void onProcessTerminated(int resultCode) {
ProcessPropertyTester.firePropertyChange(ProcessPropertyTester.PROP_RUNNING);
}
});
// Direct execute
try {
processDescriptor.execute();
} catch (DBException e) {
showError("Execute process", processDescriptor.getName(), e);
}
if (processDescriptor.getCommand().isShowProcessPanel()) {
getActiveWorkbenchShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
try {
final ShellProcessView processView =
(ShellProcessView) DBeaverUI.getActiveWorkbenchWindow().getActivePage().showView(
ShellProcessView.VIEW_ID,
ShellProcessView.getNextId(),
IWorkbenchPage.VIEW_VISIBLE
);
processView.initProcess(processDescriptor);
} catch (PartInitException e) {
log.error(e);
}
}
});
}
}
}
......@@ -20,14 +20,10 @@ package org.jkiss.dbeaver.runtime.jobs;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.ISaveablePart;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.DBPConnectionEventType;
import org.jkiss.dbeaver.model.DBPDataSourceUser;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.actions.datasource.DataSourceHandler;
import org.jkiss.dbeaver.utils.GeneralUtils;
/**
......@@ -52,21 +48,11 @@ public class DisconnectJob extends EventProcessorJob
@Override
protected IStatus run(DBRProgressMonitor monitor)
{
// Save users
for (DBPDataSourceUser user : container.getUsers()) {
if (user instanceof ISaveablePart) {
if (!UIUtils.validateAndSave(monitor, (ISaveablePart) user)) {
return (connectStatus = Status.OK_STATUS);
}
}
}
try {
if (DataSourceHandler.checkAndCloseActiveTransaction(monitor, container)) {
processEvents(DBPConnectionEventType.BEFORE_DISCONNECT);
container.disconnect(monitor);
processEvents(DBPConnectionEventType.AFTER_DISCONNECT);
}
processEvents(DBPConnectionEventType.BEFORE_DISCONNECT);
container.disconnect(monitor);
processEvents(DBPConnectionEventType.AFTER_DISCONNECT);
connectStatus = Status.OK_STATUS;
}
catch (Throwable ex) {
......
......@@ -18,22 +18,14 @@
package org.jkiss.dbeaver.runtime.jobs;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPConnectionEventType;
import org.jkiss.dbeaver.model.DBPConnectionConfiguration;
import org.jkiss.dbeaver.model.DBPConnectionEventType;
import org.jkiss.dbeaver.model.runtime.DBRProcessDescriptor;
import org.jkiss.dbeaver.model.runtime.DBRProcessListener;
import org.jkiss.dbeaver.model.runtime.DBRShellCommand;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.AbstractJob;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.views.process.ProcessPropertyTester;
import org.jkiss.dbeaver.ui.views.process.ShellProcessView;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.utils.CommonUtils;
import java.util.HashMap;
......@@ -79,56 +71,14 @@ public abstract class EventProcessorJob extends AbstractJob {
variables.put(VARIABLE_PASSWORD, info.getUserPassword());
variables.put(VARIABLE_URL, info.getUrl());
DBRProcessDescriptor process = processCommand(command, variables);
if (process != null) {
container.addChildProcess(process);
final DBRProcessDescriptor processDescriptor = new DBRProcessDescriptor(command, variables);
DBUserInterface.getInstance().executeProcess(processDescriptor);
if (command.isWaitProcessFinish()) {
processDescriptor.waitFor();
}
}
}
private DBRProcessDescriptor processCommand(DBRShellCommand command, Map<String, Object> variables) {
final Shell shell = DBeaverUI.getActiveWorkbenchShell();
final DBRProcessDescriptor processDescriptor = new DBRProcessDescriptor(command, variables);
processDescriptor.setProcessListener(new DBRProcessListener() {
@Override
public void onProcessStarted() {
ProcessPropertyTester.firePropertyChange(ProcessPropertyTester.PROP_RUNNING);
}
@Override
public void onProcessTerminated(int resultCode) {
ProcessPropertyTester.firePropertyChange(ProcessPropertyTester.PROP_RUNNING);
}
});
// Direct execute
try {
processDescriptor.execute();
} catch (DBException e) {
UIUtils.showErrorDialog(shell, "Execute process", processDescriptor.getName(), e);
}
if (command.isShowProcessPanel()) {
shell.getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
try {
final ShellProcessView processView =
(ShellProcessView) DBeaverUI.getActiveWorkbenchWindow().getActivePage().showView(
ShellProcessView.VIEW_ID,
ShellProcessView.getNextId(),
IWorkbenchPage.VIEW_VISIBLE
);
processView.initProcess(processDescriptor);
} catch (PartInitException e) {
log.error(e);
}
}
});
}
if (command.isWaitProcessFinish()) {
processDescriptor.waitFor();
container.addChildProcess(processDescriptor);
}
return processDescriptor;
}
......
......@@ -24,7 +24,6 @@ import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
......
......@@ -37,7 +37,7 @@ import org.jkiss.dbeaver.model.sql.SQLQuery;
import org.jkiss.dbeaver.model.sql.SQLQueryParameter;
import org.jkiss.dbeaver.model.sql.SQLQueryResult;
import org.jkiss.dbeaver.runtime.RunnableWithResult;
import org.jkiss.dbeaver.runtime.exec.ExecutionQueueErrorJob;
import org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorJob;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
......
......@@ -22,6 +22,7 @@ import org.eclipse.core.runtime.IStatus;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.access.DBAAuthInfo;
import org.jkiss.dbeaver.model.runtime.DBRProcessDescriptor;
/**
* User interface interactions
......@@ -29,7 +30,10 @@ import org.jkiss.dbeaver.model.access.DBAAuthInfo;
public interface DBUICallback {
void showError(@NotNull final String title, @Nullable final String message, @NotNull final IStatus status);
void showError(@NotNull final String title, @Nullable final String message, @NotNull final Throwable e);
DBAAuthInfo promptUserCredentials(String prompt, String userName, String userPassword);
void executeProcess(DBRProcessDescriptor processDescriptor);
}
\ No newline at end of file
......@@ -21,7 +21,9 @@ package org.jkiss.dbeaver.runtime.ui;
import org.eclipse.core.runtime.IStatus;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.access.DBAAuthInfo;
import org.jkiss.dbeaver.model.runtime.DBRProcessDescriptor;
/**
* User interface interactions
......@@ -35,6 +37,12 @@ public class DBUserInterface {
printStatus(status, 0);
}
@Override
public void showError(@NotNull String title, @Nullable String message, @NotNull Throwable e) {
System.out.println(title + (message == null ? "" : ": " + message));
e.printStackTrace(System.out);
}
private void printStatus(@NotNull IStatus status, int level) {
char[] indent = new char[level * 4];
for (int i = 0; i < indent.length; i++) indent[i] = ' ';
......@@ -50,6 +58,15 @@ public class DBUserInterface {
public DBAAuthInfo promptUserCredentials(String prompt, String userName, String userPassword) {
return null;
}
@Override
public void executeProcess(DBRProcessDescriptor processDescriptor) {
try {
processDescriptor.execute();
} catch (DBException e) {
DBUserInterface.getInstance().showError("Execute process", processDescriptor.getName(), e);
}
}
};
public static DBUICallback getInstance() {
......
......@@ -30,7 +30,7 @@ import org.jkiss.dbeaver.model.sql.SQLDataSource;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import org.jkiss.dbeaver.model.struct.rdb.DBSSchema;
import org.jkiss.dbeaver.runtime.exec.ExecutionQueueErrorJob;
import org.jkiss.dbeaver.ui.dialogs.exec.ExecutionQueueErrorJob;
import org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer;
import org.jkiss.dbeaver.tools.transfer.IDataTransferProcessor;
import org.jkiss.dbeaver.ui.UIUtils;
......
......@@ -32,6 +32,7 @@ import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPDataSourceUser;
import org.jkiss.dbeaver.model.DBPEvent;
......@@ -46,6 +47,7 @@ import org.jkiss.dbeaver.model.qm.meta.QMMTransactionSavepointInfo;
import org.jkiss.dbeaver.model.runtime.DBRProgressListener;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataSourceContainer;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
......@@ -203,6 +205,19 @@ public class DataSourceHandler
}
public static void disconnectDataSource(DBSDataSourceContainer dataSourceContainer, @Nullable final Runnable onFinish) {
// Save users
for (DBPDataSourceUser user : dataSourceContainer.getUsers()) {
if (user instanceof ISaveablePart) {
if (!UIUtils.validateAndSave(VoidProgressMonitor.INSTANCE, (ISaveablePart) user)) {
return;
}
}
}
if (!checkAndCloseActiveTransaction(dataSourceContainer)) {
return;
}
if (dataSourceContainer instanceof DataSourceDescriptor && dataSourceContainer.isConnected()) {
final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor)dataSourceContainer;
if (!ArrayUtils.isEmpty(Job.getJobManager().find(dataSourceDescriptor))) {
......@@ -230,23 +245,23 @@ public class DataSourceHandler
}
}
public static boolean checkAndCloseActiveTransaction(final DBRProgressMonitor monitor, DBSDataSourceContainer container) {
public static boolean checkAndCloseActiveTransaction(DBSDataSourceContainer container) {
DBPDataSource dataSource = container.getDataSource();
if (dataSource == null) {
return true;
}
return checkAndCloseActiveTransaction(monitor, container, dataSource.getAllContexts());
return checkAndCloseActiveTransaction(container, dataSource.getAllContexts());
}
public static boolean checkAndCloseActiveTransaction(final DBRProgressMonitor monitor, DBSDataSourceContainer container, Collection<? extends DBCExecutionContext> contexts)
public static boolean checkAndCloseActiveTransaction(DBSDataSourceContainer container, Collection<? extends DBCExecutionContext> contexts)
{
if (container.getDataSource() == null) {
return true;
}
Boolean commitTxn = null;
for (DBCExecutionContext context : contexts) {
for (final DBCExecutionContext context : contexts) {
// First rollback active transaction
try {
if (isContextTransactionAffected(context)) {
......@@ -265,12 +280,16 @@ public class DataSourceHandler
return false;
}
}
closeActiveTransaction(monitor, context, commitTxn);
final boolean commit = commitTxn;
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
closeActiveTransaction(monitor, context, commit);
}
});
}
} catch (Throwable e) {
log.warn("Can't rollback active transaction before disconnect", e);
} finally {
monitor.worked(1);
}
}
return true;
......
......@@ -15,7 +15,7 @@
* 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.runtime.exec;
package org.jkiss.dbeaver.ui.dialogs.exec;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.ErrorDialog;
......
......@@ -15,7 +15,7 @@
* 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.runtime.exec;
package org.jkiss.dbeaver.ui.dialogs.exec;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
......
......@@ -16,7 +16,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.runtime.exec;
package org.jkiss.dbeaver.ui.dialogs.exec;
/**
* ExecutionQueueErrorResponse
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册