001: /*
002: * Stingray Software Objective Blend
003: * Copyright (C) 1996 Stingray Software, Inc.
004: * All Rights Reserved
005: *
006: * This source code is only intended as a supplement to
007: * the Stingray Objective Blend product. See the Objective
008: * Blend html help documentation for detailed information regarding
009: * using OB classes.
010: *
011: * Author : LingFeng Wang
012: * Description : TreeItem.java - Tree item
013: *
014: * CHANGELOG:
015: * 7/09/96 LFW Created
016: */
017:
018: /**
019: TreeItem class
020:
021: This class is provided for partial backwards compatibility with the
022: version 1.1 TreeItem control. Much of the logic contained in the old
023: version of the TreeItem class has been eliminated due to the new architecture
024: of the TreeBase control; however, those developers who wish to have some
025: of the same naming/constructions methods in place can still use this
026: control as a subclass of the new Node class.
027:
028: @see ob.tree.Node
029: @see ob.tree.TreeBase
030: */package ob.tree;
031:
032: import java.awt.*;
033: import com.sun.portal.log.common.PortalLogger;
034: import java.util.Vector;
035: import ob.tree.Node;
036:
037: public class TreeItem extends Node {
038:
039: /**
040: * indicates an EXPANDed state for the tree item
041: */
042: public static final int EXPAND = 1;
043: /**
044: * indicates an UNEXPANDed state for the tree item
045: */
046: public static final int UNEXPAND = 2;
047:
048: /**
049: * parent of the tree item
050: */
051: boolean Visible = true;
052:
053: int Image = -1;
054: int SelImage = -1;
055: int Status = EXPAND;
056: int nChild = 0;
057: int Indent = 0;
058: Rectangle BaseBound;
059: Object Data = null;
060:
061: /**
062: * constructor accepts a label for the item
063: * @param label the label for the tree item
064: */
065: public TreeItem(String label) {
066: super (label, -1, -1);
067: }
068:
069: /**
070: * adds a TreeItem to be a child of this TreeItem.
071: * @param item the new item to add as a child
072: */
073: public void addItem(TreeItem child) {
074: super .addChild(child);
075: }
076:
077: /**
078: * gets the label for the tree item
079: * @return the label as type String
080: * @see TreeItem#setLabel
081: */
082: public String getLabel() {
083: return super .getText();
084: }
085:
086: /**
087: * sets the label for the tree item
088: * @param s label as type String
089: * @see TreeItem#getLabel
090: */
091: public void setLabel(String s) {
092: super .setText(s);
093: }
094:
095: /**
096: * retrieves the data associated with this item
097: * @return data as type Object
098: * @see TreeItem#setData
099: */
100: public Object getData() {
101: return Data;
102: }
103:
104: /**
105: * sets the data associated with this item
106: * @param data new data for this item as type Object
107: * @see TreeItem#getData
108: */
109: public void setData(Object data) {
110: this .Data = data;
111: }
112:
113: /**
114: * gets the expanded status of the tree item.
115: * @return expanded status as one of following int values: EXPAND or UNEXPAND
116: * @see TreeItem#setStatus
117: */
118: public int getStatus() {
119:
120: return Status;
121: }
122:
123: /**
124: * sets the expanded status of the tree item
125: * @param s can be EXPAND or UNEXPAND
126: * @see getStatus
127: */
128: public void setStatus(int s) {
129: this .Status = s;
130: if (s == EXPAND) {
131: super .expand(true);
132: }
133: }
134:
135: /**
136: * returns the position of the tree item relative to the parent.
137: * @return position of tree item. If no parent, returns a -1.
138: * @see TreeItem#getNextItem
139: */
140: public int getIndex() {
141: int nCount = 1;
142: TreeItem sib = (TreeItem) getFirstSibling();
143: while (sib != this ) {
144: sib = (TreeItem) sib.getNextSibling();
145: nCount++;
146: }
147: return nCount;
148: }
149:
150: /**
151: * retrieves the next sequential tree item on the same level with the
152: * same parent.
153: * @return the next tree item on this level with the same parent as type TreeItem.
154: * if none found, then return null.
155: * @see TreeItem#getIndex
156: * @see TreeItem#getPrevItem
157: */
158: public TreeItem getNextItem() {
159: return ((TreeItem) getNextSibling());
160: }
161:
162: /**
163: * retrieves the previous sequential tree item on the same level with the
164: * same parent.
165: * @return the previous tree item on this level with the same parent; if none
166: * found return null.
167: * @see TreeItem#getIndex
168: * @see TreeItem#getNextItem
169: */
170:
171: public TreeItem getPrevItem() {
172: return ((TreeItem) getPrevSibling());
173: }
174:
175: /**
176: * determines whether tree item has child items
177: * @return true if children exist, otherwise false
178: * @see TreeItem#getCount
179: * @see TreeItem#getChild
180: */
181: public boolean hasChild() {
182: return super .hasChildren();
183: }
184:
185: /**
186: * returns the number of children the tree item has.
187: * @return the number of children as type int
188: * @see TreeItem#hasChild
189: * @see TreeItem#getChild
190: */
191: public int getCount() {
192: int nCount = 0;
193: //System.out.println("In getCount function....");
194: TreeItem ti = (TreeItem) getFirstChild();
195: try {
196: //System.out.println("First Child is : "+ ti.getText());
197: } catch (NullPointerException n) {
198: }
199: while (ti != null) {
200: //System.out.println("In While Loop");
201: try {
202: ti = (TreeItem) ti.getNextSibling();
203: //System.out.println("Next Child is : " + ti.getText());
204: nCount++;
205: } catch (NullPointerException n) {
206: }
207: }
208:
209: //System.out.println("Total count is ......: " + nCount);
210: //System.out.println("Out of getcount Function");
211: return nCount;
212: }
213:
214: /**
215: * retrieves the bounding rectangle for the item
216: * @return bounding rectangle as type Rectangle
217: */
218: public Rectangle bounds() {
219: return BaseBound;
220: }
221:
222: /**
223: * retrieves a child of the tree item
224: * @param index which child to retrieve
225: * @return the child as type TreeItem
226: * @see TreeItem#hasChild
227: * @see TreeItem#getCount
228: */
229: public TreeItem getChild(int index) {
230: int nCount = 0;
231: TreeItem ti = null;
232:
233: ti = (TreeItem) getFirstChild();
234:
235: while (ti != null && index < nCount) {
236:
237: if (ti != null)
238: ti = (TreeItem) ti.getNextSibling();
239: nCount++;
240: }
241: return ti;
242: }
243:
244: /**
245: * determines whether item is the parent of a this item
246: * @param item the item to check if it is a child of this item
247: * @return whether the item is a child as type boolean
248: */
249: public boolean isParentOf(TreeItem item) {
250:
251: if (item == null)
252: return false;
253: return super .isParent(item);
254: }
255:
256: /**
257: * determines whether this item is the child of one of the items
258: * @param items the items to check if it is a parent of this item
259: * @return whether this item is a child as type boolean
260: */
261: public boolean isChildOf(TreeItem[] items) {
262:
263: for (int i = 0; i < items.length; i++) {
264: if (items[i].isParentOf(this ))
265: return true;
266: }
267:
268: return false;
269: }
270:
271: /**
272: * removes a child item from the tree item
273: * @param item item to remove
274: */
275: public void remove(TreeItem item) {
276: item.detachFromTree();
277: }
278:
279: // VICKY: added methods
280:
281: // we apparently used to have a removeAll() function
282: public void removeAll() {
283: System.out.println("in removeAll....");
284: if (getSubItems().isEmpty()) {
285:
286: System.out.println("Vector is empty");
287: } else {
288: System.out.println("Vector has elements ");
289: System.out
290: .println("Vector size is " + getSubItems().size());
291: getSubItems().removeAllElements();
292: }
293: }
294:
295: /* recursive search through the tree control to find
296: the treeitem that matches my data object
297: */
298: public TreeItem findObject(Object o) {
299: if (Data == o) {
300: // System.out.println("Before Bina... Tree item is : " + this);
301: return this ;
302: } else {
303: int i = 0;
304: int count = this .getCount();
305: // System.out.println("Bina.. Count is ......: " + count);
306: TreeItem ti = null;
307: while (ti == null && i < count) {
308: try {
309: ti = ((TreeItem) getItemChild(i)).findObject(o);
310: // System.out.println("Bina... Tree item is : " + ti);
311: i++;
312: } catch (NullPointerException n) {
313: }
314: }
315: // System.out.println("Before returnBina... Tree item is : " + ti);
316: return ti;
317: }
318: }
319:
320: /* recursive run through the tree control to print
321: all the treeitems
322: */
323: public void printTree() {
324: int i = 0;
325:
326: // print this TreeItem
327: //System.out.println(this.Label + "\n");
328:
329: while (i < getCount()) {
330: getChild(i).printTree();
331: i++;
332: }
333:
334: return;
335: }
336:
337: ////BINA
338: /// added becuase getChild does not work ok with findObject
339: ////
340: public TreeItem getItemChild(int index) {
341: int nChild = getSubItems().size();
342: //System.out.println("In getItemCHild.... nChild is : " + nChild);
343: //System.out.println("In getItemCHild.... Index passed is : " + index);
344: if (index >= 0 && index < nChild) {
345: //System.out.println("Element at : " + index + " is " + getSubItems().elementAt(index));
346: return (TreeItem) getSubItems().elementAt(index);
347: } else {
348: return null;
349: }
350: }
351:
352: /////BINA
353:
354: /* delete a child from the tree */
355: public synchronized void deleteChild(TreeItem item) {
356:
357: int i = 0;
358: //System.out.println("IN deleteChild ....");
359: TreeItem firstChild = (TreeItem) item.getFirstChild();
360: while (firstChild != null) {
361: //System.out.println("Child is ........"+firstChild.getText());
362: item.remove(firstChild);
363: firstChild = (TreeItem) item.getFirstChild();
364: }
365: }
366: // BINA
367:
368: //VICKY: end added methods
369:
370: }
|