ConfigurableLanternaWindowDisplayHandler.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 griffon.core.GriffonApplication;
22 import griffon.exceptions.InstanceNotFoundException;
23 import griffon.lanterna.LanternaWindowDisplayHandler;
24 import org.codehaus.griffon.runtime.core.view.ConfigurableWindowDisplayHandler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 
28 import javax.annotation.Nonnull;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 
32 import static griffon.util.AnnotationUtils.named;
33 
34 /**
35  @author Andres Almiray
36  @since 2.0.0
37  */
38 public class ConfigurableLanternaWindowDisplayHandler extends ConfigurableWindowDisplayHandler<Window> implements LanternaWindowDisplayHandler {
39     private static final Logger LOG = LoggerFactory.getLogger(ConfigurableLanternaWindowDisplayHandler.class);
40 
41     @Inject
42     public ConfigurableLanternaWindowDisplayHandler(@Nonnull GriffonApplication application, @Nonnull @Named("defaultWindowDisplayHandler"LanternaWindowDisplayHandler delegateWindowsDisplayHandler) {
43         super(application, delegateWindowsDisplayHandler);
44     }
45 
46     @Nonnull
47     @Override
48     protected LanternaWindowDisplayHandler fetchDefaultWindowDisplayHandler() {
49         Object handler = windowManagerBlock().get("defaultHandler");
50         return (LanternaWindowDisplayHandler) (handler instanceof LanternaWindowDisplayHandler ? handler : getDelegateWindowsDisplayHandler());
51     }
52 
53     @Override
54     protected boolean handleShowByInjectedHandler(@Nonnull String name, @Nonnull Window window) {
55         try {
56             LanternaWindowDisplayHandler handler = getApplication().getInjector()
57                 .getInstance(LanternaWindowDisplayHandler.class, named(name));
58             LOG.trace("Showing {} with injected handler", name);
59             handler.show(name, window);
60             return true;
61         catch (InstanceNotFoundException infe) {
62             return super.handleShowByInjectedHandler(name, window);
63         }
64     }
65 
66     @Override
67     protected boolean handleHideByInjectedHandler(@Nonnull String name, @Nonnull Window window) {
68         try {
69             LanternaWindowDisplayHandler handler = getApplication().getInjector()
70                 .getInstance(LanternaWindowDisplayHandler.class, named(name));
71             LOG.trace("Hide {} with injected handler", name);
72             handler.hide(name, window);
73             return true;
74         catch (InstanceNotFoundException infe) {
75             return super.handleHideByInjectedHandler(name, window);
76         }
77     }
78 }