Merge branch 'subtree'
[fanfix.git] / src / jexer / TWidget.java
index 5d2612b3092e961d82ad8b6beea94ce93a20d15d..d60efd8d3a321236e2069199c6db2fb364505d69 100644 (file)
@@ -36,6 +36,7 @@ import java.util.ArrayList;
 import jexer.backend.Screen;
 import jexer.bits.Cell;
 import jexer.bits.CellAttributes;
+import jexer.bits.Clipboard;
 import jexer.bits.ColorTheme;
 import jexer.event.TCommandEvent;
 import jexer.event.TInputEvent;
@@ -184,14 +185,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);
     }
 
     /**
@@ -598,9 +592,8 @@ public abstract class TWidget implements Comparable<TWidget> {
      * @param command command event
      */
     public void onCommand(final TCommandEvent command) {
-        // Default: do nothing, pass to children instead
-        for (TWidget widget: children) {
-            widget.onCommand(command);
+        if (activeChild != null) {
+            activeChild.onCommand(command);
         }
     }
 
@@ -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));
@@ -1132,6 +1126,18 @@ public abstract class TWidget implements Comparable<TWidget> {
         return null;
     }
 
+    /**
+     * Get the Clipboard.
+     *
+     * @return the Clipboard, or null if not assigned
+     */
+    public Clipboard getClipboard() {
+        if (window != null) {
+            return window.getApplication().getClipboard();
+        }
+        return null;
+    }
+
     /**
      * Comparison operator.  For various subclasses it sorts on:
      * <ul>
@@ -1144,7 +1150,8 @@ 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)
      */
-    public final int compareTo(final TWidget that) {
+    @Override
+    public int compareTo(final TWidget that) {
         if ((this instanceof TWindow)
             && (that instanceof TWindow)
         ) {
@@ -1243,6 +1250,25 @@ public abstract class TWidget implements Comparable<TWidget> {
         return window.getApplication().getTheme();
     }
 
+    /**
+     * See if this widget can be drawn onto a screen.
+     *
+     * @return true if this widget is part of the hierarchy that can draw to
+     * a screen
+     */
+    public final boolean isDrawable() {
+        if ((window == null)
+            || (window.getScreen() == null)
+            || (parent == null)
+        ) {
+            return false;
+        }
+        if (parent == this) {
+            return true;
+        }
+        return (parent.isDrawable());
+    }
+
     /**
      * Draw my specific widget.  When called, the screen rectangle I draw
      * into is already setup (offset and clipping).
@@ -1255,7 +1281,7 @@ public abstract class TWidget implements Comparable<TWidget> {
      * Called by parent to render to TWindow.  Note package private access.
      */
     final void drawChildren() {
-        if (window == null) {
+        if (!isDrawable()) {
             return;
         }
 
@@ -1310,6 +1336,12 @@ public abstract class TWidget implements Comparable<TWidget> {
 
         // Draw me
         draw();
+        if (!isDrawable()) {
+            // An action taken by a draw method unhooked me from the UI.
+            // Bail out.
+            return;
+        }
+
         assert (visible == true);
 
         // Continue down the chain.  Draw the active child last so that it
@@ -1317,6 +1349,11 @@ public abstract class TWidget implements Comparable<TWidget> {
         for (TWidget widget: children) {
             if (widget.isVisible() && (widget != activeChild)) {
                 widget.drawChildren();
+                if (!isDrawable()) {
+                    // An action taken by a draw method unhooked me from the UI.
+                    // Bail out.
+                    return;
+                }
             }
         }
         if (activeChild != null) {
@@ -1337,7 +1374,7 @@ public abstract class TWidget implements Comparable<TWidget> {
      *
      * @param child TWidget to add
      */
-    private void addChild(final TWidget child) {
+    public void addChild(final TWidget child) {
         children.add(child);
 
         if ((child.enabled)
@@ -1367,6 +1404,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.
@@ -1391,9 +1451,9 @@ public abstract class TWidget implements Comparable<TWidget> {
                 if (activeChild != null) {
                     activeChild.active = false;
                 }
-                child.active = true;
-                activeChild = child;
             }
+            child.active = true;
+            activeChild = child;
         }
     }
 
@@ -1445,6 +1505,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.
      *
@@ -1575,11 +1648,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);
@@ -1627,11 +1699,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);
@@ -2109,6 +2180,21 @@ public abstract class TWidget implements Comparable<TWidget> {
         return new TRadioGroup(this, x, y, label);
     }
 
+    /**
+     * Convenience function to add a radio button group to this
+     * container/window.
+     *
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param width width of group
+     * @param label label to display on the group box
+     */
+    public final TRadioGroup addRadioGroup(final int x, final int y,
+        final int width, final String label) {
+
+        return new TRadioGroup(this, x, y, width, label);
+    }
+
     /**
      * Convenience function to add a text field to this container/window.
      *