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.html.editor;
015:
016: import java.beans.PropertyVetoException;
017: import javax.swing.AbstractCellEditor;
018: import org.itsnat.comp.ItsNatComponent;
019: import org.itsnat.comp.html.ItsNatHTMLInputCheckBox;
020: import org.itsnat.comp.html.ItsNatHTMLInputText;
021: import org.itsnat.comp.html.ItsNatHTMLInputTextFormatted;
022: import org.itsnat.comp.html.ItsNatHTMLSelectComboBox;
023: import org.itsnat.comp.html.ItsNatHTMLTextArea;
024: import org.itsnat.core.ItsNatException;
025: import org.itsnat.impl.comp.html.ItsNatHTMLComponentManagerImpl;
026: import org.itsnat.impl.core.ItsNatDocumentImpl;
027: import org.itsnat.impl.core.domutil.ItsNatDOMUtilInternal;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.events.Event;
031: import org.w3c.dom.events.EventListener;
032: import org.w3c.dom.html.HTMLInputElement;
033: import org.w3c.dom.html.HTMLTextAreaElement;
034:
035: /**
036: *
037: * @author jmarranz
038: */
039: public abstract class ItsNatHTMLCellEditorBaseImpl extends
040: AbstractCellEditor implements EventListener {
041: protected EditorDelegate delegate;
042:
043: /**
044: * Creates a new instance of ItsNatHTMLCellEditorBaseImpl
045: */
046: public ItsNatHTMLCellEditorBaseImpl(ItsNatComponent compEditor,
047: ItsNatHTMLComponentManagerImpl componentMgr) {
048: if (compEditor != null) {
049: if (compEditor instanceof ItsNatHTMLInputText)
050: this .delegate = new EditorDelegateInputText(
051: (ItsNatHTMLInputText) compEditor);
052: else if (compEditor instanceof ItsNatHTMLSelectComboBox)
053: this .delegate = new EditorDelegateComboBox(
054: (ItsNatHTMLSelectComboBox) compEditor);
055: else if (compEditor instanceof ItsNatHTMLInputCheckBox)
056: this .delegate = new EditorDelegateCheckBox(
057: (ItsNatHTMLInputCheckBox) compEditor);
058: else if (compEditor instanceof ItsNatHTMLTextArea)
059: this .delegate = new EditorDelegateTextArea(
060: (ItsNatHTMLTextArea) compEditor);
061: else
062: throw new ItsNatException(
063: "Component is not supported as editor: "
064: + compEditor);
065: } else {
066: this .delegate = new EditorDelegateInputText(componentMgr
067: .createItsNatHTMLInputText(null, null));
068: }
069: }
070:
071: public Object getCellEditorValue() {
072: return delegate.getCellEditorValue();
073: }
074:
075: public ItsNatComponent getCellEditorComponent(Object value,
076: Element cellElem) {
077: ItsNatComponent compEditor = delegate.getCellEditorComponent();
078: compEditor.removeEventListener("blur", this ); // Por si acaso no se recibió el blur en una anterior edición
079:
080: Node nodeEditor = compEditor.getNode();
081: cellElem.appendChild(nodeEditor); // Se detecta y se añaden los DOM listeners automáticamente en el componente, cuando se quite del árbol también se detecta y se quitan los listeners antes
082:
083: delegate.setValue(value);
084:
085: compEditor.addEventListener("blur", this );
086:
087: return compEditor;
088: }
089:
090: public void handleEvent(Event evt) {
091: String type = evt.getType();
092: if (type.equals("blur")/* || type.equals("change")*/) {
093: ItsNatComponent compEditor = delegate
094: .getCellEditorComponent();
095: compEditor.removeEventListener("blur", this );
096:
097: stopCellEditing();
098: }
099: }
100:
101: protected abstract class EditorDelegate {
102: public abstract Object getCellEditorValue();
103:
104: public abstract ItsNatComponent getCellEditorComponent();
105:
106: public abstract void setValue(Object value);
107: }
108:
109: protected class EditorDelegateInputText extends EditorDelegate {
110: protected ItsNatHTMLInputText compEditor;
111:
112: public EditorDelegateInputText(ItsNatHTMLInputText compEditor) {
113: this .compEditor = compEditor;
114: }
115:
116: public Object getCellEditorValue() {
117: if (compEditor instanceof ItsNatHTMLInputTextFormatted)
118: return ((ItsNatHTMLInputTextFormatted) compEditor)
119: .getValue();
120: else
121: return compEditor.getText();
122: }
123:
124: public ItsNatComponent getCellEditorComponent() {
125: return compEditor;
126: }
127:
128: public void setValue(Object value) {
129: String text = value.toString();
130: HTMLInputElement inputElem = compEditor
131: .getHTMLInputElement();
132: ItsNatDOMUtilInternal.setAttribute(inputElem, "size",
133: String.valueOf(text.length() + 1)); // El + 1 es para evitar el 0 en caso de cadena nula
134:
135: if (compEditor instanceof ItsNatHTMLInputTextFormatted) {
136: try {
137: ((ItsNatHTMLInputTextFormatted) compEditor)
138: .setValue(value);
139: } catch (PropertyVetoException ex) {
140: throw new ItsNatException(ex);
141: }
142: } else
143: compEditor.setText(text);
144:
145: compEditor.focus();
146:
147: ItsNatDocumentImpl itsNatDoc = (ItsNatDocumentImpl) compEditor
148: .getItsNatDocument();
149: if (itsNatDoc.isMSIE())
150: compEditor.focus(); // El primero parece que falla en Internet Explorer 6
151: //compEditor.select(); no es necesario, solucionaba el focus en MSIE
152: }
153: }
154:
155: protected class EditorDelegateComboBox extends EditorDelegate {
156: protected ItsNatHTMLSelectComboBox compEditor;
157:
158: public EditorDelegateComboBox(
159: ItsNatHTMLSelectComboBox compEditor) {
160: this .compEditor = compEditor;
161: }
162:
163: public Object getCellEditorValue() {
164: return compEditor.getSelectedItem();
165: }
166:
167: public ItsNatComponent getCellEditorComponent() {
168: return compEditor;
169: }
170:
171: public void setValue(Object value) {
172: compEditor.setSelectedItem(value);
173:
174: compEditor.focus();
175: }
176: }
177:
178: protected class EditorDelegateCheckBox extends EditorDelegate {
179: protected ItsNatHTMLInputCheckBox compEditor;
180:
181: public EditorDelegateCheckBox(ItsNatHTMLInputCheckBox compEditor) {
182: this .compEditor = compEditor;
183: }
184:
185: public Object getCellEditorValue() {
186: return Boolean.valueOf(compEditor.isSelected());
187: }
188:
189: public ItsNatComponent getCellEditorComponent() {
190: return compEditor;
191: }
192:
193: public void setValue(Object value) {
194: boolean selected = false;
195: if (value instanceof Boolean)
196: selected = ((Boolean) value).booleanValue();
197: else if (value instanceof String)
198: selected = value.equals("true");
199:
200: compEditor.setSelected(selected);
201:
202: compEditor.focus(); // Con uno basta
203: //compEditor.select(); no es necesario
204: }
205: }
206:
207: protected class EditorDelegateTextArea extends EditorDelegate {
208: protected ItsNatHTMLTextArea compEditor;
209:
210: public EditorDelegateTextArea(ItsNatHTMLTextArea compEditor) {
211: this .compEditor = compEditor;
212: }
213:
214: public Object getCellEditorValue() {
215: return compEditor.getText();
216: }
217:
218: public ItsNatComponent getCellEditorComponent() {
219: return compEditor;
220: }
221:
222: public void setValue(Object value) {
223: String text = value.toString();
224: HTMLTextAreaElement elem = compEditor
225: .getHTMLTextAreaElement();
226:
227: int maxNumCols = 1;
228: int rows = 0;
229: int cols = 0;
230: for (int i = 0; i < text.length(); i++) {
231: if (text.charAt(i) == '\n') {
232: rows++;
233: if (cols > maxNumCols)
234: maxNumCols = cols;
235: cols = 0;
236: } else
237: cols++;
238: }
239: if (cols > maxNumCols)
240: maxNumCols = cols;
241: maxNumCols += 4; // Para que sobre algo más (o al menos una pues el cero no vale)
242: rows += 2; // Idem
243: elem.setCols(maxNumCols);
244: elem.setRows(rows);
245: compEditor.setText(text);
246:
247: compEditor.focus();
248: ItsNatDocumentImpl itsNatDoc = (ItsNatDocumentImpl) compEditor
249: .getItsNatDocument();
250: if (itsNatDoc.isMSIE())
251: compEditor.focus(); // El primero parece que falla en Internet Explorer 6
252: //compEditor.select(); no es necesario, solucionaba el focus en MSIE
253: }
254: }
255: }
|