Coverage Report - org.jtheque.core.managers.lifecycle.LifeCycleManager
 
Classes in this File Line Coverage Branch Coverage Complexity
LifeCycleManager
0 %
0/84
0 %
0/24
1.462
LifeCycleManager$1
0 %
0/5
N/A
1.462
LifeCycleManager$ApplicationShutDownHook
0 %
0/5
N/A
1.462
LifeCycleManager$TitleUpdater
0 %
0/3
N/A
1.462
 
 1  
 package org.jtheque.core.managers.lifecycle;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jdesktop.swingx.event.WeakEventListenerList;
 20  
 import org.jtheque.core.managers.Managers;
 21  
 import org.jtheque.core.managers.beans.ioc.Ioc;
 22  
 import org.jtheque.core.managers.event.EventLevel;
 23  
 import org.jtheque.core.managers.event.EventLog;
 24  
 import org.jtheque.core.managers.event.IEventManager;
 25  
 import org.jtheque.core.managers.language.ILanguageManager;
 26  
 import org.jtheque.core.managers.language.Internationalizable;
 27  
 import org.jtheque.core.managers.lifecycle.listeners.FunctionEvent;
 28  
 import org.jtheque.core.managers.lifecycle.listeners.FunctionListener;
 29  
 import org.jtheque.core.managers.lifecycle.listeners.TitleEvent;
 30  
 import org.jtheque.core.managers.lifecycle.listeners.TitleListener;
 31  
 import org.jtheque.core.managers.lifecycle.phases.IPhasesManager;
 32  
 import org.jtheque.core.managers.log.Log4JConfigurator;
 33  
 import org.jtheque.core.managers.module.IModuleManager;
 34  
 import org.jtheque.core.managers.module.loaders.ModuleLoader;
 35  
 import org.jtheque.core.managers.view.SplashManager;
 36  
 import org.jtheque.core.managers.view.able.IViewManager;
 37  
 import org.jtheque.utils.DesktopUtils;
 38  
 
 39  
 /**
 40  
  * A JTheque application implementation. This class manage the cycle life of JTheque.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  0
 public final class LifeCycleManager implements ILifeCycleManager, Internationalizable {
 45  0
     private final WeakEventListenerList listenerList = new WeakEventListenerList();
 46  
 
 47  0
     private final Thread shutdownHook = new ApplicationShutDownHook();
 48  
 
 49  
     private String currentFunction;
 50  0
     private String title = "JTheque";
 51  
 
 52  0
     private final Instances instances = new Instances();
 53  
 
 54  
     private IPhasesManager phasesManager;
 55  
 
 56  
     @Override
 57  
     public void initCycles() {
 58  0
         registerApplication();
 59  
 
 60  0
         SplashManager.getInstance().displaySplashScreen();
 61  
 
 62  0
         Log4JConfigurator.configure();
 63  
 
 64  0
         ModuleLoader.loadModules();
 65  
 
 66  0
         Ioc.getContainer().loadContext();
 67  
 
 68  0
         phasesManager = (IPhasesManager) Ioc.getContainer().getApplicationContext().getBean("phasesManager");
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Register the application.
 73  
      */
 74  
     private void registerApplication() {
 75  0
         instances.launchApplication();
 76  
 
 77  0
         Runtime.getRuntime().addShutdownHook(shutdownHook);
 78  0
     }
 79  
 
 80  
     @Override
 81  
     public void launchNextPhase() {
 82  0
         phasesManager.launchNextPhase();
 83  0
     }
 84  
 
 85  
     @Override
 86  
     public void initTitle() {
 87  0
         refreshTitle();
 88  0
         Managers.getManager(ILanguageManager.class).addInternationalizable(new TitleUpdater());
 89  0
     }
 90  
 
 91  
     @Override
 92  
     public void exit() {
 93  0
         releaseAll();
 94  
 
 95  0
         closeVM();
 96  0
     }
 97  
 
 98  
     @Override
 99  
     public void restart() {
 100  0
         releaseAll();
 101  
 
 102  0
         DesktopUtils.open(Managers.getCore().getFiles().getLauncherFile());
 103  
 
 104  0
         closeVM();
 105  0
     }
 106  
 
 107  
     @Override
 108  
     public void setCurrentFunction(String function) {
 109  0
         currentFunction = function;
 110  
 
 111  0
         fireFunctionUpdated(new FunctionEvent(this, function));
 112  
 
 113  0
         refreshTitle();
 114  0
     }
 115  
 
 116  
     @Override
 117  
     public String getCurrentFunction() {
 118  0
         return currentFunction;
 119  
     }
 120  
 
 121  
     @Override
 122  
     public String getTitle() {
 123  0
         return title;
 124  
     }
 125  
 
 126  
     @Override
 127  
     public void addTitleListener(TitleListener listener) {
 128  0
         if (listener != null) {
 129  0
             listenerList.add(TitleListener.class, listener);
 130  
         }
 131  0
     }
 132  
 
 133  
     @Override
 134  
     public void removeTitleListener(TitleListener listener) {
 135  0
         if (listener != null) {
 136  0
             listenerList.remove(TitleListener.class, listener);
 137  
         }
 138  0
     }
 139  
 
 140  
     @Override
 141  
     public void addFunctionListener(FunctionListener listener) {
 142  0
         if (listener != null) {
 143  0
             listenerList.add(FunctionListener.class, listener);
 144  
         }
 145  0
     }
 146  
 
 147  
     @Override
 148  
     public void removeFunctionListener(FunctionListener listener) {
 149  0
         if (listener != null) {
 150  0
             listenerList.remove(FunctionListener.class, listener);
 151  
         }
 152  0
     }
 153  
 
 154  
     @Override
 155  
     public void chooseCollection(String collection, String password, boolean create) {
 156  0
         boolean chosen = Managers.getManager(IModuleManager.class).chooseCollection(collection, password, create);
 157  
 
 158  0
         if (chosen) {
 159  0
             new Thread(new Runnable() {
 160  
                 @Override
 161  
                 public void run() {
 162  0
                     Managers.getManager(IViewManager.class).getSplashManager().displaySplashScreen();
 163  
 
 164  0
                     Managers.getManager(IModuleManager.class).plugCollection();
 165  
 
 166  0
                     launchNextPhase();
 167  0
                 }
 168  
             }).start();
 169  
         } else {
 170  0
             Managers.getManager(IViewManager.class).getCollectionView().setErrorMessage(Managers.getManager(ILanguageManager.class).getMessage("error.module.collection"));
 171  
         }
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Start the exit process but not stop the application.
 176  
      */
 177  
     private void releaseAll() {
 178  0
         Managers.getManager(IEventManager.class).addEventLog("JTheque Core", new EventLog(EventLevel.INFO, "User", "events.close"));
 179  
 
 180  0
         if (phasesManager != null && phasesManager.isDone()) {
 181  0
             Managers.getManager(IModuleManager.class).unplugModules();
 182  
         }
 183  
 
 184  0
         Managers.closeManagers();
 185  
 
 186  0
         if (instances != null) {
 187  0
             instances.closeInstance();
 188  
         }
 189  0
     }
 190  
 
 191  
     /**
 192  
      * Close the VM.
 193  
      */
 194  
     private void closeVM() {
 195  0
         Runtime.getRuntime().removeShutdownHook(shutdownHook);
 196  0
         System.exit(0);
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Refresh the title of the application. Call this method when the title is subject to change.
 201  
      */
 202  
     private void refreshTitle() {
 203  0
         updateTitle();
 204  
 
 205  0
         fireTitleUpdated(new TitleEvent(this, title));
 206  0
     }
 207  
 
 208  
     /**
 209  
      * Update the title.
 210  
      */
 211  
     private void updateTitle() {
 212  0
         StringBuilder builder = new StringBuilder(50);
 213  
 
 214  0
         if (currentFunction != null && !currentFunction.isEmpty()) {
 215  0
             builder.append(currentFunction);
 216  0
             builder.append(" - ");
 217  
         }
 218  
 
 219  0
         builder.append(Managers.getCore().getApplication().getName());
 220  0
         builder.append(' ');
 221  0
         builder.append(Managers.getCore().getApplication().getVersion());
 222  
 
 223  0
         title = builder.toString();
 224  0
     }
 225  
 
 226  
     /**
 227  
      * Fire a titleUpdated event. This method avert the listeners that the title of the application has changed.
 228  
      *
 229  
      * @param event The title event.
 230  
      */
 231  
     private void fireTitleUpdated(TitleEvent event) {
 232  0
         TitleListener[] listeners = listenerList.getListeners(TitleListener.class);
 233  
 
 234  0
         for (TitleListener listener : listeners) {
 235  0
             listener.titleUpdated(event);
 236  
         }
 237  0
     }
 238  
 
 239  
     /**
 240  
      * Fire a functionUpdated event. This method avert the listeners that the current function has changed.
 241  
      *
 242  
      * @param event The function event.
 243  
      */
 244  
     private void fireFunctionUpdated(FunctionEvent event) {
 245  0
         FunctionListener[] listeners = listenerList.getListeners(FunctionListener.class);
 246  
 
 247  0
         for (FunctionListener listener : listeners) {
 248  0
             listener.functionUpdated(event);
 249  
         }
 250  0
     }
 251  
 
 252  
     /* Listeners methods */
 253  
 
 254  
     @Override
 255  
     public void refreshText() {
 256  0
         refreshTitle();
 257  0
     }
 258  
 
 259  
     @Override
 260  
     public boolean isSecondPhaseDone() {
 261  0
         return phasesManager.isDone();
 262  
     }
 263  
 
 264  
     /**
 265  
      * An updater to reflect the locale in the title when a change occurs.
 266  
      *
 267  
      * @author Baptiste Wicht
 268  
      */
 269  0
     private final class TitleUpdater implements Internationalizable {
 270  
         @Override
 271  
         public void refreshText() {
 272  0
             refreshTitle();
 273  0
         }
 274  
     }
 275  
 
 276  
     /**
 277  
      * This class is a hook on the shutdown of JTheque. When a shutdown is detected, the thread
 278  
      * detect it and if this isn't JTheque who has executed the close, we properly close the
 279  
      * resources. This is for prevent accident kill of JTheque.
 280  
      *
 281  
      * @author Baptiste Wicht
 282  
      */
 283  0
     private final class ApplicationShutDownHook extends Thread {
 284  
         /**
 285  
          * Construct a new <code>ApplicationShutDownHook</code>.
 286  
          */
 287  0
         ApplicationShutDownHook() {
 288  0
             super();
 289  0
         }
 290  
 
 291  
         @Override
 292  
         public void run() {
 293  0
             exit();
 294  0
         }
 295  
     }
 296  
 }