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.core.impl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.eclipse.jface.window.WindowManager;
24  import org.eclipse.swt.widgets.Shell;
25  import org.seasar.framework.container.annotation.tiger.Binding;
26  import org.seasar.framework.container.annotation.tiger.BindingType;
27  import org.seasar.framework.container.annotation.tiger.Component;
28  import org.seasar.framework.util.AssertionUtil;
29  import org.seasar.uruma.binding.context.ApplicationContextBinder;
30  import org.seasar.uruma.binding.context.ApplicationContextDef;
31  import org.seasar.uruma.component.Template;
32  import org.seasar.uruma.component.jface.WindowComponent;
33  import org.seasar.uruma.context.ApplicationContext;
34  import org.seasar.uruma.context.ContextFactory;
35  import org.seasar.uruma.context.WindowContext;
36  import org.seasar.uruma.context.impl.ApplicationContextImpl;
37  import org.seasar.uruma.core.TemplateManager;
38  import org.seasar.uruma.core.UrumaMessageCodes;
39  import org.seasar.uruma.core.UrumaWindowManager;
40  import org.seasar.uruma.desc.PartActionDesc;
41  import org.seasar.uruma.desc.PartActionDescFactory;
42  import org.seasar.uruma.log.UrumaLogger;
43  import org.seasar.uruma.ui.UrumaApplicationWindow;
44  
45  /**
46   * ウィンドウを管理するためのクラスです。</br>
47   * 
48   * @author y-komori
49   * @author bskuroneko
50   */
51  @Component
52  public class UrumaWindowManagerImpl implements UrumaWindowManager {
53      private static final UrumaLogger logger = UrumaLogger
54              .getLogger(UrumaWindowManager.class);
55  
56      private WindowManager windowManager = new WindowManager();
57  
58      private Map<String, WindowComponent> windowMap = new HashMap<String, WindowComponent>();
59  
60      private List<UrumaApplicationWindow> windowList = new ArrayList<UrumaApplicationWindow>();
61  
62      private TemplateManager templateManager = new TemplateManagerImpl();
63  
64      /**
65       * 画面間のパラメータ共有に使用する {@link ApplicationContext} オブジェクトです。
66       */
67      @Binding(bindingType = BindingType.MUST)
68      public ApplicationContext applicationContext;
69  
70      /*
71       * @see org.seasar.uruma.core.UrumaWindowManager#close(java.lang.String)
72       */
73      public void close(final String windowId) {
74          UrumaApplicationWindow window = findWindow(windowId);
75          if (window != null) {
76              logger.log(UrumaMessageCodes.CLOSE_WINDOW, windowId);
77  
78              windowList.remove(window);
79              windowMap.remove(windowId);
80              ((ApplicationContextImpl) applicationContext)
81                      .disposeWindowContext(windowId);
82  
83              Shell shell = window.getShell();
84              if (shell != null && !shell.isDisposed()) {
85                  shell.close();
86              }
87          }
88      }
89  
90      /*
91       * @see org.seasar.uruma.core.UrumaWindowManager#openWindow(java.lang.String,
92       *      boolean)
93       */
94      public UrumaApplicationWindow openWindow(final String templatePath,
95              final boolean modal) {
96          Template template = loadTemplate(templatePath);
97          UrumaApplicationWindow window = createWindow();
98          WindowComponent windowComponent = (WindowComponent) template
99                  .getRootComponent();
100         String windowId = windowComponent.getId();
101 
102         WindowContext windowContext = ContextFactory.createWindowContext(
103                 applicationContext, windowId);
104 
105         logger.log(UrumaMessageCodes.INIT_WINDOW, windowId);
106         window.init(windowContext, windowComponent, modal);
107 
108         if (modal) {
109             window.setBlockOnOpen(true);
110         }
111         windowManager.add(window);
112 
113         logger.log(UrumaMessageCodes.OPEN_WINDOW, windowId);
114         window.open();
115         return window;
116     }
117 
118     protected Template loadTemplate(final String path) {
119         Template template = templateManager.getTemplate(path);
120         WindowComponent window = (WindowComponent) template.getRootComponent();
121         windowMap.put(window.getId(), window);
122         return template;
123     }
124 
125     /*
126      * @see org.seasar.uruma.core.UrumaWindowManager#openDialog(java.lang.String,
127      *      java.lang.Object)
128      */
129     public int openDialog(final String templatePath, final Object parentAction) {
130         // TODO 仮実装
131         // 親画面から ApplicationContext へのエクスポート処理
132         if (parentAction != null) {
133             PartActionDesc desc = PartActionDescFactory
134                     .getPartActionDesc(parentAction.getClass());
135             List<ApplicationContextDef> defs = desc
136                     .getApplicationContextDefList();
137             ApplicationContextBinder.exportObjects(parentAction, defs,
138                     applicationContext);
139         }
140 
141         openWindow(templatePath, true);
142 
143         // ApplicationContext から親画面へのインポート処理
144         if (parentAction != null) {
145             PartActionDesc desc = PartActionDescFactory
146                     .getPartActionDesc(parentAction.getClass());
147             List<ApplicationContextDef> defs = desc
148                     .getApplicationContextDefList();
149             ApplicationContextBinder.importObjects(parentAction, defs,
150                     applicationContext);
151         }
152         return 0;
153     }
154 
155     protected UrumaApplicationWindow createWindow() {
156         UrumaApplicationWindow window = new UrumaApplicationWindow(this);
157         windowList.add(window);
158         return window;
159     }
160 
161     protected UrumaApplicationWindow findWindow(final String windowId) {
162         AssertionUtil.assertNotNull("windowId", windowId);
163 
164         for (UrumaApplicationWindow window : windowList) {
165             if (windowId.equals(window.getWindowId())) {
166                 return window;
167             }
168         }
169         return null;
170     }
171 }