Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[nikiroo-utils.git] / src / jexer / TWidget.java
index ba18989bfc43d38ea0549e42f5f20dfb8358e1ab..eb06175092d9a8dcd53c20a4d8e75b856d200967 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);
     }
 
     /**
@@ -935,8 +928,9 @@ public abstract class TWidget implements Comparable<TWidget> {
 
         this.x = x;
         this.y = y;
-        this.width = width;
-        this.height = height;
+        // Call the functions so that subclasses can choose how to handle it.
+        setWidth(width);
+        setHeight(height);
         if (layout != null) {
             layout.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
                     width, height));
@@ -1144,6 +1138,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)
@@ -1397,6 +1392,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.
@@ -1475,6 +1493,19 @@ public abstract class TWidget implements Comparable<TWidget> {
         }
     }
 
+    /**
+     * Make this widget, all of its parents, the active child.
+     */
+    public final void activateAll() {
+        activate();
+        if (parent == this) {
+            return;
+        }
+        if (parent != null) {
+            parent.activateAll();
+        }
+    }
+
     /**
      * Switch the active widget with the next in the tab order.
      *
@@ -1605,11 +1636,10 @@ public abstract class TWidget implements Comparable<TWidget> {
             splitPane.setLeft(this);
             splitPane.setRight(newWidget);
         }
-        splitPane.activate();
         if (newWidget != null) {
-            newWidget.activate();
+            newWidget.activateAll();
         } else {
-            activate();
+            activateAll();
         }
 
         assert (parent != null);
@@ -1657,11 +1687,10 @@ public abstract class TWidget implements Comparable<TWidget> {
             splitPane.setTop(this);
             splitPane.setBottom(newWidget);
         }
-        splitPane.activate();
         if (newWidget != null) {
-            newWidget.activate();
+            newWidget.activateAll();
         } else {
-            activate();
+            activateAll();
         }
 
         assert (parent != null);