view src/samer/core_/util/swing/ButtonBar.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	ButtonBar.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.core.util.swing;
import  samer.core.util.*;
import  samer.core.*;
import  java.awt.event.*;
import  javax.swing.*;
import  javax.swing.border.*;

/**
	Displays Agent commands as buttons in a JToolBar.
  */

public class ButtonBar extends JToolBar implements Agent.Registry
{
	private static java.awt.Insets ins = new java.awt.Insets(2,6,2,6);

	private ActionListener handler=null;
	private ActionListener broadcaster=null;
	private ItemListener	  itemHandler=null;

	public ButtonBar() {}

	/** Set the target agent for subsequent buttons added via add() method.
		If null, then subsequently added buttons will broadcast their command
		using the ActionListener registered via setBroadcaster.
	*/
	public void setTarget(Agent a) {
		if (a!=null) {
			AgentAdapter adapter=new AgentAdapter(a);
			itemHandler=adapter;
			handler=adapter;
		} else {
			itemHandler=null;
			handler=broadcaster;
		}
	}

	/** Set the ActionListener to be used when no specific target agent
		is specified for a button. */
	public void setBroadcaster(ActionListener b) { broadcaster=b; }

	/** Add a new button with the given text.  */
	public Agent.Registry add(String l)
	{
		JButton b = new JButton(l);
		// could try to get an icon and some tool tips here
		b.setMargin(ins);
		b.addActionListener(handler);
		add(b);
		return this;
	}

	/** Add a boolean state button */
	public Agent.Registry add(String l, boolean state)
	{
		JToggleButton b = new JToggleButton(l);
		b.setName(l);
		b.setMargin(ins);

		if (itemHandler!=null)
			b.addItemListener(itemHandler);
		else
			b.addActionListener(handler);

		add(b);
		return this;
	}

	/** Make logical gap between previous buttons and ones to follow */
	public void group() {}
}