KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > Composite


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * A type of widget that can wrap another widget, hiding the wrapped widget's
23  * methods. When added to a panel, a composite behaves exactly as if the widget
24  * it wraps had been added.
25  *
26  * <p>
27  * The composite is useful for creating a single widget out of an aggregate of
28  * multiple other widgets contained in a single panel.
29  * </p>
30  *
31  * <p>
32  * <h3>Example</h3>
33  * {@example com.google.gwt.examples.CompositeExample}
34  * </p>
35  */

36 public abstract class Composite extends Widget {
37
38   private Widget widget;
39
40   /**
41    * This override checks to ensure {@link #initWidget(Widget)} has been called.
42    */

43   public Element getElement() {
44     if (widget == null) {
45       throw new IllegalStateException JavaDoc("initWidget() was never called in "
46           + GWT.getTypeName(this));
47     }
48     return super.getElement();
49   }
50
51   /**
52    * Provides subclasses access to the topmost widget that defines this
53    * composite.
54    *
55    * @return the widget
56    */

57   protected Widget getWidget() {
58     return widget;
59   }
60
61   /**
62    * Sets the widget to be wrapped by the composite. The wrapped widget must be
63    * set before calling any {@link Widget} methods on this object, or adding it
64    * to a panel. This method may only be called once for a given composite.
65    *
66    * @param widget the widget to be wrapped
67    */

68   protected void initWidget(Widget widget) {
69     // Make sure the widget is not being set twice.
70
if (this.widget != null) {
71       throw new IllegalStateException JavaDoc("Composite.initWidget() may only be "
72           + "called once.");
73     }
74
75     widget.removeFromParent();
76
77     // Use the contained widget's element as the composite's element,
78
// effectively merging them within the DOM.
79
setElement(widget.getElement());
80
81     // The Composite now owns this widget.
82
this.widget = widget;
83     widget.setParent(this);
84   }
85
86   protected void onAttach() {
87     super.onAttach();
88
89     // Call onAttach() on behalf of the contained widget.
90
widget.onAttach();
91   }
92
93   protected void onDetach() {
94     super.onDetach();
95
96     // Call onDetach() on behalf of the contained widget.
97
widget.onDetach();
98   }
99
100   /**
101    * Sets the widget to be wrapped by the composite.
102    *
103    * @param widget the widget to be wrapped
104    * @deprecated this method is deprecated, and will be removed when GWT leaves
105    * beta (use {@link #initWidget(Widget)} instead)
106    */

107   protected void setWidget(Widget widget) {
108     initWidget(widget);
109   }
110 }
111
Popular Tags