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