[libbluray-devel] Implement HToggleGroup

Ian Curtis git at videolan.org
Tue May 6 11:27:48 CEST 2014


libbluray | branch: master | Ian Curtis <i.curtis at gmail.com> | Tue May  6 12:23:03 2014 +0300| [2cd2cb6bffc7e0538514e41e6c1c269635bf4b47] | committer: hpi1

Implement HToggleGroup

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=2cd2cb6bffc7e0538514e41e6c1c269635bf4b47
---

 .../bdj/java/org/havi/ui/HToggleGroup.java         |  118 +++++++++++++++++---
 1 file changed, 105 insertions(+), 13 deletions(-)

diff --git a/src/libbluray/bdj/java/org/havi/ui/HToggleGroup.java b/src/libbluray/bdj/java/org/havi/ui/HToggleGroup.java
index d59bda0..8bd4366 100644
--- a/src/libbluray/bdj/java/org/havi/ui/HToggleGroup.java
+++ b/src/libbluray/bdj/java/org/havi/ui/HToggleGroup.java
@@ -19,55 +19,147 @@
 
 package org.havi.ui;
 
+import java.util.Enumeration;
+import java.util.Vector;
+
 public class HToggleGroup {
-    private HToggleButton current = null;
 
     public HToggleGroup()
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "");
     }
 
     public HToggleButton getCurrent()
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "getCurrent");
         return current;
     }
 
     public void setCurrent(HToggleButton selection)
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "setCurrent");
-        current = selection;
+        // Treat null as unselection
+        if (selection == null) {
+            HToggleButton tmp = getCurrent();
+            if (tmp != null && getForcedSelection() && buttons.size() > 0) {
+                // Enforce forced selection (reselect button!)
+                tmp.setSwitchableState(true);
+            } else {
+                // General behavior
+                current = null;
+                if (tmp != null) tmp.setSwitchableState(false);
+            }
+        }
+        // Set current only if part of this group
+        // And isn't the current selection (stops infinite loop)
+        else if (buttons.contains(selection) && getCurrent() != selection) {
+            current = selection;
+            selection.setSwitchableState(true);
+            unswitch(selection); // Enforce single selection
+        }
     }
 
     public void setForcedSelection(boolean forceSelection)
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "setForcedSelection");
+        this.forceSelection = forceSelection;
+
+        // Enforce new setting
+        if (forceSelection && getCurrent() == null && buttons.size() > 0) {
+            forceSelect();
+        }
     }
 
     public boolean getForcedSelection()
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "getForcedSelection");
-        return false;
+        return forceSelection;
     }
 
     public void setEnabled(boolean enable)
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "setEnabled");
+        enabled = enable;
+        for (Enumeration e = buttons.elements(); e.hasMoreElements();) {
+            setEnabled((HToggleButton) e.nextElement(), enable);
+        }
     }
 
     public boolean isEnabled()
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "isEnabled");
-        return false;
+        return enabled;
     }
 
     protected void add(HToggleButton button)
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "add");
+        // Only add if not already added
+        if (!buttons.contains(button)) {
+            buttons.addElement(button);
+            setEnabled(button, isEnabled()); // Enforce enabled state
+
+            // Enforce forced selection (if first addition)
+            if (getForcedSelection() && buttons.size() == 1 && getCurrent() != button) {
+                button.setSwitchableState(true);
+                current = button;
+                // Assume that if size()>=1 that it's already enforced!
+            }
+            // If currently selected, unselect all others!
+            else if (button.getSwitchableState()) {
+                current = button;
+                if (buttons.size() > 1) unswitch(button); // Enforce single
+                                                          // selection
+            }
+        }
     }
 
     protected void remove(HToggleButton button)
     {
-        org.videolan.Logger.unimplemented(HToggleGroup.class.getName(), "remove");
+        if (!buttons.removeElement(button)) {
+            throw new IllegalArgumentException("Not a member of this HToggleGroup");
+        } else {
+            if (button == getCurrent()) {
+                current = null;
+                if (getForcedSelection() && buttons.size() > 0) {
+                    current = null;
+                    forceSelect();
+                }
+            }
+        }
+    }
+
+
+    private void unswitch(HToggleButton button)
+    {
+        for (Enumeration e = buttons.elements(); e.hasMoreElements();) {
+            HToggleButton b = (HToggleButton) e.nextElement();
+            if (b != button) b.setSwitchableState(false);
+        }
     }
+
+    private void forceSelect()
+    {
+        // assert(getCurrent() == null);
+        if (buttons.size() > 0) {
+            HToggleButton b = (HToggleButton) buttons.elementAt(0);
+            b.setSwitchableState(true);
+            current = b;
+        }
+    }
+
+    private void setEnabled(HToggleButton tb, boolean enable)
+    {
+        if (false) { // If HAVi 1.1
+            tb.setEnabled(enable);
+        } else {
+            // HAVI 1.01beta
+            int state = tb.getInteractionState();
+            tb.setInteractionState(enable ? (state & ~HState.DISABLED_STATE_BIT) : (state | HState.DISABLED_STATE_BIT));
+        }
+    }
+
+    /** Whether the buttons in this group are enabled or now. */
+    private boolean enabled = true;
+
+    /** Controls whether a selection must always be made or not. */
+    private boolean forceSelection = false;
+
+    /** The currently selected {@link HToggleButton}. */
+    private HToggleButton current = null;
+
+    /** The buttons currently added to this group. */
+    private Vector buttons = new Vector();
 }



More information about the libbluray-devel mailing list