annotate 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
rev   line source
samer@0 1 /*
samer@0 2 * ButtonBar.java
samer@0 3 *
samer@0 4 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@0 12 package samer.core.util.swing;
samer@0 13 import samer.core.util.*;
samer@0 14 import samer.core.*;
samer@0 15 import java.awt.event.*;
samer@0 16 import javax.swing.*;
samer@0 17 import javax.swing.border.*;
samer@0 18
samer@0 19 /**
samer@0 20 Displays Agent commands as buttons in a JToolBar.
samer@0 21 */
samer@0 22
samer@0 23 public class ButtonBar extends JToolBar implements Agent.Registry
samer@0 24 {
samer@0 25 private static java.awt.Insets ins = new java.awt.Insets(2,6,2,6);
samer@0 26
samer@0 27 private ActionListener handler=null;
samer@0 28 private ActionListener broadcaster=null;
samer@0 29 private ItemListener itemHandler=null;
samer@0 30
samer@0 31 public ButtonBar() {}
samer@0 32
samer@0 33 /** Set the target agent for subsequent buttons added via add() method.
samer@0 34 If null, then subsequently added buttons will broadcast their command
samer@0 35 using the ActionListener registered via setBroadcaster.
samer@0 36 */
samer@0 37 public void setTarget(Agent a) {
samer@0 38 if (a!=null) {
samer@0 39 AgentAdapter adapter=new AgentAdapter(a);
samer@0 40 itemHandler=adapter;
samer@0 41 handler=adapter;
samer@0 42 } else {
samer@0 43 itemHandler=null;
samer@0 44 handler=broadcaster;
samer@0 45 }
samer@0 46 }
samer@0 47
samer@0 48 /** Set the ActionListener to be used when no specific target agent
samer@0 49 is specified for a button. */
samer@0 50 public void setBroadcaster(ActionListener b) { broadcaster=b; }
samer@0 51
samer@0 52 /** Add a new button with the given text. */
samer@0 53 public Agent.Registry add(String l)
samer@0 54 {
samer@0 55 JButton b = new JButton(l);
samer@0 56 // could try to get an icon and some tool tips here
samer@0 57 b.setMargin(ins);
samer@0 58 b.addActionListener(handler);
samer@0 59 add(b);
samer@0 60 return this;
samer@0 61 }
samer@0 62
samer@0 63 /** Add a boolean state button */
samer@0 64 public Agent.Registry add(String l, boolean state)
samer@0 65 {
samer@0 66 JToggleButton b = new JToggleButton(l);
samer@0 67 b.setName(l);
samer@0 68 b.setMargin(ins);
samer@0 69
samer@0 70 if (itemHandler!=null)
samer@0 71 b.addItemListener(itemHandler);
samer@0 72 else
samer@0 73 b.addActionListener(handler);
samer@0 74
samer@0 75 add(b);
samer@0 76 return this;
samer@0 77 }
samer@0 78
samer@0 79 /** Make logical gap between previous buttons and ones to follow */
samer@0 80 public void group() {}
samer@0 81 }
samer@0 82