001        package com.croftsoft.apps.exemplar;
002         
003        import java.awt.*;
004        import javax.swing.*;
005        
006        import com.croftsoft.core.gui.LifecycleWindowListener;
007        import com.croftsoft.core.lang.lifecycle.Lifecycle;
008        import com.croftsoft.core.lang.lifecycle.LifecycleLib;
009        import com.croftsoft.core.lang.lifecycle.Updatable;
010        import com.croftsoft.core.util.loop.EventQueueUpdateLoop;
011        import com.croftsoft.core.util.loop.Looper;
012        import com.croftsoft.core.util.loop.NanoTimeLoopGovernor;
013        import com.croftsoft.core.util.mail.FlipMail;
014         
015        /***********************************************************************
016        * Main.
017        *
018        * Launches the application within a framework.
019        * 
020        * @version
021        *   $Id: ExemplarMain.java,v 1.8 2008/02/18 23:22:49 croft Exp $
022        * @since
023        *   2006-01-03
024        * @author
025        *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
026        ***********************************************************************/
027    
028        public final class  ExemplarMain
029          implements Lifecycle
030        ////////////////////////////////////////////////////////////////////////
031        ////////////////////////////////////////////////////////////////////////
032        {
033           
034        private final ExemplarConfig      exemplarConfig;
035         
036        private final ExemplarModelImp    exemplarModelImp;
037         
038        private final ExemplarController  exemplarController;
039           
040        private final ExemplarView        exemplarView;
041         
042        private final Looper              looper;
043           
044        ////////////////////////////////////////////////////////////////////////
045        // public static methods
046        ////////////////////////////////////////////////////////////////////////
047    
048        public static void  main ( final String [ ]  args )
049        ////////////////////////////////////////////////////////////////////////
050        {
051          final ExemplarMain  exemplarMain = new ExemplarMain ( args );
052           
053          final JFrame  jFrame = new JFrame (
054            exemplarMain.exemplarConfig.getFrameTitle ( ) );
055           
056          exemplarMain.setContentPane ( jFrame.getContentPane ( ) );
057           
058          // The Frame is the framework.
059           
060          LifecycleWindowListener.launchFrameAsDesktopApp (
061            jFrame,
062            exemplarMain,
063            exemplarMain.exemplarConfig.getFrameSize ( ),
064            exemplarMain.exemplarConfig.getShutdownConfirmationPrompt ( ) );
065        }
066         
067        ////////////////////////////////////////////////////////////////////////
068        // constructor methods
069        ////////////////////////////////////////////////////////////////////////
070         
071        public  ExemplarMain ( final String [ ]  args )
072        ////////////////////////////////////////////////////////////////////////
073        {
074          exemplarConfig = ExemplarConfigImp.load ( args );
075           
076          System.out.println ( "\n" + exemplarConfig.getInfo ( ) );
077          
078          final FlipMail<ExemplarMessage>  flipMail
079            = new FlipMail<ExemplarMessage> ( );
080           
081          exemplarModelImp = new ExemplarModelImp (
082            exemplarConfig,
083            flipMail );
084           
085          exemplarController = new ExemplarController ( flipMail );
086           
087          exemplarView = new ExemplarView (
088            exemplarConfig,
089            flipMail,
090            exemplarModelImp );
091           
092          exemplarView.addMouseListener ( exemplarController );
093           
094          final Updatable [ ]  updatables = new Updatable [ ] {
095            flipMail,
096            exemplarModelImp,
097            exemplarView,
098            exemplarController };
099           
100          looper = new Looper (
101            new EventQueueUpdateLoop ( updatables ), // loopable
102            new NanoTimeLoopGovernor ( exemplarConfig.getUpdateRate ( ) ),
103            null, // exceptionHandler
104            exemplarConfig.getThreadName ( ),
105            Thread.MIN_PRIORITY,
106            true ); // useDaemonThread
107        }
108         
109        ////////////////////////////////////////////////////////////////////////
110        // accessor methods
111        ////////////////////////////////////////////////////////////////////////
112         
113        public ExemplarConfig  getExemplarConfig ( )
114        ////////////////////////////////////////////////////////////////////////
115        {
116          return exemplarConfig;
117        }
118         
119        ////////////////////////////////////////////////////////////////////////
120        // mutator methods
121        ////////////////////////////////////////////////////////////////////////
122         
123        public void  setContentPane ( final Container  contentPane )
124        ////////////////////////////////////////////////////////////////////////
125        {
126          exemplarView.setContentPane ( contentPane );
127        }
128         
129        ////////////////////////////////////////////////////////////////////////
130        // interface Lifecycle methods
131        ////////////////////////////////////////////////////////////////////////
132         
133        public void  init ( )
134        ////////////////////////////////////////////////////////////////////////
135        {
136          LifecycleLib.init ( exemplarView, looper );
137        }
138         
139        public void  start ( )
140        ////////////////////////////////////////////////////////////////////////
141        {
142          LifecycleLib.start (
143            exemplarView, exemplarController, exemplarModelImp, looper );
144        }
145         
146        public void  stop ( )
147        ////////////////////////////////////////////////////////////////////////
148        {
149          LifecycleLib.stop ( exemplarView, looper );
150        }
151         
152        public void  destroy ( )
153        ////////////////////////////////////////////////////////////////////////
154        {
155          LifecycleLib.destroy ( exemplarView, looper );
156        }     
157           
158        ////////////////////////////////////////////////////////////////////////
159        ////////////////////////////////////////////////////////////////////////
160        }