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