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.trees;
017:
018: import javax.swing.event.TreeExpansionEvent;
019: import javax.swing.event.TreeModelEvent;
020: import javax.swing.event.TreeModelListener;
021: import javax.swing.event.TreeSelectionEvent;
022: import javax.swing.event.TreeSelectionListener;
023: import javax.swing.event.TreeWillExpandListener;
024: import javax.swing.tree.DefaultMutableTreeNode;
025: import javax.swing.tree.DefaultTreeModel;
026: import javax.swing.tree.ExpandVetoException;
027: import javax.swing.tree.MutableTreeNode;
028: import javax.swing.tree.TreePath;
029: import javax.swing.tree.TreeSelectionModel;
030: import org.itsnat.comp.ItsNatComponentManager;
031: import org.itsnat.comp.free.ItsNatFreeTree;
032: import org.itsnat.comp.html.ItsNatHTMLInputButton;
033: import org.itsnat.comp.html.ItsNatHTMLInputText;
034: import org.itsnat.core.ItsNatDocument;
035: import org.itsnat.feashow.FeatureTreeNode;
036: import org.itsnat.feashow.FreeTreeDecorator;
037: import org.w3c.dom.events.Event;
038: import org.w3c.dom.events.EventListener;
039: import org.w3c.dom.events.EventTarget;
040:
041: public class FreeTreeTreeNode extends FeatureTreeNode implements
042: EventListener, TreeModelListener, TreeSelectionListener,
043: TreeWillExpandListener {
044: protected ItsNatFreeTree treeComp;
045: protected ItsNatHTMLInputButton removeButton;
046: protected ItsNatHTMLInputText itemComp;
047: protected ItsNatHTMLInputText posComp;
048: protected ItsNatHTMLInputButton updateButton;
049: protected ItsNatHTMLInputButton insertBeforeButton;
050: protected ItsNatHTMLInputButton insertAfterButton;
051: protected ItsNatHTMLInputButton insertChildButton;
052:
053: public FreeTreeTreeNode() {
054: }
055:
056: public void startExamplePanel() {
057: ItsNatDocument itsNatDoc = getItsNatDocument();
058: ItsNatComponentManager componentMgr = itsNatDoc
059: .getItsNatComponentManager();
060:
061: this .treeComp = (ItsNatFreeTree) componentMgr
062: .createItsNatComponentById("compId", "freeTree", null);
063:
064: DefaultTreeModel dataModel = (DefaultTreeModel) treeComp
065: .getTreeModel();
066: treeComp.setTreeModel(dataModel);
067:
068: new FreeTreeDecorator(treeComp).bind();
069:
070: DefaultMutableTreeNode parentNode;
071:
072: DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
073: "Grey's Anatomy");
074: dataModel.setRoot(rootNode);
075:
076: parentNode = addNode("Characters", rootNode, dataModel);
077:
078: addNode("Meredith Grey", parentNode, dataModel);
079: addNode("Cristina Yang", parentNode, dataModel);
080: addNode("Alex Karev", parentNode, dataModel);
081: addNode("George O'Malley", parentNode, dataModel);
082:
083: parentNode = addNode("Actors", rootNode, dataModel);
084:
085: addNode("Ellen Pompeo", parentNode, dataModel);
086: addNode("Sandra Oh", parentNode, dataModel);
087: addNode("Justin Chambers", parentNode, dataModel);
088: addNode("T.R. Knight", parentNode, dataModel);
089:
090: TreeSelectionModel selModel = treeComp.getTreeSelectionModel();
091: selModel
092: .setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
093:
094: selModel.addSelectionPath(new TreePath(parentNode.getPath())); // Actors
095:
096: treeComp.addEventListener("click", this );
097: dataModel.addTreeModelListener(this );
098: selModel.addTreeSelectionListener(this );
099:
100: treeComp.addTreeWillExpandListener(this );
101:
102: this .removeButton = (ItsNatHTMLInputButton) componentMgr
103: .createItsNatComponentById("removeId");
104: removeButton.addEventListener("click", this );
105:
106: this .itemComp = (ItsNatHTMLInputText) componentMgr
107: .createItsNatComponentById("itemId");
108: itemComp.setText(selModel.getSelectionPath()
109: .getLastPathComponent().toString());
110:
111: this .posComp = (ItsNatHTMLInputText) componentMgr
112: .createItsNatComponentById("posId");
113: posComp
114: .setText(Integer
115: .toString(selModel.getMinSelectionRow()));
116:
117: this .updateButton = (ItsNatHTMLInputButton) componentMgr
118: .createItsNatComponentById("updateId");
119: updateButton.addEventListener("click", this );
120:
121: this .insertBeforeButton = (ItsNatHTMLInputButton) componentMgr
122: .createItsNatComponentById("insertBeforeId");
123: insertBeforeButton.addEventListener("click", this );
124:
125: this .insertAfterButton = (ItsNatHTMLInputButton) componentMgr
126: .createItsNatComponentById("insertAfterId");
127: insertAfterButton.addEventListener("click", this );
128:
129: this .insertChildButton = (ItsNatHTMLInputButton) componentMgr
130: .createItsNatComponentById("insertChildId");
131: insertChildButton.addEventListener("click", this );
132: }
133:
134: public static DefaultMutableTreeNode addNode(Object userObject,
135: DefaultMutableTreeNode parentNode,
136: DefaultTreeModel dataModel) {
137: DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
138: userObject);
139: int count = dataModel.getChildCount(parentNode);
140: dataModel.insertNodeInto(childNode, parentNode, count);
141: return childNode;
142: }
143:
144: public void endExamplePanel() {
145: this .treeComp.dispose();
146: this .treeComp = null;
147:
148: this .removeButton.dispose();
149: this .removeButton = null;
150:
151: this .itemComp.dispose();
152: this .itemComp = null;
153:
154: this .posComp.dispose();
155: this .posComp = null;
156:
157: this .updateButton.dispose();
158: this .updateButton = null;
159:
160: this .insertBeforeButton.dispose();
161: this .insertBeforeButton = null;
162:
163: this .insertAfterButton.dispose();
164: this .insertAfterButton = null;
165:
166: this .insertChildButton.dispose();
167: this .insertChildButton = null;
168: }
169:
170: public void handleEvent(Event evt) {
171: log(evt.getCurrentTarget() + " " + evt.getType());
172:
173: EventTarget currentTarget = evt.getCurrentTarget();
174: if (currentTarget == removeButton.getHTMLInputElement()) {
175: DefaultTreeModel dataModel = (DefaultTreeModel) treeComp
176: .getTreeModel();
177: TreeSelectionModel selModel = treeComp
178: .getTreeSelectionModel();
179: if (!selModel.isSelectionEmpty()) {
180: // Selection Model is in CONTIGUOUS_TREE_SELECTION mode
181: TreePath[] paths = selModel.getSelectionPaths();
182: for (int i = 0; i < paths.length; i++) {
183: MutableTreeNode node = (MutableTreeNode) paths[i]
184: .getLastPathComponent();
185: if (node == dataModel.getRoot())
186: dataModel.setRoot(null);
187: else if (node.getParent() != null) // If null is already removed (a previous removed node was the parent)
188: dataModel.removeNodeFromParent(node);
189: }
190: }
191: } else if ((currentTarget == updateButton.getHTMLInputElement())
192: || (currentTarget == insertBeforeButton
193: .getHTMLInputElement())
194: || (currentTarget == insertAfterButton
195: .getHTMLInputElement())
196: || (currentTarget == insertChildButton
197: .getHTMLInputElement())) {
198: String newItem = itemComp.getText();
199: int row;
200: try {
201: row = Integer.parseInt(posComp.getText());
202: DefaultTreeModel dataModel = (DefaultTreeModel) treeComp
203: .getTreeModel();
204: if (currentTarget == updateButton.getHTMLInputElement()) {
205: TreePath path = treeComp.getTreePathForRow(row);
206: if (path != null) {
207: DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
208: .getLastPathComponent();
209: node.setUserObject(newItem);
210: dataModel.nodeChanged(node);
211: } else
212: getItsNatDocument().addCodeToSend(
213: "alert('Bad Position')");
214: } else if ((currentTarget == insertBeforeButton
215: .getHTMLInputElement())
216: || (currentTarget == insertAfterButton
217: .getHTMLInputElement())
218: || (currentTarget == insertChildButton
219: .getHTMLInputElement())) {
220: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
221: newItem);
222: TreePath refPath = treeComp.getTreePathForRow(row);
223: if (refPath != null) {
224: DefaultMutableTreeNode refNode = (DefaultMutableTreeNode) refPath
225: .getLastPathComponent();
226: if ((currentTarget == insertBeforeButton
227: .getHTMLInputElement())
228: || (currentTarget == insertAfterButton
229: .getHTMLInputElement())) {
230: if (row == 0) {
231: getItsNatDocument().addCodeToSend(
232: "alert('Bad Position')");
233: } else {
234: TreePath parentPath = refPath
235: .getParentPath();
236: DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parentPath
237: .getLastPathComponent();
238: int childIndex = dataModel
239: .getIndexOfChild(parentNode,
240: refNode);
241: if (currentTarget == insertAfterButton
242: .getHTMLInputElement())
243: childIndex = childIndex + 1;
244: dataModel.insertNodeInto(newNode,
245: parentNode, childIndex);
246: }
247: } else // insert child
248: {
249: // refNode is the parent node
250: int childIndex = dataModel
251: .getChildCount(refNode);
252: dataModel.insertNodeInto(newNode, refNode,
253: childIndex);
254: }
255: } else {
256: if ((row == 0) && (dataModel.getRoot() == null))
257: dataModel.setRoot(newNode);
258: else
259: getItsNatDocument().addCodeToSend(
260: "alert('Bad Position')");
261: }
262: }
263:
264: } catch (NumberFormatException ex) {
265: getItsNatDocument().addCodeToSend(
266: "alert('Bad Position')");
267: } catch (ArrayIndexOutOfBoundsException ex) {
268: getItsNatDocument().addCodeToSend(
269: "alert('Bad Position')");
270: }
271: }
272: }
273:
274: public void treeNodesChanged(TreeModelEvent e) {
275: treeChangedLog(e);
276: }
277:
278: public void treeNodesInserted(TreeModelEvent e) {
279: treeChangedLog(e);
280: }
281:
282: public void treeNodesRemoved(TreeModelEvent e) {
283: treeChangedLog(e);
284: }
285:
286: public void treeStructureChanged(TreeModelEvent e) {
287: treeChangedLog(e);
288: }
289:
290: public void treeChangedLog(TreeModelEvent e) {
291: log(e.toString());
292: }
293:
294: public void valueChanged(TreeSelectionEvent e) {
295: TreeSelectionModel selModel = (TreeSelectionModel) e
296: .getSource();
297:
298: TreePath[] paths = e.getPaths();
299: String fact = "";
300: for (int i = 0; i < paths.length; i++) {
301: TreePath path = paths[i];
302: boolean selected = selModel.isPathSelected(path);
303: if (selected)
304: fact += ", selected ";
305: else
306: fact += ", deselected ";
307: fact += path.getLastPathComponent();
308: }
309:
310: log(e.getClass().toString() + " " + fact);
311:
312: int row = selModel.getMinSelectionRow(); // First selected
313: if (row != -1) {
314: TreePath path = treeComp.getTreePathForRow(row);
315: Object value = path.getLastPathComponent();
316: itemComp.setText(value.toString());
317: posComp.setText(Integer.toString(row));
318: }
319: }
320:
321: public void treeWillExpand(TreeExpansionEvent event)
322: throws ExpandVetoException {
323: // Will expand
324: }
325:
326: public void treeWillCollapse(TreeExpansionEvent event)
327: throws ExpandVetoException {
328: // Will collapse
329: }
330: }
|