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.ItsNatTreeCellEditor;
017: import org.itsnat.comp.ui.ItsNatTreeCellUI;
018: import org.itsnat.impl.core.domutil.ItsNatDOMUtilInternal;
019: import javax.swing.event.CellEditorListener;
020: import javax.swing.tree.TreeModel;
021: import javax.swing.tree.TreePath;
022: import org.itsnat.comp.ItsNatComponent;
023: import org.itsnat.comp.ui.ItsNatTreeUI;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.events.Event;
027:
028: /**
029: *
030: * @author jmarranz
031: */
032: public class TreeCellEditorProcessor extends EditorProcessorBase
033: implements CellEditorListener {
034: protected ItsNatTreeCellUI cellInfo;
035: protected TreePath path;
036: protected int row;
037:
038: public TreeCellEditorProcessor(ItsNatTreeImpl compParent) {
039: super (compParent);
040: }
041:
042: public ItsNatTreeImpl getItsNatTree() {
043: return (ItsNatTreeImpl) compParent;
044: }
045:
046: public ItsNatTreeCellEditor getItsNatTreeCellEditor() {
047: return (ItsNatTreeCellEditor) cellEditor;
048: }
049:
050: public void setItsNatTreeCellEditor(ItsNatTreeCellEditor cellEditor) {
051: setCellEditor(cellEditor);
052: }
053:
054: public void setCurrentContext(ItsNatTreeCellUI cellInfo,
055: TreePath path, int row) {
056: this .cellInfo = cellInfo;
057: this .path = path;
058: this .row = row;
059: }
060:
061: public TreePath getTreePath() {
062: return path; // El actualmente editado
063: }
064:
065: public int getRow() {
066: return row; // El actualmente editado
067: }
068:
069: public void startEdition(TreePath path) {
070: if (prepareEdition()) {
071: openEditor(path);
072: }
073: }
074:
075: public void startEdition(int row) {
076: if (prepareEdition()) {
077: openEditor(row);
078: }
079: }
080:
081: private void openEditor(TreePath path) {
082: ItsNatTreeUI compUI = getItsNatTree().getItsNatTreeUI();
083: ItsNatTreeCellUI cellInfo = (ItsNatTreeCellUI) compUI
084: .getItsNatTreeCellUIFromTreePath(path);
085: openEditor(path, cellInfo.getRow(), cellInfo);
086: }
087:
088: private void openEditor(int row) {
089: ItsNatTreeUI compUI = getItsNatTree().getItsNatTreeUI();
090: ItsNatTreeCellUI cellInfo = (ItsNatTreeCellUI) compUI
091: .getItsNatTreeCellUIFromRow(row);
092: openEditor(cellInfo.getTreePath(), row, cellInfo);
093: }
094:
095: private void openEditor(ItsNatTreeCellUI cellInfo) {
096: openEditor(cellInfo.getTreePath(), cellInfo.getRow(), cellInfo);
097: }
098:
099: private void openEditor(TreePath path, int row,
100: ItsNatTreeCellUI cellInfo) {
101: if (row >= 0) {
102: setCurrentContext(cellInfo, path, row);
103:
104: ItsNatTreeImpl tree = getItsNatTree();
105: TreeModel dataModel = tree.getTreeModel();
106: Element labelElem = cellInfo.getLabelElement();
107: TreeSelectionModelMgrImpl selModelMgr = tree
108: .getTreeSelectionModelMgr();
109: boolean isSelected = selModelMgr.isRowSelected(row);
110: Object dataNode = path.getLastPathComponent();
111: boolean isLeaf = dataModel.isLeaf(dataNode);
112: boolean expanded = cellInfo.isExpanded();
113: beforeShow(labelElem);
114: ItsNatTreeCellEditor cellEditor = getItsNatTreeCellEditor();
115: ItsNatComponent compEditor = cellEditor
116: .getTreeCellEditorComponent(tree, row, dataNode,
117: isSelected, expanded, isLeaf, labelElem);
118: afterShow(compEditor);
119: }
120: }
121:
122: protected void openEditor(Event evt) {
123: Node nodeClicked = (Node) evt.getTarget(); // Puede ser un nodo interior del elemento pulsado
124:
125: ItsNatTreeUI compUI = getItsNatTree().getItsNatTreeUI();
126: ItsNatTreeCellUI cellInfo = (ItsNatTreeCellUI) compUI
127: .getItsNatTreeCellUIFromNode(nodeClicked);
128: if (cellInfo != null) // Se ha pulsado un elemento verdaderamente
129: {
130: if (ItsNatDOMUtilInternal.isChildOrSame(nodeClicked,
131: cellInfo.getLabelElement(), cellInfo
132: .getParentElement())) {
133: // Se ha pulsado el nodo contenido (el handle y el icon no valen para editar)
134: openEditor(cellInfo);
135: }
136: }
137: }
138:
139: public void acceptNewValue(Object value) {
140: TreeModel dataModel = getItsNatTree().getTreeModel();
141: dataModel.valueForPathChanged(path, value); // Oportunidad del modelo de actualizarse, ver por ejemplo en DefaultTreeModel
142: }
143:
144: public void clearCurrentContext() {
145: setCurrentContext(null, null, -1);
146: }
147: }
|