From 82e99467f5940d4fabbf62e3981556c103406984 Mon Sep 17 00:00:00 2001 From: serge-rider Date: Sat, 7 Apr 2018 00:33:50 +0300 Subject: [PATCH] #3278 Notification sample (commit) --- .../dbeaver/ui/DBeaverNotifications.java | 46 +++++++++++++++++++ .../datasource/DataSourceCommitHandler.java | 33 +++++++------ .../NotificationPopupMessage.java | 34 +++++++++----- 3 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverNotifications.java diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverNotifications.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverNotifications.java new file mode 100644 index 0000000000..7885a1e831 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverNotifications.java @@ -0,0 +1,46 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jkiss.dbeaver.ui; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.jkiss.dbeaver.DBeaverPreferences; +import org.jkiss.dbeaver.core.DBeaverCore; +import org.jkiss.dbeaver.model.DBPDataSource; +import org.jkiss.dbeaver.ui.notifications.NotificationPopupMessage; + +/** + * DBeaverIcons + */ +public class DBeaverNotifications +{ + public static void showInformation(DBPDataSource dataSource, String text) { +/* + Display.getDefault().syncExec(() -> { + int delayClose = DBeaverCore.getGlobalPreferenceStore().getLong(DBeaverPreferences.AGENT_LONG_OPERATION_NOTIFY) + NotificationPopupMessage popup = new NotificationPopupMessage(dataSource, text, SWT.ICON_INFORMATION); + if (delayClose > 0) { + popup.setDelayClose(delayClose); + } + + popup.open(); + }); +*/ + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/datasource/DataSourceCommitHandler.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/datasource/DataSourceCommitHandler.java index ec7b7807a7..81b8d5a2cc 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/datasource/DataSourceCommitHandler.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/actions/datasource/DataSourceCommitHandler.java @@ -18,13 +18,16 @@ package org.jkiss.dbeaver.ui.actions.datasource; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.swt.SWT; import org.jkiss.code.NotNull; import org.jkiss.dbeaver.model.DBUtils; import org.jkiss.dbeaver.model.exec.*; -import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; -import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress; +import org.jkiss.dbeaver.model.qm.QMTransactionState; +import org.jkiss.dbeaver.model.qm.QMUtils; import org.jkiss.dbeaver.runtime.TasksJob; import org.jkiss.dbeaver.ui.actions.AbstractDataSourceHandler; +import org.jkiss.dbeaver.ui.notifications.NotificationPopupMessage; +import org.jkiss.dbeaver.utils.RuntimeUtils; import java.lang.reflect.InvocationTargetException; @@ -41,18 +44,22 @@ public class DataSourceCommitHandler extends AbstractDataSourceHandler } public static void execute(@NotNull final DBCExecutionContext context) { - TasksJob.runTask("Commit transaction", new DBRRunnableWithProgress() { - @Override - public void run(DBRProgressMonitor monitor) - throws InvocationTargetException, InterruptedException { - DBCTransactionManager txnManager = DBUtils.getTransactionManager(context); - if (txnManager != null) { - try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Commit transaction")) { - txnManager.commit(session); - } catch (DBCException e) { - throw new InvocationTargetException(e); - } + TasksJob.runTask("Commit transaction", monitor -> { + DBCTransactionManager txnManager = DBUtils.getTransactionManager(context); + if (txnManager != null) { + QMTransactionState txnInfo = QMUtils.getTransactionState(context); + try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Commit transaction")) { + txnManager.commit(session); + } catch (DBCException e) { + throw new InvocationTargetException(e); } + NotificationPopupMessage.showMessage( + context.getDataSource(), + "Transaction has been committed\n\n" + + "Query count: " + txnInfo.getUpdateCount() + "\n" + + "Duration: " + RuntimeUtils.formatExecutionTime(System.currentTimeMillis() - txnInfo.getTransactionStartTime()) + "\n", + 3000, SWT.ICON_INFORMATION + ); } }); } diff --git a/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/notifications/NotificationPopupMessage.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/notifications/NotificationPopupMessage.java index b6e891ee6d..e868258005 100644 --- a/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/notifications/NotificationPopupMessage.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/notifications/NotificationPopupMessage.java @@ -1,6 +1,8 @@ package org.jkiss.dbeaver.ui.notifications; import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; @@ -13,17 +15,14 @@ public class NotificationPopupMessage extends NotificationPopup { private final DBPDataSource dataSource; private String messageText; + private int iconType; - public NotificationPopupMessage(String text) { - this(null, text); - } - - public NotificationPopupMessage(DBPDataSource dataSource, String text) { + public NotificationPopupMessage(DBPDataSource dataSource, String text, int iconType) { super(PlatformUI.getWorkbench().getDisplay()); this.dataSource = dataSource; this.messageText = text; - setDelayClose(3000); + this.iconType = iconType; } @Override @@ -31,17 +30,28 @@ public class NotificationPopupMessage extends NotificationPopup { return dataSource == null ? GeneralUtils.getProductName() : dataSource.getContainer().getName(); } + @Override + protected Image getPopupShellImage(int maximumHeight) { + boolean hasIcon = iconType == SWT.ICON_ERROR || iconType == SWT.ICON_WARNING || iconType == SWT.ICON_QUESTION; + return hasIcon ? getShell().getDisplay().getSystemImage(iconType) : null; + } + @Override protected void createContentArea(Composite composite) { - composite.setLayout(new GridLayout(1, true)); - Label linkGoogleNews = new Label(composite, SWT.NONE); - linkGoogleNews.setText(messageText); - //linkGoogleNews.setSize(400, 100); + Label textLabel = new Label(composite, SWT.NONE); + textLabel.setText(messageText); } - public static void showMessage(DBPDataSource dataSource, String text) { - Display.getDefault().syncExec(() -> new NotificationPopupMessage(dataSource, text).open()); + public static void showMessage(DBPDataSource dataSource, String text, long delayClose, int iconType) { + Display.getDefault().syncExec(() -> { + NotificationPopupMessage popup = new NotificationPopupMessage(dataSource, text, iconType); + if (delayClose > 0) { + popup.setDelayClose(delayClose); + } + + popup.open(); + }); } } \ No newline at end of file -- GitLab