01: /*
02: *
03: * Copyright (c) 2007, Sun Microsystems, Inc.
04: *
05: * All rights reserved.
06: *
07: * Redistribution and use in source and binary forms, with or without
08: * modification, are permitted provided that the following conditions
09: * are met:
10: *
11: * * Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * * Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * * Neither the name of Sun Microsystems nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31: */
32: package com.sun.svg.component;
33:
34: import org.w3c.dom.Document;
35: import org.w3c.dom.svg.SVGElement;
36:
37: /**
38: * Simple helper class to bind an SVG <code><text></code> element with a
39: * data source.
40: */
41: public class SVGTextBinding {
42: /**
43: * The text binding's default value.
44: */
45: public static final String DEFAULT_TEXT_VALUE = "---";
46:
47: /**
48: * The id of the bound <text> element.
49: */
50: protected String id;
51:
52: /**
53: * A reference to the bound <text> element instance in the
54: * current skin.
55: */
56: protected SVGElement text;
57:
58: /**
59: * @param id the <text> element in which the "#text" trait should
60: * be set.
61: */
62: public SVGTextBinding(final String id) {
63: if (id == null || "".equals(id)) {
64: throw new IllegalArgumentException();
65: }
66:
67: this .id = id;
68: }
69:
70: /**
71: * @param doc - the <code>Document</code> skin where an element with this
72: * binding's id should be found.
73: */
74: public void hookSkin(final Document doc) {
75: text = (SVGElement) doc.getElementById(id);
76:
77: if (text == null) {
78: throw new IllegalArgumentException(
79: "SVGTextBinding: Element with id " + id
80: + " does not exist in skin");
81: }
82:
83: // Initially set the text to the default value.
84: set(DEFAULT_TEXT_VALUE);
85: }
86:
87: /**
88: * Sets the binding's value.
89: * @param value the new value to display in the text element.
90: */
91: public void set(String value) {
92: if (value == null) {
93: value = "null";
94: }
95: text.setTrait("#text", value);
96: }
97: }
|