Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / ConfirmationDialog.java @ 1206:292dec4eae08

History | View | Annotate | Download (3.9 kB)

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 org.gss_project.gss.web.client;
20

    
21
import com.google.gwt.dom.client.NativeEvent;
22
import com.google.gwt.event.dom.client.ClickEvent;
23
import com.google.gwt.event.dom.client.ClickHandler;
24
import com.google.gwt.event.dom.client.KeyCodes;
25
import com.google.gwt.user.client.Event.NativePreviewEvent;
26
import com.google.gwt.user.client.ui.AbstractImagePrototype;
27
import com.google.gwt.user.client.ui.Button;
28
import com.google.gwt.user.client.ui.DialogBox;
29
import com.google.gwt.user.client.ui.HTML;
30
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
31
import com.google.gwt.user.client.ui.HorizontalPanel;
32
import com.google.gwt.user.client.ui.VerticalPanel;
33

    
34

    
35
/**
36
 * A dialog for requesting confirmation from the user.
37
 *
38
 * @author kman
39
 */
40
public abstract class ConfirmationDialog extends DialogBox {
41

    
42
        /**
43
         * The widget's constructor.
44
         *
45
         * @param message the message to display
46
         * @param buttonLabel the label of the confirmation button
47
         */
48
        public ConfirmationDialog(String message, String buttonLabel) {
49
                // Set the dialog's caption.
50
                setText("Confirmation");
51
                setAnimationEnabled(true);
52
                // Create a VerticalPanel to contain the label and the buttons.
53
                VerticalPanel outer = new VerticalPanel();
54
                HorizontalPanel buttons = new HorizontalPanel();
55

    
56
                HTML text = new HTML("<table><tr><td rowspan='2'> " +
57
                                AbstractImagePrototype.create(MessagePanel.images.warn()).getHTML() +
58
                                "</td><td>" + message + "</td></tr></table>");
59
                text.setStyleName("gss-warnMessage");
60
                outer.add(text);
61

    
62
                // Create the 'Update' button, along with a listener that hides the
63
                // dialog when the button is clicked and renames the file.
64
                Button ok = new Button(buttonLabel, new ClickHandler() {
65

    
66
                        @Override
67
                        public void onClick(ClickEvent event) {
68
                                confirm();
69
                                hide();
70
                        }
71
                });
72
                ok.getElement().setId("confirmation.ok");
73
                buttons.add(ok);
74
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
75
                // Create the 'Cancel' button, along with a listener that hides the
76
                // dialog when the button is clicked.
77
                Button cancel = new Button("Cancel", new ClickHandler() {
78

    
79
                        @Override
80
                        public void onClick(ClickEvent event) {
81
                                hide();
82
                                cancel();
83
                        }
84
                });
85
                cancel.getElement().setId("confirmation.cancel");
86
                buttons.add(cancel);
87
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
88
                buttons.setSpacing(8);
89
                buttons.setStyleName("gss-warnMessage");
90
                outer.setStyleName("gss-warnMessage");
91
                outer.add(buttons);
92
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
93
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
94
                setWidget(outer);
95
        }
96

    
97
        @Override
98
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
99
                super.onPreviewNativeEvent(preview);
100
                NativeEvent evt = preview.getNativeEvent();
101
                if (evt.getType().equals("keydown"))
102
                        // Use the popup's key preview hooks to close the dialog when either
103
                        // enter or escape is pressed.
104
                        switch (evt.getKeyCode()) {
105
                                case KeyCodes.KEY_ENTER:
106
                                        hide();
107
                                        confirm();
108
                                        break;
109
                                case KeyCodes.KEY_ESCAPE:
110
                                        hide();
111
                                        cancel();
112
                                        break;
113
                        }
114
        }
115

    
116
        public abstract void confirm();
117

    
118
        public abstract void cancel();
119
}