+ // ------------------------------------------------------------------------
+ // TTreeItem --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the parent TTreeView.
+ *
+ * @return the parent TTreeView
+ */
+ public final TTreeView getTreeView() {
+ return view;
+ }
+
+ /**
+ * Get the displayable text for this item.
+ *
+ * @return the displayable text for this item
+ */
+ public final String getText() {
+ return text;
+ }
+
+ /**
+ * Set the displayable text for this item.
+ *
+ * @param text the displayable text for this item
+ */
+ public final void setText(final String text) {
+ this.text = text;
+ }
+
+ /**
+ * Get expanded value.
+ *
+ * @return if true, this item is expanded
+ */
+ public final boolean isExpanded() {
+ return expanded;
+ }
+
+ /**
+ * Set expanded value.
+ *
+ * @param expanded new value
+ */
+ public final void setExpanded(final boolean expanded) {
+ if (level == 0) {
+ // Root node can't be unexpanded, ever.
+ this.expanded = true;
+ return;
+ }
+ if (level > 0) {
+ this.expanded = expanded;
+ }
+ }
+
+ /**
+ * Get expandable value.
+ *
+ * @return if true, this item is expandable
+ */
+ public final boolean isExpandable() {
+ return expandable;
+ }
+
+ /**
+ * Set expandable value.
+ *
+ * @param expandable new value
+ */
+ public final void setExpandable(final boolean expandable) {
+ if (level == 0) {
+ // Root node can't be unexpanded, ever.
+ this.expandable = true;
+ return;
+ }
+ if (level > 0) {
+ this.expandable = expandable;
+ }
+ }
+
+ /**
+ * Get the vertical bars and such along the left side.
+ *
+ * @return the vertical bars and such along the left side
+ */
+ public final String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * Get selected value.
+ *
+ * @return if true, this item is selected
+ */
+ public final boolean isSelected() {
+ return selected;
+ }
+
+ /**
+ * Set selected value.
+ *
+ * @param selected new value
+ */
+ public final void setSelected(final boolean selected) {
+ this.selected = selected;
+ }
+
+ /**
+ * Set selectable value.
+ *
+ * @param selectable new value
+ */
+ public final void setSelectable(final boolean selectable) {
+ this.selectable = selectable;
+ }
+
+ /**
+ * Get the length of the widest item to display.
+ *
+ * @return the maximum number of columns for this item or its children
+ */
+ public int getMaximumColumn() {
+ int max = prefix.length() + 4 + text.length();
+ for (TWidget widget: getChildren()) {
+ TTreeItem item = (TTreeItem) widget;
+ int n = item.prefix.length() + 4 + item.text.length();
+ if (n > max) {
+ max = n;
+ }
+ }
+ return max;
+ }
+
+ /**
+ * Recursively expand the tree into a linear array of items.
+ *
+ * @param prefix vertical bar of parent levels and such that is set on
+ * each child
+ * @param last if true, this is the "last" leaf node of a tree
+ * @return additional items to add to the array
+ */
+ public List<TTreeItem> expandTree(final String prefix, final boolean last) {
+ List<TTreeItem> array = new ArrayList<TTreeItem>();
+ this.last = last;
+ this.prefix = prefix;
+ array.add(this);
+
+ if ((getChildren().size() == 0) || !expanded) {
+ return array;
+ }
+
+ String newPrefix = prefix;
+ if (level > 0) {
+ if (last) {
+ newPrefix += " ";
+ } else {
+ newPrefix += GraphicsChars.CP437[0xB3];
+ newPrefix += ' ';
+ }
+ }
+ for (int i = 0; i < getChildren().size(); i++) {
+ TTreeItem item = (TTreeItem) getChildren().get(i);
+ if (i == getChildren().size() - 1) {
+ array.addAll(item.expandTree(newPrefix, true));
+ } else {
+ array.addAll(item.expandTree(newPrefix, false));
+ }
+ }
+ return array;
+ }
+
+ /**
+ * Get the x spot for the + or - to expand/collapse.
+ *
+ * @return column of the expand/collapse button
+ */
+ private int getExpanderX() {
+ if ((level == 0) || (!expandable)) {
+ return 0;
+ }
+ return prefix.length() + 3;
+ }
+
+ /**
+ * Recursively unselect me and my children.
+ */
+ public void unselect() {
+ if (selected == true) {
+ selected = false;
+ view.setSelected(null, false);
+ }
+ for (TWidget widget: getChildren()) {
+ if (widget instanceof TTreeItem) {
+ TTreeItem item = (TTreeItem) widget;
+ item.unselect();
+ }
+ }
+ }
+