提交 82e99467 编写于 作者: S serge-rider

#3278 Notification sample (commit)

上级 3ffbac56
/*
* 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();
});
*/
}
}
......@@ -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
);
}
});
}
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册