DefaultLanternaWindowManager.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.lanterna;
19 
20 import com.googlecode.lanterna.gui.Window;
21 import com.googlecode.lanterna.gui.listener.WindowAdapter;
22 import griffon.core.ApplicationEvent;
23 import griffon.core.GriffonApplication;
24 import griffon.core.env.ApplicationPhase;
25 import griffon.lanterna.LanternaWindowDisplayHandler;
26 import griffon.lanterna.LanternaWindowManager;
27 import org.codehaus.griffon.runtime.core.view.AbstractWindowManager;
28 
29 import javax.annotation.Nonnull;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 
33 import static java.util.Arrays.asList;
34 import static java.util.Objects.requireNonNull;
35 
36 /**
37  @author Andres Almiray
38  @since 2.0.0
39  */
40 public class DefaultLanternaWindowManager extends AbstractWindowManager<Window> implements LanternaWindowManager {
41     private final WindowHelper windowHelper = new WindowHelper();
42 
43     @Inject
44     @Nonnull
45     public DefaultLanternaWindowManager(@Nonnull GriffonApplication application, @Nonnull @Named("windowDisplayHandler"LanternaWindowDisplayHandler windowDisplayHandler) {
46         super(application, windowDisplayHandler);
47         requireNonNull(application.getEventRouter()"Argument 'application.eventRouter' must not be null");
48     }
49 
50     @Override
51     protected void doAttach(@Nonnull Window window) {
52         requireNonNull(window, ERROR_WINDOW_NULL);
53         window.addWindowListener(windowHelper);
54     }
55 
56     @Override
57     protected void doDetach(@Nonnull Window window) {
58         requireNonNull(window, ERROR_WINDOW_NULL);
59     }
60 
61     @Override
62     protected boolean isWindowVisible(@Nonnull Window window) {
63         requireNonNull(window, ERROR_WINDOW_NULL);
64         return true;
65     }
66 
67     public void handleClose(@Nonnull Window widget) {
68         if (getApplication().getPhase() == ApplicationPhase.SHUTDOWN) {
69             return;
70         }
71 
72         int visibleWindows = getWindows().size();
73         if (visibleWindows <= && isAutoShutdown() && !getApplication().shutdown()) {
74             show(widget);
75         }
76     }
77 
78     /**
79      * WindowAdapter that optionally invokes hide() when the window is about to be closed.
80      *
81      @author Andres Almiray
82      */
83     private class WindowHelper extends WindowAdapter {
84         @Override
85         public void onWindowClosed(@Nonnull Window window) {
86             super.onWindowClosed(window);
87             event(ApplicationEvent.WINDOW_HIDDEN, asList(findWindowName(window), window));
88             handleClose(window);
89         }
90 
91         @Override
92         public void onWindowShown(Window window) {
93             super.onWindowShown(window);
94             event(ApplicationEvent.WINDOW_SHOWN, asList(findWindowName(window), window));
95         }
96     }
97 }