View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.util;
17  
18  import org.eclipse.swt.widgets.Display;
19  import org.eclipse.swt.widgets.Shell;
20  import org.seasar.framework.container.S2Container;
21  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
22  import org.seasar.uruma.context.ApplicationContext;
23  import org.seasar.uruma.context.WidgetHandle;
24  import org.seasar.uruma.context.WindowContext;
25  import org.seasar.uruma.core.UrumaConstants;
26  import org.seasar.uruma.rcp.UrumaService;
27  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
28  
29  /**
30   * {@link Shell} に関するユーティリティクラスです。<br />
31   * 
32   * @author y-komori
33   */
34  public class ShellUtil {
35      /**
36       * 現在のウィンドウの {@link Shell} オブジェクトを返します。<br />
37       * 
38       * @return {@link Shell} オブジェクト
39       */
40      public static Shell getShell() {
41          UrumaService service = UrumaServiceUtil.getService();
42          if (service != null) {
43              return service.getWorkbench().getActiveWorkbenchWindow().getShell();
44          } else {
45              S2Container container = SingletonS2ContainerFactory.getContainer();
46              ApplicationContext context = (ApplicationContext) container
47                      .getComponent(ApplicationContext.class);
48              WindowContext wContext = context.getWindowContexts().iterator()
49                      .next();
50              WidgetHandle handle = wContext
51                      .getWidgetHandle(UrumaConstants.SHELL_CID);
52              if (handle != null) {
53                  return handle.<Shell> getCastWidget();
54              } else {
55                  return Display.getCurrent().getActiveShell();
56              }
57          }
58      }
59  }