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