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