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