Coverage Report - org.seasar.uruma.rcp.ui.UrumaWorkbenchWindowAdvisor
 
Classes in this File Line Coverage Branch Coverage Complexity
UrumaWorkbenchWindowAdvisor
0%
0/67
0%
0/16
0
 
 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  0
         super(configurer);
 67  
 
 68  0
         registComponents(configurer);
 69  0
     }
 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  0
         super.createWindowContents(shell);
 77  
 
 78  0
         WindowContext windowContext = UrumaServiceUtil.getService()
 79  
                 .getWorkbenchWindowContext();
 80  
 
 81  
         // Workbench InitMethod
 82  0
         ComponentUtil.setupWorkbenchAction(windowContext,
 83  
                 UrumaConstants.WORKBENCH_WINDOW_CONTEXT_ID);
 84  0
         ComponentUtil.invokeInitMethodOnAction(windowContext);
 85  0
     }
 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  0
         return new UrumaActionBarAdvisor(configurer);
 94  
     }
 95  
 
 96  
     @Override
 97  
     public void preWindowOpen() {
 98  0
         workbench = UrumaServiceUtil.getService().getWorkbenchComponent();
 99  
 
 100  0
         IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
 101  0
         configurer.setTitle(workbench.title);
 102  
 
 103  0
         configurer.setInitialSize(calcInitialSize(workbench.initWidth,
 104  
                 workbench.initHeight));
 105  
 
 106  0
         setupStatusLine(workbench, configurer);
 107  
 
 108  0
         WindowContext windowContext = UrumaServiceUtil.getService()
 109  
                 .getWorkbenchWindowContext();
 110  
 
 111  0
         setupCommandHandler(configurer, windowContext);
 112  0
         setupEnablesDependable(windowContext);
 113  0
         setupWorkbench(configurer);
 114  0
     }
 115  
 
 116  
     /*
 117  
      * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowCreate()
 118  
      */
 119  
     @Override
 120  
     public void postWindowCreate() {
 121  
         // Enable Depending の準備
 122  
         // TODO 後から開いたビューに対しては EnableDepending が効かない
 123  0
         WindowContext context = UrumaServiceUtil.getService()
 124  
                 .getWorkbenchWindowContext();
 125  0
         EnablesDependingListenerSupport.setupEnableDependingListeners(context);
 126  
 
 127  
         // Method Binding の準備
 128  
         // 各 ViewPart 固有の MethodBinding
 129  
         // は、GenericViewPart#createPartControlInternal() の中で行われる
 130  0
         MethodBindingSupport.createListeners(context);
 131  
 
 132  0
         ViewPartUtil.findUrumaAppViewRefs();
 133  0
     }
 134  
 
 135  
     protected void setupWorkbench(final IWorkbenchWindowConfigurer configurer) {
 136  0
         configurer.setShowMenuBar(workbench.menuBar == null ? true : Boolean
 137  
                 .parseBoolean(workbench.menuBar));
 138  0
         configurer.setShowCoolBar(Boolean.parseBoolean(workbench.coolBar));
 139  0
         configurer.setShowFastViewBars(Boolean
 140  
                 .parseBoolean(workbench.fastViewBars));
 141  0
         configurer.setShowPerspectiveBar(Boolean
 142  
                 .parseBoolean(workbench.perspectiveBar));
 143  0
         configurer.setShowProgressIndicator(Boolean
 144  
                 .parseBoolean(workbench.progressIndicator));
 145  0
     }
 146  
 
 147  
     protected Point calcInitialSize(final String width, final String height) {
 148  0
         String widthStr = StringUtil.isNotBlank(width) ? width
 149  
                 : UrumaConstants.DEFAULT_WORKBENCH_WIDTH;
 150  0
         String heightStr = StringUtil.isNotBlank(height) ? height
 151  
                 : UrumaConstants.DEFAULT_WORKBENCH_HEIGHT;
 152  
 
 153  0
         int xSize = GeometryUtil.calcSize(widthStr, Display.getCurrent()
 154  
                 .getClientArea().width);
 155  0
         int ySize = GeometryUtil.calcSize(heightStr, Display.getCurrent()
 156  
                 .getClientArea().height);
 157  0
         return new Point(xSize, ySize);
 158  
     }
 159  
 
 160  
     protected void setupImage(final WorkbenchComponent workbench) {
 161  0
         if (StringUtil.isNotBlank(workbench.image)) {
 162  0
             Image image = ImageManager.getImage(workbench.image);
 163  0
             if (image == null) {
 164  0
                 String path = PathUtil.createPath(workbench.getBasePath(),
 165  
                         workbench.image);
 166  0
                 image = ImageManager.loadImage(path);
 167  
             }
 168  
 
 169  0
             if (image != null) {
 170  0
                 IWorkbenchWindowConfigurer configurator = getWindowConfigurer();
 171  0
                 Shell shell = configurator.getWindow().getShell();
 172  0
                 shell.setImage(image);
 173  
             }
 174  
         }
 175  0
     }
 176  
 
 177  
     protected void setupStatusLine(final WorkbenchComponent workbench,
 178  
             final IWorkbenchWindowConfigurer configurer) {
 179  0
         configurer
 180  
                 .setShowStatusLine(Boolean.parseBoolean(workbench.statusLine));
 181  0
     }
 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  0
         IWorkbench workbench = configurer.getWorkbenchConfigurer()
 192  
                 .getWorkbench();
 193  0
         IHandlerService service = (IHandlerService) workbench
 194  
                 .getService(IHandlerService.class);
 195  
 
 196  0
         CommandRegistry registry = UrumaServiceUtil.getService()
 197  
                 .getCommandRegistry();
 198  0
         for (CommandDesc desc : registry.getCommandDescs()) {
 199  0
             GenericHandler handler = new GenericHandler();
 200  0
             service.activateHandler(desc.getCommandId(), handler);
 201  
 
 202  0
             WidgetHandle handle = new WidgetHandleImpl(handler);
 203  0
             handle.setId(desc.getUrumaId());
 204  0
             context.putWidgetHandle(handle);
 205  0
         }
 206  0
     }
 207  
 
 208  
     protected void setupEnablesDependable(final WindowContext context) {
 209  0
         EnablesDependableVisitor visitor = new EnablesDependableVisitor(context);
 210  0
         for (MenuComponent menu : workbench.getMenus()) {
 211  0
             menu.accept(visitor);
 212  
         }
 213  0
     }
 214  
 
 215  
     protected void registComponents(final IWorkbenchWindowConfigurer configurer) {
 216  0
         S2Container container = UrumaServiceUtil.getService().getContainer();
 217  0
         container.register(configurer);
 218  0
         container.register(configurer.getWindow());
 219  0
         container.register(configurer.getWindow().getWorkbench());
 220  0
     }
 221  
 }