Coverage Report - org.seasar.uruma.renderer.impl.MenuItemRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
MenuItemRenderer
82%
49/60
71%
17/24
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.renderer.impl;
 17  
 
 18  
 import org.eclipse.jface.action.IAction;
 19  
 import org.eclipse.jface.action.MenuManager;
 20  
 import org.eclipse.jface.resource.ImageDescriptor;
 21  
 import org.eclipse.swt.widgets.MenuItem;
 22  
 import org.seasar.framework.util.StringUtil;
 23  
 import org.seasar.uruma.binding.method.GenericAction;
 24  
 import org.seasar.uruma.component.EnablesDependable;
 25  
 import org.seasar.uruma.component.UIComponent;
 26  
 import org.seasar.uruma.component.jface.MenuItemComponent;
 27  
 import org.seasar.uruma.context.PartContext;
 28  
 import org.seasar.uruma.context.WidgetHandle;
 29  
 import org.seasar.uruma.context.WindowContext;
 30  
 import org.seasar.uruma.renderer.RendererSupportUtil;
 31  
 
 32  
 /**
 33  
  * {@link MenuItem} のレンダリングを行うクラスです。<br />
 34  
  * 
 35  
  * @author bskuroneko
 36  
  */
 37  4
 public class MenuItemRenderer extends AbstractRenderer {
 38  
 
 39  
     /*
 40  
      * @see org.seasar.uruma.renderer.impl.AbstractRenderer#preRender(org.seasar.uruma.component.UIComponent,
 41  
      *      org.seasar.uruma.context.WidgetHandle,
 42  
      *      org.seasar.uruma.context.WindowContext)
 43  
      */
 44  
     @Override
 45  
     public WidgetHandle preRender(final UIComponent uiComponent,
 46  
             final WidgetHandle parent, final WindowContext context) {
 47  108
         setWindowContext(context);
 48  
 
 49  108
         MenuItemComponent menuItemComponent = (MenuItemComponent) uiComponent;
 50  108
         IAction action = new GenericAction(null, getStyle(menuItemComponent));
 51  
 
 52  108
         setText(action, menuItemComponent);
 53  108
         setAccelerator(action, menuItemComponent);
 54  108
         setChecked(action, menuItemComponent);
 55  108
         setEnabled(action, menuItemComponent);
 56  108
         setImageDescriptor(action, menuItemComponent);
 57  108
         setDisabledImageDescriptor(action, menuItemComponent);
 58  
 
 59  108
         MenuManager parentMenuManager = parent.<MenuManager> getCastWidget();
 60  108
         parentMenuManager.add(action);
 61  
 
 62  108
         WidgetHandle handle = createWidgetHandle(uiComponent, action);
 63  
 
 64  108
         if (uiComponent instanceof EnablesDependable) {
 65  108
             setupEnablesDependingDef(handle, (EnablesDependable) uiComponent);
 66  
         }
 67  
 
 68  108
         return handle;
 69  
     }
 70  
 
 71  
     /*
 72  
      * @see org.seasar.uruma.renderer.Renderer#render(org.seasar.uruma.component.UIComponent,
 73  
      *      org.seasar.uruma.context.WidgetHandle,
 74  
      *      org.seasar.uruma.context.PartContext)
 75  
      */
 76  
     public WidgetHandle render(final UIComponent uiComponent,
 77  
             final WidgetHandle parent, final PartContext context) {
 78  
         // Do nothing.
 79  0
         return null;
 80  
     }
 81  
 
 82  
     /*
 83  
      * @see org.seasar.uruma.renderer.Renderer#renderAfter(org.seasar.uruma.context.WidgetHandle,
 84  
      *      org.seasar.uruma.component.UIComponent,
 85  
      *      org.seasar.uruma.context.WidgetHandle,
 86  
      *      org.seasar.uruma.context.PartContext)
 87  
      */
 88  
     public void renderAfter(final WidgetHandle widget,
 89  
             final UIComponent uiComponent, final WidgetHandle parent,
 90  
             final PartContext context) {
 91  
         // Do nothing.
 92  0
     }
 93  
 
 94  
     protected int getStyle(final MenuItemComponent menuItemComponent) {
 95  108
         String style = menuItemComponent.getStyle();
 96  108
         if (MenuItemComponent.PUSH.equals(style)) {
 97  0
             return IAction.AS_PUSH_BUTTON;
 98  108
         } else if (MenuItemComponent.RADIO.equals(style)) {
 99  12
             return IAction.AS_RADIO_BUTTON;
 100  96
         } else if (MenuItemComponent.CHECK.equals(style)) {
 101  16
             return IAction.AS_CHECK_BOX;
 102  
         } else {
 103  80
             return IAction.AS_PUSH_BUTTON;
 104  
         }
 105  
     }
 106  
 
 107  
     protected void setText(final IAction action,
 108  
             final MenuItemComponent menuItemComponent) {
 109  108
         String text = menuItemComponent.text;
 110  108
         if (!StringUtil.isEmpty(text)) {
 111  108
             action.setText(RendererSupportUtil.convertText(text));
 112  
         }
 113  108
     }
 114  
 
 115  
     protected void setAccelerator(final IAction action,
 116  
             final MenuItemComponent menuItemComponent) {
 117  108
         String accelStr = menuItemComponent.accelerator;
 118  108
         if (accelStr != null) {
 119  8
             action.setAccelerator(RendererSupportUtil
 120  
                     .convertAccelerator(accelStr));
 121  
         }
 122  108
     }
 123  
 
 124  
     protected void setChecked(final IAction action,
 125  
             final MenuItemComponent menuItemComponent) {
 126  108
         String selection = menuItemComponent.selection;
 127  108
         if (selection != null) {
 128  12
             action.setChecked(RendererSupportUtil.convertBoolean(selection));
 129  
         }
 130  108
     }
 131  
 
 132  
     protected void setDescription(final IAction action,
 133  
             final MenuItemComponent menuItemComponent) {
 134  0
         if (menuItemComponent.description != null) {
 135  0
             action.setDescription(RendererSupportUtil
 136  
                     .convertText(menuItemComponent.description));
 137  
         }
 138  0
     }
 139  
 
 140  
     protected void setEnabled(final IAction action,
 141  
             final MenuItemComponent menuItemComponent) {
 142  108
         String checked = menuItemComponent.enabled;
 143  108
         if (checked != null) {
 144  8
             action.setEnabled(RendererSupportUtil.convertBoolean(checked));
 145  
         }
 146  108
     }
 147  
 
 148  
     protected void setImageDescriptor(final IAction action,
 149  
             final MenuItemComponent menuItemComponent) {
 150  108
         String path = menuItemComponent.image;
 151  108
         if (!StringUtil.isEmpty(path)) {
 152  4
             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
 153  
                     path, menuItemComponent.getBasePath());
 154  4
             action.setImageDescriptor(desc);
 155  
         }
 156  108
     }
 157  
 
 158  
     protected void setDisabledImageDescriptor(final IAction action,
 159  
             final MenuItemComponent menuItemComponent) {
 160  108
         String path = menuItemComponent.disabledImage;
 161  108
         if (!StringUtil.isEmpty(path)) {
 162  4
             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
 163  
                     path, menuItemComponent.getBasePath());
 164  4
             action.setDisabledImageDescriptor(desc);
 165  
         }
 166  108
     }
 167  
 
 168  
     protected void setHoverImageDescriptor(final IAction action,
 169  
             final MenuItemComponent menuItemComponent) {
 170  0
         String path = menuItemComponent.disabledImage;
 171  0
         if (!StringUtil.isEmpty(path)) {
 172  0
             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
 173  
                     path, menuItemComponent.getBasePath());
 174  0
             action.setHoverImageDescriptor(desc);
 175  
         }
 176  0
     }
 177  
 
 178  
 }