01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.JavaScriptObject;
19: import com.google.gwt.user.client.DOM;
20: import com.google.gwt.user.client.Element;
21:
22: /**
23: * A {@link com.google.gwt.user.client.ui.Frame} that has a 'name' associated
24: * with it. This allows the frame to be the target of a
25: * {@link com.google.gwt.user.client.ui.FormPanel}
26: */
27: public class NamedFrame extends Frame {
28:
29: // Used inside JSNI, so please don't delete this field just because
30: // your compiler or IDE says it's unused.
31: private static JavaScriptObject PATTERN_NAME;
32:
33: static {
34: initStatics();
35: }
36:
37: private static native void initStatics() /*-{
38: @com.google.gwt.user.client.ui.NamedFrame::PATTERN_NAME = /^[^<>&\'\"]+$/;
39: }-*/;
40:
41: /**
42: * @param name the specified frame name to be checked
43: * @return <code>true</code> if the name is valid, <code>false</code> if
44: * not
45: */
46: private static native boolean isValidName(String name) /*-{
47: return @com.google.gwt.user.client.ui.NamedFrame::PATTERN_NAME.test(name);
48: }-*/;
49:
50: /**
51: * Constructs a frame with the given name.
52: *
53: * @param name the name of the frame, which must contain at least one
54: * non-whitespace character and must not contain reserved HTML markup
55: * characters such as '<code><</code>', '<code>></code>',
56: * or '<code>&</code>'
57: *
58: * @throws IllegalArgumentException if the supplied name is not allowed
59: */
60: public NamedFrame(String name) {
61: if (name == null || !isValidName(name.trim())) {
62: throw new IllegalArgumentException(
63: "expecting one or more non-whitespace chars with no '<', '>', or '&'");
64: }
65:
66: // Use innerHTML to implicitly create the <iframe>. This is necessary
67: // because most browsers will not respect a dynamically-set iframe name.
68: Element div = DOM.createDiv();
69: DOM.setInnerHTML(div, "<iframe name='" + name + "'>");
70:
71: Element iframe = DOM.getFirstChild(div);
72: setElement(iframe);
73: }
74:
75: /**
76: * Gets the name associated with this frame.
77: *
78: * @return the frame's name
79: */
80: public String getName() {
81: return DOM.getElementProperty(getElement(), "name");
82: }
83: }
|