01: /*
02: * This file is not part of the ItsNat framework.
03: *
04: * Original source code use and closed source derivatives are authorized
05: * to third parties with no restriction or fee.
06: * The original source code is owned by the author.
07: *
08: * This program is distributed AS IS in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * Author: Jose Maria Arranz Santamaria
13: * (C) Innowhere Software Services S.L., Spanish company, year 2007
14: */
15: package org.itsnat.manual;
16:
17: import javax.swing.event.DocumentEvent;
18: import javax.swing.event.DocumentListener;
19: import javax.swing.text.BadLocationException;
20: import javax.swing.text.PlainDocument;
21: import org.itsnat.comp.html.ItsNatHTMLComponentManager;
22: import org.itsnat.comp.html.ItsNatHTMLForm;
23: import org.itsnat.comp.html.ItsNatHTMLInputText;
24: import org.itsnat.core.html.ItsNatHTMLDocument;
25: import org.w3c.dom.Element;
26: import org.w3c.dom.events.Event;
27: import org.w3c.dom.events.EventListener;
28: import org.w3c.dom.html.HTMLDocument;
29:
30: public class CompExampleProcessor implements EventListener,
31: DocumentListener {
32: protected ItsNatHTMLDocument itsNatDoc;
33: protected ItsNatHTMLInputText inputComp;
34:
35: public CompExampleProcessor(ItsNatHTMLDocument itsNatDoc) {
36: this .itsNatDoc = itsNatDoc;
37: load();
38: }
39:
40: public void load() {
41: // HTMLDocument doc = itsNatDoc.getHTMLDocument();
42: ItsNatHTMLComponentManager componentMgr = itsNatDoc
43: .getItsNatHTMLComponentManager();
44:
45: this .inputComp = (ItsNatHTMLInputText) componentMgr
46: .addItsNatComponentById("inputId");
47: inputComp.setText("Change this text and lost the focus");
48:
49: inputComp.addEventListener("change", this );
50:
51: PlainDocument dataModel = (PlainDocument) inputComp
52: .getDocument();
53: dataModel.addDocumentListener(this );
54:
55: ItsNatHTMLForm formComp = componentMgr.createItsNatHTMLForm(
56: inputComp.getHTMLInputElement().getForm(), null);
57: formComp.reset();
58:
59: inputComp.focus();
60: inputComp.select();
61: }
62:
63: public void handleEvent(Event evt) {
64: log("Text has been changed");
65: }
66:
67: public void insertUpdate(DocumentEvent e) {
68: javax.swing.text.Document docModel = e.getDocument();
69: int offset = e.getOffset();
70: int len = e.getLength();
71:
72: try {
73: log("Text inserted: " + offset + "-" + len + " chars,\""
74: + docModel.getText(offset, len) + "\"");
75: } catch (BadLocationException ex) {
76: throw new RuntimeException(ex);
77: }
78: }
79:
80: public void removeUpdate(DocumentEvent e) {
81: // javax.swing.text.Document docModel = e.getDocument();
82: int offset = e.getOffset();
83: int len = e.getLength();
84:
85: log("Text removed: " + offset + "-" + len + " chars");
86: }
87:
88: public void changedUpdate(DocumentEvent e) {
89: // A PlainDocument has no attributes
90: }
91:
92: public void log(String msg) {
93: HTMLDocument doc = itsNatDoc.getHTMLDocument();
94: Element noteElem = doc.createElement("p");
95: noteElem.appendChild(doc.createTextNode(msg));
96: doc.getBody().appendChild(noteElem);
97: }
98: }
|