1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
/**
* StatusButtonPanel extends ButtonPanel to provide a space to display status
* messages in a consistent manner.<BR>
* <BR>
* Messages are erased after a certain predefined interval, defaulting to 10
* seconds.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 904 $
*/
public class StatusButtonPanel extends ButtonPanel {
/**
* This is the action command to all listeners when the status text is changed.
*/
public static final String STATUS_CHANGED = "STATUS_CHANGED";
// note: weirdness happens if you initialize
// this variable. Because it is set by initLayout
// and initLayout is called by the superclass constructor,
// this variable would get initialized after initLayout
// is called...
protected Component statusComponent; // = null;
protected Timer timer = null;
protected int interval = 10000; // adjust as needed
/**
* Constructs a StatusButtonPanel. Three buttons are created so the panel is
* filled when used in a GUI-builder environment.
*/
public StatusButtonPanel() {
super();
setupTimer();
}
/**
* Constructs a StatusButtonPanel using specified buttons.
*
* @param buttonList An array containing the strings to be used in labeling the
* buttons.
*/
public StatusButtonPanel(String[] buttonList) {
super(buttonList);
setupTimer();
}
/**
* Initializes the timer instance variable.
*/
protected void setupTimer() {
timer = new Timer(interval, this);
timer.addActionListener(this);
timer.setRepeats(false);
timer.start();
}
/**
* Returns the number of milliseconds before the status message is cleared. The
* default is 10000.
*
* @return The current delay interval in milliseconds.
*/
public int getDelayInterval() {
return interval;
}
/**
* Sets the number of milliseconds before the status message is cleared.
*
* @param millis The new delay interval in milliseconds.
*/
public void setDelayInterval(int millis) {
interval = millis;
timer.setDelay(interval);
}
/**
* Returns the visual component used to display the status.
*
* @return A component used for displaying status.
*/
public Component getStatusComponent() {
return statusComponent;
}
/**
* Receives ActionEvents from the internal timer.
*
* @param e The action event in question.
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
setText("");
return;
}
// otherwise continue with superclass implementation
super.actionPerformed(e);
}
/**
* This method is responsible for the initial layout of the panel. Subclasses
* can implement different layouts, but this method is responsible for
* initializing buttonPanelLayout to a valid layout manager and setting this
* panel to use it. This method must should initialize statusComponent to a
* component that ideally has get/setText methods, although this is not
* required.
*/
protected void initLayout() {
statusComponent = new JTextField();
JTextField textField = (JTextField) statusComponent;
textField.setColumns(20);
textField.setBackground(getBackground());
textField.setEditable(false);
// statusComponent = new PickListPanel(); // for testing
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 5, 0, 10);
gbc.ipadx = 0;
gbc.ipady = 0;
//1.2 new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
//1.2 GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 10), 0, 0 );
this.add(statusComponent, gbc);
buttonContainer = new JPanel();
buttonPanelLayout = new BetterFlowLayout();
buttonContainer.setLayout(buttonPanelLayout);
buttonPanelLayout.setAlignment(BetterFlowLayout.RIGHT);
((BetterFlowLayout) buttonPanelLayout).setWidthUniform(true);
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 0);
this.add(buttonContainer, gbc);
}
/**
* Sets the text to appear in the status area.
*
* @param newText A string to appear in the status area. Nulls are allowed.
*/
public void setText(String newText) {
// TODO: should use property introspection instead
// use reflection to call the "setText" method, if any.
try {
Class c = statusComponent.getClass();
Method m = c.getMethod("setText", new Class[] { new String().getClass() });
m.invoke(statusComponent, new Object[] { newText });
broadcastEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, STATUS_CHANGED));
statusComponent.paint(statusComponent.getGraphics());
} catch (Exception exc) {
// "setText" method does not exist; do nothing.
}
// if non-empty string, start the timer
if (!"".equals(newText)) {
timer.restart();
}
}
/**
* Gets the text in the status area.
*
* @return The string being displayed in the status area.
*/
public String getText() {
// TODO: should use property introspection instead
String value = "";
// use reflection to call the "setText" method, if any.
try {
Class c = statusComponent.getClass();
Method m = c.getMethod("getText", (Class[]) null);
value = (String) m.invoke(statusComponent, (Object[]) null);
} catch (Exception exc) {
// "getText" method does not exist; do nothing.
}
return value;
}
// for testing
public static void main(String[] argv) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
}
JFrame dialog = new JFrame();
BorderLayout bl = new BorderLayout(20, 20);
// StatusButtonPanel panel = new StatusButtonPanel();
// System.out.println( panel.statusComponent );
StatusButtonPanel panel = new StatusButtonPanel(new String[] { "Okay", "Cancel" });
dialog.getContentPane().setLayout(bl);
dialog.getContentPane().add(panel, BorderLayout.SOUTH);
dialog.setLocation(50, 50);
// dialog.setSize( 450, 150 );
dialog.pack();
dialog.setVisible(true);
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setAlignment(BetterFlowLayout.RIGHT);
// panel.getButton( "One" ).setEnabled( false );
panel.setText("File saved.");
System.out.println(panel.getText());
}
}
|