root / src / gr / grnet / pithos / web / client / ConfirmationDialog.java @ a853017c
History | View | Annotate | Download (3.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
package gr.grnet.pithos.web.client; |
5 |
|
6 |
import com.google.gwt.dom.client.NativeEvent; |
7 |
import com.google.gwt.event.dom.client.ClickEvent; |
8 |
import com.google.gwt.event.dom.client.ClickHandler; |
9 |
import com.google.gwt.event.dom.client.KeyCodes; |
10 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
11 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
12 |
import com.google.gwt.user.client.ui.Button; |
13 |
import com.google.gwt.user.client.ui.DialogBox; |
14 |
import com.google.gwt.user.client.ui.HTML; |
15 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
16 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
17 |
import com.google.gwt.user.client.ui.VerticalPanel; |
18 |
|
19 |
|
20 |
/**
|
21 |
* A dialog for requesting confirmation from the user.
|
22 |
*/
|
23 |
public abstract class ConfirmationDialog extends DialogBox { |
24 |
|
25 |
/**
|
26 |
* The widget's constructor.
|
27 |
*
|
28 |
* @param message the message to display
|
29 |
* @param buttonLabel the label of the confirmation button
|
30 |
*/
|
31 |
public ConfirmationDialog(String message, String buttonLabel) { |
32 |
// Set the dialog's caption.
|
33 |
setText("Confirmation");
|
34 |
setAnimationEnabled(true);
|
35 |
// Create a VerticalPanel to contain the label and the buttons.
|
36 |
VerticalPanel outer = new VerticalPanel();
|
37 |
HorizontalPanel buttons = new HorizontalPanel();
|
38 |
|
39 |
HTML text = new HTML("<table><tr><td rowspan='2'> " + |
40 |
AbstractImagePrototype.create(MessagePanel.images.warn()).getHTML() + |
41 |
"</td><td>" + message + "</td></tr></table>"); |
42 |
text.setStyleName("pithos-warnMessage");
|
43 |
outer.add(text); |
44 |
|
45 |
// Create the 'Update' button, along with a listener that hides the
|
46 |
// dialog when the button is clicked and renames the file.
|
47 |
Button ok = new Button(buttonLabel, new ClickHandler() { |
48 |
|
49 |
@Override
|
50 |
public void onClick(ClickEvent event) { |
51 |
confirm(); |
52 |
hide(); |
53 |
} |
54 |
}); |
55 |
ok.getElement().setId("confirmation.ok");
|
56 |
buttons.add(ok); |
57 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
58 |
// Create the 'Cancel' button, along with a listener that hides the
|
59 |
// dialog when the button is clicked.
|
60 |
Button cancel = new Button("Cancel", new ClickHandler() { |
61 |
|
62 |
@Override
|
63 |
public void onClick(ClickEvent event) { |
64 |
hide(); |
65 |
cancel(); |
66 |
} |
67 |
}); |
68 |
cancel.getElement().setId("confirmation.cancel");
|
69 |
buttons.add(cancel); |
70 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
71 |
buttons.setSpacing(8);
|
72 |
buttons.setStyleName("pithos-warnMessage");
|
73 |
outer.setStyleName("pithos-warnMessage");
|
74 |
outer.add(buttons); |
75 |
outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER); |
76 |
outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
77 |
setWidget(outer); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
82 |
super.onPreviewNativeEvent(preview);
|
83 |
NativeEvent evt = preview.getNativeEvent(); |
84 |
if (evt.getType().equals("keydown")) |
85 |
// Use the popup's key preview hooks to close the dialog when either
|
86 |
// enter or escape is pressed.
|
87 |
switch (evt.getKeyCode()) {
|
88 |
case KeyCodes.KEY_ENTER:
|
89 |
hide(); |
90 |
confirm(); |
91 |
break;
|
92 |
case KeyCodes.KEY_ESCAPE:
|
93 |
hide(); |
94 |
cancel(); |
95 |
break;
|
96 |
} |
97 |
} |
98 |
|
99 |
public abstract void confirm(); |
100 |
|
101 |
public abstract void cancel(); |
102 |
} |