All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.swingui.ExclusiveEnableGroup Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.darwinsys.swingui;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * Manage the "enabled" property on a group of Components,
 * such that only one will be enabled.
 * May malfunction if you directly invoke setEnabled() on any component.
 * NOT THREADSAFE; use single-threadedly.
 */
public class ExclusiveEnableGroup implements java.io.Serializable {

	private List groupComps = new ArrayList();

	/** Add the Component to the list of managed Components
	 * @param c The component to be added
	 */
	public void add(Component c) {
		c.setEnabled(false);
		groupComps.add(c);
	}

	/** Enable the given Component; this is the only way you
	 * should control enabled properties on the components.
	 * @param targetComp The component to be enabled
	 */
	public void enable(Component targetComp) {
		if (!groupComps.contains(targetComp)) {
			throw new IllegalArgumentException(targetComp + " not in list");
		}
		// Walk the list once, disabling all but the target,
		// and enabling the target.
		for (Component c : groupComps) {
			targetComp.setEnabled(c == targetComp);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy