|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2010 William Hahne
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
package org.havi.ui;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.util.Enumeration;
|
|
Packit |
5e46da |
import java.util.Vector;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public class HToggleGroup {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public HToggleGroup() {
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public HToggleButton getCurrent() {
|
|
Packit |
5e46da |
return current;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public void setCurrent(HToggleButton selection) {
|
|
Packit |
5e46da |
// Treat null as unselection
|
|
Packit |
5e46da |
if (selection == null) {
|
|
Packit |
5e46da |
HToggleButton tmp = getCurrent();
|
|
Packit |
5e46da |
if (tmp != null && getForcedSelection() && buttons.size() > 0) {
|
|
Packit |
5e46da |
// Enforce forced selection (reselect button!)
|
|
Packit |
5e46da |
tmp.setSwitchableState(true);
|
|
Packit |
5e46da |
} else {
|
|
Packit |
5e46da |
// General behavior
|
|
Packit |
5e46da |
current = null;
|
|
Packit |
5e46da |
if (tmp != null) tmp.setSwitchableState(false);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
// Set current only if part of this group
|
|
Packit |
5e46da |
// And isn't the current selection (stops infinite loop)
|
|
Packit |
5e46da |
else if (buttons.contains(selection) && getCurrent() != selection) {
|
|
Packit |
5e46da |
current = selection;
|
|
Packit |
5e46da |
selection.setSwitchableState(true);
|
|
Packit |
5e46da |
unswitch(selection); // Enforce single selection
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public void setForcedSelection(boolean forceSelection) {
|
|
Packit |
5e46da |
this.forceSelection = forceSelection;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
// Enforce new setting
|
|
Packit |
5e46da |
if (forceSelection && getCurrent() == null && buttons.size() > 0) {
|
|
Packit |
5e46da |
forceSelect();
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public boolean getForcedSelection() {
|
|
Packit |
5e46da |
return forceSelection;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public void setEnabled(boolean enable) {
|
|
Packit |
5e46da |
enabled = enable;
|
|
Packit |
5e46da |
for (Enumeration e = buttons.elements(); e.hasMoreElements();) {
|
|
Packit |
5e46da |
setEnabled((HToggleButton) e.nextElement(), enable);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public boolean isEnabled() {
|
|
Packit |
5e46da |
return enabled;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected void add(HToggleButton button) {
|
|
Packit |
5e46da |
// Only add if not already added
|
|
Packit |
5e46da |
if (!buttons.contains(button)) {
|
|
Packit |
5e46da |
buttons.addElement(button);
|
|
Packit |
5e46da |
setEnabled(button, isEnabled()); // Enforce enabled state
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
// Enforce forced selection (if first addition)
|
|
Packit |
5e46da |
if (getForcedSelection() && buttons.size() == 1 && getCurrent() != button) {
|
|
Packit |
5e46da |
button.setSwitchableState(true);
|
|
Packit |
5e46da |
current = button;
|
|
Packit |
5e46da |
// Assume that if size()>=1 that it's already enforced!
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
// If currently selected, unselect all others!
|
|
Packit |
5e46da |
else if (button.getSwitchableState()) {
|
|
Packit |
5e46da |
current = button;
|
|
Packit |
5e46da |
if (buttons.size() > 1) unswitch(button); // Enforce single
|
|
Packit |
5e46da |
// selection
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected void remove(HToggleButton button) {
|
|
Packit |
5e46da |
if (!buttons.removeElement(button)) {
|
|
Packit |
5e46da |
throw new IllegalArgumentException("Not a member of this HToggleGroup");
|
|
Packit |
5e46da |
} else {
|
|
Packit |
5e46da |
if (button == getCurrent()) {
|
|
Packit |
5e46da |
current = null;
|
|
Packit |
5e46da |
if (getForcedSelection() && buttons.size() > 0) {
|
|
Packit |
5e46da |
current = null;
|
|
Packit |
5e46da |
forceSelect();
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private void unswitch(HToggleButton button) {
|
|
Packit |
5e46da |
for (Enumeration e = buttons.elements(); e.hasMoreElements();) {
|
|
Packit |
5e46da |
HToggleButton b = (HToggleButton) e.nextElement();
|
|
Packit |
5e46da |
if (b != button) b.setSwitchableState(false);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private void forceSelect() {
|
|
Packit |
5e46da |
// assert(getCurrent() == null);
|
|
Packit |
5e46da |
if (buttons.size() > 0) {
|
|
Packit |
5e46da |
HToggleButton b = (HToggleButton) buttons.elementAt(0);
|
|
Packit |
5e46da |
b.setSwitchableState(true);
|
|
Packit |
5e46da |
current = b;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private void setEnabled(HToggleButton tb, boolean enable) {
|
|
Packit |
5e46da |
if (false) { // If HAVi 1.1
|
|
Packit |
5e46da |
tb.setEnabled(enable);
|
|
Packit |
5e46da |
} else {
|
|
Packit |
5e46da |
// HAVI 1.01beta
|
|
Packit |
5e46da |
int state = tb.getInteractionState();
|
|
Packit |
5e46da |
tb.setInteractionState(enable ? (state & ~HState.DISABLED_STATE_BIT) : (state | HState.DISABLED_STATE_BIT));
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/** Whether the buttons in this group are enabled or now. */
|
|
Packit |
5e46da |
private boolean enabled = true;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/** Controls whether a selection must always be made or not. */
|
|
Packit |
5e46da |
private boolean forceSelection = false;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/** The currently selected {@link HToggleButton}. */
|
|
Packit |
5e46da |
private HToggleButton current = null;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/** The buttons currently added to this group. */
|
|
Packit |
5e46da |
private Vector buttons = new Vector();
|
|
Packit |
5e46da |
}
|