javauis/eswt_qt/com.nokia.swt.extensions/midp_symbian/org/eclipse/ercp/swt/midp/UIThreadSupport.java
changeset 35 85266cc22c7f
parent 26 dc7c549001d5
child 40 c6043ea9b06a
child 44 0105bdca6f9c
child 47 f40128debb5d
child 49 35baca0e7a2e
equal deleted inserted replaced
26:dc7c549001d5 35:85266cc22c7f
     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 import org.eclipse.swt.internal.qt.s60.OS;
       
    15 
       
    16 /**
       
    17  * <p>
       
    18  * To use eSWT API a MIDlet needs to dedicate a thread as the UI thread for
       
    19  * running the event loop. This can't happen in the MIDlet.startApp() because
       
    20  * it's required by the MIDP specification that the call to startApp() returns
       
    21  * quickly. Additionally, many native UI toolkits have restrictions for the
       
    22  * thread that can be used as the UI thread. Thus, a random Java thread can't be
       
    23  * used either.
       
    24  * </p>
       
    25  * <p>
       
    26  * This class provides means to obtain a thread that can be used as the UI
       
    27  * thread on the platform. All MIDlets must use this class to obtain the UI
       
    28  * thread. If a MIDlet uses some other thread as the UI thread then the behavior
       
    29  * is undefined.
       
    30  * </p>
       
    31  * 
       
    32  * @see MIDlet
       
    33  * @see MIDlet#startApp
       
    34  */
       
    35 public class UIThreadSupport {
       
    36 
       
    37 private static boolean started;
       
    38 
       
    39 /**
       
    40  * <p>
       
    41  * Calls back the given Runnable in a thread that can be used as the UI thread.
       
    42  * The method must not be called more than once as there might be only one
       
    43  * thread in the process capable of being used as the UI thread.
       
    44  * </p>
       
    45  * 
       
    46  * An example: <code><pre>
       
    47  * ...
       
    48  * class MyMIDlet extends javax.microedition.midlet.MIDlet {
       
    49  *     ...
       
    50  *     public void startApp() {
       
    51  *         UIThreadSupport.startInUIThread(new Runnable() {
       
    52  *             public void run() {
       
    53  *                 Display display = new Display();
       
    54  *                 ...
       
    55  *             }
       
    56  *          });
       
    57  *     }
       
    58  *     ...
       
    59  * }
       
    60  * </pre></code>
       
    61  * 
       
    62  * @param runnable The Runnable object to call back
       
    63  * @exception SWTError <ul>
       
    64  *                <li>ERROR_NO_HANDLES if a handle could not be obtained for
       
    65  *                thread creation</li>
       
    66  * @exception SWTError <ul>
       
    67  *                <li>ERROR_FAILED_EXEC if called more than once</li>
       
    68  * @see MIDlet
       
    69  * @see MIDlet#startApp
       
    70  */
       
    71 public static void startInUIThread(Runnable runnable) {
       
    72     synchronized(UIThreadSupport.class) {
       
    73         if(started) {
       
    74             SWT.error(SWT.ERROR_FAILED_EXEC);
       
    75         }
       
    76         started = true;
       
    77     }
       
    78     int status = OS.startUI(runnable);
       
    79     if(status != 0) {
       
    80         SWT.error(SWT.ERROR_NO_HANDLES);
       
    81     }
       
    82 }
       
    83 
       
    84 }