Allow parentless TWidget (req. for TComboBox fixes)
authorNiki Roo <niki@nikiroo.be>
Tue, 24 Sep 2019 07:18:50 +0000 (09:18 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 24 Sep 2019 07:18:50 +0000 (09:18 +0200)
src/jexer/TWidget.java

index e94fed25077c68ca763b8dc90e76b98d36898469..32b55983613a67914aec832d2d0ac7c6ab323287 100644 (file)
@@ -184,14 +184,7 @@ public abstract class TWidget implements Comparable<TWidget> {
      * @param enabled if true assume enabled
      */
     protected TWidget(final TWidget parent, final boolean enabled) {
-        this.enabled = enabled;
-        this.parent = parent;
-        children = new ArrayList<TWidget>();
-
-        if (parent != null) {
-            this.window = parent.window;
-            parent.addChild(this);
-        }
+        this(parent, enabled, 0, 0, 0, 0);
     }
 
     /**
@@ -215,9 +208,20 @@ public abstract class TWidget implements Comparable<TWidget> {
         }
 
         this.enabled = enabled;
-        this.parent = parent;
         children = new ArrayList<TWidget>();
 
+        // Allow parentless widgets
+       if (parent != null) {
+               // Do not add TStatusBars, they are drawn by TApplication.
+               if (this instanceof TStatusBar) {
+                   // We don't add the child to the children list here
+                   this.parent = parent;
+                   this.window = parent.window;
+               } else {
+                       parent.addChild(this);
+               }
+       }
+
         this.x = x;
         this.y = y;
         this.width = width;
@@ -1145,6 +1149,7 @@ public abstract class TWidget implements Comparable<TWidget> {
      * @return difference between this.tabOrder and that.tabOrder, or
      * difference between this.z and that.z, or String.compareTo(text)
      */
+    @Override
     public final int compareTo(final TWidget that) {
         if ((this instanceof TWindow)
             && (that instanceof TWindow)
@@ -1370,6 +1375,8 @@ public abstract class TWidget implements Comparable<TWidget> {
      */
     private void addChild(final TWidget child) {
         children.add(child);
+        child.parent = this;
+        child.window = this.window;
 
         if ((child.enabled)
             && !(child instanceof THScroller)
@@ -1398,6 +1405,29 @@ public abstract class TWidget implements Comparable<TWidget> {
             children.get(i).tabOrder = i;
         }
     }
+    
+    /**
+     * Remove and {@link TWidget#close()} the given child from this {@link TWidget}.
+     * <p>
+     * Will also reorder the tab values of the remaining children.
+     * 
+     * @param child the child to remove
+     * 
+     * @return TRUE if the child was removed, FALSE if it was not found
+     */
+    public boolean removeChild(final TWidget child) {
+       if (children.remove(child)) {
+               child.close();
+               child.parent = null;
+               child.window = null;
+               
+               resetTabOrder();
+               
+               return true;
+       }
+       
+       return false;
+    }
 
     /**
      * Switch the active child.