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.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  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          setWindowContext(context);
48  
49          MenuItemComponent menuItemComponent = (MenuItemComponent) uiComponent;
50          IAction action = new GenericAction(null, getStyle(menuItemComponent));
51  
52          setText(action, menuItemComponent);
53          setAccelerator(action, menuItemComponent);
54          setChecked(action, menuItemComponent);
55          setEnabled(action, menuItemComponent);
56          setImageDescriptor(action, menuItemComponent);
57          setDisabledImageDescriptor(action, menuItemComponent);
58  
59          MenuManager parentMenuManager = parent.<MenuManager> getCastWidget();
60          parentMenuManager.add(action);
61  
62          WidgetHandle handle = createWidgetHandle(uiComponent, action);
63  
64          if (uiComponent instanceof EnablesDependable) {
65              setupEnablesDependingDef(handle, (EnablesDependable) uiComponent);
66          }
67  
68          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          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      }
93  
94      protected int getStyle(final MenuItemComponent menuItemComponent) {
95          String style = menuItemComponent.getStyle();
96          if (MenuItemComponent.PUSH.equals(style)) {
97              return IAction.AS_PUSH_BUTTON;
98          } else if (MenuItemComponent.RADIO.equals(style)) {
99              return IAction.AS_RADIO_BUTTON;
100         } else if (MenuItemComponent.CHECK.equals(style)) {
101             return IAction.AS_CHECK_BOX;
102         } else {
103             return IAction.AS_PUSH_BUTTON;
104         }
105     }
106 
107     protected void setText(final IAction action,
108             final MenuItemComponent menuItemComponent) {
109         String text = menuItemComponent.text;
110         if (!StringUtil.isEmpty(text)) {
111             action.setText(RendererSupportUtil.convertText(text));
112         }
113     }
114 
115     protected void setAccelerator(final IAction action,
116             final MenuItemComponent menuItemComponent) {
117         String accelStr = menuItemComponent.accelerator;
118         if (accelStr != null) {
119             action.setAccelerator(RendererSupportUtil
120                     .convertAccelerator(accelStr));
121         }
122     }
123 
124     protected void setChecked(final IAction action,
125             final MenuItemComponent menuItemComponent) {
126         String selection = menuItemComponent.selection;
127         if (selection != null) {
128             action.setChecked(RendererSupportUtil.convertBoolean(selection));
129         }
130     }
131 
132     protected void setDescription(final IAction action,
133             final MenuItemComponent menuItemComponent) {
134         if (menuItemComponent.description != null) {
135             action.setDescription(RendererSupportUtil
136                     .convertText(menuItemComponent.description));
137         }
138     }
139 
140     protected void setEnabled(final IAction action,
141             final MenuItemComponent menuItemComponent) {
142         String checked = menuItemComponent.enabled;
143         if (checked != null) {
144             action.setEnabled(RendererSupportUtil.convertBoolean(checked));
145         }
146     }
147 
148     protected void setImageDescriptor(final IAction action,
149             final MenuItemComponent menuItemComponent) {
150         String path = menuItemComponent.image;
151         if (!StringUtil.isEmpty(path)) {
152             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
153                     path, menuItemComponent.getBasePath());
154             action.setImageDescriptor(desc);
155         }
156     }
157 
158     protected void setDisabledImageDescriptor(final IAction action,
159             final MenuItemComponent menuItemComponent) {
160         String path = menuItemComponent.disabledImage;
161         if (!StringUtil.isEmpty(path)) {
162             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
163                     path, menuItemComponent.getBasePath());
164             action.setDisabledImageDescriptor(desc);
165         }
166     }
167 
168     protected void setHoverImageDescriptor(final IAction action,
169             final MenuItemComponent menuItemComponent) {
170         String path = menuItemComponent.disabledImage;
171         if (!StringUtil.isEmpty(path)) {
172             ImageDescriptor desc = RendererSupportUtil.convertImageDescriptor(
173                     path, menuItemComponent.getBasePath());
174             action.setHoverImageDescriptor(desc);
175         }
176     }
177 
178 }