001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * TreeNode.java
028: *
029: * Created on 28 novembre 2005, 12.12
030: *
031: */
032:
033: package it.businesslogic.ireport.util;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: /**
041: *
042: * @author Administrator
043: */
044: public class TreeNode {
045:
046: private List childs = new ArrayList();
047: private Object userObject = null;
048: private TreeNode parent = null;
049:
050: /** Creates a new instance of TreeNode */
051: public TreeNode(Object aUserObject) {
052: setUserObject(aUserObject);
053: }
054:
055: public String getName() {
056: return "" + getUserObject();
057: }
058:
059: public List getChilds() {
060: return childs;
061: }
062:
063: public boolean isLeaf() {
064: return getChilds().isEmpty();
065: }
066:
067: public Object getUserObject() {
068: return userObject;
069: }
070:
071: public void setUserObject(Object userObject) {
072: this .userObject = userObject;
073: }
074:
075: public void addChild(TreeNode node) {
076: node.setParent(this );
077: getChilds().add(node);
078: }
079:
080: public void addChilds(Collection nodes) {
081: Iterator myIterator = nodes.iterator();
082: while (myIterator.hasNext()) {
083: addChild((TreeNode) (myIterator.next()));
084: }
085: }
086:
087: public boolean moveUp(Object userObject) {
088: if (((TreeNode) (getChilds().get(0))).getUserObject() == userObject)
089: return true;
090: else {
091: for (int i = 1; i < getChilds().size(); ++i) {
092: TreeNode node = (TreeNode) (getChilds().get(i));
093: if (node.getUserObject() == userObject) {
094: int oldPosition = i;
095: getChilds().remove(i);
096: getChilds().add(i - 1, node);
097: return true;
098: }
099: }
100: }
101:
102: // Search for the node in the
103: for (int i = 0; i < getChilds().size(); ++i) {
104: TreeNode parent = (TreeNode) getChilds().get(i);
105: if (parent.isLeaf())
106: continue;
107: else {
108: if (parent.moveUp(userObject))
109: return true;
110: }
111: }
112:
113: return false;
114: }
115:
116: public boolean moveDown(Object userObject) {
117: if (((TreeNode) (getChilds().get(getChilds().size() - 1)))
118: .getUserObject() == userObject)
119: return true;
120: else {
121: for (int i = 0; i < getChilds().size() - 1; ++i) {
122: TreeNode node = (TreeNode) (getChilds().get(i));
123: if (node.getUserObject() == userObject) {
124: int oldPosition = i;
125: getChilds().remove(i);
126: getChilds().add(i + 1, node);
127: return true;
128: }
129: }
130: }
131:
132: // Search for the node in the
133: for (int i = 0; i < getChilds().size(); ++i) {
134: TreeNode parent = (TreeNode) getChilds().get(i);
135: if (parent.isLeaf())
136: continue;
137: else {
138: if (parent.moveDown(userObject))
139: return true;
140: }
141: }
142:
143: return false;
144: }
145:
146: public TreeNode getParent() {
147: return parent;
148: }
149:
150: public void setParent(TreeNode parent) {
151: this.parent = parent;
152: }
153:
154: }
|