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.swt.SWT;
19  import org.seasar.eclipse.common.util.SWTUtil;
20  import org.seasar.framework.util.StringUtil;
21  import org.seasar.uruma.binding.enables.EnablesDependingDef;
22  import org.seasar.uruma.binding.enables.EnablesForType;
23  import org.seasar.uruma.component.EnablesDependable;
24  import org.seasar.uruma.component.UIComponent;
25  import org.seasar.uruma.context.ContextFactory;
26  import org.seasar.uruma.context.PartContext;
27  import org.seasar.uruma.context.WidgetHandle;
28  import org.seasar.uruma.context.WindowContext;
29  import org.seasar.uruma.renderer.Renderer;
30  import org.seasar.uruma.util.AssertionUtil;
31  
32  /**
33   * {@link Renderer} の基底クラスです。<br />
34   * 
35   * @author y-komori
36   */
37  public abstract class AbstractRenderer implements Renderer {
38      private WindowContext windowContext;
39  
40      private PartContext context;
41  
42      /*
43       * @see org.seasar.uruma.renderer.Renderer#preRender(org.seasar.uruma.component.UIComponent,
44       *      org.seasar.uruma.context.WidgetHandle,
45       *      org.seasar.uruma.context.WindowContext)
46       */
47      public WidgetHandle preRender(final UIComponent uiComponent,
48              final WidgetHandle parent, final WindowContext context) {
49          setWindowContext(context);
50  
51          return null;
52      }
53  
54      /*
55       * @see org.seasar.uruma.renderer.Renderer#reRender(org.seasar.uruma.context.WidgetHandle)
56       */
57      public void reRender(final WidgetHandle widget) {
58          // Do nothing.
59      }
60  
61      /**
62       * {@link WindowContext} を取得します。<br />
63       * 
64       * @return {@link WindowContext} オブジェクト
65       */
66      protected WindowContext getWindowContext() {
67          return this.windowContext;
68      }
69  
70      /**
71       * {@link WindowContext} を設定します。<br />
72       * 
73       * @param context
74       *            {@link WindowContext} オブジェクト
75       */
76      protected void setWindowContext(final WindowContext context) {
77          AssertionUtil.assertNotNull("context", context);
78          this.windowContext = context;
79      }
80  
81      /**
82       * {@link PartContext} を取得します。
83       * 
84       * @return {@link PartContext} オブジェクト
85       */
86      protected PartContext getContext() {
87          return this.context;
88      }
89  
90      /**
91       * {@link PartContext} を設定します。<br />
92       * 
93       * @param context
94       *            {@link PartContext} オブジェクト
95       */
96      protected void setContext(final PartContext context) {
97          AssertionUtil.assertNotNull("context", context);
98          this.context = context;
99      }
100 
101     /**
102      * {@link WidgetHandle} の実装クラスを生成して返します。<br />
103      * 
104      * @param uiComponent
105      *            {@link WidgetHandle} へ格納する {@link UIComponent} オブジェクト
106      * @param widget
107      *            {@link WidgetHandle} へ格納するオブジェクト
108      * @return 生成した {@link WidgetHandle}
109      */
110     protected WidgetHandle createWidgetHandle(final UIComponent uiComponent,
111             final Object widget) {
112         WidgetHandle handle = ContextFactory.createWidgetHandle(widget);
113         handle.setUiComponent(uiComponent);
114         String id = uiComponent.getId();
115         if (!StringUtil.isEmpty(id)) {
116             handle.setId(id);
117         }
118 
119         return handle;
120     }
121 
122     /**
123      * {@link UIComponent} の保持する文字列のスタイル属性を <code>int</code> 値に変換します。<br />
124      * 
125      * @param uiComponent
126      *            {@link UIComponent} オブジェクト
127      * @return 変換されたスタイル属性
128      */
129     protected int getStyle(final UIComponent uiComponent) {
130         return SWTUtil.getStyle(uiComponent.getStyle(), getDefaultStyle());
131     }
132 
133     /**
134      * スタイル属性が指定されていない場合のデフォルト値を返します。<br />
135      * 通常は、 {@link SWT#NONE} を返します。<br />
136      * デフォルト値を変更したい場合、本メソッドをオーバーライドしてください。<br />
137      * 
138      * @return デフォルトのスタイル属性
139      */
140     protected int getDefaultStyle() {
141         return SWT.NONE;
142     }
143 
144     /**
145      * {@link EnablesDependingDef} のセットアップを行います。<br />
146      * 本メソッドは必要に応じてサブクラス内から呼び出してください。<br />
147      * 
148      * @param handle
149      *            {@link WidgetHandle} オブジェクト
150      * @param dependable
151      *            {@link EnablesDependable} コンポーネント
152      */
153     protected void setupEnablesDependingDef(final WidgetHandle handle,
154             final EnablesDependable dependable) {
155         String enablesDependingId = dependable.getEnablesDependingId();
156         String enablesForType = dependable.getEnablesFor();
157 
158         if (!StringUtil.isEmpty(enablesDependingId)) {
159             EnablesForType type = EnablesForType.SELECTION;
160             if (!StringUtil.isEmpty(enablesForType)) {
161                 type = EnablesForType.valueOf(enablesForType);
162             }
163 
164             EnablesDependingDef def = new EnablesDependingDef(handle,
165                     enablesDependingId, type);
166             getWindowContext().addEnablesDependingDef(def);
167         }
168     }
169 }