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