| 
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.lanterna;
 017
 018 import com.googlecode.lanterna.gui.Window;
 019 import com.googlecode.lanterna.gui.listener.WindowAdapter;
 020 import griffon.core.ApplicationEvent;
 021 import griffon.core.GriffonApplication;
 022 import griffon.core.env.ApplicationPhase;
 023 import griffon.core.event.EventRouter;
 024 import griffon.exceptions.InstanceNotFoundException;
 025 import griffon.lanterna.LanternaWindowDisplayHandler;
 026 import griffon.lanterna.LanternaWindowManager;
 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 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 DefaultLanternaWindowManager extends AbstractWindowManager<Window> implements LanternaWindowManager {
 042     private final WindowHelper windowHelper = new WindowHelper();
 043
 044     @Inject
 045     @Nonnull
 046     public DefaultLanternaWindowManager(@Nonnull GriffonApplication application, @Nonnull @Named("windowDisplayHandler") LanternaWindowDisplayHandler windowDisplayHandler) {
 047         super(application, windowDisplayHandler);
 048         requireNonNull(application.getEventRouter(), "Argument 'application.eventRouter' must not be null");
 049     }
 050
 051     @Override
 052     protected void doAttach(@Nonnull Window window) {
 053         requireNonNull(window, ERROR_WINDOW_NULL);
 054         window.addWindowListener(windowHelper);
 055     }
 056
 057     @Override
 058     protected void doDetach(@Nonnull Window window) {
 059         requireNonNull(window, ERROR_WINDOW_NULL);
 060     }
 061
 062     @Override
 063     protected boolean isWindowVisible(@Nonnull Window window) {
 064         requireNonNull(window, ERROR_WINDOW_NULL);
 065         return true;
 066     }
 067
 068     public void handleClose(@Nonnull Window widget) {
 069         if (getApplication().getPhase() == ApplicationPhase.SHUTDOWN) {
 070             return;
 071         }
 072
 073         int visibleWindows = getWindows().size();
 074         if (visibleWindows <= 1 && isAutoShutdown()) {
 075             if (!getApplication().shutdown())
 076                 show(widget);
 077         }
 078     }
 079
 080     /**
 081      * WindowAdapter that optionally invokes hide() when the window is about to be closed.
 082      *
 083      * @author Andres Almiray
 084      */
 085     private class WindowHelper extends WindowAdapter {
 086         @Override
 087         public void onWindowClosed(@Nonnull Window window) {
 088             super.onWindowClosed(window);
 089             event(ApplicationEvent.WINDOW_HIDDEN, asList(window));
 090             handleClose(window);
 091         }
 092
 093         @Override
 094         public void onWindowShown(Window window) {
 095             super.onWindowShown(window);
 096             event(ApplicationEvent.WINDOW_SHOWN, asList(window));
 097         }
 098     }
 099
 100     private void event(@Nonnull ApplicationEvent evt, List<?> args) {
 101         try {
 102             EventRouter eventRouter = getApplication().getEventRouter();
 103             eventRouter.publishEvent(evt.getName(), args);
 104         } catch (InstanceNotFoundException infe) {
 105             if (getApplication().getPhase() != ApplicationPhase.SHUTDOWN) {
 106                 throw infe;
 107             }
 108         }
 109     }
 110 }
 |