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