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:
016: package org.itsnat.feashow.features.components.lists;
017:
018: import javax.swing.DefaultListModel;
019: import javax.swing.event.DocumentEvent;
020: import javax.swing.event.DocumentListener;
021: import org.itsnat.comp.ItsNatComponentManager;
022: import org.itsnat.comp.ItsNatList;
023: import org.itsnat.comp.ItsNatListCellRenderer;
024: import org.itsnat.comp.ItsNatListMultSel;
025: import org.itsnat.comp.html.ItsNatHTMLInputText;
026: import org.itsnat.core.ItsNatDocument;
027: import org.itsnat.core.domutil.ItsNatDOMUtil;
028: import org.itsnat.feashow.features.components.shared.Person;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031:
032: public class PersonRenderer implements ItsNatListCellRenderer {
033: public PersonRenderer() {
034: }
035:
036: public static ItsNatHTMLInputText getInputText(String id,
037: Node containerNode, ItsNatList list) {
038: Element firstName = ItsNatDOMUtil.getElementById(id,
039: (Element) containerNode);
040: ItsNatComponentManager componentMgr = list
041: .getItsNatComponentManager();
042: return (ItsNatHTMLInputText) componentMgr
043: .findItsNatComponent(firstName);
044: }
045:
046: public void renderListCell(ItsNatList list, int index,
047: Object value, boolean isSelected, boolean hasFocus,
048: Element cellContentElem, boolean isNew) {
049: ItsNatListMultSel listMult = (ItsNatListMultSel) list;
050:
051: final DefaultListModel listModel = (DefaultListModel) listMult
052: .getListModel();
053: final Person person = (Person) listModel.getElementAt(index);
054:
055: if (isNew) {
056: ItsNatDocument itsNatDoc = listMult.getItsNatDocument();
057:
058: if (!itsNatDoc.getDocumentTemplate()
059: .isAutoBuildComponents()) {
060: ItsNatComponentManager componentMgr = listMult
061: .getItsNatComponentManager();
062: componentMgr.buildItsNatComponents(cellContentElem);
063: }
064:
065: final ItsNatHTMLInputText firstNameComp = getInputText(
066: "firstNameId", cellContentElem, listMult);
067: final ItsNatHTMLInputText lastNameComp = getInputText(
068: "lastNameId", cellContentElem, listMult);
069:
070: firstNameComp.setText(person.getFirstName());
071: lastNameComp.setText(person.getLastName());
072:
073: DocumentListener firstNameListener = new DocumentListener() {
074: public void insertUpdate(DocumentEvent e) {
075: update();
076: }
077:
078: public void removeUpdate(DocumentEvent e) {
079: update();
080: }
081:
082: public void changedUpdate(DocumentEvent e) {
083: update();
084: }
085:
086: public void update() {
087: person.setFirstName(firstNameComp.getText());
088: }
089: };
090:
091: firstNameComp.getDocument().addDocumentListener(
092: firstNameListener);
093:
094: DocumentListener lastNameListener = new DocumentListener() {
095: public void insertUpdate(DocumentEvent e) {
096: update();
097: }
098:
099: public void removeUpdate(DocumentEvent e) {
100: update();
101: }
102:
103: public void changedUpdate(DocumentEvent e) {
104: update();
105: }
106:
107: public void update() {
108: person.setLastName(lastNameComp.getText());
109: }
110:
111: };
112:
113: lastNameComp.getDocument().addDocumentListener(
114: lastNameListener);
115: } else {
116: ItsNatHTMLInputText firstNameComp = getInputText(
117: "firstNameId", cellContentElem, listMult);
118: ItsNatHTMLInputText lastNameComp = getInputText(
119: "lastNameId", cellContentElem, listMult);
120:
121: firstNameComp.setText(person.getFirstName());
122: lastNameComp.setText(person.getLastName());
123: }
124: }
125:
126: public void unrenderListCell(ItsNatList list, int index,
127: Element cellContentElem) {
128: ItsNatDocument itsNatDoc = list.getItsNatDocument();
129: if (!itsNatDoc.getDocumentTemplate().isAutoBuildComponents()) {
130: ItsNatComponentManager componentMgr = list
131: .getItsNatComponentManager();
132: componentMgr.removeItsNatComponents(cellContentElem, true);
133: }
134: }
135: }
|