001: /* Text.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Nov 24 15:17:07 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zhtml;
020:
021: import java.io.Writer;
022: import java.io.IOException;
023:
024: import org.zkoss.lang.Objects;
025:
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.Components;
028: import org.zkoss.zk.ui.AbstractComponent;
029: import org.zkoss.zk.ui.UiException;
030: import org.zkoss.zk.ui.WrongValueException;
031: import org.zkoss.zk.ui.ext.RawId;
032: import org.zkoss.zk.ui.metainfo.LanguageDefinition;
033: import org.zkoss.zk.ui.sys.ComponentCtrl;
034:
035: /**
036: * Represents a piece of text (of DOM).
037: *
038: * @author tomyeh
039: */
040: public class Text extends AbstractComponent implements RawId {
041: private String _value = "";
042:
043: public Text() {
044: }
045:
046: public Text(String value) {
047: setValue(value);
048: }
049:
050: /** Returns the value.
051: * <p>Default: "".
052: */
053: public String getValue() {
054: return _value;
055: }
056:
057: /** Sets the value.
058: */
059: public void setValue(String value) {
060: if (value == null)
061: value = "";
062: if (!Objects.equals(_value, value)) {
063: _value = value;
064: invalidate();
065: }
066: }
067:
068: /** Whether to generate the value directly without ID. */
069: private boolean isIdRequired() {
070: final Component p = getParent();
071: return p == null || !isVisible()
072: || !Components.isAutoId(getId()) || !isRawLabel(p);
073: }
074:
075: private static boolean isRawLabel(Component comp) {
076: final LanguageDefinition langdef = comp.getDefinition()
077: .getLanguageDefinition();
078: return langdef != null && langdef.isRawLabel();
079: }
080:
081: //-- Component --//
082: public void setParent(Component parent) {
083: if (!isIdRequired()) {
084: final Component old = getParent();
085: if (old != parent) {
086: if (old != null)
087: old.invalidate();
088: if (parent != null)
089: parent.invalidate();
090: }
091: }
092: super .setParent(parent);
093: }
094:
095: public void invalidate() {
096: if (isIdRequired())
097: super .invalidate();
098: else
099: getParent().invalidate();
100: }
101:
102: public void redraw(Writer out) throws IOException {
103: final boolean idRequired = isIdRequired();
104: if (idRequired) {
105: out.write("<span id=\"");
106: out.write(getUuid());
107: out.write("\">");
108: }
109:
110: out.write(_value);
111:
112: if (idRequired)
113: out.write("</span>");
114: }
115:
116: public boolean isChildable() {
117: return false;
118: }
119: }
|