001 /*
002 * Copyright 2008-2015 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 org.codehaus.griffon.runtime.javafx;
017
018 import griffon.core.ApplicationEvent;
019 import griffon.core.GriffonApplication;
020 import griffon.core.env.ApplicationPhase;
021 import griffon.javafx.JavaFXWindowDisplayHandler;
022 import griffon.javafx.JavaFXWindowManager;
023 import javafx.event.EventHandler;
024 import javafx.stage.Window;
025 import javafx.stage.WindowEvent;
026 import org.codehaus.griffon.runtime.core.view.AbstractWindowManager;
027
028 import javax.annotation.Nonnull;
029 import javax.inject.Inject;
030 import javax.inject.Named;
031
032 import static java.util.Arrays.asList;
033 import static java.util.Objects.requireNonNull;
034
035 /**
036 * @author Andres Almiray
037 * @since 2.0.0
038 */
039 public class DefaultJavaFXWindowManager extends AbstractWindowManager<Window> implements JavaFXWindowManager {
040 private final OnWindowHidingHelper onWindowHiding = new OnWindowHidingHelper();
041 private final OnWindowShownHelper onWindowShown = new OnWindowShownHelper();
042 private final OnWindowHiddenHelper onWindowHidden = new OnWindowHiddenHelper();
043
044 @Inject
045 @Nonnull
046 public DefaultJavaFXWindowManager(@Nonnull GriffonApplication application, @Nonnull @Named("windowDisplayHandler") JavaFXWindowDisplayHandler windowDisplayHandler) {
047 super(application, windowDisplayHandler);
048 requireNonNull(application.getEventRouter(), "Argument 'application.eventRouter' must not be null");
049 }
050
051 @Override
052 protected void doAttach(@Nonnull Window window) {
053 requireNonNull(window, ERROR_WINDOW_NULL);
054 window.setOnHiding(onWindowHiding);
055 window.setOnShown(onWindowShown);
056 window.setOnHidden(onWindowHidden);
057 }
058
059 @Override
060 protected void doDetach(@Nonnull Window window) {
061 requireNonNull(window, ERROR_WINDOW_NULL);
062 window.setOnHiding(null);
063 window.setOnShown(null);
064 window.setOnHidden(null);
065 }
066
067 @Override
068 protected boolean isWindowVisible(@Nonnull Window window) {
069 requireNonNull(window, ERROR_WINDOW_NULL);
070 return window.isShowing();
071 }
072
073 public void handleClose(@Nonnull Window widget) {
074 if (getApplication().getPhase() == ApplicationPhase.SHUTDOWN) {
075 return;
076 }
077 int visibleWindows = 0;
078 for (Window window : getWindows()) {
079 if (window.isShowing()) {
080 visibleWindows++;
081 }
082 }
083
084 if (visibleWindows <= 1 && isAutoShutdown()) {
085 if (!getApplication().shutdown())
086 show(widget);
087 }
088 }
089
090 /**
091 * WindowAdapter that invokes hide() when the window is about to be closed.
092 *
093 * @author Andres Almiray
094 */
095 private class OnWindowHidingHelper implements EventHandler<WindowEvent> {
096 public void handle(WindowEvent event) {
097 hide((Window) event.getSource());
098 handleClose((Window) event.getSource());
099 }
100 }
101
102 /**
103 * Listener that triggers application events when a window is shown.
104 *
105 * @author Andres Almiray
106 */
107 private class OnWindowShownHelper implements EventHandler<WindowEvent> {
108 /**
109 * Triggers a <tt>WindowShown</tt> event with the window as sole argument
110 */
111 public void handle(WindowEvent windowEvent) {
112 event(ApplicationEvent.WINDOW_SHOWN, asList(windowEvent.getSource()));
113 }
114 }
115
116 /**
117 * Listener that triggers application events when a window is hidden.
118 *
119 * @author Andres Almiray
120 */
121 private class OnWindowHiddenHelper implements EventHandler<WindowEvent> {
122 /**
123 * Triggers a <tt>WindowHidden</tt> event with the window as sole argument
124 */
125 public void handle(WindowEvent windowEvent) {
126 event(ApplicationEvent.WINDOW_HIDDEN, asList(windowEvent.getSource()));
127 }
128 }
129 }
|