001 /*
002 * Copyright 2008-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.griffon.runtime.swing;
017
018 import griffon.core.CallableWithArgs;
019 import griffon.core.GriffonApplication;
020 import griffon.core.view.WindowDisplayHandler;
021 import griffon.exceptions.InstanceNotFoundException;
022 import griffon.swing.SwingWindowDisplayHandler;
023 import org.codehaus.griffon.runtime.core.view.ConfigurableWindowDisplayHandler;
024 import org.slf4j.Logger;
025 import org.slf4j.LoggerFactory;
026
027 import javax.annotation.Nonnull;
028 import javax.inject.Inject;
029 import javax.inject.Named;
030 import javax.swing.JInternalFrame;
031 import java.awt.Window;
032 import java.util.Map;
033
034 import static griffon.util.AnnotationUtils.named;
035 import static griffon.util.GriffonNameUtils.requireNonBlank;
036 import static java.util.Objects.requireNonNull;
037
038 /**
039 * @author Andres Almiray
040 * @since 2.0.0
041 */
042 public class ConfigurableSwingWindowDisplayHandler extends ConfigurableWindowDisplayHandler<Window> implements SwingWindowDisplayHandler {
043 private static final Logger LOG = LoggerFactory.getLogger(ConfigurableSwingWindowDisplayHandler.class);
044
045 @Inject
046 public ConfigurableSwingWindowDisplayHandler(@Nonnull GriffonApplication application, @Nonnull @Named("defaultWindowDisplayHandler") SwingWindowDisplayHandler delegateWindowsDisplayHandler) {
047 super(application, delegateWindowsDisplayHandler);
048 }
049
050 public void show(@Nonnull String name, @Nonnull JInternalFrame window) {
051 requireNonBlank(name, ERROR_NAME_BLANK);
052 requireNonNull(window, ERROR_WINDOW_NULL);
053
054 Map<String, Object> options = windowBlock(name);
055 if (!options.isEmpty()) {
056 Object handler = options.get("show");
057 if (canBeRun(handler)) {
058 LOG.trace("Showing {} with show: handler", name);
059 run(handler, name, window);
060 return;
061 } else if (options.get("handler") instanceof SwingWindowDisplayHandler) {
062 LOG.trace("Showing {} with handler: handler", name);
063 ((SwingWindowDisplayHandler) options.get("handler")).show(name, window);
064 return;
065 }
066 }
067
068 SwingWindowDisplayHandler handler = resolveSwingWindowDisplayHandler(name);
069 if (handler != null) {
070 LOG.trace("Showing {} with injected handler", name);
071 handler.show(name, window);
072 return;
073 }
074
075 options = windowManagerBlock();
076 if (!options.isEmpty()) {
077 Object defaultShow = options.get("defaultShow");
078 if (canBeRun(defaultShow)) {
079 LOG.trace("Showing {} with defaultShow: handler", name);
080 run(defaultShow, name, window);
081 return;
082 }
083 }
084
085 LOG.trace("Showing {} with default handler", name);
086 fetchDefaultWindowDisplayHandler().show(name, window);
087 }
088
089 public void hide(@Nonnull String name, @Nonnull JInternalFrame window) {
090 requireNonBlank(name, ERROR_NAME_BLANK);
091 requireNonNull(window, ERROR_WINDOW_NULL);
092
093 Map<String, Object> options = windowBlock(name);
094 if (!options.isEmpty()) {
095 Object handler = options.get("hide");
096 if (canBeRun(handler)) {
097 LOG.trace("Hiding {} with hide: handler", name);
098 run(handler, name, window);
099 return;
100 } else if (options.get("handler") instanceof SwingWindowDisplayHandler) {
101 LOG.trace("Hiding {} with handler: handler", name);
102 ((SwingWindowDisplayHandler) options.get("handler")).hide(name, window);
103 return;
104 }
105 }
106
107 SwingWindowDisplayHandler handler = resolveSwingWindowDisplayHandler(name);
108 if (handler != null) {
109 LOG.trace("Hiding {} with injected handler", name);
110 handler.hide(name, window);
111 return;
112 }
113
114 options = windowManagerBlock();
115 if (!options.isEmpty()) {
116 Object defaultHide = options.get("defaultHide");
117 if (canBeRun(defaultHide)) {
118 LOG.trace("Hiding {} with defaultHide: handler", name);
119 run(defaultHide, name, window);
120 return;
121 }
122 }
123
124 LOG.trace("Hiding {} with default handler", name);
125 fetchDefaultWindowDisplayHandler().hide(name, window);
126 }
127
128 protected void run(@Nonnull Object handler, @Nonnull String name, @Nonnull JInternalFrame window) {
129 if (handler instanceof CallableWithArgs) {
130 ((CallableWithArgs<?>) handler).call(name, window);
131 }
132 }
133
134 @Nonnull
135 protected SwingWindowDisplayHandler fetchDefaultWindowDisplayHandler() {
136 Object handler = windowManagerBlock().get("defaultHandler");
137 return (SwingWindowDisplayHandler) (handler instanceof SwingWindowDisplayHandler ? handler : getDelegateWindowsDisplayHandler());
138 }
139
140 @Override
141 protected boolean handleShowByInjectedHandler(@Nonnull String name, @Nonnull Window window) {
142 try {
143 SwingWindowDisplayHandler handler = getApplication().getInjector()
144 .getInstance(SwingWindowDisplayHandler.class, named(name));
145 LOG.trace("Showing {} with injected handler", name);
146 handler.show(name, window);
147 return true;
148 } catch (InstanceNotFoundException infe) {
149 return super.handleShowByInjectedHandler(name, window);
150 }
151 }
152
153 @Override
154 protected boolean handleHideByInjectedHandler(@Nonnull String name, @Nonnull Window window) {
155 try {
156 SwingWindowDisplayHandler handler = getApplication().getInjector()
157 .getInstance(SwingWindowDisplayHandler.class, named(name));
158 LOG.trace("Hiding {} with injected handler", name);
159 handler.hide(name, window);
160 return true;
161 } catch (InstanceNotFoundException infe) {
162 return super.handleHideByInjectedHandler(name, window);
163 }
164 }
165
166 protected SwingWindowDisplayHandler resolveSwingWindowDisplayHandler(@Nonnull String name) {
167 try {
168 return getApplication().getInjector()
169 .getInstance(SwingWindowDisplayHandler.class, named(name));
170 } catch (InstanceNotFoundException infe1) {
171 try {
172 WindowDisplayHandler handler = getApplication().getInjector()
173 .getInstance(WindowDisplayHandler.class, named(name));
174 if (handler instanceof SwingWindowDisplayHandler) {
175 return ((SwingWindowDisplayHandler) handler);
176 }
177 } catch (InstanceNotFoundException infe2) {
178 // ignore
179 }
180 }
181
182 return null;
183 }
184 }
|