samer@0: /* samer@0: * ButtonBar.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util.swing; samer@0: import samer.core.util.*; samer@0: import samer.core.*; samer@0: import java.awt.event.*; samer@0: import javax.swing.*; samer@0: import javax.swing.border.*; samer@0: samer@0: /** samer@0: Displays Agent commands as buttons in a JToolBar. samer@0: */ samer@0: samer@0: public class ButtonBar extends JToolBar implements Agent.Registry samer@0: { samer@0: private static java.awt.Insets ins = new java.awt.Insets(2,6,2,6); samer@0: samer@0: private ActionListener handler=null; samer@0: private ActionListener broadcaster=null; samer@0: private ItemListener itemHandler=null; samer@0: samer@0: public ButtonBar() {} samer@0: samer@0: /** Set the target agent for subsequent buttons added via add() method. samer@0: If null, then subsequently added buttons will broadcast their command samer@0: using the ActionListener registered via setBroadcaster. samer@0: */ samer@0: public void setTarget(Agent a) { samer@0: if (a!=null) { samer@0: AgentAdapter adapter=new AgentAdapter(a); samer@0: itemHandler=adapter; samer@0: handler=adapter; samer@0: } else { samer@0: itemHandler=null; samer@0: handler=broadcaster; samer@0: } samer@0: } samer@0: samer@0: /** Set the ActionListener to be used when no specific target agent samer@0: is specified for a button. */ samer@0: public void setBroadcaster(ActionListener b) { broadcaster=b; } samer@0: samer@0: /** Add a new button with the given text. */ samer@0: public Agent.Registry add(String l) samer@0: { samer@0: JButton b = new JButton(l); samer@0: // could try to get an icon and some tool tips here samer@0: b.setMargin(ins); samer@0: b.addActionListener(handler); samer@0: add(b); samer@0: return this; samer@0: } samer@0: samer@0: /** Add a boolean state button */ samer@0: public Agent.Registry add(String l, boolean state) samer@0: { samer@0: JToggleButton b = new JToggleButton(l); samer@0: b.setName(l); samer@0: b.setMargin(ins); samer@0: samer@0: if (itemHandler!=null) samer@0: b.addItemListener(itemHandler); samer@0: else samer@0: b.addActionListener(handler); samer@0: samer@0: add(b); samer@0: return this; samer@0: } samer@0: samer@0: /** Make logical gap between previous buttons and ones to follow */ samer@0: public void group() {} samer@0: } samer@0: