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;
015:
016: import org.itsnat.comp.ItsNatListCellEditor;
017: import javax.swing.DefaultListModel;
018: import javax.swing.ListModel;
019: import org.itsnat.comp.ItsNatComponent;
020: import org.itsnat.comp.ui.ItsNatListCellUI;
021: import org.itsnat.comp.ui.ItsNatListUI;
022: import org.itsnat.impl.comp.free.ItsNatFreeListMultSelImpl;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.events.Event;
026:
027: /**
028: *
029: * @author jmarranz
030: */
031: public class ListCellEditorProcessor extends EditorProcessorBase {
032: protected ItsNatListCellUI cellInfo;
033:
034: public ListCellEditorProcessor(ItsNatFreeListMultSelImpl compParent) {
035: super (compParent);
036: }
037:
038: public ItsNatFreeListMultSelImpl getItsNatFreeListMultSel() {
039: return (ItsNatFreeListMultSelImpl) compParent;
040: }
041:
042: public ItsNatListCellEditor getItsNatListCellEditor() {
043: return (ItsNatListCellEditor) cellEditor;
044: }
045:
046: public void setItsNatListCellEditor(ItsNatListCellEditor cellEditor) {
047: setCellEditor(cellEditor);
048: }
049:
050: public void setCurrentContext(ItsNatListCellUI cellInfo) {
051: this .cellInfo = cellInfo;
052: }
053:
054: public int getIndex() {
055: if (cellInfo == null)
056: return -1;
057: return cellInfo.getIndex();
058: }
059:
060: public void startEdition(int index) {
061: if (prepareEdition()) {
062: openEditor(index);
063: }
064: }
065:
066: protected void openEditor(int index) {
067: ItsNatListUI compUI = getItsNatFreeListMultSel()
068: .getItsNatListUI();
069: ItsNatListCellUI cellInfo = compUI.getItsNatListCellUIAt(index);
070: openEditor(cellInfo);
071: }
072:
073: protected void openEditor(Event evt) {
074: Node nodeClicked = (Node) evt.getTarget(); // Puede ser un nodo interior del elemento pulsado
075:
076: ItsNatListUI compUI = getItsNatFreeListMultSel()
077: .getItsNatListUI();
078: ItsNatListCellUI cellInfo = compUI
079: .getItsNatListCellUIFromNode(nodeClicked);
080: openEditor(cellInfo);
081: }
082:
083: private void openEditor(ItsNatListCellUI cellInfo) {
084: if (cellInfo != null) // Se ha pulsado un elemento verdaderamente
085: {
086: setCurrentContext(cellInfo);
087:
088: int index = cellInfo.getIndex();
089: ItsNatFreeListMultSelImpl list = getItsNatFreeListMultSel();
090: ListModel dataModel = list.getListModel();
091: Element cellContentElem = cellInfo.getContentElement();
092: Object value = dataModel.getElementAt(index);
093: boolean isSelected = list.getListSelectionModel()
094: .isSelectedIndex(index);
095: beforeShow(cellContentElem);
096: ItsNatListCellEditor cellEditor = getItsNatListCellEditor();
097: ItsNatComponent compEditor = cellEditor
098: .getListCellEditorComponent(list, index, value,
099: isSelected, cellContentElem);
100: afterShow(compEditor);
101: }
102: }
103:
104: public void acceptNewValue(Object value) {
105: ListModel dataModel = getItsNatFreeListMultSel().getListModel();
106: if (dataModel instanceof DefaultListModel) {
107: int index = cellInfo.getIndex();
108: ((DefaultListModel) dataModel).setElementAt(value, index); // Si se ha cambiado se renderiza el cambio
109: }
110: // En el caso de que el ListModel del usuario no fuera un DefaultListModel
111: // no sabemos como actualizar el modelo por lo que será el usuario
112: // el que tenga que hacer su propio CellEditorListener y recibir el evento en
113: // editingStopped
114: }
115:
116: public void clearCurrentContext() {
117: setCurrentContext(null);
118: }
119: }
|