ConfigurablePivotWindowDisplayHandler.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.pivot;
17 
18 import griffon.core.GriffonApplication;
19 import griffon.exceptions.InstanceNotFoundException;
20 import griffon.pivot.PivotWindowDisplayHandler;
21 import org.apache.pivot.wtk.Window;
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 ConfigurablePivotWindowDisplayHandler extends ConfigurableWindowDisplayHandler<Window> implements PivotWindowDisplayHandler {
37     private static final Logger LOG = LoggerFactory.getLogger(ConfigurablePivotWindowDisplayHandler.class);
38 
39     @Inject
40     public ConfigurablePivotWindowDisplayHandler(@Nonnull GriffonApplication application, @Nonnull @Named("defaultWindowDisplayHandler"PivotWindowDisplayHandler delegateWindowsDisplayHandler) {
41         super(application, delegateWindowsDisplayHandler);
42     }
43 
44     @Nonnull
45     @Override
46     protected PivotWindowDisplayHandler fetchDefaultWindowDisplayHandler() {
47         Object handler = windowManagerBlock().get("defaultHandler");
48         return (PivotWindowDisplayHandler) (handler instanceof PivotWindowDisplayHandler ? handler : getDelegateWindowsDisplayHandler());
49     }
50 
51     @Override
52     protected boolean handleShowByInjectedHandler(@Nonnull String name, @Nonnull Window window) {
53         try {
54             PivotWindowDisplayHandler handler = getApplication().getInjector()
55                 .getInstance(PivotWindowDisplayHandler.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             PivotWindowDisplayHandler handler = getApplication().getInjector()
68                 .getInstance(PivotWindowDisplayHandler.class, named(name));
69             LOG.trace("Hiding {} 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 }