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
|
package cuchaz.enigma.gui.elements;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* Implements a generic status bar for use in windows. The API is loosely based
* on Qt's QStatusBar.
*/
public class StatusBar {
private final JPanel ui = new JPanel(new BorderLayout());
private final JPanel leftPanel = new JPanel(new GridLayout(1, 1, 0, 0));
private final JPanel components = new JPanel();
private final JPanel permanentComponents = new JPanel();
private final JLabel temporaryMessage = new JLabel();
private final Timer timer = new Timer(0, e -> this.clearMessage());
public StatusBar() {
this.timer.setRepeats(false);
this.components.setLayout(new BoxLayout(this.components, BoxLayout.LINE_AXIS));
this.permanentComponents.setLayout(new BoxLayout(this.permanentComponents, BoxLayout.LINE_AXIS));
this.permanentComponents.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
this.leftPanel.add(this.components);
this.temporaryMessage.setHorizontalTextPosition(JLabel.LEFT);
this.ui.add(this.leftPanel, BorderLayout.CENTER);
this.ui.add(this.permanentComponents, BorderLayout.EAST);
}
/**
* Displays a temporary message in the status bar. The message is displayed
* until it is explicitly cleared through {@link #clearMessage()} or a new
* message is displayed.
*
* @param message the message to display
*/
public void showMessage(String message) {
this.showMessage(message, 0);
}
/**
* Displays a temporary message in the status bar. The message is displayed
* until it is explicitly cleared through {@link #clearMessage()}, a new
* message is displayed, or the timeout, if any, is reached.
*
* @param message the message to display
* @param timeout the timeout in milliseconds to wait until clearing the
* message; if 0, the message is not automatically cleared
*/
public void showMessage(String message, int timeout) {
this.timer.stop();
this.temporaryMessage.setText(message);
this.leftPanel.removeAll();
this.leftPanel.add(this.temporaryMessage);
this.leftPanel.revalidate();
if (timeout > 0) {
this.timer.setInitialDelay(timeout);
this.timer.start();
}
}
/**
* Clears any currently displayed temporary message.
*/
public void clearMessage() {
this.timer.stop();
this.leftPanel.removeAll();
this.leftPanel.add(this.components);
this.leftPanel.revalidate();
this.temporaryMessage.setText("");
}
/**
* Returns the currently displayed message, or the empty string otherwise.
*
* @return the currently displayed message
*/
public String currentMessage() {
return this.temporaryMessage.getText();
}
/**
* Adds a component to the status bar. These components are positioned on
* the left side of the status bar. When a temporary message is displayed,
* the component will be hidden until the message is cleared.
*
* @param comp the component to add
*/
public void addComponent(Component comp) {
this.components.add(comp);
}
/**
* Removes a component from the status bar.
*
* @param comp the component to remove
*/
public void removeComponent(Component comp) {
this.components.remove(comp);
}
/**
* Adds a permanent component to the status bar. These components will not
* be hidden by temporary messages and will be displayed on the right side
* of the status bar.
*
* @param comp the component to add
*/
public void addPermanentComponent(Component comp) {
this.permanentComponents.add(comp);
}
/**
* Removes a permanent component from the status bar.
*
* @param comp the component to remove
*/
public void removePermanentComponent(Component comp) {
this.permanentComponents.remove(comp);
}
public JPanel getUi() {
return this.ui;
}
}
|