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.rcp.ui;
17  
18  import org.eclipse.core.commands.IHandler;
19  import org.eclipse.swt.graphics.Image;
20  import org.eclipse.swt.graphics.Point;
21  import org.eclipse.swt.widgets.Display;
22  import org.eclipse.swt.widgets.Shell;
23  import org.eclipse.ui.IWorkbench;
24  import org.eclipse.ui.application.ActionBarAdvisor;
25  import org.eclipse.ui.application.IActionBarConfigurer;
26  import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
27  import org.eclipse.ui.application.WorkbenchWindowAdvisor;
28  import org.eclipse.ui.handlers.IHandlerService;
29  import org.seasar.eclipse.common.util.GeometryUtil;
30  import org.seasar.eclipse.common.util.ImageManager;
31  import org.seasar.framework.container.S2Container;
32  import org.seasar.framework.util.StringUtil;
33  import org.seasar.uruma.binding.enables.EnablesDependingListenerSupport;
34  import org.seasar.uruma.binding.method.MethodBindingSupport;
35  import org.seasar.uruma.component.EnablesDependableVisitor;
36  import org.seasar.uruma.component.jface.MenuComponent;
37  import org.seasar.uruma.component.rcp.WorkbenchComponent;
38  import org.seasar.uruma.context.WidgetHandle;
39  import org.seasar.uruma.context.WindowContext;
40  import org.seasar.uruma.context.impl.WidgetHandleImpl;
41  import org.seasar.uruma.core.ComponentUtil;
42  import org.seasar.uruma.core.UrumaConstants;
43  import org.seasar.uruma.rcp.binding.CommandDesc;
44  import org.seasar.uruma.rcp.binding.CommandRegistry;
45  import org.seasar.uruma.rcp.binding.GenericHandler;
46  import org.seasar.uruma.rcp.util.UrumaServiceUtil;
47  import org.seasar.uruma.rcp.util.ViewPartUtil;
48  import org.seasar.uruma.util.PathUtil;
49  
50  /**
51   * ワークベンチウィンドウに関する設定を行うクラスです。<br />
52   * 
53   * @author y-komori
54   */
55  public class UrumaWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
56      private WorkbenchComponent workbench;
57  
58      /**
59       * {@link UrumaWorkbenchWindowAdvisor} を構築します。<br />
60       * 
61       * @param configurer
62       *            {@link IWorkbenchWindowConfigurer} オブジェクト
63       */
64      public UrumaWorkbenchWindowAdvisor(
65              final IWorkbenchWindowConfigurer configurer) {
66          super(configurer);
67  
68          registComponents(configurer);
69      }
70  
71      /*
72       * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createWindowContents(org.eclipse.swt.widgets.Shell)
73       */
74      @Override
75      public void createWindowContents(final Shell shell) {
76          super.createWindowContents(shell);
77  
78          WindowContext windowContext = UrumaServiceUtil.getService()
79                  .getWorkbenchWindowContext();
80  
81          // Workbench InitMethod
82          ComponentUtil.setupWorkbenchAction(windowContext,
83                  UrumaConstants.WORKBENCH_WINDOW_CONTEXT_ID);
84          ComponentUtil.invokeInitMethodOnAction(windowContext);
85      }
86  
87      /*
88       * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
89       */
90      @Override
91      public ActionBarAdvisor createActionBarAdvisor(
92              final IActionBarConfigurer configurer) {
93          return new UrumaActionBarAdvisor(configurer);
94      }
95  
96      @Override
97      public void preWindowOpen() {
98          workbench = UrumaServiceUtil.getService().getWorkbenchComponent();
99  
100         IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
101         configurer.setTitle(workbench.title);
102 
103         configurer.setInitialSize(calcInitialSize(workbench.initWidth,
104                 workbench.initHeight));
105 
106         setupStatusLine(workbench, configurer);
107 
108         WindowContext windowContext = UrumaServiceUtil.getService()
109                 .getWorkbenchWindowContext();
110 
111         setupCommandHandler(configurer, windowContext);
112         setupEnablesDependable(windowContext);
113         setupWorkbench(configurer);
114     }
115 
116     /*
117      * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowCreate()
118      */
119     @Override
120     public void postWindowCreate() {
121         // Enable Depending の準備
122         // TODO 後から開いたビューに対しては EnableDepending が効かない
123         WindowContext context = UrumaServiceUtil.getService()
124                 .getWorkbenchWindowContext();
125         EnablesDependingListenerSupport.setupEnableDependingListeners(context);
126 
127         // Method Binding の準備
128         // 各 ViewPart 固有の MethodBinding
129         // は、GenericViewPart#createPartControlInternal() の中で行われる
130         MethodBindingSupport.createListeners(context);
131 
132         ViewPartUtil.findUrumaAppViewRefs();
133     }
134 
135     protected void setupWorkbench(final IWorkbenchWindowConfigurer configurer) {
136         configurer.setShowMenuBar(workbench.menuBar == null ? true : Boolean
137                 .parseBoolean(workbench.menuBar));
138         configurer.setShowCoolBar(Boolean.parseBoolean(workbench.coolBar));
139         configurer.setShowFastViewBars(Boolean
140                 .parseBoolean(workbench.fastViewBars));
141         configurer.setShowPerspectiveBar(Boolean
142                 .parseBoolean(workbench.perspectiveBar));
143         configurer.setShowProgressIndicator(Boolean
144                 .parseBoolean(workbench.progressIndicator));
145     }
146 
147     protected Point calcInitialSize(final String width, final String height) {
148         String widthStr = StringUtil.isNotBlank(width) ? width
149                 : UrumaConstants.DEFAULT_WORKBENCH_WIDTH;
150         String heightStr = StringUtil.isNotBlank(height) ? height
151                 : UrumaConstants.DEFAULT_WORKBENCH_HEIGHT;
152 
153         int xSize = GeometryUtil.calcSize(widthStr, Display.getCurrent()
154                 .getClientArea().width);
155         int ySize = GeometryUtil.calcSize(heightStr, Display.getCurrent()
156                 .getClientArea().height);
157         return new Point(xSize, ySize);
158     }
159 
160     protected void setupImage(final WorkbenchComponent workbench) {
161         if (StringUtil.isNotBlank(workbench.image)) {
162             Image image = ImageManager.getImage(workbench.image);
163             if (image == null) {
164                 String path = PathUtil.createPath(workbench.getBasePath(),
165                         workbench.image);
166                 image = ImageManager.loadImage(path);
167             }
168 
169             if (image != null) {
170                 IWorkbenchWindowConfigurer configurator = getWindowConfigurer();
171                 Shell shell = configurator.getWindow().getShell();
172                 shell.setImage(image);
173             }
174         }
175     }
176 
177     protected void setupStatusLine(final WorkbenchComponent workbench,
178             final IWorkbenchWindowConfigurer configurer) {
179         configurer
180                 .setShowStatusLine(Boolean.parseBoolean(workbench.statusLine));
181     }
182 
183     /**
184      * {@link IHandler} の実装クラスを {@link IHandlerService} へ登録します。<br />
185      * {@link IHandler} は {@link WidgetHandle} として {@link WindowContext}
186      * へも登録されます。<br />
187      */
188     protected void setupCommandHandler(
189             final IWorkbenchWindowConfigurer configurer,
190             final WindowContext context) {
191         IWorkbench workbench = configurer.getWorkbenchConfigurer()
192                 .getWorkbench();
193         IHandlerService service = (IHandlerService) workbench
194                 .getService(IHandlerService.class);
195 
196         CommandRegistry registry = UrumaServiceUtil.getService()
197                 .getCommandRegistry();
198         for (CommandDesc desc : registry.getCommandDescs()) {
199             GenericHandler handler = new GenericHandler();
200             service.activateHandler(desc.getCommandId(), handler);
201 
202             WidgetHandle handle = new WidgetHandleImpl(handler);
203             handle.setId(desc.getUrumaId());
204             context.putWidgetHandle(handle);
205         }
206     }
207 
208     protected void setupEnablesDependable(final WindowContext context) {
209         EnablesDependableVisitor visitor = new EnablesDependableVisitor(context);
210         for (MenuComponent menu : workbench.getMenus()) {
211             menu.accept(visitor);
212         }
213     }
214 
215     protected void registComponents(final IWorkbenchWindowConfigurer configurer) {
216         S2Container container = UrumaServiceUtil.getService().getContainer();
217         container.register(configurer);
218         container.register(configurer.getWindow());
219         container.register(configurer.getWindow().getWorkbench());
220     }
221 }