diff src/samer/core_/util/swing/ButtonBar.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/core_/util/swing/ButtonBar.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,82 @@
+/*
+ *	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() {}
+}
+