KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * A panel that contains HTML, and which can attach child widgets to identified
25  * elements within that HTML.
26  */

27 public class HTMLPanel extends ComplexPanel {
28
29   private static int sUid;
30
31   /**
32    * A helper method for creating unique IDs for elements within dynamically-
33    * generated HTML. This is important because no two elements in a document
34    * should have the same id.
35    *
36    * @return a new unique identifier
37    */

38   public static String JavaDoc createUniqueId() {
39     return "HTMLPanel_" + (++sUid);
40   }
41
42   /**
43    * Creates an HTML panel with the specified HTML contents. Any element within
44    * this HTML that has a specified id can contain a child widget.
45    *
46    * @param html the panel's HTML
47    */

48   public HTMLPanel(String JavaDoc html) {
49     setElement(DOM.createDiv());
50     DOM.setInnerHTML(getElement(), html);
51   }
52
53   /**
54    * Adds a child widget to the panel, contained within the HTML element
55    * specified by a given id.
56    *
57    * @param widget the widget to be added
58    * @param id the id of the element within which it will be contained
59    */

60   public void add(Widget widget, String JavaDoc id) {
61     Element elem = getElementById(getElement(), id);
62     if (elem == null) {
63       throw new NoSuchElementException JavaDoc(id);
64     }
65
66     super.add(widget, elem);
67   }
68
69   /*
70    * Implements getElementById() downward from the given element. We need to do
71    * this because {@link #add(Widget, String)} must often be called before the
72    * panel is attached to the DOM, so {@link Dom#getElementById} won't yet work.
73    */

74   private Element getElementById(Element elem, String JavaDoc id) {
75     String JavaDoc elemId = DOM.getElementProperty(elem, "id");
76     if ((elemId != null) && elemId.equals(id)) {
77       return elem;
78     }
79
80     Element child = DOM.getFirstChild(elem);
81     while (child != null) {
82       Element ret = getElementById(child, id);
83       if (ret != null) {
84         return ret;
85       }
86       child = DOM.getNextSibling(child);
87     }
88
89     return null;
90   }
91 }
92
Popular Tags