X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fttree%2FTTreeItem.java;fp=src%2Fjexer%2FTTreeItem.java;h=901ce8590a78095bdf00a5831f86331b5b84d9e9;hb=d36057dfab8def933a64be042b039d76708ac5ba;hp=7de5e123587fe16aa06a62ab5737f27afa5d41ff;hpb=eb29bbb5ec70c43895dd0f053630c7e3cd402cba;p=fanfix.git diff --git a/src/jexer/TTreeItem.java b/src/jexer/ttree/TTreeItem.java similarity index 76% rename from src/jexer/TTreeItem.java rename to src/jexer/ttree/TTreeItem.java index 7de5e12..901ce85 100644 --- a/src/jexer/TTreeItem.java +++ b/src/jexer/ttree/TTreeItem.java @@ -26,11 +26,12 @@ * @author Kevin Lamonte [kevin.lamonte@gmail.com] * @version 1 */ -package jexer; +package jexer.ttree; import java.util.ArrayList; import java.util.List; +import jexer.TWidget; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; import jexer.event.TKeypressEvent; @@ -42,164 +43,55 @@ import static jexer.TKeypress.*; */ public class TTreeItem extends TWidget { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Hang onto reference to my parent TTreeView so I can call its reflow() * when I add a child node. */ private TTreeView view; - /** - * Get the parent TTreeView. - * - * @return the parent TTreeView - */ - public final TTreeView getTreeView() { - return view; - } - /** * Displayable text for this item. */ private String text; - /** - * 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; - } - /** * If true, this item is expanded in the tree view. */ private boolean expanded = true; - /** - * 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) { - this.expanded = expanded; - } - /** * If true, this item can be expanded in the tree view. */ private boolean expandable = false; - /** - * 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) { - this.expandable = expandable; - } - /** * The vertical bars and such along the left side. */ private String prefix = ""; /** - * Get the vertical bars and such along the left side. - * - * @return the vertical bars and such along the left side + * Tree level. */ - public final String getPrefix() { - return prefix; - } - - /** - * Whether or not this item is last in its parent's list of children. - */ - private boolean last = false; - - /** - * Tree level. Note package private access. - */ - int level = 0; - - /** - * If true, this item will not be drawn. - */ - private boolean invisible = false; - - /** - * Set invisible value. - * - * @param invisible new value - */ - public final void setInvisible(final boolean invisible) { - this.invisible = invisible; - } + protected int level = 0; /** * True means selected. */ private boolean selected = false; - /** - * 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; - } - /** * True means select-able. */ private boolean selectable = true; /** - * Set selectable value. - * - * @param selectable new value + * Whether or not this item is last in its parent's list of children. */ - public final void setSelectable(final boolean selectable) { - this.selectable = selectable; - } + private boolean last = false; /** * Pointer to the previous keyboard-navigable item (kbUp). Note package @@ -213,6 +105,10 @@ public class TTreeItem extends TWidget { */ TTreeItem keyboardNext = null; + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Public constructor. * @@ -224,107 +120,21 @@ public class TTreeItem extends TWidget { final boolean expanded) { super(view, 0, 0, view.getWidth() - 3, 1); + this.text = text; this.expanded = expanded; this.view = view; if (view.getTreeRoot() == null) { - view.setTreeRoot(this, true); - } - - view.reflowData(); - } - - /** - * Add a child item. - * - * @param text text for this item - * @return the new child item - */ - public TTreeItem addChild(final String text) { - return addChild(text, true); - } - - /** - * Add a child item. - * - * @param text text for this item - * @param expanded if true, have it expanded immediately - * @return the new child item - */ - public TTreeItem addChild(final String text, final boolean expanded) { - TTreeItem item = new TTreeItem(view, text, expanded); - item.level = this.level + 1; - getChildren().add(item); - view.reflowData(); - return item; - } - - /** - * 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 expandTree(final String prefix, final boolean last) { - List array = new ArrayList(); - 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; + view.setTreeRoot(this); + } else { + view.alignTree(); } - return prefix.length() + 3; } - /** - * Recursively unselect my or my children. - */ - public void unselect() { - if (selected == true) { - selected = false; - view.setSelected(null); - } - for (TWidget widget: getChildren()) { - if (widget instanceof TTreeItem) { - TTreeItem item = (TTreeItem) widget; - item.unselect(); - } - } - } + // ------------------------------------------------------------------------ + // Event handlers --------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Handle mouse release events. @@ -333,9 +143,13 @@ public class TTreeItem extends TWidget { */ @Override public void onMouseUp(final TMouseEvent mouse) { - if ((mouse.getX() == (getExpanderX() - view.getHorizontalValue())) + if ((mouse.getX() == (getExpanderX() - view.getLeftColumn())) && (mouse.getY() == 0) ) { + if (level == 0) { + // Root node can't switch. + return; + } if (selectable) { // Flip expanded flag expanded = !expanded; @@ -343,16 +157,18 @@ public class TTreeItem extends TWidget { // Unselect children that became invisible unselect(); } + view.setSelected(this, false); } // Let subclasses do something with this onExpand(); + + // Update the screen after any thing has expanded/contracted + view.alignTree(); } else if (mouse.getY() == 0) { - view.setSelected(this); + // Do the action associated with this item. + view.setSelected(this, false); view.dispatch(); } - - // Update the screen after any thing has expanded/contracted - view.reflowData(); } /** @@ -377,6 +193,10 @@ public class TTreeItem extends TWidget { || keypress.equals(kbRight) || keypress.equals(kbSpace) ) { + if (level == 0) { + // Root node can't switch. + return; + } if (selectable) { // Flip expanded flag expanded = !expanded; @@ -384,26 +204,33 @@ public class TTreeItem extends TWidget { // Unselect children that became invisible unselect(); } - view.setSelected(this); + view.setSelected(this, false); } // Let subclasses do something with this onExpand(); + } else if (keypress.equals(kbEnter)) { + // Do the action associated with this item. + view.dispatch(); } else { // Pass other keys (tab etc.) on to TWidget's handler. super.onKeypress(keypress); } } + // ------------------------------------------------------------------------ + // TWidget ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Draw this item to a window. */ @Override public void draw() { - if (invisible) { + if ((getY() < 0) || (getY() > getParent().getHeight() - 1)) { return; } - int offset = -view.getHorizontalValue(); + int offset = -view.getLeftColumn(); CellAttributes color = getTheme().getColor("ttreeview"); CellAttributes textColor = getTheme().getColor("ttreeview"); @@ -413,6 +240,7 @@ public class TTreeItem extends TWidget { if (!getParent().isAbsoluteActive()) { color = getTheme().getColor("ttreeview.inactive"); textColor = getTheme().getColor("ttreeview.inactive"); + selectedColor = getTheme().getColor("ttreeview.selected.inactive"); } if (!selectable) { @@ -432,6 +260,8 @@ public class TTreeItem extends TWidget { line += GraphicsChars.CP437[0xC4]; if (expandable) { line += "[ ] "; + } else { + line += " "; } } getScreen().putStringXY(offset, 0, line, color); @@ -452,4 +282,204 @@ public class TTreeItem extends TWidget { } } + // ------------------------------------------------------------------------ + // 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 expandTree(final String prefix, final boolean last) { + List array = new ArrayList(); + 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(); + } + } + } + }