Merge with d1e79f3c8c8779d14ab5297049bdc0812f942654
[pithos] / src / gr / ebs / gss / client / ConfirmationDialog.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import com.google.gwt.user.client.ui.Button;
22 import com.google.gwt.user.client.ui.ClickListener;
23 import com.google.gwt.user.client.ui.DialogBox;
24 import com.google.gwt.user.client.ui.HTML;
25 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
26 import com.google.gwt.user.client.ui.HorizontalPanel;
27 import com.google.gwt.user.client.ui.KeyboardListener;
28 import com.google.gwt.user.client.ui.VerticalPanel;
29 import com.google.gwt.user.client.ui.Widget;
30
31
32 /**
33  * A dialog for requesting confirmation from the user.
34  *
35  * @author kman
36  */
37 public abstract class ConfirmationDialog extends DialogBox {
38
39         /**
40          * The widget's constructor.
41          *
42          * @param message the message to display
43          * @param buttonLabel the label of the confirmation button
44          */
45         public ConfirmationDialog(String message, String buttonLabel) {
46                 // Set the dialog's caption.
47                 setText("Confirmation");
48                 setAnimationEnabled(true);
49                 // Create a VerticalPanel to contain the label and the buttons.
50                 VerticalPanel outer = new VerticalPanel();
51                 HorizontalPanel buttons = new HorizontalPanel();
52
53                 HTML text = new HTML("<table><tr><td rowspan='2'>" + MessagePanel.images.warn().getHTML() +
54                                         "</td><td>" + message + "</td></tr></table>");
55                 text.setStyleName("gss-warnMessage");
56                 outer.add(text);
57
58                 // Create the 'Update' button, along with a listener that hides the dialog
59                 // when the button is clicked and renames the file.
60                 Button ok = new Button(buttonLabel, new ClickListener() {
61
62                         public void onClick(Widget sender) {
63                                 confirm();
64                                 hide();
65                         }
66                 });
67                 buttons.add(ok);
68                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
69                 // Create the 'Cancel' button, along with a listener that hides the
70                 // dialog when the button is clicked.
71                 Button cancel = new Button("Cancel", new ClickListener() {
72
73                         public void onClick(Widget sender) {
74                                 hide();
75                                 cancel();
76                         }
77                 });
78                 buttons.add(cancel);
79                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
80                 buttons.setSpacing(8);
81                 buttons.setStyleName("gss-warnMessage");
82                 outer.setStyleName("gss-warnMessage");
83                 outer.add(buttons);
84                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
85                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
86                 setWidget(outer);
87         }
88
89         @Override
90         public boolean onKeyDownPreview(final char key, final int modifiers) {
91                 // Use the popup's key preview hooks to close the dialog when either
92                 // enter or escape is pressed.
93                 switch (key) {
94                         case KeyboardListener.KEY_ENTER:
95                                 hide();
96                                 confirm();
97                                 break;
98                         case KeyboardListener.KEY_ESCAPE:
99                                 hide();
100                                 cancel();
101                                 break;
102                 }
103                 return true;
104         }
105
106         public abstract void confirm();
107
108         public abstract void cancel();
109 }