/
Alert.java
363 lines (294 loc) · 11.2 KB
/
Alert.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pivot.wtk;
import java.util.Iterator;
import org.apache.pivot.beans.DefaultProperty;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.json.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.util.ImmutableIterator;
import org.apache.pivot.util.ListenerList;
import org.apache.pivot.util.Resources;
/**
* Class representing an "alert", a dialog commonly used to facilitate simple
* user interaction.
*/
@DefaultProperty("body")
public class Alert extends Dialog {
/**
* Option sequence implementation.
*/
public final class OptionSequence implements Sequence<Object>, Iterable<Object> {
private OptionSequence() {
}
@Override
public int add(Object option) {
int i = getLength();
insert(option, i);
return i;
}
@Override
public void insert(Object option, int index) {
if (option == null) {
throw new IllegalArgumentException("option is null.");
}
options.insert(option, index);
if (selectedOptionIndex >= index) {
selectedOptionIndex++;
}
alertListeners.optionInserted(Alert.this, index);
}
@Override
public Component update(int index, Object option) {
throw new UnsupportedOperationException();
}
@Override
public int remove(Object option) {
int index = indexOf(option);
if (index != -1) {
remove(index, 1);
}
return index;
}
@Override
public Sequence<Object> remove(int index, int count) {
Sequence<Object> removed = options.remove(index, count);
if (removed.getLength() > 0) {
if (selectedOptionIndex >= index) {
if (selectedOptionIndex < index + count) {
selectedOptionIndex = -1;
} else {
selectedOptionIndex -= count;
}
}
alertListeners.optionsRemoved(Alert.this, index, removed);
}
return removed;
}
@Override
public Object get(int index) {
return options.get(index);
}
@Override
public int indexOf(Object option) {
return options.indexOf(option);
}
@Override
public int getLength() {
return options.getLength();
}
@Override
public Iterator<Object> iterator() {
return new ImmutableIterator<Object>(options.iterator());
}
}
private static class AlertListenerList extends ListenerList<AlertListener>
implements AlertListener {
@Override
public void messageTypeChanged(Alert alert, MessageType previousMessageType) {
for (AlertListener listener : this) {
listener.messageTypeChanged(alert, previousMessageType);
}
}
@Override
public void messageChanged(Alert alert, String previousMessage) {
for (AlertListener listener : this) {
listener.messageChanged(alert, previousMessage);
}
}
@Override
public void bodyChanged(Alert alert, Component previousBody) {
for (AlertListener listener : this) {
listener.bodyChanged(alert, previousBody);
}
}
@Override
public void optionInserted(Alert alert, int index) {
for (AlertListener listener : this) {
listener.optionInserted(alert, index);
}
}
@Override
public void optionsRemoved(Alert alert, int index, Sequence<?> removed) {
for (AlertListener listener : this) {
listener.optionsRemoved(alert, index, removed);
}
}
@Override
public void selectedOptionChanged(Alert alert, int previousSelectedOption) {
for (AlertListener listener : this) {
listener.selectedOptionChanged(alert, previousSelectedOption);
}
}
}
private MessageType messageType = null;
private String message = null;
private Component body = null;
private ArrayList<Object> options = new ArrayList<Object>();
private OptionSequence optionSequence = new OptionSequence();
private int selectedOptionIndex = -1;
private AlertListenerList alertListeners = new AlertListenerList();
private static Resources resources = null;
static {
try {
resources = new Resources(Alert.class.getName());
} catch(Exception exception) {
throw new RuntimeException(exception);
}
}
public Alert() {
this(null, null, null);
}
public Alert(MessageType type, String message, Sequence<?> options) {
this(type, message, options, true);
}
public Alert(MessageType type, String message, Sequence<?> options, boolean modal) {
this(type, message, options, null, modal);
}
public Alert(MessageType type, String message, Sequence<?> options, Component body) {
this(type, message, options, body, true);
}
public Alert(MessageType messageType, String message, Sequence<?> options, Component body, boolean modal) {
super(modal);
if (messageType == null) {
messageType = MessageType.INFO;
}
setMessageType(messageType);
setMessage(message);
setBody(body);
setOptions(options);
setTitle((String)resources.get("defaultTitle"));
installThemeSkin(Alert.class);
}
public MessageType getMessageType() {
return messageType;
}
public void setMessageType(MessageType messageType) {
if (messageType == null) {
throw new IllegalArgumentException();
}
MessageType previousMessageType = this.messageType;
if (previousMessageType != messageType) {
this.messageType = messageType;
alertListeners.messageTypeChanged(this, previousMessageType);
}
}
public void setMessageType(String messageType) {
if (messageType == null) {
throw new IllegalArgumentException("messageType is null.");
}
setMessageType(MessageType.valueOf(messageType.toUpperCase()));
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
String previousMessage = this.message;
if (previousMessage != message) {
this.message = message;
alertListeners.messageChanged(this, previousMessage);
}
}
public Component getBody() {
return body;
}
public void setBody(Component body) {
Component previousBody = this.body;
if (previousBody != body) {
this.body = body;
alertListeners.bodyChanged(this, previousBody);
}
}
public OptionSequence getOptions() {
return optionSequence;
}
public void setOptions(Sequence<?> options) {
optionSequence.remove(0, optionSequence.getLength());
if (options != null) {
for (int i = 0, n = options.getLength(); i < n; i++) {
optionSequence.add(options.get(i));
}
setSelectedOptionIndex(0);
}
}
public void setOptions(String options) {
try {
setOptions(JSONSerializer.parseList(options));
} catch (SerializationException exception) {
throw new IllegalArgumentException(exception);
}
}
public int getSelectedOptionIndex() {
return selectedOptionIndex;
}
public void setSelectedOptionIndex(int selectedOption) {
if (selectedOption < -1
|| selectedOption > options.getLength() - 1) {
throw new IndexOutOfBoundsException();
}
int previousSelectedOption = this.selectedOptionIndex;
if (selectedOption != previousSelectedOption) {
this.selectedOptionIndex = selectedOption;
alertListeners.selectedOptionChanged(this, previousSelectedOption);
}
}
public Object getSelectedOption() {
return (selectedOptionIndex == -1) ? null : options.get(selectedOptionIndex);
}
public void setSelectedOption(Object selectedOption) {
setSelectedOptionIndex(options.indexOf(selectedOption));
}
public ListenerList<AlertListener> getAlertListeners() {
return alertListeners;
}
public static void alert(String message, Display display) {
alert(MessageType.INFO, message, null, display, null);
}
public static void alert(MessageType messageType, String message, Display display) {
alert(messageType, message, null, display, null);
}
public static void alert(MessageType messageType, String message, Component body, Display display) {
alert(messageType, message, body, display, null);
}
public static void alert(MessageType messageType, String message, Component body, Display display,
DialogCloseListener dialogCloseListener) {
Alert alert = new Alert(messageType, message,
new ArrayList<Object>(resources.get("defaultOption")), body);
alert.setModal(false);
alert.open(display, dialogCloseListener);
}
public static void alert(String message, Window owner) {
alert(MessageType.INFO, message, null, owner, null);
}
public static void alert(MessageType messageType, String message, Window owner) {
alert(messageType, message, null, owner, null);
}
public static void alert(MessageType messageType, String message, Window owner,
DialogCloseListener dialogCloseListener) {
alert(messageType, message, null, owner, dialogCloseListener);
}
public static void alert(MessageType messageType, String message, Component body, Window owner) {
alert(messageType, message, body, owner, null);
}
public static void alert(MessageType messageType, String message, Component body, Window owner,
DialogCloseListener dialogCloseListener) {
Alert alert = new Alert(messageType, message,
new ArrayList<Object>(resources.get("defaultOption")), body);
alert.open(owner.getDisplay(), owner, dialogCloseListener);
}
}