JavaFXGriffonApplication.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.javafx;
017 
018 import griffon.core.ApplicationBootstrapper;
019 import javafx.application.Application;
020 import javafx.stage.Stage;
021 import org.codehaus.griffon.runtime.core.DefaultApplicationBootstrapper;
022 
023 import javax.annotation.Nonnull;
024 import java.util.Map;
025 
026 import static griffon.core.GriffonExceptionHandler.registerExceptionHandler;
027 
028 /**
029  @author Dean Iverson
030  @author Andres Almiray
031  */
032 public class JavaFXGriffonApplication extends AbstractJavaFXGriffonApplication {
033     protected Stage primaryStage;
034     private boolean primaryStageDispensed = false;
035 
036     public JavaFXGriffonApplication() {
037         this(EMPTY_ARGS);
038     }
039 
040     public JavaFXGriffonApplication(@Nonnull String[] args) {
041         super(args);
042     }
043 
044     public static void run(Class<? extends Application> applicationClass, String[] args) {
045         registerExceptionHandler();
046         Application.launch(applicationClass, args);
047     }
048 
049     public static void main(String[] args) {
050         run(JavaFXGriffonApplication.class, args);
051     }
052 
053     @Nonnull
054     @Override
055     public Object createApplicationContainer(@Nonnull Map<String, Object> attributes) {
056         if (primaryStageDispensed) {
057             return new Stage();
058         else {
059             primaryStageDispensed = true;
060             return primaryStage;
061         }
062     }
063 
064     @Override
065     public void init() throws Exception {
066         ApplicationBootstrapper bootstrapper = createApplicationBootstrapper();
067         bootstrapper.bootstrap();
068         afterInit();
069     }
070 
071     protected void afterInit() {
072         initialize();
073     }
074 
075     @Nonnull
076     protected ApplicationBootstrapper createApplicationBootstrapper() {
077         return new DefaultApplicationBootstrapper(this);
078     }
079 
080     @Override
081     public void start(Stage stagethrows Exception {
082         super.start(stage);
083         primaryStage = stage;
084 
085         afterStart();
086     }
087 
088     protected void afterStart() {
089         getUIThreadManager().runOutsideUI(new Runnable() {
090             @Override
091             public void run() {
092                 startup();
093                 ready();
094                 afterReady();
095             }
096         });
097     }
098 
099     protected void afterReady() {
100         // empty
101     }
102 
103     public boolean shutdown() {
104         if (super.shutdown()) {
105             exit();
106         }
107         return false;
108     }
109 
110     public void exit() {
111         System.exit(0);
112     }
113 }