1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

SWT: Custom AWT event queue to trap X errors and avoid application crash

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1509 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-10-17 17:04:29 +00:00
parent adfc4a8103
commit e517cc0433
2 changed files with 129 additions and 6 deletions

View File

@ -0,0 +1,116 @@
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 davmail.ui.tray;
import org.apache.log4j.Logger;
import org.eclipse.swt.internal.gtk.OS;
import java.awt.*;
import java.util.EmptyStackException;
/**
* Custom AWT event queue to trap X errors and avoid application crash.
*/
public class SwtAwtEventQueue extends EventQueue {
protected static final Logger LOGGER = Logger.getLogger(SwtAwtEventQueue.class);
/**
* Register SWT GDK error handler
*/
public static void registerErrorHandler() {
OS.gdk_error_trap_push();
}
/**
* Handle errors caught by SWT GDK error handler
*/
public static void handleGdkError() {
OS.gdk_flush();
int errorCode = OS.gdk_error_trap_pop();
if (errorCode != 0) {
LOGGER.debug("Uncaught GDK X error: " + errorCode);
}
}
/**
* @inheritDoc
*/
public void postEvent(AWTEvent event) {
registerErrorHandler();
super.postEvent(event);
handleGdkError();
}
/**
* @inheritDoc
*/
protected void dispatchEvent(AWTEvent event) {
registerErrorHandler();
super.dispatchEvent(event);
handleGdkError();
}
/**
* @inheritDoc
*/
public AWTEvent getNextEvent() throws InterruptedException {
registerErrorHandler();
AWTEvent event = super.getNextEvent();
handleGdkError();
return event;
}
/**
* @inheritDoc
*/
public synchronized AWTEvent peekEvent() {
registerErrorHandler();
AWTEvent event = super.peekEvent();
handleGdkError();
return event;
}
/**
* @inheritDoc
*/
public synchronized AWTEvent peekEvent(int id) {
registerErrorHandler();
AWTEvent event = super.peekEvent(id);
handleGdkError();
return event;
}
/**
* @inheritDoc
*/
protected void pop() throws EmptyStackException {
registerErrorHandler();
super.pop();
handleGdkError();
}
/**
* @inheritDoc
*/
public synchronized void push(EventQueue newEventQueue) {
registerErrorHandler();
super.push(newEventQueue);
handleGdkError();
}
}

View File

@ -29,11 +29,14 @@ import org.apache.log4j.lf5.LF5Appender;
import org.apache.log4j.lf5.LogLevel;
import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.DeviceData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
@ -174,20 +177,24 @@ public class SwtGatewayTray implements DavGatewayTrayInterface {
// workaround for bug when SWT and AWT both try to access Gtk
if (lafClassName.indexOf("gtk") > 0) {
lafClassName = UIManager.getCrossPlatformLookAndFeelClassName();
// replace AWT event queue Gdk error handler to avoid application crash
Toolkit.getDefaultToolkit().getSystemEventQueue().push(new SwtAwtEventQueue());
SwtAwtEventQueue.registerErrorHandler();
UIManager.setLookAndFeel(lafClassName);
SwtAwtEventQueue.handleGdkError();
} else {
UIManager.setLookAndFeel(lafClassName);
}
UIManager.setLookAndFeel(lafClassName);
} catch (Exception e) {
DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_SET_LOOK_AND_FEEL"));
}
new Thread("SWT") {
@Override
public void run() {
try {
DeviceData data = new DeviceData();
data.debug = true;
display = new Display(data);
display = new Display();
shell = new Shell(display);
final Tray tray = display.getSystemTray();