001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015: package org.itsnat.feashow;
016:
017: import org.itsnat.core.DocumentTemplate;
018: import org.itsnat.core.ItsNatDocument;
019: import org.itsnat.core.domutil.ItsNatDOMUtil;
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.NodeList;
023: import org.w3c.dom.html.HTMLAnchorElement;
024: import org.w3c.dom.html.HTMLTextAreaElement;
025:
026: public abstract class FeatureTreeNode {
027: public static final int NONE_PANEL = 0;
028: public static final int EXAMPLE_PANEL = 1;
029: public static final int DOC_PANEL = 2;
030: public static final int CODE_PANEL = 3;
031:
032: protected FeatureTreeBuilder treeBuilder;
033: protected int currentPanel = NONE_PANEL;
034: protected Element logElem;
035: protected boolean hasExample;
036: protected boolean hasExplanation;
037: protected boolean hasSourceCode;
038: protected String featureName;
039: protected String treeNodeText;
040: protected String title;
041:
042: public FeatureTreeNode() {
043: }
044:
045: public void init(FeatureTreeBuilder treeBuilder,
046: boolean hasExample, boolean hasExplanation,
047: boolean hasSourceCode, String featureName,
048: String treeNodeText, String title) {
049: this .treeBuilder = treeBuilder;
050: this .hasExample = hasExample;
051: this .hasExplanation = hasExplanation;
052: this .hasSourceCode = hasSourceCode;
053: this .featureName = featureName;
054: this .treeNodeText = treeNodeText;
055: this .title = title;
056: }
057:
058: public int getPanelCode(String panelName) {
059: if (panelName.equals("ex"))
060: return EXAMPLE_PANEL;
061: else if (panelName.equals("code"))
062: return CODE_PANEL;
063: else if (panelName.equals("doc"))
064: return DOC_PANEL;
065: else if (panelName.equals("default"))
066: return getFirstPanel();
067: else
068: return NONE_PANEL;
069: }
070:
071: public int getFirstPanel() {
072: if (hasExample)
073: return EXAMPLE_PANEL;
074: else if (hasSourceCode)
075: return CODE_PANEL;
076: else if (hasExplanation)
077: return DOC_PANEL;
078:
079: return 0;
080: }
081:
082: public Element getTabElement(int panel) {
083: switch (panel) {
084: case FeatureTreeNode.EXAMPLE_PANEL:
085: return treeBuilder.getExampleTabElement();
086: case FeatureTreeNode.CODE_PANEL:
087: return treeBuilder.getCodeTabElement();
088: case FeatureTreeNode.DOC_PANEL:
089: return treeBuilder.getDocTabElement();
090: default:
091: return null;
092: }
093: }
094:
095: public boolean hasExample() {
096: return hasExample;
097: }
098:
099: public boolean hasSourceCode() {
100: return hasSourceCode;
101: }
102:
103: public boolean hasExplanation() {
104: return hasExplanation;
105: }
106:
107: public String getFeatureName() {
108: return featureName;
109: }
110:
111: public String getTreeNodeText() {
112: return treeNodeText;
113: }
114:
115: public String getTitle() {
116: return title;
117: }
118:
119: public FeatureTreeBuilder getFeatureTreeBuilder() {
120: return treeBuilder;
121: }
122:
123: public ItsNatDocument getItsNatDocument() {
124: return treeBuilder.getItsNatDocument();
125: }
126:
127: public String getExamplePanelName() {
128: return getFeatureName() + ".ex";
129: }
130:
131: public String getDocPanelName() {
132: return getFeatureName() + ".doc";
133: }
134:
135: public String getCodePanelName() {
136: return getFeatureName() + ".code";
137: }
138:
139: public String toString() {
140: return getTreeNodeText();
141: }
142:
143: public void endPanels() {
144: endCurrentPanel();
145: }
146:
147: public void endCurrentPanel() {
148: if (currentPanel == EXAMPLE_PANEL)
149: endExamplePanel();
150:
151: this .currentPanel = NONE_PANEL;
152: this .logElem = null;
153: }
154:
155: public String getPanelName(int panel) {
156: switch (panel) {
157: case FeatureTreeNode.EXAMPLE_PANEL:
158: return getExamplePanelName();
159: case FeatureTreeNode.DOC_PANEL:
160: return getDocPanelName();
161: case FeatureTreeNode.CODE_PANEL:
162: return getCodePanelName();
163: }
164: return null;
165: }
166:
167: public abstract void startExamplePanel();
168:
169: public abstract void endExamplePanel();
170:
171: public boolean hasLog() {
172: return (logElem != null);
173: }
174:
175: public void log(String text) {
176: ItsNatDocument itsNatDoc = getItsNatDocument();
177: Document doc = itsNatDoc.getDocument();
178: if (logElem == null) {
179: this .logElem = doc.getElementById("logId");
180: if (logElem == null)
181: throw new RuntimeException(
182: "Internal Error: Log element is not defined");
183: }
184:
185: logElem.removeAttribute("style"); // makes visible
186: int len = logElem.getChildNodes().getLength();
187: if (len <= 30) {
188: if (len == 30)
189: text = "Reached the log view limit!!"; // To avoid overloading the server
190:
191: Element msgElem = doc.createElement("div");
192: msgElem.appendChild(doc.createTextNode(text));
193:
194: logElem.appendChild(msgElem);
195: }
196:
197: }
198:
199: public Element getFeatureBoxElement() {
200: return treeBuilder.getFeatureBoxElement();
201: }
202:
203: public void startCodePanel() {
204: ItsNatDocument itsNatDoc = getItsNatDocument();
205:
206: Element parent = getFeatureBoxElement();
207: NodeList textAreas = parent.getElementsByTagName("textarea");
208: for (int i = 0; i < textAreas.getLength(); i++) {
209: HTMLTextAreaElement textAreaElem = (HTMLTextAreaElement) textAreas
210: .item(i);
211: String classAttr = textAreaElem.getAttribute("class");
212: textAreaElem.setAttribute("class", classAttr
213: + ":collapse:nogutter");
214: if (classAttr.startsWith("html"))
215: SyntaxHighlighter.highlightMarkup(textAreaElem,
216: itsNatDoc);
217: else if (classAttr.startsWith("xml"))
218: SyntaxHighlighter.highlightMarkup(textAreaElem,
219: itsNatDoc);
220: else
221: // java
222: SyntaxHighlighter
223: .highlightJava(textAreaElem, itsNatDoc);
224: }
225: }
226:
227: public void selectFeature() {
228: ItsNatDOMUtil.setTextContent(treeBuilder
229: .getFeatureTitleElement(), getTitle());
230:
231: initTab(treeBuilder.getExampleTabElement(), hasExample());
232: initTab(treeBuilder.getCodeTabElement(), hasSourceCode());
233: initTab(treeBuilder.getDocTabElement(), hasExplanation());
234:
235: initTabPermalink(treeBuilder.getExampleTabPermalinkElement(),
236: hasExample(), "ex");
237: initTabPermalink(treeBuilder.getCodeTabPermalinkElement(),
238: hasSourceCode(), "code");
239: initTabPermalink(treeBuilder.getDocTabPermalinkElement(),
240: hasExplanation(), "doc");
241:
242: selectPanel(getFirstPanel());
243: }
244:
245: public void initTabPermalink(HTMLAnchorElement link,
246: boolean isVisible, String tabName) {
247: if (isVisible) {
248: DocumentTemplate mainTemplate = getItsNatDocument()
249: .getDocumentTemplate();
250: link.setHref("?itsnat_doc_name=" + mainTemplate.getName()
251: + "&feature=" + getFeatureName() + "." + tabName);
252: } else
253: link.setHref("javascript:void(0);");
254: }
255:
256: public void selectPanel(int panel) {
257: endCurrentPanel();
258:
259: this .currentPanel = panel;
260:
261: String panelName = getPanelName(panel);
262: try {
263: treeBuilder.getItsNatFreeInclude().includeFragment(
264: panelName, false);
265: } catch (Exception ex) {
266: return;
267: }
268:
269: switch (currentPanel) {
270: case EXAMPLE_PANEL:
271: startExamplePanel();
272: break;
273: case CODE_PANEL:
274: startCodePanel();
275: break;
276: }
277:
278: if (hasExample())
279: setTabNotActive(treeBuilder.getExampleTabElement());
280: if (hasSourceCode())
281: setTabNotActive(treeBuilder.getCodeTabElement());
282: if (hasExplanation())
283: setTabNotActive(treeBuilder.getDocTabElement());
284:
285: Element tabElem = getTabElement(panel);
286: setTabActive(tabElem);
287: }
288:
289: public void setTabNotActive(Element elem) {
290: elem.removeAttribute("style");
291: }
292:
293: public void setTabActive(Element elem) {
294: elem.setAttribute("style",
295: "background: #ED752A; border: 3px #ED752A solid;");
296: }
297:
298: public void initTab(Element elem, boolean visible) {
299: if (visible)
300: elem.removeAttribute("style");
301: else
302: elem.setAttribute("style", "visibility: hidden;");
303: }
304: }
|