001package votorola.a.web.gwt; // Copyright 2012-2013, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import com.google.gwt.core.client.*;
004import java.util.*;
005
006
007/** A modified entry point for modules whose execution depends on other application
008  * modules.  It need only be implemented by modules that require the deferred {@linkplain
009  * #execute execute}() method.  For this method to work, one of the top level modules of
010  * the application (on which no other module depends) must call EntryPointS.U.{@linkplain
011  * EntryPointS.U#execute() execute}() at the end of its own {@linkplain #onModuleLoad()
012  * onModuleLoad}().
013  */
014public interface EntryPointS extends EntryPoint, Scheduler.ScheduledCommand
015{
016
017
018   // - E n t r y - P o i n t ------------------------------------------------------------
019
020
021    /** Called first during the load of any module M that declares an
022      * <code>entry-point</code> in its declaration file <code>M.gwt.xml</code>, this
023      * method (a) finalizes the {@linkplain GWTConfigCallback configuration} of any other modules
024      * on which this one depends, and (b) ensures that any views are solidly placed that
025      * might affect the position or size of already placed views, such as those of an
026      * embedding page or those previously attached by other modules.  This is to ensure
027      * that the page content does not jitter from late attachments.  Otherwise this
028      * method depends as little as possible on other {@linkplain votorola.a application}
029      * modules, as they might not be fully functional yet.
030      *
031      * <p>This method may also (c) {@linkplain EntryPointS.U#schedule(EntryPointS)
032      * schedule} the invocation of the {@linkplain #execute() execute method}.</p>
033      */
034    public void onModuleLoad();
035
036
037
038   // - S c h e d u l e r . S c h e d u l e d - C o m m a n d ----------------------------
039
040
041    /** Called after all modules are loaded provided the call was previously {@linkplain
042      * EntryPointS.U#schedule(EntryPointS) scheduled}.
043      *
044      *     @see #onModuleLoad()
045      */
046    public void execute();
047
048
049
050   // ====================================================================================
051
052
053    /** EntryPointS utilities.
054      */
055    public static final class U
056    {
057
058
059        private U() {}
060
061
062        private static ArrayList<EntryPointS> entryPointList = // nulled after execution
063          new ArrayList<EntryPointS>( /*initial capacity*/8 );
064
065
066       // --------------------------------------------------------------------------------
067
068
069       /** Triggers execution of all modules that were previously {@linkplain
070         * #schedule(EntryPointS) scheduled}.  Actual execution is not necessarily
071         * immediate, but may be further delayed.  Call this method exactly once from the
072         * top level module of the application (the module on which no other module
073         * depends) at the end of its {@linkplain EntryPointS#onModuleLoad()
074         * onModuleLoad}().
075         */
076        public static void execute()
077        {
078            if( entryPointList == null ) throw new IllegalStateException( "called twice" );
079
080            final ArrayList<EntryPointS> pList = entryPointList;
081            entryPointList = null; // all modules executed, free the memory
082            for( EntryPointS p: pList ) p.execute(); // immediately, no need for async execution yet
083        }
084
085
086        /** Schedules p.{@linkplain EntryPointS#execute() execute}() to be called after
087          * all modules are loaded.
088          */
089        public static void schedule( final EntryPointS p )
090        {
091            assert entryPointList != null: "called before modules executed";
092            entryPointList.add( p );
093        }
094
095
096    }
097
098
099}