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.context.impl;
17  
18  import org.seasar.uruma.component.UIComponent;
19  import org.seasar.uruma.context.ContextFactory;
20  import org.seasar.uruma.context.WidgetHandle;
21  import org.seasar.uruma.util.AssertionUtil;
22  
23  /**
24   * {@link WidgetHandle} の実装クラスです。<br />
25   * 
26   * @author y-komori
27   */
28  public class WidgetHandleImpl implements WidgetHandle {
29      private String id;
30  
31      private Object widget;
32  
33      private UIComponent uiComponent;
34  
35      /**
36       * {@link WidgetHandleImpl} を構築します。<br />
37       * 本クラスのインスタンスを生成するには、{@link ContextFactory#createWidgetHandle(Object)}
38       * メソッドを利用してください。<br />
39       * 
40       * @param widget
41       *            ウィジットオブジェクト
42       */
43      public WidgetHandleImpl(final Object widget) {
44          AssertionUtil.assertNotNull("widget", widget);
45  
46          this.widget = widget;
47      }
48  
49      /*
50       * @see org.seasar.uruma.context.WidgetHandle#getId()
51       */
52      public String getId() {
53          return this.id;
54      }
55  
56      /*
57       * @see org.seasar.uruma.context.WidgetHandle#getWidget()
58       */
59      public Object getWidget() {
60          return this.widget;
61      }
62  
63      /*
64       * @see org.seasar.uruma.context.WidgetHandle#getCastWidget()
65       */
66      @SuppressWarnings("unchecked")
67      public <T> T getCastWidget() {
68          return (T) this.getWidgetClass().cast(this.getWidget());
69      }
70  
71      /*
72       * @see org.seasar.uruma.context.WidgetHandle#getWidgetClass()
73       */
74      public Class<?> getWidgetClass() {
75          return widget.getClass();
76      }
77  
78      /*
79       * @see org.seasar.uruma.context.WidgetHandle#getUiComponent()
80       */
81      public UIComponent getUiComponent() {
82          return this.uiComponent;
83      }
84  
85      /*
86       * @see org.seasar.uruma.context.WidgetHandle#instanceOf(java.lang.Class)
87       */
88      public boolean instanceOf(final Class<?> clazz) {
89          if (clazz != null) {
90              return clazz.isAssignableFrom(widget.getClass());
91          } else {
92              throw new IllegalArgumentException();
93          }
94      }
95  
96      /*
97       * @see org.seasar.uruma.context.WidgetHandle#setId(java.lang.String)
98       */
99      public void setId(final String id) {
100         AssertionUtil.assertNotEmpty("id", id);
101         this.id = id;
102     }
103 
104     /*
105      * @see org.seasar.uruma.context.WidgetHandle#setUiComponent(org.seasar.uruma.component.UIComponent)
106      */
107     public void setUiComponent(final UIComponent uiComponent) {
108         AssertionUtil.assertNotNull("uiComponent", uiComponent);
109         this.uiComponent = uiComponent;
110     }
111 
112     /*
113      * @see java.lang.Object#hashCode()
114      */
115     @Override
116     public int hashCode() {
117         final int prime = 31;
118         int result = 1;
119         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
120         return result;
121     }
122 
123     /*
124      * @see java.lang.Object#equals(java.lang.Object)
125      */
126     @Override
127     public boolean equals(final Object obj) {
128         if (this == obj)
129             return true;
130         if (obj == null)
131             return false;
132         if (getClass() != obj.getClass())
133             return false;
134         final WidgetHandleImpl other = (WidgetHandleImpl) obj;
135         if (this.id == null) {
136             if (other.id != null)
137                 return false;
138         } else if (!this.id.equals(other.id))
139             return false;
140         return true;
141     }
142 }