JavaFXGriffonApplication.java
001 /*
002  * Copyright 2008-2014 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     private boolean primaryStageDispensed = false;
034     protected Stage primaryStage;
035 
036     public JavaFXGriffonApplication() {
037         this(EMPTY_ARGS);
038     }
039 
040     public JavaFXGriffonApplication(@Nonnull String[] args) {
041         super(args);
042     }
043 
044     @Nonnull
045     @Override
046     public Object createApplicationContainer(@Nonnull Map<String, Object> attributes) {
047         if (primaryStageDispensed) {
048             return new Stage();
049         else {
050             primaryStageDispensed = true;
051             return primaryStage;
052         }
053     }
054 
055     @Override
056     public void init() throws Exception {
057         ApplicationBootstrapper bootstrapper = createApplicationBootstrapper();
058         bootstrapper.bootstrap();
059         afterInit();
060     }
061 
062     protected void afterInit() {
063         initialize();
064     }
065 
066     @Nonnull
067     protected ApplicationBootstrapper createApplicationBootstrapper() {
068         return new DefaultApplicationBootstrapper(this);
069     }
070 
071     @Override
072     public void start(Stage stagethrows Exception {
073         super.start(stage);
074         primaryStage = stage;
075 
076         getUIThreadManager().runOutsideUI(new Runnable() {
077             @Override
078             public void run() {
079                 startup();
080                 ready();
081                 afterReady();
082             }
083         });
084     }
085 
086     protected void afterReady() {
087         // empty
088     }
089 
090     public boolean shutdown() {
091         if (super.shutdown()) {
092             exit();
093         }
094         return false;
095     }
096 
097     public void exit() {
098         System.exit(0);
099     }
100 
101     public static void run(Class<? extends Application> applicationClass, String[] args) {
102         registerExceptionHandler();
103         Application.launch(applicationClass, args);
104     }
105 
106     public static void main(String[] args) {
107         run(JavaFXGriffonApplication.class, args);
108     }
109 }