javauis/eswt_qt/com.nokia.swt.extensions/midp_common/org/eclipse/ercp/swt/midp/UIThreadSupport.java
branchGCC_SURGE
changeset 40 c6043ea9b06a
parent 38 854be117f6a5
parent 35 85266cc22c7f
child 41 6918cb13caa7
equal deleted inserted replaced
38:854be117f6a5 40:c6043ea9b06a
     1 /*******************************************************************************
       
     2  * Copyright (c) 2009, 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials 
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  * 
       
     8  * Contributors:
       
     9  *     Nokia Corporation - initial implementation 
       
    10  *******************************************************************************/
       
    11 package org.eclipse.ercp.swt.midp;
       
    12 
       
    13 import org.eclipse.swt.SWT;
       
    14 
       
    15 /**
       
    16  * <p>
       
    17  * To use eSWT API a MIDlet needs to dedicate a thread as the UI thread for
       
    18  * running the event loop. This can't happen in the MIDlet.startApp() because
       
    19  * it's required by the MIDP specification that the call to startApp() returns
       
    20  * quickly. Additionally, many native UI toolkits have restrictions for the
       
    21  * thread that can be used as the UI thread. Thus, a random Java thread can't be
       
    22  * used either.
       
    23  * </p>
       
    24  * <p>
       
    25  * This class provides means to obtain a thread that can be used as the UI
       
    26  * thread on the platform. All MIDlets must use this class to obtain the UI
       
    27  * thread. If a MIDlet uses some other thread as the UI thread then the behavior
       
    28  * is undefined.
       
    29  * </p>
       
    30  * 
       
    31  * @see MIDlet
       
    32  * @see MIDlet#startApp
       
    33  */
       
    34 public class UIThreadSupport {
       
    35 
       
    36 private static boolean started;
       
    37 
       
    38 /**
       
    39  * <p>
       
    40  * Calls back the given Runnable in a thread that can be used as the UI thread.
       
    41  * The method must not be called more than once as there might be only one
       
    42  * thread in the process capable of being used as the UI thread.
       
    43  * </p>
       
    44  * 
       
    45  * An example: <code><pre>
       
    46  * ...
       
    47  * class MyMIDlet extends javax.microedition.midlet.MIDlet {
       
    48  *     ...
       
    49  *     public void startApp() {
       
    50  *         UIThreadSupport.startInUIThread(new Runnable() {
       
    51  *             public void run() {
       
    52  *                 Display display = new Display();
       
    53  *                 ...
       
    54  *             }
       
    55  *          });
       
    56  *     }
       
    57  *     ...
       
    58  * }
       
    59  * </pre></code>
       
    60  * 
       
    61  * @param runnable The Runnable object to call back
       
    62  * @exception SWTError <ul>
       
    63  *                <li>ERROR_NO_HANDLES if a handle could not be obtained for
       
    64  *                thread creation</li>
       
    65  * @exception SWTError <ul>
       
    66  *                <li>ERROR_FAILED_EXEC if called more than once</li>
       
    67  * @see MIDlet
       
    68  * @see MIDlet#startApp
       
    69  */
       
    70 public static void startInUIThread(Runnable runnable) {
       
    71     synchronized(UIThreadSupport.class) {
       
    72         if(started) {
       
    73             SWT.error(SWT.ERROR_FAILED_EXEC);
       
    74         }
       
    75         started = true;
       
    76     }
       
    77     try {
       
    78         new Thread(runnable).start();
       
    79     } catch(Exception e) {
       
    80         SWT.error(SWT.ERROR_NO_HANDLES);
       
    81     }
       
    82 }
       
    83 
       
    84 }