001 /*
002 * Copyright 2008-2017 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.application.Platform;
021 import javafx.stage.Stage;
022 import org.codehaus.griffon.runtime.core.DefaultApplicationBootstrapper;
023
024 import javax.annotation.Nonnull;
025 import java.util.Map;
026
027 import static griffon.core.GriffonExceptionHandler.registerExceptionHandler;
028
029 /**
030 * @author Dean Iverson
031 * @author Andres Almiray
032 */
033 public class JavaFXGriffonApplication extends AbstractJavaFXGriffonApplication {
034 protected Stage primaryStage;
035 private boolean primaryStageDispensed = false;
036
037 public JavaFXGriffonApplication() {
038 this(EMPTY_ARGS);
039 }
040
041 public JavaFXGriffonApplication(@Nonnull String[] args) {
042 super(args);
043 }
044
045 public static void run(Class<? extends Application> applicationClass, String[] args) {
046 registerExceptionHandler();
047 Application.launch(applicationClass, args);
048 }
049
050 public static void main(String[] args) {
051 run(JavaFXGriffonApplication.class, args);
052 }
053
054 @Nonnull
055 @Override
056 public Object createApplicationContainer(@Nonnull Map<String, Object> attributes) {
057 if (primaryStageDispensed) {
058 return new Stage();
059 } else {
060 primaryStageDispensed = true;
061 return primaryStage;
062 }
063 }
064
065 @Override
066 public void init() throws Exception {
067 ApplicationBootstrapper bootstrapper = createApplicationBootstrapper();
068 bootstrapper.bootstrap();
069 afterInit();
070 }
071
072 protected void afterInit() {
073 initialize();
074 }
075
076 @Nonnull
077 protected ApplicationBootstrapper createApplicationBootstrapper() {
078 return new DefaultApplicationBootstrapper(this);
079 }
080
081 @Override
082 public void start(Stage stage) throws Exception {
083 super.start(stage);
084 primaryStage = stage;
085
086 afterStart();
087 }
088
089 protected void afterStart() {
090 getUIThreadManager().runOutsideUI(() -> {
091 startup();
092 ready();
093 afterReady();
094 });
095 }
096
097 protected void afterReady() {
098 // empty
099 }
100
101 @Override
102 public boolean shutdown() {
103 if (super.shutdown()) {
104 exit();
105 }
106 return false;
107 }
108
109 public void exit() {
110 Platform.exit();
111 System.exit(0);
112 }
113 }
|