Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [albireo-dev] component creation protocol: allow for some hooks

I wrote:
> OK, we said that all behaviours that are not default Eclipse/AWT behaviours
> should be customizable. I'll make is customizable.

I made it customizable like this:

Index: src/org/eclipse/albireo/core/SwingControl.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.core/src/org/eclipse/albireo/core/SwingControl.java,v
retrieving revision 1.3
diff -c -3 -r1.3 SwingControl.java
*** src/org/eclipse/albireo/core/SwingControl.java	23 Jan 2008 18:28:48 -0000	1.3
--- src/org/eclipse/albireo/core/SwingControl.java	23 Jan 2008 20:07:36 -0000
***************
*** 127,144 ****
                  // Invoke hooks, for use by the application.
                  afterComponentCreatedAWTThread();
                  try {
!                     // TODO: Use IlvSWTUtil.asyncExec or have the spurious NPE
!                     // fixed in SWT proper.
!                     getDisplay().asyncExec(
                          new Runnable() {
                              public void run() {
!                                 try {
!                                     afterComponentCreatedSWTThread();
!                                 } catch (Throwable e) {
!                                     // Be friendly to the developer:
!                                     // Show stack trace.
!                                     e.printStackTrace();
!                                 }
                              }
                          });
                  } catch (SWTException e) {
--- 127,137 ----
                  // Invoke hooks, for use by the application.
                  afterComponentCreatedAWTThread();
                  try {
!                     ThreadingHandler.getInstance().asyncExec(
!                         getDisplay(),
                          new Runnable() {
                              public void run() {
!                                 afterComponentCreatedSWTThread();
                              }
                          });
                  } catch (SWTException e) {
*** /dev/null	2006-05-02 08:46:16.000000000 +0200
--- src/org/eclipse/albireo/core/ThreadingHandler.java	2008-01-23 21:06:40.000000000 +0100
***************
*** 0 ****
--- 1,120 ----
+ /*******************************************************************************
+  * Copyright (c) 2005-2008 SAS Institute Inc., ILOG S.A.
+  * All rights reserved. This program and the accompanying materials
+  * are made available under the terms of the Eclipse Public License v1.0
+  * which accompanies this distribution, and is available at
+  * http://www.eclipse.org/legal/epl-v10.html
+  *
+  * Contributors:
+  *     SAS Institute Inc. - initial API and implementation
+  *     ILOG S.A. - initial API and implementation
+  *******************************************************************************/
+ package org.eclipse.albireo.core;
+ 
+ import org.eclipse.swt.SWT;
+ import org.eclipse.swt.SWTException;
+ import org.eclipse.swt.widgets.Display;
+ 
+ /**
+  * This class deals with threading between SWT and AWT.
+  * <p>
+  * It is customizable through the "replaceable singleton" design pattern.
+  * <p>
+  * Note: This class does <em>not</em> combine the two event threads.
+  */
+ public class ThreadingHandler {
+ 
+     // ========================================================================
+     // Accessors
+ 
+     private boolean reportingAsyncExecExceptions = true;
+ 
+     /**
+      * Returns true if exceptions and throwables occurring during asynchronous
+      * execution should be logged on <code>System.err</code>.
+      */
+     public boolean isReportingAsyncExecExceptions() {
+         return reportingAsyncExecExceptions;
+     }
+ 
+     /**
+      * Specifies whether exceptions and throwables occurring during asynchronous
+      * execution should be logged on <code>System.err</code>.
+      * If <code>true</code>, the exception will be shown on the console.
+      * If <code>false</code>, the only way to see the exception is
+      * <ol>
+      *   <li>to be in an application that provides the "Error Log" view,</li>
+      *   <li>to open the "Error Log" view,</li>
+      *   <li>to look at the event details of all "unhandled event loop
+      *       exceptions".</li>
+      * </ol>
+      * The default is <code>true</code>.
+      */
+     public void setReportingAsyncExecExceptions(boolean doReporting) {
+         reportingAsyncExecExceptions = doReporting;
+     }
+ 
+     // ========================================================================
+     // Overridable API
+ 
+     /**
+      * Executes the given <code><var>task</var></code> in the given display's
+      * thread.
+      * <p>Note: <code>Throwable</code>s thrown by <code><var>task</var></code>
+      * will not be displayed. If you want them to be displayed, use a try/catch
+      * block with <code>printStackTrace()</code> inside
+      * <code><var>task</var></code> yourself.
+      *
+      * @throws SWTException <ul>
+      *    <li>ERROR_DEVICE_DISPOSED - if the display has been disposed</li>
+      * </ul>
+      */
+     public void asyncExec(Display display, final Runnable task) {
+         final Runnable final_task;
+         if (reportingAsyncExecExceptions) {
+             final_task =
+                 new Runnable() {
+                     public void run() {
+                         try {
+                             task.run();
+                         } catch (Throwable e) {
+                             e.printStackTrace();
+                         }
+                     }
+                 };
+         } else {
+             final_task = task;
+         }
+         try {
+             display.asyncExec(final_task);
+         } catch (NullPointerException e) {
+             // Workaround for wrong order of actions inside Display.dispose().
+             // http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg30856.html
+             StackTraceElement[] stack = e.getStackTrace();
+             if (stack.length > 0
+                     && "org.eclipse.swt.widgets.Display".equals(stack[0].getClassName())
+                     && "asyncExec".equals(stack[0].getMethodName())) {
+                 SWTException swte = new SWTException(SWT.ERROR_DEVICE_DISPOSED);
+                 swte.throwable = e;
+                 throw swte;
+             } else
+                 throw e;
+         }
+     }
+ 
+     // ========================================================================
+     // Singleton design pattern
+ 
+     private static ThreadingHandler theHandler = new ThreadingHandler();
+ 
+     /**
+      * Returns the currently active singleton of this class.
+      */
+     public static ThreadingHandler getInstance() {
+         return theHandler;
+     }
+     public static void setInstance(ThreadingHandler instance) {
+         theHandler = instance;
+     }
+ 
+ }


Back to the top