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.pivot;
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.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 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 DefaultPivotWindowManager extends AbstractWindowManager<Window> implements PivotWindowManager {
044 private final WindowStateHelper windowStateHelper = new WindowStateHelper();
045
046 @Inject
047 @Nonnull
048 public DefaultPivotWindowManager(@Nonnull GriffonApplication application, @Nonnull @Named("windowDisplayHandler") PivotWindowDisplayHandler 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.getWindowStateListeners().add(windowStateHelper);
057 }
058
059 @Override
060 protected void doDetach(@Nonnull Window window) {
061 requireNonNull(window, ERROR_WINDOW_NULL);
062 window.getWindowStateListeners().remove(windowStateHelper);
063 }
064
065 @Override
066 protected boolean isWindowVisible(@Nonnull Window window) {
067 requireNonNull(window, ERROR_WINDOW_NULL);
068 return window.isVisible();
069 }
070
071 public void handleClose(@Nonnull Window widget) {
072 if (getApplication().getPhase() == ApplicationPhase.SHUTDOWN) {
073 return;
074 }
075 int visibleWindows = 0;
076 for (Window window : getWindows()) {
077 if (window.isShowing()) {
078 visibleWindows++;
079 }
080 }
081
082 if (visibleWindows <= 1 && isAutoShutdown()) {
083 if (!getApplication().shutdown())
084 show(widget);
085 }
086 }
087
088 /**
089 * WindowStateAdapter that triggers application events when a window is shown/hidden.
090 *
091 * @author Andres Almiray
092 */
093 private class WindowStateHelper extends WindowStateAdapter {
094 @Override
095 public Vote previewWindowOpen(Window arg0) {
096 return Vote.APPROVE;
097 }
098
099 @Override
100 public Vote previewWindowClose(Window arg0) {
101 handleClose(arg0);
102 return Vote.APPROVE;
103 }
104
105 /**
106 * Triggers a <tt>WindowShown</tt> event with the window as sole argument
107 */
108 public void windowOpened(Window arg0) {
109 event(ApplicationEvent.WINDOW_SHOWN, asList(arg0));
110 }
111
112 /**
113 * Triggers a <tt>WindowHidden</tt> event with the window as sole argument
114 */
115 public void windowClosed(Window arg0, Display arg1, Window arg2) {
116 event(ApplicationEvent.WINDOW_HIDDEN, asList(arg0));
117 }
118 }
119
120 private void event(@Nonnull ApplicationEvent evt, List<?> args) {
121 try {
122 EventRouter eventRouter = getApplication().getEventRouter();
123 eventRouter.publishEvent(evt.getName(), args);
124 } catch (InstanceNotFoundException infe) {
125 if (getApplication().getPhase() != ApplicationPhase.SHUTDOWN) {
126 throw infe;
127 }
128 }
129 }
130 }
|