-
Notifications
You must be signed in to change notification settings - Fork 33
/
TimeField.java
223 lines (183 loc) · 5.9 KB
/
TimeField.java
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
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022 Contributors to Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.admin.apps.console.util;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import com.sun.messaging.jmq.admin.util.Globals;
import com.sun.messaging.jmq.admin.resources.AdminConsoleResources;
/**
* This class implements a field used to enter some amount of time. It supports the selection of the time unit:
*
* <UL>
* <LI>Milliseconds
* <LI>Seconds
* <LI>Minutes
* <LI>Hours
* <LI>Days
* </UL>
* The default unit displayed is Seconds.
* <P>
*
* The getValue() method returns the value entered in <STRONG>Milliseconds</STRONG>. (because this class gives you the
* option of displaying the Milliseconds unit or not).
*/
public class TimeField extends JPanel {
private static final long serialVersionUID = 7137164379701741563L;
/*
* Unit types. These are not indices
*/
public static final int MILLISECONDS = 0;
public static final int SECONDS = 1;
public static final int MINUTES = 2;
public static final int HOURS = 3;
public static final int DAYS = 4;
/*
* Indices that indicate what string is displayed in the unit combobox
*/
private int msecPos = 0, secPos = 1, minPos = 2, hrPos = 3, dayPos = 4;
private boolean showMillis = false;
private static AdminConsoleResources acr = Globals.getAdminConsoleResources();
private IntegerField intF;
private JComboBox unitCB;
public TimeField(long max, String text) {
this(max, text, 0);
}
public TimeField(long max, int columns) {
this(max, null, columns);
}
public TimeField(long max, String text, int columns) {
this(max, text, columns, false);
}
public TimeField(long max, String text, int columns, boolean showMillis) {
this.showMillis = showMillis;
initGui(max, text, columns);
setUnit(SECONDS);
}
public void addActionListener(ActionListener l) {
intF.addActionListener(l);
}
public void setText(String s) {
intF.setText(s);
}
public String getText() {
return (intF.getText());
}
@Override
public void setEnabled(boolean b) {
intF.setEnabled(b);
unitCB.setEnabled(b);
}
/**
* Set the byte unit.
*
* @param unit byte unit. Can be a number from 0 to 4 (inclusive) depending if Milliseconds is shown or not.
*/
public void setUnit(int unit) {
int index = secPos;
switch (unit) {
case MILLISECONDS:
index = msecPos;
break;
case SECONDS:
index = secPos;
break;
case MINUTES:
index = minPos;
break;
case HOURS:
index = hrPos;
break;
case DAYS:
index = dayPos;
break;
}
if ((index < 0) || (index > dayPos)) {
return;
}
unitCB.setSelectedIndex(index);
}
/**
* Return the value in milliseconds.
*
* @return The value entered in milliseconds.
*/
public long getValue() {
String s;
int selIndex;
long tmpLong;
s = intF.getText();
try {
tmpLong = Long.parseLong(s);
} catch (Exception e) {
return (-1);
}
selIndex = unitCB.getSelectedIndex();
if (showMillis) {
if (selIndex == msecPos) {
return (tmpLong);
} else if (selIndex == secPos) {
return (tmpLong * 1000);
} else if (selIndex == minPos) {
return (tmpLong * 1000 * 60);
} else if (selIndex == hrPos) {
return (tmpLong * 1000 * 60 * 60);
} else if (selIndex == dayPos) {
return (tmpLong * 1000 * 60 * 60 * 24);
}
} else {
if (selIndex == secPos) {
return (tmpLong * 1000);
} else if (selIndex == minPos) {
return (tmpLong * 1000 * 60);
} else if (selIndex == hrPos) {
return (tmpLong * 1000 * 60 * 60);
} else if (selIndex == dayPos) {
return (tmpLong * 1000 * 60 * 60 * 24);
}
}
return (-1);
}
private void initGui(long max, String text, int collumns) {
String[] units;
intF = new IntegerField(0, max, text, collumns);
if (showMillis) {
msecPos = 0;
secPos = 1;
minPos = 2;
hrPos = 3;
dayPos = 4;
units = new String[5];
units[msecPos] = acr.getString(acr.I_MILLISECONDS);
} else {
secPos = 0;
minPos = 1;
hrPos = 2;
dayPos = 3;
units = new String[4];
}
units[secPos] = acr.getString(acr.I_SECONDS);
units[minPos] = acr.getString(acr.I_MINUTES);
units[hrPos] = acr.getString(acr.I_HOURS);
units[dayPos] = acr.getString(acr.I_DAYS);
unitCB = new JComboBox(units);
setLayout(new BorderLayout());
add(intF, "Center");
add(unitCB, "East");
}
}