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