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