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