001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.comp.free;
015:
016: import org.itsnat.comp.free.ItsNatFreeInclude;
017: import org.itsnat.comp.ui.ItsNatComponentUI;
018: import org.itsnat.core.DocFragmentTemplate;
019: import org.itsnat.core.ItsNatException;
020: import org.itsnat.core.ItsNatServlet;
021: import org.itsnat.core.NameValue;
022: import org.itsnat.core.domutil.ItsNatDOMUtil;
023: import org.itsnat.impl.comp.*;
024: import org.itsnat.impl.comp.free.ui.ItsNatFreeElementComponentUIImpl;
025: import org.itsnat.impl.core.ItsNatDocumentImpl;
026: import org.w3c.dom.DocumentFragment;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.Node;
029:
030: /**
031: *
032: * @author jmarranz
033: */
034: public class ItsNatFreeIncludeImpl extends
035: ItsNatFreeElementComponentImpl implements ItsNatFreeInclude {
036: protected boolean included;
037: protected boolean buildComp;
038: protected String fragmentName;
039:
040: /**
041: * Creates a new instance of ItsNatFreeIncludeImpl
042: */
043: public ItsNatFreeIncludeImpl(Element element,
044: NameValue[] artifacts,
045: ItsNatComponentManagerImpl componentMgr) {
046: super (element, artifacts, componentMgr);
047:
048: init();
049: }
050:
051: public Class getStructureClass() {
052: return null;
053: }
054:
055: public Object createDefaultStructure() {
056: return null;
057: }
058:
059: public boolean isIncluded() {
060: return included;
061: }
062:
063: public void enableEventListeners() {
064: // No hay eventos por defecto
065: }
066:
067: public void bindDataModel() {
068: // No hay modelo
069: }
070:
071: public void unbindDataModel() {
072: // No hay modelo
073: }
074:
075: public ItsNatComponentUI createDefaultItsNatComponentUI() {
076: return new ItsNatFreeElementComponentUIImpl(this );
077: }
078:
079: public void syncUIWithDataModel() {
080: }
081:
082: public Object createDefaultModelInternal() {
083: return null; // No hay modelo
084: }
085:
086: public String getIncludedFragmentName() {
087: return fragmentName;
088: }
089:
090: public void includeFragment(String name, boolean buildComp) {
091: removeFragment();
092:
093: ItsNatDocumentImpl itsNatDoc = getItsNatDocumentImpl();
094: ItsNatServlet servlet = itsNatDoc.getDocumentTemplateImpl()
095: .getItsNatServlet();
096: DocFragmentTemplate docFragDesc = servlet
097: .getDocFragmentTemplate(name);
098: if (docFragDesc == null)
099: throw new ItsNatException("Document fragment not found: "
100: + name);
101: DocumentFragment docFrag = docFragDesc
102: .loadDocumentFragment(itsNatDoc);
103:
104: Element parent = getElement();
105: ItsNatDOMUtil.removeAllChildren(parent); // Por si acaso no está vacío
106: parent.appendChild(docFrag); // El DocumentFragment queda vacío (creo)
107:
108: if (buildComp) {
109: Node child = parent.getFirstChild();
110: while (child != null) {
111: componentMgr.buildItsNatComponents(child);
112: child = child.getNextSibling();
113: }
114: }
115:
116: this .fragmentName = name;
117: this .included = true;
118: this .buildComp = buildComp;
119: }
120:
121: public void removeFragment() {
122: if (!included)
123: return;
124:
125: Element parent = getElement();
126:
127: if (buildComp) {
128: Node child = parent.getFirstChild();
129: while (child != null) {
130: componentMgr.removeItsNatComponents(child, true, null);
131: child = child.getNextSibling();
132: }
133: }
134:
135: ItsNatDOMUtil.removeAllChildren(parent);
136:
137: this .included = false;
138: this.fragmentName = null;
139: }
140: }
|