DefaultPivotWindowManager.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.pivot;
017 
018 import griffon.core.ApplicationEvent;
019 import griffon.core.GriffonApplication;
020 import griffon.core.env.ApplicationPhase;
021 import griffon.pivot.PivotWindowDisplayHandler;
022 import griffon.pivot.PivotWindowManager;
023 import griffon.pivot.support.adapters.WindowStateAdapter;
024 import org.apache.pivot.util.Vote;
025 import org.apache.pivot.wtk.Display;
026 import org.apache.pivot.wtk.Window;
027 import org.codehaus.griffon.runtime.core.view.AbstractWindowManager;
028 
029 import javax.annotation.Nonnull;
030 import javax.inject.Inject;
031 import javax.inject.Named;
032 
033 import static java.util.Arrays.asList;
034 import static java.util.Objects.requireNonNull;
035 
036 /**
037  @author Andres Almiray
038  @since 2.0.0
039  */
040 public class DefaultPivotWindowManager extends AbstractWindowManager<Window> implements PivotWindowManager {
041     private final WindowStateHelper windowStateHelper = new WindowStateHelper();
042 
043     @Inject
044     @Nonnull
045     public DefaultPivotWindowManager(@Nonnull GriffonApplication application, @Nonnull @Named("windowDisplayHandler"PivotWindowDisplayHandler windowDisplayHandler) {
046         super(application, windowDisplayHandler);
047         requireNonNull(application.getEventRouter()"Argument 'application.eventRouter' must not be null");
048     }
049 
050     @Override
051     protected void doAttach(@Nonnull Window window) {
052         requireNonNull(window, ERROR_WINDOW_NULL);
053         window.getWindowStateListeners().add(windowStateHelper);
054     }
055 
056     @Override
057     protected void doDetach(@Nonnull Window window) {
058         requireNonNull(window, ERROR_WINDOW_NULL);
059         window.getWindowStateListeners().remove(windowStateHelper);
060     }
061 
062     @Override
063     protected boolean isWindowVisible(@Nonnull Window window) {
064         requireNonNull(window, ERROR_WINDOW_NULL);
065         return window.isVisible();
066     }
067 
068     public void handleClose(@Nonnull Window widget) {
069         if (getApplication().getPhase() == ApplicationPhase.SHUTDOWN) {
070             return;
071         }
072         int visibleWindows = 0;
073         for (Window window : getWindows()) {
074             if (window.isShowing()) {
075                 visibleWindows++;
076             }
077         }
078 
079         if (visibleWindows <= && isAutoShutdown()) {
080             if (!getApplication().shutdown())
081                 show(widget);
082         }
083     }
084 
085     /**
086      * WindowStateAdapter that triggers application events when a window is shown/hidden.
087      *
088      @author Andres Almiray
089      */
090     private class WindowStateHelper extends WindowStateAdapter {
091         @Override
092         public Vote previewWindowOpen(Window arg0) {
093             return Vote.APPROVE;
094         }
095 
096         @Override
097         public Vote previewWindowClose(Window arg0) {
098             handleClose(arg0);
099             return Vote.APPROVE;
100         }
101 
102         /**
103          * Triggers a <tt>WindowShown</tt> event with the window as sole argument
104          */
105         public void windowOpened(Window window) {
106             event(ApplicationEvent.WINDOW_SHOWN, asList(findWindowName(window), window));
107         }
108 
109         /**
110          * Triggers a <tt>WindowHidden</tt> event with the window as sole argument
111          */
112         public void windowClosed(Window window, Display arg1, Window arg2) {
113             event(ApplicationEvent.WINDOW_HIDDEN, asList(findWindowName(window), window));
114         }
115     }
116 }